All checks were successful
PR Check / check-and-test (pull_request) Successful in 30s
74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
// vfs/gc/gc_test.go
|
|
package gc
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"s1d3sw1ped/SteamCache2/vfs/memory"
|
|
"s1d3sw1ped/SteamCache2/vfs/vfserror"
|
|
"testing"
|
|
)
|
|
|
|
func TestGCOnFull(t *testing.T) {
|
|
m := memory.New(10)
|
|
gc := New(m, 2, LRUGC)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
w, err := gc.Create(fmt.Sprintf("key%d", i), 2)
|
|
if err != nil {
|
|
t.Fatalf("Create failed: %v", err)
|
|
}
|
|
w.Write([]byte("ab"))
|
|
w.Close()
|
|
}
|
|
|
|
// Cache full at 10 bytes
|
|
w, err := gc.Create("key5", 2)
|
|
if err != nil {
|
|
t.Fatalf("Create failed: %v", err)
|
|
}
|
|
w.Write([]byte("cd"))
|
|
w.Close()
|
|
|
|
if gc.Size() > 10 {
|
|
t.Errorf("Size exceeded: %d > 10", gc.Size())
|
|
}
|
|
|
|
// Check if older keys were evicted
|
|
_, err = m.Open("key0")
|
|
if err == nil {
|
|
t.Error("Expected key0 to be evicted")
|
|
}
|
|
}
|
|
|
|
func TestNoGCNeeded(t *testing.T) {
|
|
m := memory.New(20)
|
|
gc := New(m, 2, LRUGC)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
w, err := gc.Create(fmt.Sprintf("key%d", i), 2)
|
|
if err != nil {
|
|
t.Fatalf("Create failed: %v", err)
|
|
}
|
|
w.Write([]byte("ab"))
|
|
w.Close()
|
|
}
|
|
|
|
if gc.Size() != 10 {
|
|
t.Errorf("Size: got %d, want 10", gc.Size())
|
|
}
|
|
}
|
|
|
|
func TestGCInsufficientSpace(t *testing.T) {
|
|
m := memory.New(5)
|
|
gc := New(m, 1, LRUGC)
|
|
|
|
w, err := gc.Create("key0", 10)
|
|
if err == nil {
|
|
w.Close()
|
|
t.Error("Expected ErrDiskFull")
|
|
} else if !errors.Is(err, vfserror.ErrDiskFull) {
|
|
t.Errorf("Unexpected error: %v", err)
|
|
}
|
|
}
|