Skip to content
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2241,16 +2241,24 @@ public async Task MemoryCache_Consistency_ConcurrentOperationsWithEviction_Inter
cache.BorrowedEntryCount.Should().Be(0,
"All handles returned after concurrent operations");

// Size must be consistent with entry count
int entryCount = cache.EntryCount;
// Verify invariants that hold without an atomic snapshot across properties.
// EntryCount and CurrentSizeBytes are updated via separate Interlocked ops,
// so reading them sequentially can observe a transient mismatch. Instead,
// verify each property satisfies its own invariant independently.
long currentSize = cache.CurrentSizeBytes;
int entryCount = cache.EntryCount;

currentSize.Should().Be(entryCount * entrySize,
$"Size mismatch: CurrentSizeBytes={currentSize}, EntryCount={entryCount}, expected={entryCount * entrySize}");
currentSize.Should().BeGreaterThanOrEqualTo(0,
"CurrentSizeBytes must never go negative");

// Size should not exceed capacity
currentSize.Should().BeLessThanOrEqualTo(entrySize * cacheCapacity,
Comment thread
hooyao marked this conversation as resolved.
Outdated
"CurrentSizeBytes should not exceed capacity after evictions");

(currentSize % entrySize).Should().Be(0,
$"CurrentSizeBytes ({currentSize}) should be a multiple of entry size ({entrySize}) — no partial entry leaks");

if (entryCount == 0)
currentSize.Should().Be(0, "If no entries remain, size must be zero");
Comment thread
hooyao marked this conversation as resolved.
Outdated
}

/// <summary>
Expand Down
Loading