@@ -309,32 +309,6 @@ func TestExpiring(t *testing.T) {
309309 }
310310}
311311
312- func BenchmarkCache (b * testing.B ) {
313- for _ , tt := range cacheTests {
314- b .Run ("Benchmark" + tt .cont .String ()+ "Cache" , func (b * testing.B ) {
315- keys := []interface {}{}
316- cache := tt .cont .New (0 )
317-
318- for i := 0 ; i < 100 ; i ++ {
319- keys = append (keys , i )
320- }
321-
322- b .ResetTimer ()
323- b .RunParallel (func (pb * testing.PB ) {
324- for pb .Next () {
325- key := keys [rand .Intn (100 )]
326- _ , ok := cache .Load (key )
327- if ok {
328- cache .Delete (key )
329- } else {
330- cache .Store (key , struct {}{})
331- }
332- }
333- })
334- })
335- }
336- }
337-
338312func TestNotify (t * testing.T ) {
339313 for _ , tt := range cacheTests {
340314 t .Run ("Test" + tt .cont .String ()+ "CacheNotify" , func (t * testing.T ) {
@@ -359,5 +333,46 @@ func TestNotify(t *testing.T) {
359333 }
360334 })
361335 }
336+ }
337+
338+ func TestGC (t * testing.T ) {
339+ for _ , tt := range cacheTests {
340+ t .Run ("Test" + tt .cont .String ()+ "CacheGC" , func (t * testing.T ) {
341+ cache := tt .cont .NewUnsafe (0 )
342+ cache .StoreWithTTL (0 , 0 , time .Nanosecond )
343+ cache .StoreWithTTL (1 , 1 , time .Millisecond * 100 )
344+ dur := cache .GC ()
345+
346+ assert .GreaterOrEqual (t , int64 (dur ), int64 (time .Millisecond * 99 ))
347+ time .Sleep (dur )
348+
349+ assert .Zero (t , int (cache .GC ()))
350+ assert .Zero (t , cache .Len ())
351+ })
352+ }
353+ }
354+ func BenchmarkCache (b * testing.B ) {
355+ for _ , tt := range cacheTests {
356+ b .Run ("Benchmark" + tt .cont .String ()+ "Cache" , func (b * testing.B ) {
357+ keys := []interface {}{}
358+ cache := tt .cont .New (0 )
359+
360+ for i := 0 ; i < 100 ; i ++ {
361+ keys = append (keys , i )
362+ }
362363
364+ b .ResetTimer ()
365+ b .RunParallel (func (pb * testing.PB ) {
366+ for pb .Next () {
367+ key := keys [rand .Intn (100 )]
368+ _ , ok := cache .Load (key )
369+ if ok {
370+ cache .Delete (key )
371+ } else {
372+ cache .Store (key , struct {}{})
373+ }
374+ }
375+ })
376+ })
377+ }
363378}
0 commit comments