@@ -607,6 +607,43 @@ func TestIterator(t *testing.T) {
607607 }
608608}
609609
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+
610647func TestSetLargerEntryDeletesWrongEntry (t * testing.T ) {
611648 cachesize := 512 * 1024
612649 cache := NewCache (cachesize )
@@ -1041,7 +1078,8 @@ func TestUpdate(t *testing.T) {
10411078 updaterReplace = replace
10421079 }
10431080
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 ) {
10451083 failPrefix := fmt .Sprintf ("%s(%d)" , testName , testCase )
10461084
10471085 if expectedFound != found {
@@ -1054,7 +1092,8 @@ func TestUpdate(t *testing.T) {
10541092 t .Fatalf ("%s unexpected err %v" , failPrefix , err )
10551093 }
10561094 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 ))
10581097 }
10591098
10601099 // Check value
0 commit comments