@@ -607,6 +607,43 @@ func TestIterator(t *testing.T) {
607
607
}
608
608
}
609
609
610
+ func TestIteratorExpireAt (t * testing.T ) {
611
+ cache := NewCache (1024 )
612
+ expireSecond := uint32 (5 )
613
+ // Set some value that expires to make sure expired entry is not returned.
614
+ cache .Set ([]byte ("no_expire" ), []byte ("def" ), 0 )
615
+ cache .Set ([]byte ("has_expire" ), []byte ("exp" ), int (expireSecond ))
616
+
617
+ it := cache .NewIterator ()
618
+ for {
619
+ next := it .Next ()
620
+ if next == nil {
621
+ break
622
+ }
623
+ if string (next .Key ) == "no_expire" && next .ExpireAt != 0 {
624
+ t .Fatalf ("no_expire's ExpireAt should be 0" )
625
+ }
626
+ expectExpireAt := uint32 (time .Now ().Unix ()) + expireSecond
627
+ if string (next .Key ) == "has_expire" && next .ExpireAt != expectExpireAt {
628
+ t .Fatalf ("has_expire's ExpireAt should be 10,actually is %d" , next .ExpireAt )
629
+ }
630
+ }
631
+ time .Sleep (time .Duration (expireSecond ) * time .Second )
632
+ it2 := cache .NewIterator ()
633
+ for {
634
+ next := it2 .Next ()
635
+ if next == nil {
636
+ return
637
+ }
638
+ if string (next .Key ) == "no_expire" && next .ExpireAt != 0 {
639
+ t .Fatalf ("no_expire's ExpireAt should be 0" )
640
+ }
641
+ if string (next .Key ) == "has_expire" {
642
+ t .Fatalf ("has_expire should expired" )
643
+ }
644
+ }
645
+ }
646
+
610
647
func TestSetLargerEntryDeletesWrongEntry (t * testing.T ) {
611
648
cachesize := 512 * 1024
612
649
cache := NewCache (cachesize )
@@ -1041,7 +1078,8 @@ func TestUpdate(t *testing.T) {
1041
1078
updaterReplace = replace
1042
1079
}
1043
1080
1044
- assertExpectations := func (testCase int , expectedFound , expectedReplaced bool , expectedPrevVal []byte , expectedVal []byte ) {
1081
+ assertExpectations := func (testCase int , expectedFound , expectedReplaced bool , expectedPrevVal []byte ,
1082
+ expectedVal []byte ) {
1045
1083
failPrefix := fmt .Sprintf ("%s(%d)" , testName , testCase )
1046
1084
1047
1085
if expectedFound != found {
@@ -1054,7 +1092,8 @@ func TestUpdate(t *testing.T) {
1054
1092
t .Fatalf ("%s unexpected err %v" , failPrefix , err )
1055
1093
}
1056
1094
if string (prevVal ) != string (expectedPrevVal ) {
1057
- t .Fatalf ("%s previous value expected %s instead of %s" , failPrefix , string (expectedPrevVal ), string (prevVal ))
1095
+ t .Fatalf ("%s previous value expected %s instead of %s" , failPrefix , string (expectedPrevVal ),
1096
+ string (prevVal ))
1058
1097
}
1059
1098
1060
1099
// Check value
0 commit comments