All checks were successful
PR Check / check-and-test (pull_request) Successful in 30s
202 lines
3.7 KiB
Go
202 lines
3.7 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) {
|
|
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) {
|
|
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 TestCreateAndOpen(t *testing.T) {
|
|
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 TestCreateAndOpenNoFast(t *testing.T) {
|
|
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 TestCachingPromotion(t *testing.T) {
|
|
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")
|
|
|
|
ws, _ := slow.Create(key, int64(len(value)))
|
|
ws.Write(value)
|
|
ws.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)
|
|
}
|
|
|
|
// Check if promoted to fast
|
|
_, err = fast.Open(key)
|
|
if err != nil {
|
|
t.Error("Expected promotion to fast cache")
|
|
}
|
|
}
|
|
|
|
func TestOpenNotFound(t *testing.T) {
|
|
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) {
|
|
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) {
|
|
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")
|
|
}
|
|
if info.Size() != int64(len(value)) {
|
|
t.Errorf("expected size %d, got %d", len(value), info.Size())
|
|
}
|
|
}
|