73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package eviction
|
|
|
|
import (
|
|
"fmt"
|
|
"s1d3sw1ped/steamcache2/vfs"
|
|
"s1d3sw1ped/steamcache2/vfs/disk"
|
|
"s1d3sw1ped/steamcache2/vfs/memory"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetEvictionFunction_Default(t *testing.T) {
|
|
t.Parallel()
|
|
fn := GetEvictionFunction("unknown-strategy")
|
|
if fn == nil {
|
|
t.Fatal("default eviction fn nil")
|
|
}
|
|
// Should be LRU
|
|
m := memory.New(1024)
|
|
// create something to evict
|
|
w, _ := m.Create("f", 100)
|
|
w.Write(make([]byte, 100))
|
|
w.Close()
|
|
evicted := fn(m, 50)
|
|
if evicted == 0 {
|
|
t.Log("no eviction (cap may allow)")
|
|
}
|
|
}
|
|
|
|
func TestEvictLRU_Delegates(t *testing.T) {
|
|
t.Parallel()
|
|
m := memory.New(1024)
|
|
w, _ := m.Create("f1", 1000) // > cap - needed to force
|
|
w.Write(make([]byte, 1000))
|
|
w.Close()
|
|
evicted := EvictLRU(m, 100)
|
|
if evicted == 0 {
|
|
t.Error("expected some eviction under pressure")
|
|
}
|
|
}
|
|
|
|
// Table-driven coverage for all strategies + disk dispatch + unknown fallback (strengthens eviction pkg per issues9,23).
|
|
func TestEviction_StrategiesAndDispatch(t *testing.T) {
|
|
t.Parallel()
|
|
cases := []struct {
|
|
name string
|
|
fn func(vfs.VFS, uint) uint
|
|
}{
|
|
{"LRU", EvictLRU},
|
|
{"FIFO", EvictFIFO},
|
|
{"LFU", EvictLFU},
|
|
{"Largest", EvictLargest},
|
|
{"Smallest", EvictSmallest},
|
|
{"Hybrid", EvictHybrid},
|
|
{"unknown", GetEvictionFunction("nope")},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
m := memory.New(2048)
|
|
w, _ := m.Create(fmt.Sprintf("e%04d", 1), 1500)
|
|
w.Write(make([]byte, 1500))
|
|
w.Close()
|
|
_ = c.fn(m, 100)
|
|
// disk path too (no real fs ops needed for dispatch)
|
|
td := t.TempDir()
|
|
d := disk.New(td, 2048)
|
|
w2, _ := d.Create(fmt.Sprintf("e%04d", 2), 1500)
|
|
w2.Write(make([]byte, 1500))
|
|
w2.Close()
|
|
_ = c.fn(d, 100)
|
|
})
|
|
}
|
|
}
|