Path: redb-route/tests/redb.Route.Tests/Idempotent/IdempotentClaimTests.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/Idempotent/IdempotentClaimTests.cs ---- using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Processors; namespace redb.Route.Tests.Idempotent; /// /// : a held key is told apart as in flight or confirmed, an abandoned claim is /// taken over by one caller only, and a repository that does not override it keeps what Add always meant. /// public class IdempotentClaimTests { [Fact] public async Task InMemory_NewKey_IsClaimed_ThenInFlight_ThenConfirmed() { var repository = new InMemoryIdempotentRepository(); (await repository.Claim("k", TimeSpan.FromMinutes(10))).Should().Be(IdempotentClaim.Claimed); (await repository.Claim("k", TimeSpan.FromMinutes(10))).Should().Be(IdempotentClaim.InFlight); await repository.Confirm("k"); (await repository.Claim("k", TimeSpan.FromMinutes(10))).Should().Be(IdempotentClaim.Confirmed); } [Fact] public async Task InMemory_AReleasedKey_IsClaimedAgain() { var repository = new InMemoryIdempotentRepository(); await repository.Claim("k", null); await repository.Remove("k"); (await repository.Claim("k", null)).Should().Be(IdempotentClaim.Claimed); } [Fact] public async Task InMemory_AnUnconfirmedClaimOlderThanTheAge_IsTakenOver_AndTheNewClaimIsInFlight() { var repository = new InMemoryIdempotentRepository(); await repository.Add("k"); await Task.Delay(60); (await repository.Claim("k", TimeSpan.FromMilliseconds(20))).Should().Be(IdempotentClaim.TakenOver); (await repository.Claim("k", TimeSpan.FromMinutes(1))).Should().Be(IdempotentClaim.InFlight, "the takeover is a fresh claim"); } [Fact] public async Task InMemory_WithoutAnAge_AnOldUnconfirmedClaim_StaysInFlight() { var repository = new InMemoryIdempotentRepository(); await repository.Add("k"); await Task.Delay(30); (await repository.Claim("k", null)).Should().Be(IdempotentClaim.InFlight); } [Fact] public async Task InMemory_AConfirmedClaim_IsNeverTakenOver() { var repository = new InMemoryIdempotentRepository(); await repository.Add("k"); await repository.Confirm("k"); await Task.Delay(30); (await repository.Claim("k", TimeSpan.FromMilliseconds(1))).Should().Be(IdempotentClaim.Confirmed); } [Fact] public async Task InMemory_ManyCallersTakingOverOneAbandonedClaim_OneWins() { var repository = new InMemoryIdempotentRepository(); await repository.Add("k"); await Task.Delay(60); var outcomes = await Task.WhenAll(Enumerable.Range(0, 32) .Select(_ => Task.Run(() => repository.Claim("k", TimeSpan.FromMilliseconds(20))))); outcomes.Count(o => o == IdempotentClaim.TakenOver).Should().Be(1); outcomes.Where(o => o != IdempotentClaim.TakenOver).Should().AllBeEquivalentTo(IdempotentClaim.InFlight); } [Fact] public async Task ARepositoryThatDoesNotOverrideClaim_TakesAHeldKeyForConfirmed_AsAddAlwaysMeant() { IIdempotentRepository repository = new AddOnlyRepository(); (await repository.Claim("k", TimeSpan.FromMilliseconds(1))).Should().Be(IdempotentClaim.Claimed); (await repository.Claim("k", TimeSpan.FromMilliseconds(1))).Should().Be(IdempotentClaim.Confirmed); } [Fact] public async Task InMemory_StaleHolderAfterATakeover_SettlesNothing_TheNewHolderSettles() { var repository = new InMemoryIdempotentRepository(); var first = await repository.Lease("k", TimeSpan.FromMinutes(1)); first.Outcome.Should().Be(IdempotentClaim.Claimed); await Task.Delay(30); var second = await repository.Lease("k", TimeSpan.FromMilliseconds(1)); second.Outcome.Should().Be(IdempotentClaim.TakenOver); second.Token.Should().NotBe(first.Token); (await repository.Release("k", first.Token)).Should().BeFalse("the first holder's claim was taken over"); (await repository.Confirm("k", first.Token)).Should().BeFalse(); (await repository.Lease("k", null)).Outcome.Should().Be(IdempotentClaim.InFlight, "the new holder's claim stays, unconfirmed"); (await repository.Confirm("k", second.Token)).Should().BeTrue(); (await repository.Lease("k", null)).Should().Be(new IdempotentLease(IdempotentClaim.Confirmed, null)); } [Fact] public async Task InMemory_AKeyTakenByAdd_HasNoToken_AndNoTokenSettlesIt() { var repository = new InMemoryIdempotentRepository(); await repository.Add("k"); (await repository.Release("k", Guid.NewGuid().ToString("N"))).Should().BeFalse(); (await repository.Contains("k")).Should().BeTrue(); (await repository.Release("k", null)).Should().BeTrue("a null token removes the key as Remove does"); (await repository.Contains("k")).Should().BeFalse(); } [Fact] public async Task ARepositoryThatDoesNotOverrideLease_LeasesWithoutAToken_AndSettlesTheKey() { IIdempotentRepository repository = new AddOnlyRepository(); var lease = await repository.Lease("k", TimeSpan.FromMinutes(1)); lease.Should().Be(new IdempotentLease(IdempotentClaim.Claimed, null)); (await repository.Release("k", lease.Token)).Should().BeTrue(); (await repository.Contains("k")).Should().BeFalse("the default Release removes the key"); } /// A repository written before Claim: it implements the members it had then. private sealed class AddOnlyRepository : IIdempotentRepository { private readonly HashSet _keys = []; public Task Add(string key, CancellationToken ct = default) => Task.FromResult(_keys.Add(key)); public Task Confirm(string key, CancellationToken ct = default) => Task.CompletedTask; public Task Remove(string key, CancellationToken ct = default) => Task.FromResult(_keys.Remove(key)); public Task Contains(string key, CancellationToken ct = default) => Task.FromResult(_keys.Contains(key)); public Task Clear(CancellationToken ct = default) { _keys.Clear(); return Task.CompletedTask; } } }