mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-02 21:03:07 +00:00
* 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
40 lines
743 B
Go
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")
|
|
}
|
|
}
|