Path: redb/redb.Tests.Integration/Tests/Base/ConnectionTeardownTestsBase.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/ConnectionTeardownTestsBase.cs
----
using Microsoft.Extensions.Configuration;
using redb.Core.Data;
namespace redb.Tests.Integration.Tests.Base;
///
/// Scope-teardown contract of (a production report, 2026-09-11):
/// disposing a connection must not race an in-flight command. The reported shape: an HTTP
/// exchange's ReleaseScopes() hit NpgsqlOperationInProgressException because a
/// SEDA-side lazy loader was still reading through the dying scope's connection - the
/// command guard existed, but DisposeAsync was exempt from it.
///
/// Pinned contract: (1) DisposeAsync WAITS for the running command, both complete cleanly;
/// (2) a command entered after teardown is refused with -
/// the lazy loader's cue to fall back to a detached scope.
///
public abstract class ConnectionTeardownTestsBase
{
/// A fresh, self-owned connection - never the fixture's shared one.
protected abstract IRedbConnection CreateConnection();
/// A scalar query that runs for roughly the given time on this provider.
protected abstract string SleepSql(double seconds);
protected virtual string ProbeSql => "SELECT 1";
protected static string ConnString(string name) =>
new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build()
.GetConnectionString(name)!;
[Fact]
public async Task Dispose_WaitsForTheInFlightCommand()
{
var conn = CreateConnection();
var clock = System.Diagnostics.Stopwatch.StartNew();
long commandDoneAt = 0;
var command = Task.Run(async () =>
{
await conn.ExecuteScalarAsync(SleepSql(1.5));
Interlocked.Exchange(ref commandDoneAt, clock.ElapsedMilliseconds);
});
await Task.Delay(300); // the command is reliably in flight by now
// Capped awaits: the un-fixed teardown can HANG under a running command (worse than
// the reported exception) - a hang must fail the pin, not the whole run.
var dispose = conn.DisposeAsync().AsTask();
(await Task.WhenAny(dispose, Task.Delay(TimeSpan.FromSeconds(20)))).Should().Be(dispose,
"teardown must complete once the in-flight command finishes, never hang");
await dispose; // propagate a teardown fault (the reported OperationInProgress shape)
var disposeDoneAt = clock.ElapsedMilliseconds;
// The command must complete cleanly - not die under a concurrent Reset/Close.
(await Task.WhenAny(command, Task.Delay(TimeSpan.FromSeconds(20)))).Should().Be(command);
await command;
var cmdAt = Interlocked.Read(ref commandDoneAt);
cmdAt.Should().BeGreaterThan(0);
disposeDoneAt.Should().BeGreaterThanOrEqualTo(cmdAt,
"teardown must wait for the in-flight command instead of racing it");
}
[Fact]
public async Task Command_AfterDispose_IsRefusedWithObjectDisposed()
{
var conn = CreateConnection();
await conn.ExecuteScalarAsync(ProbeSql); // open the physical connection for real
await conn.DisposeAsync();
var act = async () => await conn.ExecuteScalarAsync(ProbeSql);
await act.Should().ThrowAsync(
"a command entered after teardown must be refused cleanly - the lazy loader " +
"falls back to a detached scope on this signal");
}
}