Some checks failed
PR Check / check-and-test (pull_request) Failing after 11m4s
- Updated disk_test.go to replace Set and Get with Create and Open methods for better clarity and functionality. - Modified fileinfo.go to include package comment. - Refactored gc.go to streamline garbage collection handling and removed unused statistics. - Updated gc_test.go to comment out large random tests for future implementation. - Enhanced memory.go to implement LRU caching and metrics for memory usage. - Updated memory_test.go to replace Set and Get with Create and Open methods. - Removed sync.go as it was redundant and not utilized. - Updated vfs.go to reflect changes in the VFS interface, replacing Set and Get with Create and Open. - Added package comments to vfserror.go for consistency.
241 lines
4.4 KiB
Go
241 lines
4.4 KiB
Go
// vfs/cache/cache_test.go
|
|
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"s1d3sw1ped/SteamCache2/vfs"
|
|
"s1d3sw1ped/SteamCache2/vfs/cachestate"
|
|
"s1d3sw1ped/SteamCache2/vfs/memory"
|
|
"s1d3sw1ped/SteamCache2/vfs/vfserror"
|
|
)
|
|
|
|
func testMemory() vfs.VFS {
|
|
return memory.New(1024)
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fast := testMemory()
|
|
slow := testMemory()
|
|
|
|
cache := New(nil)
|
|
cache.SetFast(fast)
|
|
cache.SetSlow(slow)
|
|
if cache == nil {
|
|
t.Fatal("expected cache to be non-nil")
|
|
}
|
|
}
|
|
|
|
func TestNewPanics(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Fatal("expected panic but did not get one")
|
|
}
|
|
}()
|
|
|
|
cache := New(nil)
|
|
cache.SetFast(nil)
|
|
cache.SetSlow(nil)
|
|
}
|
|
|
|
func TestSetAndGet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fast := testMemory()
|
|
slow := testMemory()
|
|
cache := New(nil)
|
|
cache.SetFast(fast)
|
|
cache.SetSlow(slow)
|
|
|
|
key := "test"
|
|
value := []byte("value")
|
|
|
|
w, err := cache.Create(key, int64(len(value)))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
w.Write(value)
|
|
w.Close()
|
|
|
|
rc, err := cache.Open(key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
got, _ := io.ReadAll(rc)
|
|
rc.Close()
|
|
|
|
if string(got) != string(value) {
|
|
t.Fatalf("expected %s, got %s", value, got)
|
|
}
|
|
}
|
|
|
|
func TestSetAndGetNoFast(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
slow := testMemory()
|
|
cache := New(nil)
|
|
cache.SetSlow(slow)
|
|
|
|
key := "test"
|
|
value := []byte("value")
|
|
|
|
w, err := cache.Create(key, int64(len(value)))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
w.Write(value)
|
|
w.Close()
|
|
|
|
rc, err := cache.Open(key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
got, _ := io.ReadAll(rc)
|
|
rc.Close()
|
|
|
|
if string(got) != string(value) {
|
|
t.Fatalf("expected %s, got %s", value, got)
|
|
}
|
|
}
|
|
|
|
func TestCaching(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fast := testMemory()
|
|
slow := testMemory()
|
|
cache := New(func(fi *vfs.FileInfo, cs cachestate.CacheState) bool {
|
|
return true
|
|
})
|
|
cache.SetFast(fast)
|
|
cache.SetSlow(slow)
|
|
|
|
key := "test"
|
|
value := []byte("value")
|
|
|
|
wf, _ := fast.Create(key, int64(len(value)))
|
|
wf.Write(value)
|
|
wf.Close()
|
|
|
|
ws, _ := slow.Create(key, int64(len(value)))
|
|
ws.Write(value)
|
|
ws.Close()
|
|
|
|
state := cache.cacheState(key)
|
|
if state != cachestate.CacheStateHit {
|
|
t.Fatalf("expected %v, got %v", cachestate.CacheStateHit, state)
|
|
}
|
|
|
|
err := fast.Delete(key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
rc, err := cache.Open(key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
got, _ := io.ReadAll(rc)
|
|
rc.Close()
|
|
state = cache.cacheState(key)
|
|
if state != cachestate.CacheStateMiss {
|
|
t.Fatalf("expected %v, got %v", cachestate.CacheStateMiss, state)
|
|
}
|
|
|
|
if string(got) != string(value) {
|
|
t.Fatalf("expected %s, got %s", value, got)
|
|
}
|
|
|
|
err = cache.Delete(key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
_, err = cache.Open(key)
|
|
if !errors.Is(err, vfserror.ErrNotFound) {
|
|
t.Fatalf("expected %v, got %v", vfserror.ErrNotFound, err)
|
|
}
|
|
state = cache.cacheState(key)
|
|
if state != cachestate.CacheStateNotFound {
|
|
t.Fatalf("expected %v, got %v", cachestate.CacheStateNotFound, state)
|
|
}
|
|
}
|
|
|
|
func TestGetNotFound(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fast := testMemory()
|
|
slow := testMemory()
|
|
cache := New(nil)
|
|
cache.SetFast(fast)
|
|
cache.SetSlow(slow)
|
|
|
|
_, err := cache.Open("nonexistent")
|
|
if !errors.Is(err, vfserror.ErrNotFound) {
|
|
t.Fatalf("expected %v, got %v", vfserror.ErrNotFound, err)
|
|
}
|
|
}
|
|
|
|
func TestDelete(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fast := testMemory()
|
|
slow := testMemory()
|
|
cache := New(nil)
|
|
cache.SetFast(fast)
|
|
cache.SetSlow(slow)
|
|
|
|
key := "test"
|
|
value := []byte("value")
|
|
|
|
w, err := cache.Create(key, int64(len(value)))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
w.Write(value)
|
|
w.Close()
|
|
|
|
if err := cache.Delete(key); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
_, err = cache.Open(key)
|
|
if !errors.Is(err, vfserror.ErrNotFound) {
|
|
t.Fatalf("expected %v, got %v", vfserror.ErrNotFound, err)
|
|
}
|
|
}
|
|
|
|
func TestStat(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fast := testMemory()
|
|
slow := testMemory()
|
|
cache := New(nil)
|
|
cache.SetFast(fast)
|
|
cache.SetSlow(slow)
|
|
|
|
key := "test"
|
|
value := []byte("value")
|
|
|
|
w, err := cache.Create(key, int64(len(value)))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
w.Write(value)
|
|
w.Close()
|
|
|
|
info, err := cache.Stat(key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if info == nil {
|
|
t.Fatal("expected file info to be non-nil")
|
|
}
|
|
}
|