58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package cache_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"forge.lclr.dev/AI/xdebug-mcp/internal/cache"
|
|
"forge.lclr.dev/AI/xdebug-mcp/internal/cachegrind"
|
|
)
|
|
|
|
func TestLRU_GetSet(t *testing.T) {
|
|
c := cache.New(2)
|
|
t0 := time.Now()
|
|
p := &cachegrind.Profile{Cmd: "p1"}
|
|
|
|
c.Set("/a", p, t0)
|
|
got, ok := c.Get("/a", t0)
|
|
require.True(t, ok)
|
|
assert.Equal(t, p, got)
|
|
}
|
|
|
|
func TestLRU_Eviction(t *testing.T) {
|
|
c := cache.New(2)
|
|
t0 := time.Now()
|
|
|
|
p1 := &cachegrind.Profile{Cmd: "p1"}
|
|
p2 := &cachegrind.Profile{Cmd: "p2"}
|
|
p3 := &cachegrind.Profile{Cmd: "p3"}
|
|
|
|
c.Set("/a", p1, t0)
|
|
c.Set("/b", p2, t0)
|
|
c.Get("/a", t0) // access /a → /b becomes LRU
|
|
c.Set("/c", p3, t0) // should evict /b
|
|
|
|
_, ok := c.Get("/b", t0)
|
|
assert.False(t, ok, "b should have been evicted")
|
|
|
|
got, ok := c.Get("/a", t0)
|
|
require.True(t, ok)
|
|
assert.Equal(t, p1, got)
|
|
|
|
got, ok = c.Get("/c", t0)
|
|
require.True(t, ok)
|
|
assert.Equal(t, p3, got)
|
|
}
|
|
|
|
func TestLRU_ModtimeInvalidation(t *testing.T) {
|
|
c := cache.New(2)
|
|
t1 := time.Now()
|
|
t2 := t1.Add(time.Second)
|
|
|
|
c.Set("/a", &cachegrind.Profile{Cmd: "old"}, t1)
|
|
_, ok := c.Get("/a", t2) // newer modtime → stale
|
|
assert.False(t, ok)
|
|
}
|