Fix race condition in EvictionCount_NotOvercounted_WhenEntryAlreadyRemoved test#126826
Conversation
…nd ScanForExpiredItems tasks from racing with the test's explicit TryGetValue and Compact operations. The race caused under-counting because a background scan could remove an entry from the ConcurrentDictionary but not yet increment _accumulatedEvictions when GetCurrentStatistics was called. Fixes dotnet#126818
|
Tagging subscribers to this area: @dotnet/area-extensions-caching |
There was a problem hiding this comment.
Pull request overview
This PR stabilizes MemoryCacheMetricsTests.EvictionCount_NotOvercounted_WhenEntryAlreadyRemoved by preventing MemoryCache’s background expiration scanning from racing with the test’s explicit expiration-triggering operations, which previously caused intermittent undercounting of eviction statistics.
Changes:
- Set
MemoryCacheOptions.ExpirationScanFrequencytoTimeSpan.MaxValuein the affected test to effectively disable backgroundScanForExpiredItemsscheduling during the test.
mrek-msft
left a comment
There was a problem hiding this comment.
LGTM. There are TimeSpan.Zero in other tests, but these seems correct to me.
|
It's not directly related to this PR, but would it make sense to support |
|
@svick I don't think so. The |
Use
TimeSpan.MaxValueforExpirationScanFrequencyto prevent backgroundScanForExpiredItemstasks from racing with the test's explicitTryGetValueand Compact operations. The race caused under-counting because a background scan could remove an entry from theConcurrentDictionarybut not yet increment_accumulatedEvictionswhenGetCurrentStatisticswas called.Fixes #126818