35f5084005
git-subtree-dir: backend git-subtree-mainline:dc11190527git-subtree-split:acae976622
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package cache
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestTTLCache_SetGet(t *testing.T) {
|
|
c := New[string, int](time.Minute)
|
|
c.Set("a", 42)
|
|
|
|
v, ok := c.Get("a")
|
|
if !ok || v != 42 {
|
|
t.Fatalf("expected (42, true), got (%d, %v)", v, ok)
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_MissingKey(t *testing.T) {
|
|
c := New[string, int](time.Minute)
|
|
if _, ok := c.Get("missing"); ok {
|
|
t.Fatal("expected miss for key that was never set")
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_Expiry(t *testing.T) {
|
|
c := New[string, int](10 * time.Millisecond)
|
|
c.Set("a", 1)
|
|
|
|
if _, ok := c.Get("a"); !ok {
|
|
t.Fatal("expected hit immediately after Set")
|
|
}
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
if _, ok := c.Get("a"); ok {
|
|
t.Fatal("expected miss after TTL expired")
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_Delete(t *testing.T) {
|
|
c := New[string, int](time.Minute)
|
|
c.Set("a", 1)
|
|
c.Delete("a")
|
|
|
|
if _, ok := c.Get("a"); ok {
|
|
t.Fatal("expected miss after Delete")
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_Clear(t *testing.T) {
|
|
c := New[string, int](time.Minute)
|
|
c.Set("a", 1)
|
|
c.Set("b", 2)
|
|
c.Clear()
|
|
|
|
if _, ok := c.Get("a"); ok {
|
|
t.Fatal("expected miss for 'a' after Clear")
|
|
}
|
|
if _, ok := c.Get("b"); ok {
|
|
t.Fatal("expected miss for 'b' after Clear")
|
|
}
|
|
}
|