fix(inbound): prune expired ApiMethodCache entries; pin exact-name scopes (review 2026-10-21 05/M3)

This commit is contained in:
Joseph Doherty
2026-10-22 08:29:13 -04:00
parent d7e87c0688
commit 6c9dfaf700
3 changed files with 67 additions and 0 deletions
@@ -122,4 +122,24 @@ public class ApiMethodCacheTests
/// <inheritdoc />
public override DateTimeOffset GetUtcNow() => _now;
}
/// <summary>Verifies that expired entries are pruned on a miss once the cache is large.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task GetOrFetchAsync_Miss_PrunesExpiredEntries()
{
// Review 2026-10-21 05/M3: expired entries for methods no longer called were never evicted.
var time = new FakeTimeProvider(DateTimeOffset.UtcNow);
var cache = new ApiMethodCache(TimeSpan.FromSeconds(1), time);
Task<ApiMethod?> Fetch(CancellationToken _) => Task.FromResult<ApiMethod?>(null);
for (var i = 0; i < 300; i++)
await cache.GetOrFetchAsync("m" + i, Fetch, CancellationToken.None);
Assert.Equal(300, cache.Count);
time.Advance(TimeSpan.FromSeconds(2));
await cache.GetOrFetchAsync("fresh", Fetch, CancellationToken.None);
Assert.Equal(1, cache.Count);
}
}