| 1 | package readcache |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestRoundTripAndInvalidation(t *testing.T) { |
| 11 | root := t.TempDir() |
| 12 | keep := filepath.Join(root, "data", "bluegit.db") |
| 13 | _ = os.MkdirAll(filepath.Dir(keep), 0o750) |
| 14 | _ = os.WriteFile(keep, []byte("db"), 0o600) |
| 15 | c, err := Open(root, 1<<20) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | c.PutJSON(1, false, "tree:abc", []string{"a", "b"}) |
| 20 | c.PutJSON(1, true, "branches", []string{"main"}) |
| 21 | c.PutJSON(2, true, "branches", []string{"dev"}) |
| 22 | var got []string |
| 23 | if !c.GetJSON(1, false, "tree:abc", &got) || len(got) != 2 { |
| 24 | t.Fatalf("miss: %v", got) |
| 25 | } |
| 26 | c.DropRefs(1) |
| 27 | if c.GetJSON(1, true, "branches", &got) { |
| 28 | t.Fatal("refs niet vergeten") |
| 29 | } |
| 30 | if !c.GetJSON(1, false, "tree:abc", &got) || !c.GetJSON(2, true, "branches", &got) { |
| 31 | t.Fatal("te veel vergeten") |
| 32 | } |
| 33 | c.DropRepo(2) |
| 34 | if c.GetJSON(2, true, "branches", &got) { |
| 35 | t.Fatal("repo niet vergeten") |
| 36 | } |
| 37 | c.Clear() |
| 38 | if c.Usage().Entries != 0 || c.GetJSON(1, false, "tree:abc", &got) { |
| 39 | t.Fatal("niet leeg") |
| 40 | } |
| 41 | if b, err := os.ReadFile(keep); err != nil || string(b) != "db" { |
| 42 | t.Fatal("Clear raakte data/ aan") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestCorruptEntryIsMiss(t *testing.T) { |
| 47 | c, _ := Open(t.TempDir(), 1<<20) |
| 48 | c.PutJSON(1, false, "k", 5) |
| 49 | _ = os.WriteFile(filepath.Join(c.Dir(), rel(1, false, "k")), []byte("k\n{kapot"), 0o600) |
| 50 | var n int |
| 51 | if c.GetJSON(1, false, "k", &n) { |
| 52 | t.Fatal("corrupt gelezen") |
| 53 | } |
| 54 | if c.Usage().Entries != 0 { |
| 55 | t.Fatal("corrupte entry bleef staan") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestLRUAndReopen(t *testing.T) { |
| 60 | root := t.TempDir() |
| 61 | c, _ := Open(root, 4000) |
| 62 | big := strings.Repeat("x", 900) |
| 63 | for i := 0; i < 10; i++ { |
| 64 | c.PutJSON(1, false, "k"+string(rune('a'+i)), big) |
| 65 | } |
| 66 | var s string |
| 67 | if c.Usage().Bytes > 4000 || c.GetJSON(1, false, "ka", &s) || !c.GetJSON(1, false, "kj", &s) { |
| 68 | t.Fatalf("LRU: %+v", c.Usage()) |
| 69 | } |
| 70 | c2, _ := Open(root, 4000) |
| 71 | if c2.Usage().Entries != c.Usage().Entries || !c2.GetJSON(1, false, "kj", &s) { |
| 72 | t.Fatal("heropenen") |
| 73 | } |
| 74 | var nilc *Cache |
| 75 | nilc.PutJSON(1, false, "x", 1) |
| 76 | if nilc.GetJSON(1, false, "x", &s) { |
| 77 | t.Fatal("nil-cache") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestStalePutAfterDropIsIgnored(t *testing.T) { |
| 82 | c, _ := Open(t.TempDir(), 1<<20) |
| 83 | gen := c.Gen(1) |
| 84 | c.DropRefs(1) // push tijdens het lezen uit git |
| 85 | c.PutJSONAt(1, true, "branches", []string{"oud"}, gen) |
| 86 | var got []string |
| 87 | if c.GetJSON(1, true, "branches", &got) { |
| 88 | t.Fatal("verouderde data geschreven") |
| 89 | } |
| 90 | } |