Path: redb-identity/tests/redb.Identity.Tests/TxIntegrity/DeletionHelperTransactionTests.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/TxIntegrity/DeletionHelperTransactionTests.cs
----
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Services;
using redb.Identity.Core.Models;
using redb.Identity.Core.Services;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.TxIntegrity;
///
/// Identity deletions must take part in the caller's transaction.
///
///
/// Every Identity deletion goes through . It used to
/// hand the mark to IBackgroundDeletionService.DeleteAsync, which opens its own DI
/// scope — a second connection — and marks there. Under a route-level transaction
/// (WithRedbTx, live whenever RedbInstanceName is configured, i.e. in every
/// Tsak-hosted deployment) that second connection fights the first: on SQLite it waits on
/// the writer lock the route transaction holds and dies on the busy timeout (~34 s), and on
/// PostgreSQL / MSSQL the mark commits on its own — so a route failing after the deletion
/// rolled back everything except the deletion.
///
///
///
/// These tests pin the invariant directly, without the route stack: an explicit transaction
/// on the same is the same ambient transaction WithRedbTx
/// opens. Before the fix the rollback test failed on PostgreSQL / MSSQL (the row stayed in
/// the trash) and hung, then failed, on SQLite (writer-lock timeout) — the two faces of the
/// same defect. Handed over from the core side as V5 of the trash-lock incident.
///
///
[Collection("Postgres")]
public sealed class DeletionHelperTransactionTests
{
private readonly PostgresFixture _fx;
public DeletionHelperTransactionTests(PostgresFixture fx) => _fx = fx;
private static long NewUserId() => Random.Shared.NextInt64(1_000_000_000, long.MaxValue);
// SessionProps: already schema-synced by the fixture, plain model, cheap to create.
private static SessionProps MakeSession() => new()
{
ApplicationObjectId = 0,
Status = "active",
MfaVerified = false,
MfaMethod = null,
};
private async Task CountSessionsForAsync(long userId)
=> (await _fx.Redb.Query()
.WhereRedb(o => o.Key == userId)
.ToListAsync()).Count;
/// The real service, exactly as the provider's DI extension registers it.
private IBackgroundDeletionService BackgroundDeletion
=> _fx.Services.GetRequiredService();
[Fact]
public async Task Mark_RollsBackWithTheCallerTransaction()
{
var userId = NewUserId();
var id = await _fx.Redb.SaveAsync(
new RedbObject { key = userId, Props = MakeSession() });
(await CountSessionsForAsync(userId)).Should().Be(1, "the seed row is committed before the tx opens");
await using (var tx = await _fx.Redb.Context.BeginTransactionAsync())
{
await IdentityDeletionHelper.DeleteAsync(_fx.Redb, BackgroundDeletion, id);
(await CountSessionsForAsync(userId)).Should().Be(0,
"inside the transaction the object is already under the trash container");
await tx.RollbackAsync();
}
(await CountSessionsForAsync(userId)).Should().Be(1,
"the deletion must roll back with the transaction that contained it. A mark written "
+ "on a second connection commits independently, so a route failing after the delete "
+ "would leave the object trashed — deletion without atomicity.");
}
[Fact]
public async Task Mark_CommitsWithTheCallerTransaction()
{
var userId = NewUserId();
var id = await _fx.Redb.SaveAsync(
new RedbObject { key = userId, Props = MakeSession() });
await using (var tx = await _fx.Redb.Context.BeginTransactionAsync())
{
await IdentityDeletionHelper.DeleteAsync(_fx.Redb, BackgroundDeletion, id);
await tx.CommitAsync();
}
(await CountSessionsForAsync(userId)).Should().Be(0,
"a committed deletion stays deleted — the background service finds the trash "
+ "container by polling and purges it later");
}
[Fact]
public async Task Mark_WithoutAmbientTransaction_StillDeletes()
{
var userId = NewUserId();
var id = await _fx.Redb.SaveAsync(
new RedbObject { key = userId, Props = MakeSession() });
await IdentityDeletionHelper.DeleteAsync(_fx.Redb, BackgroundDeletion, id);
(await CountSessionsForAsync(userId)).Should().Be(0,
"autocommit callers (cleanup timers, stores outside a route tx) must keep working");
}
}