-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboltcache_test.go
172 lines (147 loc) · 4.53 KB
/
boltcache_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package lru
import (
"strconv"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Boltcache", func() {
Context("openBoltDB", func() {
It("should return an error when attempting to open an invalid path", func() {
l := NewLRU("///", "", nil, nil)
defer closeBoltDB(l)
err := l.openBoltDB()
Ω(err).Should(HaveOccurred())
})
})
Context("fillCacheFromBolt", func() {
It("should attempt to fill the cache, but no data currently exists", func() {
l := NewLRU("", "", nil, nil)
defer l.Close()
err := l.openBoltDB()
Ω(err).ShouldNot(HaveOccurred())
Ω(l.lru.Len()).Should(Equal(int64(0)))
})
It("should return an error when a blank bucket name is used", func() {
l := NewLRU("", "", nil, nil)
defer l.Close()
l.bName = []byte{}
err := l.openBoltDB()
Ω(err).Should(HaveOccurred())
Ω(l.lru.Len()).Should(Equal(int64(0)))
})
It("should fill the cache with all data in the bolt database and delete items exceeding the capacity", func() {
// insert 1200 bytes into the bolt database
l := NewLRU("", "", DefaultTwoQ(1000), nil)
err := l.Open()
Ω(err).ShouldNot(HaveOccurred())
for i := 0; i < 7; i++ {
err = l.putIntoBolt([]byte(strconv.Itoa(i)), make([]byte, 150))
Ω(err).ShouldNot(HaveOccurred())
}
closeBoltDB(l)
// attempt to open and fill LRU
l = newDefaultLRU()
defer closeBoltDB(l)
Ω(l.lru.Len()).Should(Equal(int64(6)))
_, err = l.Get([]byte("6"))
Ω(err).Should(MatchError(errNoStore))
})
})
Context("getFromBolt", func() {
It("should return nil when trying to retrieve a key that doesn't exist", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
v := l.getFromBolt([]byte("key"))
Ω(v).Should(BeNil())
})
It("should return the value from bolt", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
err := l.putIntoBolt([]byte("key"), []byte("value"))
Ω(err).ShouldNot(HaveOccurred())
v := l.getFromBolt([]byte("key"))
Ω(string(v)).Should(Equal("value"))
})
It("should return nil when the db.View function returns an error", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
err := l.putIntoBolt([]byte("key"), []byte("value"))
Ω(err).ShouldNot(HaveOccurred())
l.db.Close()
v := l.getFromBolt([]byte("key"))
Ω(v).Should(BeNil())
})
})
Context("getBufFromBolt", func() {
It("should return nil when trying to retrieve a key that doesn't exist", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
b := l.getBufFromBolt([]byte("key"))
Ω(b).Should(BeNil())
})
It("should return a buffer for the value from bolt", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
err := l.putIntoBolt([]byte("key"), []byte("value"))
Ω(err).ShouldNot(HaveOccurred())
b := l.getBufFromBolt([]byte("key"))
Ω(b.String()).Should(Equal("value"))
})
It("should return nil when the db.View function returns an error", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
err := l.putIntoBolt([]byte("key"), []byte("value"))
Ω(err).ShouldNot(HaveOccurred())
l.db.Close()
b := l.getBufFromBolt([]byte("key"))
Ω(b).Should(BeNil())
})
})
Context("emptyBolt", func() {
It("should empty the bolt database's bucket", func() {
// create LRU and insert key
l := newDefaultLRU()
defer closeBoltDB(l)
err := l.putIntoBolt([]byte("key"), []byte("value"))
Ω(err).ShouldNot(HaveOccurred())
v := l.getFromBolt([]byte("key"))
Ω(string(v)).Should(Equal("value"))
// empty the database
err = l.emptyBolt()
Ω(err).ShouldNot(HaveOccurred())
v = l.getFromBolt([]byte("key"))
Ω(v).Should(BeNil())
})
It("should return an error when the bucket doesn't exist", func() {
l := newDefaultLRU()
defer closeBoltDB(l)
l.bName = []byte("badbucketname")
err := l.emptyBolt()
Ω(err).Should(HaveOccurred())
})
})
Context("deleteFromBolt", func() {
It("should delete the provided keys from the bolt database", func() {
// create LRU and insert keys
l := newDefaultLRU()
defer closeBoltDB(l)
var toRemove [][]byte
for i := 0; i < 4; i++ {
key := []byte(strconv.Itoa(i))
toRemove = append(toRemove, key)
err := l.putIntoBolt(key, []byte("value"))
Ω(err).ShouldNot(HaveOccurred())
}
// delete 3 from bolt
err := l.deleteFromBolt(toRemove[:3])
Ω(err).ShouldNot(HaveOccurred())
for i := 0; i < 3; i++ {
v := l.getFromBolt(toRemove[i])
Ω(v).Should(BeNil())
}
v := l.getFromBolt(toRemove[3])
Ω(v).ShouldNot(BeNil())
Ω(string(v)).Should(Equal("value"))
})
})
})