Skip to content

Commit d7199f1

Browse files
committed
Start working on tests for disk cache
1 parent cdb6986 commit d7199f1

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

tools/disk_cache/implementation.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (dc *DiskCache) get(key string, items []string) map[string]string {
138138
}
139139
for _, x := range items {
140140
p := filepath.Join(base, x)
141-
if s, err := os.Stat(p); err != nil || !s.IsDir() {
141+
if s, err := os.Stat(p); err != nil || s.IsDir() {
142142
continue
143143
}
144144
ans[x] = p
@@ -193,6 +193,11 @@ func (dc *DiskCache) update_timestamp(key string) {
193193
func (dc *DiskCache) update_accounting(key string, changed int64) (err error) {
194194
if err = dc.ensure_entries(); err == nil {
195195
t := dc.entry_map[key]
196+
if t == nil {
197+
t = &Entry{Key: key}
198+
dc.entry_map[key] = t
199+
dc.entries.SortedEntries = append(dc.entries.SortedEntries, t)
200+
}
196201
old_size := t.Size
197202
t.Size += changed
198203
t.Size = max(0, t.Size)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package disk_cache
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/kovidgoyal/kitty/tools/utils"
10+
)
11+
12+
var _ = fmt.Print
13+
14+
func TestDiskCache(t *testing.T) {
15+
tdir := t.TempDir()
16+
dc, err := NewDiskCache(tdir, 64)
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
m := dc.Get("missing", "one", "two")
21+
if diff := cmp.Diff(m, make(map[string]string)); diff != "" {
22+
t.Fatalf("Unexpected return from missing: %s", diff)
23+
}
24+
dc.Add("k1", map[string][]byte{"1": []byte("abcd"), "2": []byte("efgh")})
25+
26+
ad := func(key string, expected map[string]string) {
27+
actual := dc.Get(key, utils.Keys(expected)...)
28+
29+
for k, path := range actual {
30+
d, err := os.ReadFile(path)
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
actual[k] = string(d)
35+
}
36+
if diff := cmp.Diff(expected, actual); diff != "" {
37+
t.Fatalf("Data for %s not equal: %s", key, diff)
38+
}
39+
}
40+
ad("k1", map[string]string{"1": "abcd", "2": "efgh"})
41+
}

0 commit comments

Comments
 (0)