Path: redb/redb.Tests.Integration/Tests/Unit/SqlRedbTransactionServerEndedTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/SqlRedbTransactionServerEndedTests.cs ---- using System; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.Data.SqlClient; using redb.MSSql.Data; namespace redb.Tests.Integration.Tests.Unit; /// /// A transaction that the SQL Server ended itself (command timeout, deadlock victim, an error that dooms it) is a /// zombie in SqlClient: its Connection reads null and its Rollback throws "This SqlTransaction has completed". A /// rollback asked for then has already happened, so redb's RollbackAsync and DisposeAsync stay quiet; a rollback after /// redb's own Commit or Rollback still throws as before. The zombie here is the driver's own state: a SqlTransaction /// without its internal transaction, exactly what SqlClient leaves after the server ends it. No database required. /// public class SqlRedbTransactionServerEndedTests { private static SqlTransaction Zombie() { var zombie = (SqlTransaction)RuntimeHelpers.GetUninitializedObject(typeof(SqlTransaction)); zombie.Connection.Should().BeNull("the driver reports the transaction as ended"); zombie.Invoking(t => t.Rollback()).Should().Throw( "the driver's own rollback of an ended transaction throws, which is what redb must not pass on"); return zombie; } [Fact] public async Task Rollback_OfATransactionTheServerEnded_IsQuiet_AndReleasesTheConnectionOnce() { var released = 0; var tx = new SqlRedbTransaction(Zombie(), () => released++); await tx.RollbackAsync(); tx.IsActive.Should().BeFalse(); released.Should().Be(1); } [Fact] public async Task SecondRollback_StillThrows_OurOwnStateDecidesBeforeTheDriver() { var tx = new SqlRedbTransaction(Zombie(), () => { }); await tx.RollbackAsync(); var again = () => tx.RollbackAsync(); await again.Should().ThrowAsync().WithMessage("*not active*"); } [Fact] public async Task RollbackAfterOurCommitAttempt_StillThrows() { var tx = new SqlRedbTransaction(Zombie(), () => { }); var commit = () => tx.CommitAsync(); await commit.Should().ThrowAsync("a commit of an ended transaction fails as before"); var rollback = () => tx.RollbackAsync(); await rollback.Should().ThrowAsync().WithMessage("*not active*", "after our own Commit the transaction is ours to have completed, not the server's"); } [Fact] public async Task UsingBlock_AfterTheServerEndedTheTransaction_IsQuiet() { var released = 0; var tx = new SqlRedbTransaction(Zombie(), () => released++); var dispose = async () => await tx.DisposeAsync(); await dispose.Should().NotThrowAsync(); tx.IsActive.Should().BeFalse(); released.Should().BeGreaterThanOrEqualTo(1); } [Fact] public async Task UserRollbackInCatch_ThenUsingBlock_IsQuiet() { var tx = new SqlRedbTransaction(Zombie(), () => { }); await tx.RollbackAsync(); var dispose = async () => await tx.DisposeAsync(); await dispose.Should().NotThrowAsync(); } }