@@ -329,18 +329,18 @@ func Test_Cache_set(t *testing.T) {
329
329
// recreate situation when expired item gets updated
330
330
// and not auto-cleaned up yet.
331
331
c := New [string , struct {}](
332
- WithDisableTouchOnHit [string ,struct {}](),
332
+ WithDisableTouchOnHit [string , struct {}](),
333
333
)
334
334
335
335
// insert an item and let it expire
336
336
c .Set ("test" , struct {}{}, 1 )
337
- time .Sleep (50 * time .Millisecond )
337
+ time .Sleep (50 * time .Millisecond )
338
338
339
339
// update the expired item
340
340
updatedItem := c .Set ("test" , struct {}{}, 100 * time .Millisecond )
341
341
342
342
// eviction should not happen as we prolonged element
343
- cl := c .OnEviction (func (_ context.Context , _ EvictionReason , item * Item [string , struct {}]){
343
+ cl := c .OnEviction (func (_ context.Context , _ EvictionReason , item * Item [string , struct {}]) {
344
344
t .Errorf ("eviction happened even though item has not expired yet: key=%s, evicted item expiresAt=%s, updated item expiresAt=%s, now=%s" ,
345
345
item .Key (),
346
346
item .ExpiresAt ().String (),
@@ -351,7 +351,7 @@ func Test_Cache_set(t *testing.T) {
351
351
// and update expired before its removal
352
352
go c .Start ()
353
353
354
- time .Sleep (90 * time .Millisecond )
354
+ time .Sleep (90 * time .Millisecond )
355
355
cl ()
356
356
c .Stop ()
357
357
}
@@ -848,6 +848,28 @@ func Test_Cache_Range(t *testing.T) {
848
848
})
849
849
}
850
850
851
+ func Test_Cache_RangeBackwards (t * testing.T ) {
852
+ c := prepCache (DefaultTTL )
853
+ addToCache (c , time .Nanosecond , "1" )
854
+ addToCache (c , time .Hour , "2" , "3" , "4" , "5" )
855
+
856
+ var results []string
857
+
858
+ c .RangeBackwards (func (item * Item [string , string ]) bool {
859
+ results = append (results , item .Key ())
860
+ return item .Key () != "4"
861
+ })
862
+
863
+ assert .Equal (t , []string {"2" , "3" , "4" }, results )
864
+
865
+ emptyCache := New [string , string ]()
866
+ assert .NotPanics (t , func () {
867
+ emptyCache .RangeBackwards (func (item * Item [string , string ]) bool {
868
+ return false
869
+ })
870
+ })
871
+ }
872
+
851
873
func Test_Cache_Metrics (t * testing.T ) {
852
874
cache := Cache [string , string ]{
853
875
metrics : Metrics {Evictions : 10 },
0 commit comments