feda55e225
- Updated `disk.New` to support asynchronous initialization for large caches, improving responsiveness during startup. - Introduced an eviction function parameter to `disk.New`, ensuring proper handling of over-capacity scenarios. - Enhanced error handling in various components, including memory and disk tests, to ensure robustness and clarity. - Refactored tests to validate new behaviors, including checks for delayed attachment and proper error propagation. - Removed obsolete error handling code and tests related to the now-deleted errors package, streamlining the codebase.
85 lines
1.9 KiB
Go
85 lines
1.9 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, err := memory.New(1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 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, err := memory.New(1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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, err := memory.New(2048)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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, err := disk.New(td, 2048, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w2, _ := d.Create(fmt.Sprintf("e%04d", 2), 1500)
|
|
w2.Write(make([]byte, 1500))
|
|
w2.Close()
|
|
_ = c.fn(d, 100)
|
|
})
|
|
}
|
|
}
|