refactor: moved the GC stuff around and corrected all tests
All checks were successful
PR Check / check-and-test (pull_request) Successful in 30s

This commit is contained in:
2025-07-13 04:20:12 -05:00
parent 1673e9554a
commit 539f14e8ec
9 changed files with 368 additions and 376 deletions

View File

@@ -17,8 +17,6 @@ func testMemory() vfs.VFS {
}
func TestNew(t *testing.T) {
t.Parallel()
fast := testMemory()
slow := testMemory()
@@ -31,8 +29,6 @@ func TestNew(t *testing.T) {
}
func TestNewPanics(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic but did not get one")
@@ -44,9 +40,7 @@ func TestNewPanics(t *testing.T) {
cache.SetSlow(nil)
}
func TestSetAndGet(t *testing.T) {
t.Parallel()
func TestCreateAndOpen(t *testing.T) {
fast := testMemory()
slow := testMemory()
cache := New(nil)
@@ -75,9 +69,7 @@ func TestSetAndGet(t *testing.T) {
}
}
func TestSetAndGetNoFast(t *testing.T) {
t.Parallel()
func TestCreateAndOpenNoFast(t *testing.T) {
slow := testMemory()
cache := New(nil)
cache.SetSlow(slow)
@@ -104,9 +96,7 @@ func TestSetAndGetNoFast(t *testing.T) {
}
}
func TestCaching(t *testing.T) {
t.Parallel()
func TestCachingPromotion(t *testing.T) {
fast := testMemory()
slow := testMemory()
cache := New(func(fi *vfs.FileInfo, cs cachestate.CacheState) bool {
@@ -118,57 +108,29 @@ func TestCaching(t *testing.T) {
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)
// Check if promoted to fast
_, err = fast.Open(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)
t.Error("Expected promotion to fast cache")
}
}
func TestGetNotFound(t *testing.T) {
t.Parallel()
func TestOpenNotFound(t *testing.T) {
fast := testMemory()
slow := testMemory()
cache := New(nil)
@@ -182,8 +144,6 @@ func TestGetNotFound(t *testing.T) {
}
func TestDelete(t *testing.T) {
t.Parallel()
fast := testMemory()
slow := testMemory()
cache := New(nil)
@@ -211,8 +171,6 @@ func TestDelete(t *testing.T) {
}
func TestStat(t *testing.T) {
t.Parallel()
fast := testMemory()
slow := testMemory()
cache := New(nil)
@@ -237,4 +195,7 @@ func TestStat(t *testing.T) {
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())
}
}