Skip to content

Commit 1b8cef1

Browse files
orr-a-securityMzack9999
authored andcommitted
added tests for eviction strategy
1 parent 5939cea commit 1b8cef1

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

pkg/storage/storagedb_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,49 @@ func doStuffWithOtherCache(cache cache.Cache) {
133133
_, _ = cache.GetIfPresent(strconv.Itoa(i))
134134
}
135135
}
136+
137+
func TestSlidingEvictionStrategy(t *testing.T) {
138+
testTTL := 100 * time.Millisecond
139+
smallDelay := 10 * time.Millisecond
140+
mem, err := New(&Options{EvictionTTL: testTTL, EvictionStrategy: EvictionStrategySliding})
141+
require.Nil(t, err)
142+
defer mem.Close()
143+
144+
err = mem.SetID("test-sliding")
145+
require.Nil(t, err)
146+
147+
// Access after half TTL - should extend expiration
148+
time.Sleep(testTTL / 2)
149+
_, ok := mem.cache.GetIfPresent("test-sliding")
150+
require.True(t, ok)
151+
152+
// Still present after original TTL due to sliding window
153+
time.Sleep(testTTL / 2 + smallDelay)
154+
_, ok = mem.cache.GetIfPresent("test-sliding")
155+
require.True(t, ok)
156+
157+
// Should be expired after full TTL despite access
158+
time.Sleep(testTTL + smallDelay)
159+
_, ok = mem.cache.GetIfPresent("test-sliding")
160+
require.False(t, ok)
161+
}
162+
163+
func TestFixedEvictionStrategy(t *testing.T) {
164+
testTTL := 100 * time.Millisecond
165+
mem, err := New(&Options{EvictionTTL: testTTL, EvictionStrategy: EvictionStrategyFixed})
166+
require.Nil(t, err)
167+
defer mem.Close()
168+
169+
err = mem.SetID("test-fixed")
170+
require.Nil(t, err)
171+
172+
// Access after half TTL - should NOT extend expiration
173+
time.Sleep(testTTL / 2)
174+
_, ok := mem.cache.GetIfPresent("test-fixed")
175+
require.True(t, ok)
176+
177+
// Should be expired after full TTL despite access
178+
time.Sleep(testTTL / 2 + 10 * time.Millisecond)
179+
_, ok = mem.cache.GetIfPresent("test-fixed")
180+
require.False(t, ok)
181+
}

0 commit comments

Comments
 (0)