Files
s1d3sw1ped feda55e225 Enhance DiskFS initialization and error handling
- 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.
2026-05-27 13:15:33 -05:00

98 lines
2.2 KiB
Go

package gc
import (
"s1d3sw1ped/steamcache2/vfs/memory"
"testing"
)
func TestGCFS_BasicEvictOnCreate(t *testing.T) {
t.Parallel()
m, err := memory.New(400)
if err != nil {
t.Fatal(err)
}
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, err := memory.New(1 << 20)
if err != nil {
t.Fatal(err)
}
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, err := memory.New(500)
if err != nil {
t.Fatal(err)
}
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, err := memory.New(1 << 20)
if err != nil {
t.Fatal(err)
}
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
}