Path: redb-route/tests/redb.Route.Tests.Llm/InMemoryToolIdempotencyStoreTests.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.Llm/InMemoryToolIdempotencyStoreTests.cs ---- using redb.Route.Llm.Engine.Storage; using redb.Route.Llm.Extensions; using redb.Route.Processors; namespace redb.Route.Tests.Llm; /// /// The expiry of : a completed call is forgotten ttl after it completed, on a /// later reservation; a reservation in flight is never aged out. The clock is a test clock, so no test waits. /// public sealed class InMemoryToolIdempotencyStoreTests { private sealed class ManualTime : TimeProvider { public DateTimeOffset Now { get; set; } = new(2026, 11, 25, 12, 0, 0, TimeSpan.Zero); public override DateTimeOffset GetUtcNow() => Now; } private static readonly TimeSpan Ttl = TimeSpan.FromHours(24); [Fact] public async Task ACompletedCall_IsAHit_UntilTheTtlHasPassed() { var time = new ManualTime(); var store = new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository(), Ttl, time); (await store.TryReserveAsync("c", "tu-1")).IsNew.Should().BeTrue(); await store.CompleteAsync("c", "tu-1", """{"sent":true}"""); time.Now += Ttl - TimeSpan.FromMinutes(1); var hit = await store.TryReserveAsync("c", "tu-1"); hit.IsNew.Should().BeFalse(); hit.CachedOutputJson.Should().Be("""{"sent":true}"""); } [Fact] public async Task ACompletedCall_IsForgotten_OnceTheTtlHasPassed() { var time = new ManualTime(); var store = new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository(), Ttl, time); (await store.TryReserveAsync("c", "tu-1")).IsNew.Should().BeTrue(); await store.CompleteAsync("c", "tu-1", """{"sent":true}"""); time.Now += Ttl + TimeSpan.FromMinutes(1); var again = await store.TryReserveAsync("c", "tu-1"); again.IsNew.Should().BeTrue("the call completed more than the ttl ago, so it is forgotten"); } [Fact] public async Task AnotherCall_ForgetsTheExpiredOnes_SoTheStoreDoesNotGrowWithoutBound() { var time = new ManualTime(); var repository = new InMemoryIdempotentRepository(); var store = new InMemoryToolIdempotencyStore(repository, Ttl, time); for (var i = 0; i < 5; i++) { (await store.TryReserveAsync("c", $"tu-{i}")).IsNew.Should().BeTrue(); await store.CompleteAsync("c", $"tu-{i}", "{}"); } time.Now += Ttl + TimeSpan.FromMinutes(1); (await store.TryReserveAsync("c", "tu-new")).IsNew.Should().BeTrue(); repository.Count.Should().Be(1, "only the new reservation is left"); } [Fact] public async Task AReservationInFlight_IsNotAgedOut_SoASecondRunCannotStart() { var time = new ManualTime(); var store = new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository(), Ttl, time); (await store.TryReserveAsync("c", "tu-long")).IsNew.Should().BeTrue(); // the tool is running, not completed time.Now += Ttl * 3; var second = await store.TryReserveAsync("c", "tu-long"); second.IsNew.Should().BeFalse("the first run has neither completed nor released, however long it takes"); second.CachedOutputJson.Should().Be("{}"); } [Fact] public async Task AReservationInFlight_ThatCompletesLate_IsKeptForTheTtlFromItsCompletion() { var time = new ManualTime(); var store = new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository(), Ttl, time); (await store.TryReserveAsync("c", "tu-long")).IsNew.Should().BeTrue(); time.Now += Ttl * 2; await store.CompleteAsync("c", "tu-long", """{"late":true}"""); time.Now += Ttl - TimeSpan.FromMinutes(1); var hit = await store.TryReserveAsync("c", "tu-long"); hit.IsNew.Should().BeFalse(); hit.CachedOutputJson.Should().Be("""{"late":true}"""); } [Fact] public async Task WithoutATtl_NothingExpires() { var time = new ManualTime(); var store = new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository(), ttl: null, time); (await store.TryReserveAsync("c", "tu-1")).IsNew.Should().BeTrue(); await store.CompleteAsync("c", "tu-1", "{}"); time.Now += TimeSpan.FromDays(365); (await store.TryReserveAsync("c", "tu-1")).IsNew.Should().BeFalse(); } [Theory] [InlineData(0)] [InlineData(-1)] public void ANonPositiveTtl_IsRefused(int seconds) { var act = () => new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository(), TimeSpan.FromSeconds(seconds)); act.Should().Throw(); } [Fact] public void TheDefaultRegistration_RemembersACompletedCallFor24Hours() { LlmServiceCollectionExtensions.DefaultToolIdempotencyTtl.Should().Be(TimeSpan.FromHours(24)); } }