go-libp2p-pubsub/timecache/first_seen_cache_test.go
Mohsin Zaidi 973fef56e1
feat: expire messages from the cache based on last seen time (#513)
* feat: expire messages from the cache based on last seen time

* chore: minor renaming

* fix: messages should not be found after expiration

* chore: editorial

* fix: use new time cache strategy consistently

* fix: default to old time cache and add todo for background gc
2023-01-24 02:02:44 +02:00

40 lines
743 B
Go

package timecache
import (
"fmt"
"testing"
"time"
)
func TestFirstSeenCacheFound(t *testing.T) {
tc := newFirstSeenCache(time.Minute)
tc.Add("test")
if !tc.Has("test") {
t.Fatal("should have this key")
}
}
func TestFirstSeenCacheExpire(t *testing.T) {
tc := newFirstSeenCache(time.Second)
for i := 0; i < 11; i++ {
tc.Add(fmt.Sprint(i))
time.Sleep(time.Millisecond * 100)
}
if tc.Has(fmt.Sprint(0)) {
t.Fatal("should have dropped this from the cache already")
}
}
func TestFirstSeenCacheNotFoundAfterExpire(t *testing.T) {
tc := newFirstSeenCache(time.Second)
tc.Add(fmt.Sprint(0))
time.Sleep(1100 * time.Millisecond)
if tc.Has(fmt.Sprint(0)) {
t.Fatal("should have dropped this from the cache already")
}
}