Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 24 additions & 4 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,40 @@ func TestForceEvictAllEntries(t *testing.T) {
ttl := time.Hour
evictionpercentage := 100
clock := sturdyc.NewTestClock(time.Now())
metricRecorder := newTestMetricsRecorder(numShards)
c := sturdyc.New[string](capacity, numShards, ttl, evictionpercentage,
sturdyc.WithClock(clock),
sturdyc.WithMetrics(metricRecorder),
)

// Now we're going to write 101 records to the cache which should
// exceed its capacity and trigger a forced eviction.
for i := 0; i < 101; i++ {
// Fill the cache to capacity
for i := 0; i < capacity; i++ {
c.Set(strconv.Itoa(i), strconv.Itoa(i))
}

// Record metrics before eviction
preEvictionCount := metricRecorder.evictedEntries

// Trigger eviction by adding one more entry
// When the eviction is triggered by the 100th write, we expect the cache to
// be emptied. Therefore, the 101th write should mean that the size is now 1.
c.Set("trigger", "value")

// When the eviction is triggered, we expect the cache to be emptied
// and only contain the trigger value
if c.Size() != 1 {
t.Errorf("expected cache size to be 0, got %d", c.Size())
t.Errorf("expected cache size to be 1, got %d", c.Size())
}

// Verify eviction metrics
metricRecorder.Lock()
defer metricRecorder.Unlock()
evictedEntries := metricRecorder.evictedEntries - preEvictionCount
if evictedEntries != capacity {
t.Errorf("got %d evicted entries, want %d", evictedEntries, capacity)
}
if metricRecorder.forcedEvictions != 1 {
t.Errorf("got %d forced eviction events, want 1", metricRecorder.forcedEvictions)
}
}

Expand Down
3 changes: 2 additions & 1 deletion shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func (s *shard[T]) forceEvict() {

// Check if we should evict all entries.
if s.evictionPercentage == 100 {
evictedCount := len(s.entries)
s.entries = make(map[string]*entry[T])
s.reportEntriesEvicted(len(s.entries))
s.reportEntriesEvicted(evictedCount)
return
}

Expand Down