Path: redb-identity/tests/redb.Identity.Tests/Pipeline/RouteTransactionMarkingTests.cs
Snapshot: redb-identity, 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-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Pipeline/RouteTransactionMarkingTests.cs
----
using AwesomeAssertions;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NSubstitute;
using OpenIddict.Server;
using redb.Core;
using redb.Identity.Core.Configuration;
using redb.Identity.Core.OpenIddict;
using redb.Identity.Contracts.Routes;
using redb.Identity.Core.Services;
using redb.Route.Abstractions;
using Xunit;
using static OpenIddict.Server.OpenIddictServerEvents;
using redb.Identity.Core.Routes;
namespace redb.Identity.Tests.Pipeline;
///
/// G6 / E1 — verifies the route-level IsTransacted bit at definition time.
/// The bit is set by WithRedbTx / WithIdempotentTx(wrapInRedbTx:true)
/// in and is what guarantees rollback of
/// partial writes when a mid-pipeline exception fires.
///
/// As of commit 02d408a7 the mutation routes split into two architectural classes:
///
///
///
/// Still transacted ().
/// The processor body writes through the per-exchange IRedbService that
/// WithRedbTx wraps — atomicity is real and load-bearing, e.g. Logout
/// revokes session + tokens in one route-level tx; Manage* CRUD writes multi-row
/// mutations against the same connection the wrap covers.
///
///
/// No longer transacted ().
/// The processor's primary writers run through a DI-scoped store
/// (OpenIddict managers like RedbApplicationStore / RedbTokenStore,
/// or _sp.GetService<IPasswordHistoryStore>() et al.) that resolves
/// its own IRedbService on a separate DI scope. The route-level wrap
/// covered a different connection from the one doing the actual write; on SQLite
/// this held the writer lock during Argon2id + the OpenIddict pipeline (~30s)
/// while the store's separate connection raced against it on the same file, with
/// 5s busy_timeout × ~7 retries surfacing as 34-second test latencies. The wrap
/// was removed because its atomicity guarantee was already false. The proper fix
/// for these flows is for those stores to share the per-exchange IRedbService
/// (separate refactor); the assert below pins the current architecture so the
/// wrap doesn't get silently re-added and bring the 34-second self-deadlock back.
///
///
///
public sealed class RouteTransactionMarkingTests
{
private readonly IdentityCoreRouteBuilder _builder;
public RouteTransactionMarkingTests()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton(Substitute.For());
// Critical: WithRedbTx is a no-op when RedbInstanceName is null. Production
// bootstraps via AddRedbIdentityServer always populates this; mimic that here
// so the assertions below test the production code path, not the dev fallback.
services.AddSingleton(Options.Create(new RedbIdentityOptions
{
RedbInstanceName = "identity-test"
}));
services.AddDataProtection().UseEphemeralDataProtectionProvider();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddOpenIddict()
.AddServer(options =>
{
options.EnableDegradedMode();
options.SetIssuer(new Uri("https://identity.test.local/"));
options.SetTokenEndpointUris("/connect/token");
options.AllowClientCredentialsFlow();
options.AddEphemeralEncryptionKey();
options.AddEphemeralSigningKey();
options.DisableAccessTokenEncryption();
options.UseRedbRoute();
options.AddEventHandler(builder =>
builder.UseInlineHandler(_ => default)
.SetOrder(int.MaxValue - 100_000).Build());
});
var sp = services.BuildServiceProvider();
_builder = new IdentityCoreRouteBuilder(sp, sp.GetRequiredService>());
((IRouteBuilder)_builder).Configure(null!);
}
public static IEnumerable