forked from peterbourgon/diskv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_test.go
62 lines (57 loc) · 1.23 KB
/
lru_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package diskv
import (
"bytes"
"errors"
"os"
"testing"
)
func TestLruAddRemove(t *testing.T) {
c := newLru(1, func(key string) error { return nil })
k := "a"
if err := c.Add(k); err != nil {
t.Fatalf("write: %s", err)
}
if _, ok := c.items[k]; !ok {
t.Fatalf("add key failed: %s", k)
}
c.Remove(k)
if _, ok := c.items[k]; ok {
t.Fatalf("remove key failed: %s", k)
}
}
func TestLruEvict(t *testing.T) {
var (
d = New(Options{
BasePath: "test-data",
LruSize: 2,
})
keys = []string{"a", "b", "c", "d"}
v = []byte{'1'}
expected = map[string]bool{"a": true, "b": true, "c": false, "d": false}
)
defer d.EraseAll()
for _, k := range keys {
if err := d.Write(k, v); err != nil {
t.Fatalf("write: %s: %s", k, err)
}
}
for key, evict := range expected {
b, err := d.Read(key)
if evict {
if errors.Is(err, os.ErrNotExist) {
t.Logf("%s evict got: %v", key, evict)
} else {
t.Fatalf("%s expected error: %s got: %v", key, os.ErrNotExist, err)
}
} else {
if err != nil {
t.Fatalf("%s expected error: nil got: %s", key, err)
}
if bytes.Equal(v, b) {
t.Logf("%s value got: %s", key, b)
} else {
t.Logf("%s expected value: %s got: %v", key, v, b)
}
}
}
}