Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruSoakTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,54 @@ await Threaded.Run(4, () =>

cache.Metrics.Value.Evicted.Should().Be(0);
}

[Theory]
[Repeat(10)]
public async Task WhenConcurrentGetAndClearCacheEndsInConsistentState(int iteration)
{
await Threaded.Run(4, r => {
for (int i = 0; i < 100000; i++)
{
// clear 6,250 times per 1_000_000 iters
if (r == 0 && (i & 15) == 15)
{
lru.Clear();
}

lru.GetOrAdd(i + 1, i => i.ToString());
}
});

this.testOutputHelper.WriteLine($"{iteration} {lru.HotCount} {lru.WarmCount} {lru.ColdCount}");
this.testOutputHelper.WriteLine(string.Join(" ", lru.Keys));

RunIntegrityCheck();
}

[Theory]
[Repeat(10)]
public async Task WhenConcurrentGetAndClearDuringWarmupCacheEndsInConsistentState(int iteration)
{
await Threaded.Run(4, r => {
for (int i = 0; i < 100000; i++)
{
// clear 25,000 times per 1_000_000 iters
// capacity is 9, so we will try to clear before warmup is done
if (r == 0 && (i & 3) == 3)
{
lru.Clear();
}

lru.GetOrAdd(i + 1, i => i.ToString());
}
});

this.testOutputHelper.WriteLine($"{iteration} {lru.HotCount} {lru.WarmCount} {lru.ColdCount}");
this.testOutputHelper.WriteLine(string.Join(" ", lru.Keys));

RunIntegrityCheck();
}

private void RunIntegrityCheck()
{
new ConcurrentLruIntegrityChecker<int, string, LruItem<int, string>, LruPolicy<int, string>, TelemetryPolicy<int, string>>(this.lru).Validate();
Expand Down