-
Notifications
You must be signed in to change notification settings - Fork 2
/
bolt_lru_test.go
68 lines (61 loc) · 1.13 KB
/
bolt_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
63
64
65
66
67
68
package boltlru
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
)
func TestLRU(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpDir)
l, err := New(128, path.Join(tmpDir, "db"))
if err != nil {
t.Fatalf("err: %v", err)
}
for i := 0; i < 256; i++ {
kv := fmt.Sprintf("%d", i)
err := l.Add(kv, []byte(kv))
if err != nil {
t.Fatalf("failed to add value: %s", err)
}
}
if l.Len() != 128 {
t.Fatalf("bad len: %v", l.Len())
}
for i := 0; i < 128; i++ {
k := fmt.Sprintf("%d", i)
_, err := l.Get(k)
if err == nil {
t.Fatalf("should be evicted")
}
}
for i := 128; i < 256; i++ {
k := fmt.Sprintf("%d", i)
v, err := l.Get(k)
if err != nil {
t.Fatalf("should not be evicted")
}
if k != string(v) {
t.Fatalf("invalid value")
}
}
l.db.Close()
l, err = New(128, path.Join(tmpDir, "db"))
if err != nil {
t.Fatalf("err: %v", err)
}
for i := 128; i < 256; i++ {
k := fmt.Sprintf("%d", i)
v, err := l.Get(k)
if err != nil {
t.Fatalf("should not be evicted")
}
if k != string(v) {
t.Fatalf("invalid value")
}
}
}