edge/pkg/storage/driver/cache/lfu/testsuite/test_ttl.go

55 lines
1.1 KiB
Go

package testsuite
import (
"testing"
"time"
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu"
"github.com/pkg/errors"
)
func testTTL(t *testing.T, store lfu.Store[string, string]) error {
const (
key = "mykey"
value = "foobar"
)
ttl := time.Second
cache := lfu.NewCache[string, string](store,
lfu.WithTTL[string, string](ttl),
lfu.WithCapacity[string, string](10),
)
if err := cache.Set(key, value); err != nil {
return errors.WithStack(err)
}
retrieved, err := cache.Get(key)
if err != nil {
return errors.WithStack(err)
}
if e, g := value, retrieved; e != g {
t.Errorf("cache.Get(key): expected '%v', got '%v'", e, g)
}
time.Sleep(ttl * 2)
if _, err := cache.Get(key); !errors.Is(err, lfu.ErrNotFound) {
t.Errorf("cache.Get(key): expected err == lfu.ErrNotFound, got '%v'", err)
}
t.Logf("cache [capacity: %d, size: %d, len: %d]", cache.Capacity(), cache.Size(), cache.Len())
if e, g := 0, cache.Size(); e != g {
t.Errorf("cache.Size(): expected '%v', got '%v'", e, g)
}
if e, g := 0, cache.Len(); e != g {
t.Errorf("cache.Len(): expected '%v', got '%v'", e, g)
}
return nil
}