86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package gc
|
|
|
|
import (
|
|
"s1d3sw1ped/steamcache2/vfs/memory"
|
|
"testing"
|
|
)
|
|
|
|
func TestGCFS_BasicEvictOnCreate(t *testing.T) {
|
|
t.Parallel()
|
|
m := memory.New(400)
|
|
g := New(m, LRU)
|
|
|
|
// Fill over
|
|
for i := 0; i < 5; i++ {
|
|
w, err := g.Create("g"+string(rune('0'+i)), 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.Write(make([]byte, 100))
|
|
w.Close()
|
|
}
|
|
// GC should have run in Create path
|
|
if g.Size() > g.Capacity() {
|
|
t.Errorf("GCFS size %d exceeded cap %d", g.Size(), g.Capacity())
|
|
}
|
|
}
|
|
|
|
func TestAsyncGCFS_Stop(t *testing.T) {
|
|
t.Parallel()
|
|
m := memory.New(1 << 20)
|
|
ag := NewAsync(m, LRU, true, 0.7, 0.9, 1.0)
|
|
// do some creates
|
|
for i := 0; i < 3; i++ {
|
|
w, _ := ag.Create("a"+string(rune(i)), 4096)
|
|
w.Write(make([]byte, 4096))
|
|
w.Close()
|
|
}
|
|
ag.Stop()
|
|
// Stop waits on wg; no sleep needed. Post-stop calls should be safe (ctx done paths).
|
|
// (removed brittle sleep per issue7)
|
|
|
|
// Idempotent stop + post-stop ops (no panic)
|
|
ag.Stop()
|
|
_ = ag.IsGCRunning()
|
|
}
|
|
|
|
func TestGCFS_ForceAndStats(t *testing.T) {
|
|
t.Parallel()
|
|
m := memory.New(500)
|
|
g := New(m, LRU)
|
|
w, _ := g.Create("f", 400)
|
|
w.Write(make([]byte, 400))
|
|
w.Close()
|
|
// Direct Async construction + Force/IsGCRunning (fixes shallow cast that never hit Async paths)
|
|
ag := NewAsync(m, LRU, false, 0.8, 0.95, 1.0)
|
|
ag.ForceGC(100)
|
|
_ = ag.IsGCRunning()
|
|
ag.Stop()
|
|
|
|
if g.Size() > 500 {
|
|
t.Log("GC may be async")
|
|
}
|
|
_ = g.Name()
|
|
}
|
|
|
|
// TestAsyncGCFS_QueuedAndDoubleStop exercises queueing, running flag, double-stop (issue8 coverage).
|
|
func TestAsyncGCFS_QueuedAndDoubleStop(t *testing.T) {
|
|
t.Parallel()
|
|
m := memory.New(1 << 20)
|
|
ag := NewAsync(m, LRU, true, 0.5, 0.8, 1.0)
|
|
defer ag.Stop()
|
|
|
|
// Queue several (may sync or async depending on thresholds)
|
|
for i := 0; i < 5; i++ {
|
|
w, _ := ag.Create("q"+string(rune(i)), 100)
|
|
w.Write(make([]byte, 100))
|
|
w.Close()
|
|
}
|
|
// Force one
|
|
ag.ForceGC(10)
|
|
// ForceGC is synchronous (direct gcFunc); no sleep or IsGCRunning assert needed (worker flag only for async queue paths).
|
|
_ = ag.IsGCRunning() // still exercise API
|
|
ag.Stop()
|
|
ag.Stop() // double stop must not panic
|
|
}
|