-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboltdbcache_test.go
104 lines (83 loc) · 1.72 KB
/
boltdbcache_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package boltdbcache
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
bolt "go.etcd.io/bbolt"
)
func setup(t *testing.T) (string, func()) {
tempDir, err := ioutil.TempDir("", "httpcache")
if err != nil {
t.Fatal(err)
}
return tempDir, func() {
os.RemoveAll(tempDir)
}
}
func TestGetNoKey(t *testing.T) {
tempDir, teardown := setup(t)
defer teardown()
cache, err := New(filepath.Join(tempDir, "db"))
if err != nil {
t.Fatal(err)
}
defer cache.Close()
key := "test"
v, ok := cache.Get(key)
if ok || v != nil {
t.Fatal("retrieved key before adding it")
}
}
func TestSet(t *testing.T) {
tempDir, teardown := setup(t)
defer teardown()
cache, err := New(filepath.Join(tempDir, "db"))
if err != nil {
t.Fatal(err)
}
defer cache.Close()
k := "foo"
v := []byte("bar")
cache.Set(k, v)
v2, ok := cache.Get(k)
if !ok {
t.Fatalf("could not retrieve value for key %q", k)
}
if !bytes.Equal(v, v2) {
t.Fatalf("expected %q; got %q", v, v2)
}
}
func TestDelete(t *testing.T) {
tempDir, teardown := setup(t)
defer teardown()
cache, err := New(filepath.Join(tempDir, "db"))
if err != nil {
t.Fatal(err)
}
defer cache.Close()
k := "foo"
v := []byte("bar")
cache.Set(k, v)
v2, ok := cache.Get(k)
if !ok {
t.Fatalf("could not retrieve value for key %q", k)
}
if !bytes.Equal(v, v2) {
t.Fatalf("expected %q; got %q", v, v2)
}
cache.Delete(k)
v3, ok := cache.Get(k)
if ok || v3 != nil {
t.Fatalf("key still present")
}
}
func TestNilBucketName(t *testing.T) {
tempDir, teardown := setup(t)
defer teardown()
_, err := New(filepath.Join(tempDir, "db"), WithBucketName(""))
if err != bolt.ErrBucketNameRequired {
t.Fatalf("expected bolt.ErrBucketNameRequired; got %v", err)
}
}