MCPcopy
hub / github.com/coocood/freecache / TestIteratorExpireAt

Function TestIteratorExpireAt

cache_test.go:757–792  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

755}
756
757func TestIteratorExpireAt(t *testing.T) {
758 cache := NewCache(1024)
759 expireSecond := uint32(5)
760 // Set some value that expires to make sure expired entry is not returned.
761 cache.Set([]byte("no_expire"), []byte("def"), 0)
762 cache.Set([]byte("has_expire"), []byte("exp"), int(expireSecond))
763
764 it := cache.NewIterator()
765 for {
766 next := it.Next()
767 if next == nil {
768 break
769 }
770 if string(next.Key) == "no_expire" && next.ExpireAt != 0 {
771 t.Fatalf("no_expire's ExpireAt should be 0")
772 }
773 expectExpireAt := uint32(time.Now().Unix()) + expireSecond
774 if string(next.Key) == "has_expire" && next.ExpireAt != expectExpireAt {
775 t.Fatalf("has_expire's ExpireAt should be 10,actually is %d", next.ExpireAt)
776 }
777 }
778 time.Sleep(time.Duration(expireSecond) * time.Second)
779 it2 := cache.NewIterator()
780 for {
781 next := it2.Next()
782 if next == nil {
783 return
784 }
785 if string(next.Key) == "no_expire" && next.ExpireAt != 0 {
786 t.Fatalf("no_expire's ExpireAt should be 0")
787 }
788 if string(next.Key) == "has_expire" {
789 t.Fatalf("has_expire should expired")
790 }
791 }
792}
793
794func TestSetLargerEntryDeletesWrongEntry(t *testing.T) {
795 cachesize := 512 * 1024

Callers

nothing calls this directly

Calls 5

SetMethod · 0.95
NewIteratorMethod · 0.95
NextMethod · 0.95
NewCacheFunction · 0.85
NowMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…