Path: redb/redb.Tests.Integration/Tests/Base/AmbientTransactionTestsBase.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/Base/AmbientTransactionTestsBase.cs
----
using System.Transactions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Core.Data;
using redb.Core.Exceptions;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Helpers;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// redb inside an ambient .
/// A redb.Route .Transacted() block is a TransactionScope, and every database write inside it - redb
/// objects, raw SQL through redb.Context, a second DI scope of the same database - must commit or roll
/// back with that scope. Before the plan none of the three providers did it:
///
/// - MSSQL - a speculative ROLLBACK right after opening a connection killed the enlistment; raw SQL
/// autocommitted and the batch save failed on its savepoint;
/// - PostgreSQL and MSSQL - the key generator and a second scope opened a second connection inside the
/// scope (MSSQL: implicit distributed transaction refused; PostgreSQL: abort at commit);
/// - SQLite - the driver never takes part in System.Transactions, so everything autocommitted silently.
///
/// Verification always goes through a fresh DI scope OUTSIDE any transaction.
///
public abstract class AmbientTransactionTestsBase : IAsyncLifetime
{
private const string NamePrefix = "ambient-tx-";
private readonly IServiceProvider _services;
// Free and Pro collections of one provider share a database and may run at the same time: every test
// removes only the tags it created, never a whole prefix.
private readonly List _tags = new();
protected AmbientTransactionTestsBase(IServiceProvider services) => _services = services;
/// Idempotent DDL of the probe table ambient_tx_rows (tag) in the provider's dialect.
protected abstract string CreateProbeTableSql { get; }
/// A statement that runs for about two seconds in the provider's dialect and returns one number.
protected abstract string SlowStatementSql { get; }
///
/// A standalone wrapper of this collection's database, outside DI. With
/// it carries a session setting the default one does not (lazy references on MSSQL and PostgreSQL, Unicode case
/// folding on SQLite).
///
protected abstract IRedbConnection CreateStandaloneConnection(IServiceProvider services, bool differentSessionSettings);
protected static string ConnString(string name) =>
new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build()
.GetConnectionString(name)!;
public async Task InitializeAsync()
{
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
await redb.SyncSchemeAsync();
await redb.SyncSchemeAsync();
await redb.Context.ExecuteAsync(CreateProbeTableSql);
}
public async Task DisposeAsync()
{
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
foreach (var tag in _tags)
{
await redb.Context.ExecuteAsync($"DELETE FROM _objects WHERE _name LIKE '{tag}%'");
await redb.Context.ExecuteAsync($"DELETE FROM ambient_tx_rows WHERE tag LIKE '{tag}%'");
}
}
private static TransactionScope NewScope() => new(TransactionScopeAsyncFlowOption.Enabled);
private string NewTag()
{
var tag = NamePrefix + Guid.NewGuid().ToString("N")[..10];
_tags.Add(tag);
return tag;
}
private static Task InsertRowAsync(IRedbService redb, string tag)
=> redb.Context.ExecuteAsync("INSERT INTO ambient_tx_rows (tag) VALUES ($1)", tag);
private async Task<(long Objects, long Rows)> CountCommittedAsync(string tag)
{
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
var objects = await redb.Context.ExecuteScalarAsync(
$"SELECT COUNT(*) FROM _objects WHERE _name LIKE '{tag}%'");
var rows = await redb.Context.ExecuteScalarAsync(
$"SELECT COUNT(*) FROM ambient_tx_rows WHERE tag LIKE '{tag}%'");
return (objects, rows);
}
[Fact]
public async Task SaveAndRawSql_CommitWithScope()
{
var tag = NewTag();
using (var tx = NewScope())
{
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
await redb.SaveAsync(TestDataFactory.CreateSimple(tag, 1m));
await InsertRowAsync(redb, tag);
tx.Complete();
}
(await CountCommittedAsync(tag)).Should().Be((1L, 1L),
"the object and the raw row commit together with the scope");
}
[Fact]
public async Task SaveAndRawSql_RollbackWithScope()
{
var tag = NewTag();
using (NewScope())
{
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
await redb.SaveAsync(TestDataFactory.CreateSimple(tag, 2m));
await InsertRowAsync(redb, tag);
// not completed
}
(await CountCommittedAsync(tag)).Should().Be((0L, 0L),
"an abandoned scope takes back both the object and the raw row");
}
[Fact]
public async Task ConnectionOpenedBeforeScope_JoinsScope()
{
var tag = NewTag();
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
// The scope's connection is already open when the transaction starts - the shape of a route that
// used redb in an earlier step of the same exchange.
await redb.Context.ExecuteScalarAsync("SELECT COUNT(*) FROM ambient_tx_rows");
using (NewScope())
{
await redb.SaveAsync(TestDataFactory.CreateSimple(tag, 3m));
await InsertRowAsync(redb, tag);
// not completed
}
(await CountCommittedAsync(tag)).Should().Be((0L, 0L),
"work done inside the scope rolls back with it even on a connection opened before it");
}
[Fact]
public async Task ColdKeyCacheInsideScope_NoSecondConnection()
{
var committed = NewTag();
var abandoned = NewTag();
RedbKeyGeneratorBase.ClearAllCaches();
using (var tx = NewScope())
{
await using var scope = _services.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService()
.SaveAsync(TestDataFactory.CreateSimple(committed, 4m));
tx.Complete();
}
RedbKeyGeneratorBase.ClearAllCaches();
using (NewScope())
{
await using var scope = _services.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService()
.SaveAsync(TestDataFactory.CreateSimple(abandoned, 5m));
}
(await CountCommittedAsync(committed)).Objects.Should().Be(1, "a cold key cache inside the scope still commits");
(await CountCommittedAsync(abandoned)).Objects.Should().Be(0, "and still rolls back");
}
[Fact]
public async Task TwoScopesSameDatabase_OneTransaction()
{
var committed = NewTag();
var abandoned = NewTag();
using (var tx = NewScope())
{
await using var first = _services.CreateAsyncScope();
await using var second = _services.CreateAsyncScope();
await first.ServiceProvider.GetRequiredService()
.SaveAsync(TestDataFactory.CreateSimple(committed + "-a", 6m));
await InsertRowAsync(second.ServiceProvider.GetRequiredService(), committed);
tx.Complete();
}
using (NewScope())
{
await using var first = _services.CreateAsyncScope();
await using var second = _services.CreateAsyncScope();
await first.ServiceProvider.GetRequiredService()
.SaveAsync(TestDataFactory.CreateSimple(abandoned + "-a", 7m));
await InsertRowAsync(second.ServiceProvider.GetRequiredService(), abandoned);
}
(await CountCommittedAsync(committed)).Should().Be((1L, 1L),
"two DI scopes of one database write into the same transaction");
(await CountCommittedAsync(abandoned)).Should().Be((0L, 0L),
"and roll back together");
}
[Fact]
public async Task UniqueViolationInsideScope_TransactionStaysAlive()
{
var tag = NewTag();
await using (var setup = _services.CreateAsyncScope())
{
await setup.ServiceProvider.GetRequiredService().SaveAsync(new RedbObject
{ name = tag + "-winner", ValueUnique = tag + "-KEY", Props = new UniqueOrderProps { Note = "first" } });
}
using (var tx = NewScope())
{
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
var act = async () => await redb.SaveAsync(new RedbObject
{ name = tag + "-loser", ValueUnique = tag + "-KEY", Props = new UniqueOrderProps { Note = "second" } });
await act.Should().ThrowAsync();
(await redb.Context.ExecuteScalarAsync("SELECT 1")).Should().Be(1,
"the scope's transaction survives a caught unique violation");
await redb.SaveAsync(new RedbObject
{ name = tag + "-recovered", ValueUnique = tag + "-OTHER", Props = new UniqueOrderProps { Note = "third" } });
tx.Complete();
}
(await CountCommittedAsync(tag + "-recovered")).Objects.Should().Be(1,
"work done after the recovery commits with the scope");
(await CountCommittedAsync(tag + "-loser")).Objects.Should().Be(0);
}
[Fact]
public async Task ExecuteAtomicWithoutScope_Unchanged()
{
var committed = NewTag();
var failed = NewTag();
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
await redb.Context.ExecuteAtomicAsync(async () =>
{
await redb.SaveAsync(TestDataFactory.CreateSimple(committed, 8m));
await InsertRowAsync(redb, committed);
});
var act = async () => await redb.Context.ExecuteAtomicAsync(async () =>
{
await redb.SaveAsync(TestDataFactory.CreateSimple(failed, 9m));
await InsertRowAsync(redb, failed);
throw new InvalidOperationException("ambient-tx-marker");
});
await act.Should().ThrowAsync().WithMessage("*ambient-tx-marker*");
(await CountCommittedAsync(committed)).Should().Be((1L, 1L));
(await CountCommittedAsync(failed)).Should().Be((0L, 0L));
}
[Fact]
public async Task BeginTransactionInsideScope_StillRefused()
{
using var tx = NewScope();
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
var act = async () => await redb.Context.BeginTransactionAsync();
await act.Should().ThrowAsync().WithMessage("*Ambient TransactionScope*",
"an explicit transaction inside a scope stays refused; a route joins the scope instead");
}
[Fact]
public async Task BatchSaveInsideScope_RollsBack()
{
var tag = NewTag();
var batch = Enumerable.Range(0, 25)
.Select(i => (IRedbObject)TestDataFactory.CreateSimple($"{tag}-{i:D2}", i))
.ToList();
using (NewScope())
{
await using var scope = _services.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService().SaveAsync(batch);
}
(await CountCommittedAsync(tag)).Objects.Should().Be(0, "the bulk path of a batch save rolls back with the scope");
}
[Fact]
public async Task ConcurrentCommandsInOneTransaction_Fail()
{
// Owner decision P1: parallel branches in ONE transaction are not supported and must fail loudly,
// never write silently. Two dependent clones use the transaction's one connection at the same time.
//
// The collision is forced, not hoped for: the first branch writes a row and then runs a statement of about
// two seconds; the second branch starts its command a quarter of a second after the first one announced its
// statement, so that statement is in flight on the shared connection. Two branches merely released together
// (a barrier before each of 200 inserts) overlapped only by chance: an insert takes microseconds on SQLite,
// and under the load of a full run the test failed now and then.
// What it guarantees: the second command meets the first one in flight unless the first one needed more than
// the quarter second just to start, a margin of the whole slow statement.
var tag = NewTag();
Exception? failure = null;
try
{
using var tx = NewScope();
var slowAnnounced = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var branches = Enumerable.Range(0, 2).Select(branch =>
{
var dependent = Transaction.Current!.DependentClone(DependentCloneOption.BlockCommitUntilComplete);
return Task.Run(async () =>
{
try
{
using var branchScope = new TransactionScope(dependent, TransactionScopeAsyncFlowOption.Enabled);
await using var scope = _services.CreateAsyncScope();
var redb = scope.ServiceProvider.GetRequiredService();
if (branch == 0)
{
await InsertRowAsync(redb, $"{tag}-0");
slowAnnounced.SetResult();
await redb.Context.ExecuteScalarAsync(SlowStatementSql);
}
else
{
await slowAnnounced.Task;
await Task.Delay(250);
await InsertRowAsync(redb, $"{tag}-1");
}
branchScope.Complete();
}
finally
{
// A branch that fails before announcing must not leave the other waiting.
slowAnnounced.TrySetResult();
dependent.Complete();
}
});
}).ToArray();
await Task.WhenAll(branches);
tx.Complete();
}
catch (Exception ex)
{
failure = ex;
}
failure.Should().NotBeNull("concurrent writes inside one transaction must fail, not commit silently");
(await CountCommittedAsync(tag)).Rows.Should().Be(0, "nothing of a failed transaction may stay");
}
[Fact]
public async Task OtherSessionSettings_SameDatabase_OneTransaction_AreRefused()
{
// The transaction holds one connection per database, set up by the configuration that opened it. A second
// redb configuration of the same database with other session settings used to run its commands on it with
// settings that are not its own (lazy-reference JSON, collation, case folding), without any error.
await using var first = CreateStandaloneConnection(_services, differentSessionSettings: false);
await using var other = CreateStandaloneConnection(_services, differentSessionSettings: true);
using var tx = NewScope();
(await first.ExecuteScalarAsync("SELECT 1")).Should().Be(1);
var act = () => other.ExecuteScalarAsync("SELECT 1");
(await act.Should().ThrowAsync())
.Which.Message.Should().Contain("session settings");
}
[Fact]
public async Task SameSessionSettings_TwoWrappers_ShareTheTransaction()
{
// Characterization, green before and after: the refusal above must not touch identical configurations.
var tag = NewTag();
await using var first = CreateStandaloneConnection(_services, differentSessionSettings: false);
await using var second = CreateStandaloneConnection(_services, differentSessionSettings: false);
using (NewScope())
{
await first.ExecuteAsync("INSERT INTO ambient_tx_rows (tag) VALUES ($1)", tag);
await second.ExecuteAsync("INSERT INTO ambient_tx_rows (tag) VALUES ($1)", tag + "-2");
// not completed
}
(await CountCommittedAsync(tag)).Rows.Should().Be(0, "both wrappers wrote into the one abandoned transaction");
}
}