Path: redb-identity/tests/redb.Identity.Tests/Services/InMemoryRateLimitStoreTests.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/Services/InMemoryRateLimitStoreTests.cs ---- using AwesomeAssertions; using Microsoft.Extensions.Time.Testing; using redb.Identity.Core.Services; using Xunit; namespace redb.Identity.Tests.Services; /// /// C1 — Sliding-window correctness for the in-memory backend. /// Uses a deterministic so the GC timer is suppressed and /// window boundaries are tested without real wall-clock waits. /// public sealed class InMemoryRateLimitStoreTests { [Fact] public async Task FirstHitsBelowLimit_AreAllowed() { var time = new FakeTimeProvider(DateTimeOffset.UtcNow); using var store = new InMemoryRateLimitStore(time); for (int i = 1; i <= 5; i++) { var d = await store.TryIncrementAsync("k", limit: 5, window: TimeSpan.FromMinutes(1)); d.Allowed.Should().BeTrue($"hit {i} of 5 must pass"); d.Count.Should().Be(i); } } [Fact] public async Task LimitBreached_ReturnsDeniedAndRetryAfter() { var time = new FakeTimeProvider(DateTimeOffset.UtcNow); using var store = new InMemoryRateLimitStore(time); for (int i = 0; i < 5; i++) await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); var denied = await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); denied.Allowed.Should().BeFalse(); denied.RetryAfter.Should().BeGreaterThan(TimeSpan.Zero); denied.RetryAfter.Should().BeLessThanOrEqualTo(TimeSpan.FromMinutes(1)); } [Fact] public async Task SlidingWindow_ExpiresOldHits_AfterAdvance() { var time = new FakeTimeProvider(DateTimeOffset.UtcNow); using var store = new InMemoryRateLimitStore(time); for (int i = 0; i < 5; i++) await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); // Advance past the window — old hits should be dropped on next call. time.Advance(TimeSpan.FromMinutes(1) + TimeSpan.FromSeconds(1)); var allowedAgain = await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); allowedAgain.Allowed.Should().BeTrue(); allowedAgain.Count.Should().Be(1); } [Fact] public async Task ResetAsync_ClearsBucket() { var time = new FakeTimeProvider(DateTimeOffset.UtcNow); using var store = new InMemoryRateLimitStore(time); for (int i = 0; i < 5; i++) await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); await store.ResetAsync("k"); var afterReset = await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); afterReset.Allowed.Should().BeTrue(); afterReset.Count.Should().Be(1); } [Fact] public async Task PurgeStale_RemovesEmptyExpiredBuckets() { var time = new FakeTimeProvider(DateTimeOffset.UtcNow); using var store = new InMemoryRateLimitStore(time); await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); // Age the bucket past the window so the next increment dequeues hits → empty. time.Advance(TimeSpan.FromMinutes(2)); await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); // re-touches; bucket has 1 hit again // Wait long enough that the bucket is empty *and* untouched. time.Advance(TimeSpan.FromHours(2)); await store.TryIncrementAsync("k", 5, TimeSpan.FromMinutes(1)); // dequeues → empty after this we manually trigger purge // Now mark it really stale: age past 1 hour with no further activity. time.Advance(TimeSpan.FromHours(2)); // Drain the bucket explicitly: a final call after the window leaves count=1 then // we age again and PurgeStale should reap it once it's both empty and untouched. // Simpler invariant test: just call PurgeStale and assert nothing throws. store.PurgeStale(); } }