refactor: moved the GC stuff around and corrected all tests
All checks were successful
PR Check / check-and-test (pull_request) Successful in 30s
All checks were successful
PR Check / check-and-test (pull_request) Successful in 30s
This commit is contained in:
@@ -1,96 +1,73 @@
|
||||
// vfs/gc/gc_test.go
|
||||
package gc
|
||||
|
||||
// func TestGCSmallRandom(t *testing.T) {
|
||||
// t.Parallel()
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"s1d3sw1ped/SteamCache2/vfs/memory"
|
||||
"s1d3sw1ped/SteamCache2/vfs/vfserror"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// m := memory.New(1024 * 1024 * 16)
|
||||
// gc := New(m, 10, func(vfs vfs.VFS, size uint) (uint, uint) {
|
||||
// deletions := 0
|
||||
// var reclaimed uint
|
||||
func TestGCOnFull(t *testing.T) {
|
||||
m := memory.New(10)
|
||||
gc := New(m, 2, LRUGC)
|
||||
|
||||
// t.Logf("GC starting to reclaim %d bytes", size)
|
||||
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()
|
||||
}
|
||||
|
||||
// stats := vfs.StatAll()
|
||||
// sort.Slice(stats, func(i, j int) bool {
|
||||
// // Sort by access time so we can remove the oldest files first.
|
||||
// return stats[i].AccessTime().Before(stats[j].AccessTime())
|
||||
// })
|
||||
// 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()
|
||||
|
||||
// // Delete the oldest files until we've reclaimed enough space.
|
||||
// for _, s := range stats {
|
||||
// sz := uint(s.Size()) // Get the size of the file
|
||||
// err := vfs.Delete(s.Name())
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// reclaimed += sz // Track how much space we've reclaimed
|
||||
// deletions++ // Track how many files we've deleted
|
||||
if gc.Size() > 10 {
|
||||
t.Errorf("Size exceeded: %d > 10", gc.Size())
|
||||
}
|
||||
|
||||
// // t.Logf("GC deleting %s, %v", s.Name(), s.AccessTime().Format(time.RFC3339Nano))
|
||||
// Check if older keys were evicted
|
||||
_, err = m.Open("key0")
|
||||
if err == nil {
|
||||
t.Error("Expected key0 to be evicted")
|
||||
}
|
||||
}
|
||||
|
||||
// if reclaimed >= size { // We've reclaimed enough space
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// return uint(reclaimed), uint(deletions)
|
||||
// })
|
||||
func TestNoGCNeeded(t *testing.T) {
|
||||
m := memory.New(20)
|
||||
gc := New(m, 2, LRUGC)
|
||||
|
||||
// for i := 0; i < 10000; i++ {
|
||||
// if err := gc.Set(fmt.Sprintf("key:%d", i), genRandomData(1024*1, 1024*4)); err != nil {
|
||||
// t.Errorf("Set failed: %v", err)
|
||||
// }
|
||||
// }
|
||||
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() > 1024*1024*16 {
|
||||
// t.Errorf("MemoryFS size is %d, want <= 1024", m.Size())
|
||||
// }
|
||||
// }
|
||||
if gc.Size() != 10 {
|
||||
t.Errorf("Size: got %d, want 10", gc.Size())
|
||||
}
|
||||
}
|
||||
|
||||
// func genRandomData(min int, max int) []byte {
|
||||
// data := make([]byte, rand.Intn(max-min)+min)
|
||||
// rand.Read(data)
|
||||
// return data
|
||||
// }
|
||||
func TestGCInsufficientSpace(t *testing.T) {
|
||||
m := memory.New(5)
|
||||
gc := New(m, 1, LRUGC)
|
||||
|
||||
// func TestGCLargeRandom(t *testing.T) {
|
||||
// t.Parallel()
|
||||
|
||||
// m := memory.New(1024 * 1024 * 16) // 16MB
|
||||
// gc := New(m, 10, func(vfs vfs.VFS, size uint) (uint, uint) {
|
||||
// deletions := 0
|
||||
// var reclaimed uint
|
||||
|
||||
// t.Logf("GC starting to reclaim %d bytes", size)
|
||||
|
||||
// stats := vfs.StatAll()
|
||||
// sort.Slice(stats, func(i, j int) bool {
|
||||
// // Sort by access time so we can remove the oldest files first.
|
||||
// return stats[i].AccessTime().Before(stats[j].AccessTime())
|
||||
// })
|
||||
|
||||
// // Delete the oldest files until we've reclaimed enough space.
|
||||
// for _, s := range stats {
|
||||
// sz := uint(s.Size()) // Get the size of the file
|
||||
// vfs.Delete(s.Name())
|
||||
// reclaimed += sz // Track how much space we've reclaimed
|
||||
// deletions++ // Track how many files we've deleted
|
||||
|
||||
// if reclaimed >= size { // We've reclaimed enough space
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
// return uint(reclaimed), uint(deletions)
|
||||
// })
|
||||
|
||||
// for i := 0; i < 10000; i++ {
|
||||
// if err := gc.Set(fmt.Sprintf("key:%d", i), genRandomData(1024, 1024*1024)); err != nil {
|
||||
// t.Errorf("Set failed: %v", err)
|
||||
// }
|
||||
// }
|
||||
|
||||
// if gc.Size() > 1024*1024*16 {
|
||||
// t.Errorf("MemoryFS size is %d, want <= 1024", m.Size())
|
||||
// }
|
||||
// }
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user