Path: redb-route/tests/redb.Route.Tests.Core/GetRedbServiceExchangeScopeTests.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.Core/GetRedbServiceExchangeScopeTests.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.RedbCore.Extensions;
namespace redb.Route.Tests.Core;
///
/// A host outside Tsak registers its named database with RegisterRedbService and publishes no scope factory.
/// With an exchange, handed every
/// exchange that one instance: one connection for all parallel exchanges. An instance built through a container now
/// opens a scope of the same database per exchange, as the published factory does in Tsak.
///
public sealed class GetRedbServiceExchangeScopeTests
{
private sealed class TrackingScope : IServiceScope, IAsyncDisposable
{
public int DisposeCount;
public IServiceProvider ServiceProvider { get; } = Substitute.For();
public void Dispose() => Interlocked.Increment(ref DisposeCount);
public ValueTask DisposeAsync() { Interlocked.Increment(ref DisposeCount); return ValueTask.CompletedTask; }
}
private static (IRedbService Instance, List Opened) ScopeSource()
{
var opened = new List();
var instance = Substitute.For();
instance.CanCreateScope.Returns(true);
instance.CreateScope().Returns(_ =>
{
var tracking = new TrackingScope();
opened.Add(tracking);
return new RedbScope(Substitute.For(), tracking);
});
return (instance, opened);
}
private static IRouteContext NamedWithoutFactory(IRedbService instance)
{
var context = Substitute.For();
context.GetFromRegistry(Arg.Any()).Returns((IServiceScopeFactory?)null);
context.GetFromRegistry("redb:orders").Returns(instance);
return context;
}
[Fact]
public async Task Named_NoFactory_InstanceFromContainer_EachExchangeGetsItsOwnScope()
{
var (instance, opened) = ScopeSource();
var context = NamedWithoutFactory(instance);
var first = new Exchange();
var second = new Exchange();
var a1 = context.GetRedbService("orders", first);
var a2 = context.GetRedbService("orders", first);
var b = context.GetRedbService("orders", second);
a1.Should().NotBeSameAs(instance, "parallel exchanges must not share the registered instance's connection");
a2.Should().BeSameAs(a1, "one exchange keeps its scope for every step");
b.Should().NotBeSameAs(a1);
opened.Should().HaveCount(2);
await first.DisposeAsync();
opened[0].DisposeCount.Should().Be(1, "the scope ends with its exchange");
opened[1].DisposeCount.Should().Be(0);
await second.DisposeAsync();
opened[1].DisposeCount.Should().Be(1);
}
[Fact]
public void Named_NoFactory_InstanceWithoutContainer_KeepsTheSharedInstance()
{
var instance = Substitute.For();
instance.CanCreateScope.Returns(false);
var context = NamedWithoutFactory(instance);
context.GetRedbService("orders", new Exchange()).Should().BeSameAs(instance);
instance.DidNotReceive().CreateScope();
}
[Fact]
public void Named_PublishedFactory_Wins_InstanceIsNotAsked()
{
var (instance, opened) = ScopeSource();
var context = Substitute.For();
context.GetFromRegistry("redb-factory:orders").Returns(new ServiceCollection()
.AddScoped(_ => Substitute.For())
.BuildServiceProvider()
.GetRequiredService());
context.GetFromRegistry("redb:orders").Returns(instance);
context.GetRedbService("orders", new Exchange()).Should().NotBeSameAs(instance);
opened.Should().BeEmpty();
}
[Fact]
public void Named_WithoutExchange_KeepsTheInstance_ForStartUpCode()
{
var (instance, opened) = ScopeSource();
var context = NamedWithoutFactory(instance);
context.GetRedbService("orders", exchange: null).Should().BeSameAs(instance);
opened.Should().BeEmpty();
}
[Fact]
public async Task Default_ServiceRegisteredOnContext_NoProvider_EachExchangeGetsItsOwnScope()
{
var (registered, opened) = ScopeSource();
var context = Substitute.For();
context.GetService().Returns(registered);
context.GetServiceProvider().Returns((IServiceProvider?)null);
var first = new Exchange();
var second = new Exchange();
var a1 = context.GetRedbService(string.Empty, first);
var a2 = context.GetRedbService(string.Empty, first);
var b = context.GetRedbService(string.Empty, second);
a1.Should().NotBeSameAs(registered);
a2.Should().BeSameAs(a1);
b.Should().NotBeSameAs(a1);
await first.DisposeAsync();
opened[0].DisposeCount.Should().Be(1);
}
}