feat(backend): immutable cache dir scheme + stale sweep
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package bookfile
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mk(t *testing.T, cacheDir, kind, key, file string) {
|
||||
t.Helper()
|
||||
d := filepath.Join(cacheDir, kind, key)
|
||||
os.MkdirAll(d, 0o755)
|
||||
os.WriteFile(filepath.Join(d, file), []byte("x"), 0o644)
|
||||
}
|
||||
|
||||
func TestSweepStale(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
mk(t, dir, "covers", "1-aaa", "cover.jpg")
|
||||
mk(t, dir, "covers", "2-bbb", "cover.png")
|
||||
mk(t, dir, "pages", "1-aaa", "0.jpg")
|
||||
mk(t, dir, "pages", "3-ccc", "0.jpg")
|
||||
os.WriteFile(filepath.Join(dir, "covers", "stray.txt"), []byte("x"), 0o644) // 非目录,忽略
|
||||
|
||||
n, err := SweepStale(dir, map[string]bool{"1-aaa": true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 2 {
|
||||
t.Fatalf("removed %d want 2", n)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "covers", "1-aaa")); err != nil {
|
||||
t.Fatal("live dir removed")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "pages", "1-aaa")); err != nil {
|
||||
t.Fatal("live pages removed")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "covers", "2-bbb")); !os.IsNotExist(err) {
|
||||
t.Fatal("stale cover remains")
|
||||
}
|
||||
// cacheDir 不存在 = 首次运行,不算错
|
||||
if n, err := SweepStale(filepath.Join(dir, "nope"), nil); n != 0 || err != nil {
|
||||
t.Fatalf("%d %v", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaths(t *testing.T) {
|
||||
if DirKey(12, "ab") != "12-ab" {
|
||||
t.Fatal("DirKey")
|
||||
}
|
||||
if CoverDir("/c", "12-ab") != filepath.Join("/c", "covers", "12-ab") {
|
||||
t.Fatal("CoverDir")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user