Path: redb/redb.Tests.Integration/Tests/Base/DisposedScopeGuardTestsBase.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/DisposedScopeGuardTestsBase.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Core.Data;
namespace redb.Tests.Integration.Tests.Base;
///
/// A context whose DI scope has ended must refuse to work, not quietly open a fresh physical
/// connection on behalf of a dead scope. Before the fix, GetOpenConnectionAsync saw the
/// null left behind by Dispose and re-opened: the new connection belonged to nobody, the
/// second Dispose was a no-op, and the pool slot was gone for the life of the process (a consumer's production database,
/// 2026-09: ~30 idle PostgreSQL sessions a day until restart).
///
public abstract class DisposedScopeGuardTestsBase
{
private readonly IServiceProvider _root;
protected DisposedScopeGuardTestsBase(IServiceProvider root) => _root = root;
[Fact]
public async Task Context_AfterItsScopeIsDisposed_ThrowsInsteadOfReopening()
{
var scope = _root.CreateAsyncScope();
var ctx = scope.ServiceProvider.GetRequiredService();
// The context has a live connection now; the scope then ends.
(await ctx.ExecuteScalarAsync("SELECT 1")).Should().Be(1);
await scope.DisposeAsync();
ctx.IsDisposed.Should().BeTrue();
var act = () => ctx.ExecuteScalarAsync("SELECT 1");
await act.Should().ThrowAsync(
"a disposed context must fail loudly; re-opening a connection here leaks it for the life of the process");
}
}