@@ -174,6 +174,86 @@ func TestForcedEvictions(t *testing.T) {
174
174
}
175
175
}
176
176
177
+ func TestForceEvictAllEntries (t * testing.T ) {
178
+ t .Parallel ()
179
+ capacity := 100
180
+ numShards := 1
181
+ ttl := time .Hour
182
+ evictionpercentage := 100
183
+ clock := sturdyc .NewTestClock (time .Now ())
184
+ c := sturdyc .New [string ](capacity , numShards , ttl , evictionpercentage ,
185
+ sturdyc .WithClock (clock ),
186
+ )
187
+
188
+ // Now we're going to write 101 records to the cache which should
189
+ // exceed its capacity and trigger a forced eviction.
190
+ for i := 0 ; i < 101 ; i ++ {
191
+ c .Set (strconv .Itoa (i ), strconv .Itoa (i ))
192
+ }
193
+
194
+ // When the eviction is triggered by the 100th write, we expect the cache to
195
+ // be emptied. Therefore, the 101th write should mean that the size is now 1.
196
+ if c .Size () != 1 {
197
+ t .Errorf ("expected cache size to be 0, got %d" , c .Size ())
198
+ }
199
+ }
200
+
201
+ func TestForceEvictionSameTime (t * testing.T ) {
202
+ t .Parallel ()
203
+ capacity := 100
204
+ numShards := 2
205
+ ttl := time .Hour
206
+ evictionpercentage := 50
207
+ clock := sturdyc .NewTestClock (time .Now ())
208
+ c := sturdyc .New [string ](capacity , numShards , ttl , evictionpercentage ,
209
+ sturdyc .WithClock (clock ),
210
+ )
211
+
212
+ // Now we're going to write 1000 records to the cache which should
213
+ // exceed its capacity and trigger a couple of forced evictions.
214
+ for i := 0 ; i < 1000 ; i ++ {
215
+ c .Set (strconv .Itoa (i ), strconv .Itoa (i ))
216
+ }
217
+
218
+ // Assert that even though we're writing 1000
219
+ // records we never exceed the capacity of 100.
220
+ if c .Size () > 100 {
221
+ t .Errorf ("exceeded the cache size of 100, got %d" , c .Size ())
222
+ }
223
+ }
224
+
225
+ func TestForceEvictionTwoDifferentTimes (t * testing.T ) {
226
+ t .Parallel ()
227
+ capacity := 100
228
+ numShards := 1
229
+ ttl := time .Hour
230
+ evictionpercentage := 10
231
+ clock := sturdyc .NewTestClock (time .Now ())
232
+ c := sturdyc .New [string ](capacity , numShards , ttl , evictionpercentage ,
233
+ sturdyc .WithClock (clock ),
234
+ )
235
+
236
+ // We're going to write 50 records, then move the clock forward
237
+ // and write another 50 to reach the capacity of the cache.
238
+ for i := 0 ; i < 50 ; i ++ {
239
+ c .Set (strconv .Itoa (i ), strconv .Itoa (i ))
240
+ }
241
+ clock .Add (time .Hour )
242
+ for i := 0 ; i < 50 ; i ++ {
243
+ c .Set (strconv .Itoa (i + 50 ), strconv .Itoa (i + 50 ))
244
+ }
245
+
246
+ // At this point, the cache should be at its capacity so
247
+ // adding another item should trigger a forced eviction.
248
+ // Given our eviction percentage of 10%, we expect the
249
+ // cache to first remove 10 items, and then write this
250
+ // record afterwards.
251
+ c .Set (strconv .Itoa (100 ), strconv .Itoa (100 ))
252
+ if c .Size () != 91 {
253
+ t .Errorf ("expected cache size to be 91, got %d" , c .Size ())
254
+ }
255
+ }
256
+
177
257
func TestDisablingForcedEvictionMakesSetANoop (t * testing.T ) {
178
258
t .Parallel ()
179
259
0 commit comments