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

71 lines
1.5 KiB
Go

package testsuite
import (
"testing"
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu"
"github.com/pkg/errors"
)
func testEviction(t *testing.T, store lfu.Store[string, string]) error {
cache := lfu.NewCache[string, string](store,
lfu.WithCapacity[string, string](10),
lfu.WithLog[string, string](t.Logf),
)
if err := cache.Set("key1", "key1"); err != nil {
return errors.WithStack(err)
}
if err := cache.Set("key2", "key2"); err != nil {
return errors.WithStack(err)
}
// Increment frequency of key2
if _, err := cache.Get("key2"); err != nil {
return errors.WithStack(err)
}
if e, g := 8, cache.Size(); e != g {
t.Errorf("cache.Size(): expected '%v', got '%v'", e, g)
}
if err := cache.Set("key3", "key3"); err != nil {
return errors.WithStack(err)
}
t.Logf("cache [capacity: %d, size: %d, len: %d]", cache.Capacity(), cache.Size(), cache.Len())
_, err := cache.Get("key1")
if err == nil {
t.Errorf("expected 'key1' to be evicted")
}
if !errors.Is(err, lfu.ErrNotFound) {
t.Errorf("expected err to be 'ErrNotFound'")
}
value, err := cache.Get("key2")
if err != nil {
return errors.WithStack(err)
}
if e, g := "key2", value; e < g {
t.Errorf("cache.Get(\"key2\"): expected %v, got %v", e, g)
}
if e, g := cache.Capacity(), cache.Size(); e < g {
t.Errorf("cache.Size(): expected <= %d, got %d", e, g)
}
if e, g := 2, cache.Len(); e != g {
t.Errorf("cache.Len(): expected %d, got %d", e, g)
}
if cache.Size() < 0 {
t.Errorf("cache.Size(): expected value >= 0, got %d", cache.Size())
}
return nil
}