Path: redb-route/tests/redb.Route.Tests.Sql/E2E/Base/SequentialConnectionsInScopeTestsBase.cs
Snapshot: redb-route, 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-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/E2E/Base/SequentialConnectionsInScopeTestsBase.cs
----
using System.Transactions;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql.E2E.Base;
///
/// Two sql: steps in one route transaction, as the connector runs them: each step opens a connection, runs its
/// statement and closes the connection before the next step opens one. Measured per driver: whether the second
/// connection reuses the first one's enlistment and the scope commits both statements, or the second open needs a
/// distributed transaction.
///
public abstract class SequentialConnectionsInScopeTestsBase : IAsyncLifetime
{
private SqlE2EDatabase? _db;
/// The provider under test.
protected abstract SqlE2EProvider Provider { get; }
private SqlE2EDatabase Db => _db ?? throw new InvalidOperationException("The table is created in InitializeAsync.");
///
public async Task InitializeAsync() => _db = await SqlE2EDatabase.CreateAsync(Provider);
///
public async Task DisposeAsync()
{
try
{
if (_db is not null)
await _db.DisposeAsync();
}
finally
{
(Provider as IDisposable)?.Dispose();
}
}
[Fact]
public async Task Two_steps_one_after_the_other_inside_one_scope_commit_both_statements()
{
var failure = await Outcome.Of(async () =>
{
using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
await using (var first = await Db.OpenAsync())
await SqlE2EDatabase.ExecuteAsync(first, null, Provider.InsertLiteral(Db.Table, 1, "a"));
await using (var second = await Db.OpenAsync())
await SqlE2EDatabase.ExecuteAsync(second, null, Provider.InsertLiteral(Db.Table, 2, "b"));
scope.Complete();
});
failure.Should().BeNull($"the second step reuses the first one's enlistment ({Outcome.Describe(failure)})");
(await Db.ReadIdsAsync()).Should().BeEquivalentTo([1, 2]);
}
[Fact]
public async Task Two_steps_one_after_the_other_inside_one_scope_roll_back_together()
{
var failure = await Outcome.Of(async () =>
{
using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
await using (var first = await Db.OpenAsync())
await SqlE2EDatabase.ExecuteAsync(first, null, Provider.InsertLiteral(Db.Table, 1, "a"));
await using (var second = await Db.OpenAsync())
await SqlE2EDatabase.ExecuteAsync(second, null, Provider.InsertLiteral(Db.Table, 2, "b"));
// Not completed: the route failed after both steps.
});
failure.Should().BeNull(Outcome.Describe(failure));
(await Db.ReadIdsAsync()).Should().BeEmpty();
}
}