/
adapters_test.go
355 lines (279 loc) · 8.82 KB
/
adapters_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package cacher_test
import (
"encoding/binary"
"reflect"
"testing"
"github.com/garyburd/redigo/redis"
"github.com/jelmersnoeck/cacher"
"github.com/jelmersnoeck/cacher/internal/encoding"
"github.com/jelmersnoeck/cacher/internal/tests"
"github.com/jelmersnoeck/cacher/memory"
rcache "github.com/jelmersnoeck/cacher/redis"
)
func TestAdd(t *testing.T) {
for _, cache := range testDrivers() {
if err := cache.Add("key1", []byte("value1"), 0); err != nil {
tests.FailMsg(t, cache, "Expecting `key1` to be added to the cache.")
}
if err := cache.Add("key1", []byte("value2"), 0); err == nil {
tests.FailMsg(t, cache, "Expecting `key1` not to be added to the cache.")
}
tests.Compare(t, cache, "key1", "value1")
}
}
func TestSet(t *testing.T) {
values := map[string][]byte{
"key1": []byte("value"),
"key2": encoding.Int64Bytes(2),
}
for _, cache := range testDrivers() {
for key, value := range values {
if err := cache.Set(key, value, 0); err != nil {
tests.FailMsg(t, cache, "Expecting `key1` to be `value`")
}
val, _, _ := cache.Get(key)
if !reflect.DeepEqual(val, value) {
tests.FailMsg(t, cache, "Value for key `"+key+"` does not match.")
}
}
cache.Set("key1", []byte("value"), -1)
_, _, err := cache.Get("key1")
if err == nil {
tests.FailMsg(t, cache, "key1 should be deleted with negative value")
}
}
}
func TestSetMulti(t *testing.T) {
for _, cache := range testDrivers() {
items := map[string][]byte{
"item1": encoding.Int64Bytes(1),
"item2": []byte("string"),
}
cache.SetMulti(items, 0)
tests.Compare(t, cache, "item1", 1)
tests.Compare(t, cache, "item2", "string")
}
}
func TestCompareAndReplace(t *testing.T) {
for _, cache := range testDrivers() {
cache.Set("key1", []byte("CompareAndReplace"), 0)
val1, token1, _ := cache.Get("key1")
if string(val1) != "CompareAndReplace" {
tests.FailMsg(t, cache, "`key1` should equal `CompareAndReplace`")
}
err := cache.CompareAndReplace(token1, "key1", []byte("ReplacementValue"), 0)
if err != nil {
tests.FailMsg(t, cache, "CompareAndReplace should be executed.")
}
val2, token2, _ := cache.Get("key1")
if string(val2) != "ReplacementValue" {
tests.FailMsg(t, cache, "`key1` should equal `ReplacementValue`")
}
err = cache.CompareAndReplace(token2+"WRONG", "key1", []byte("WrongValue"), 0)
if err == nil {
tests.FailMsg(t, cache, "WrongValue should not be set.")
}
val3, _, _ := cache.Get("key1")
if string(val3) != "ReplacementValue" {
tests.FailMsg(t, cache, "`key1` should equal `ReplacementValue`")
}
}
}
func TestReplace(t *testing.T) {
for _, cache := range testDrivers() {
if err := cache.Replace("key1", []byte("value1"), 0); err == nil {
tests.FailMsg(t, cache, "Key1 is not set yet, should not be able to replace.")
}
cache.Set("key1", []byte("value1"), 0)
if err := cache.Replace("key1", []byte("value1"), 0); err != nil {
tests.FailMsg(t, cache, "Key1 has been set, should be able to replace.")
}
}
}
func TestCache_Increment(t *testing.T) {
for _, cache := range testDrivers() {
cache.Increment("key1", 0, 1, 0)
cache.Increment("key1", 0, 1, 0)
v, _, _ := cache.Get("key1")
num, _ := encoding.BytesInt64(v)
if num != 1 {
tests.FailMsg(t, cache, "Expected the value to be 1, got %d", num)
}
cache.Set("key2", []byte("string value, not incrementable"), 0)
err := cache.Increment("key2", 0, 5, 0)
if err == nil {
tests.FailMsg(t, cache, "Expected the error not to be nil")
}
}
}
func TestIncrement(t *testing.T) {
for _, cache := range testDrivers() {
cache.Increment("key1", 0, 1, 0)
tests.Compare(t, cache, "key1", 0)
cache.Increment("key1", 0, 1, 0)
tests.Compare(t, cache, "key1", 1)
cache.Set("string", []byte("value"), 0)
if err := cache.Increment("string", 0, 1, 0); err == nil {
tests.FailMsg(t, cache, "Can't increment a string value.")
}
if err := cache.Increment("key2", 0, 0, 0); err == nil {
tests.FailMsg(t, cache, "Can't have an offset of <= 0")
}
if err := cache.Increment("key3", -1, 1, 0); err == nil {
tests.FailMsg(t, cache, "Can't have an initial value of < 0")
}
}
}
func TestCache_Decrement(t *testing.T) {
for _, cache := range testDrivers() {
cache.Decrement("key1", 10, 1, 0)
cache.Decrement("key1", 10, 3, 0)
v, _, _ := cache.Get("key1")
num, _ := encoding.BytesInt64(v)
if num != 7 {
tests.FailMsg(t, cache, "Expected value to be 7, got %d", num)
}
cache.Set("key2", []byte("string value, not decrementable"), 0)
err := cache.Decrement("key2", 0, 5, 0)
if err == nil {
tests.FailMsg(t, cache, "Expected error not to be nil")
}
}
}
func TestDecrement(t *testing.T) {
for _, cache := range testDrivers() {
cache.Decrement("key1", 10, 1, 0)
tests.Compare(t, cache, "key1", 10)
cache.Decrement("key1", 10, 1, 0)
tests.Compare(t, cache, "key1", 9)
cache.Set("string", []byte("value"), 0)
if err := cache.Decrement("string", 0, 1, 0); err == nil {
tests.FailMsg(t, cache, "Can't decrement a string value.")
}
if err := cache.Decrement("key2", 0, 0, 0); err == nil {
tests.FailMsg(t, cache, "Can't have an offset of <= 0")
}
if err := cache.Decrement("key3", -1, 1, 0); err == nil {
tests.FailMsg(t, cache, "Can't have an initial value of < 0")
}
if err := cache.Decrement("key1", 10, 10, 0); err == nil {
tests.FailMsg(t, cache, "Can't decrement below 0")
}
}
}
func TestGet(t *testing.T) {
for _, cache := range testDrivers() {
cache.Set("key1", []byte("value1"), 0)
tests.Compare(t, cache, "key1", "value1")
if _, _, err := cache.Get("key2"); err == nil {
tests.FailMsg(t, cache, "Key2 is not present, err should not be nil.")
}
}
}
func TestGetToken(t *testing.T) {
for _, cache := range testDrivers() {
cache.Set("key1", []byte("value1"), 0)
_, token1, _ := cache.Get("key1")
cache.Set("key1", []byte("value2"), 0)
_, token2, _ := cache.Get("key1")
if token1 == token2 {
tests.FailMsg(t, cache, "token1 should not equal token2.")
}
}
}
func TestGetMulti(t *testing.T) {
for _, cache := range testDrivers() {
items := map[string][]byte{
"item1": encoding.Int64Bytes(1),
"item2": []byte("string"),
}
cache.SetMulti(items, 0)
var keys []string
for k := range items {
keys = append(keys, k)
}
values, tokens, errs := cache.GetMulti(keys)
_, val := binary.Varint(values["item1"])
if val != 1 {
tests.FailMsg(t, cache, "Expected `item1` to equal `1`")
}
if err, ok := errs["item1"]; !ok || err != nil {
tests.FailMsg(t, cache, "Expected `item1` to be ok.")
}
if tokens["item1"] == "" {
tests.FailMsg(t, cache, "Expected `item1` to have a valid token.")
}
if string(values["item2"]) != "string" {
tests.FailMsg(t, cache, "Expected `item2` to equal `string`")
}
}
}
func TestDelete(t *testing.T) {
for _, cache := range testDrivers() {
cache.Set("key1", []byte("value1"), 0)
tests.Compare(t, cache, "key1", "value1")
cache.Delete("key1")
if _, _, err := cache.Get("key1"); err == nil {
tests.FailMsg(t, cache, "`key1` should be deleted from the cache.")
}
}
}
func TestDeleteMulti(t *testing.T) {
for _, cache := range testDrivers() {
items := map[string][]byte{
"item1": encoding.Int64Bytes(1),
"item2": []byte("string"),
}
cache.SetMulti(items, 0)
cache.Set("key1", []byte("value1"), 0)
var keys []string
for k := range items {
keys = append(keys, k)
}
cache.DeleteMulti(keys)
if _, _, err := cache.Get("item1"); err == nil {
tests.FailMsg(t, cache, "`item1` should be deleted from the cache.")
}
if _, _, err := cache.Get("item2"); err == nil {
tests.FailMsg(t, cache, "`item2` should be deleted from the cache.")
}
tests.Compare(t, cache, "key1", "value1")
}
}
func TestFlush(t *testing.T) {
for _, cache := range testDrivers() {
cache.Set("key1", []byte("value1"), 0)
tests.Compare(t, cache, "key1", "value1")
if err := cache.Flush(); err != nil {
tests.FailMsg(t, cache, "Cache should be able to flush")
}
if _, _, err := cache.Get("key1"); err == nil {
tests.FailMsg(t, cache, "Expecting `key1` to be nil")
}
}
}
func TestTouch(t *testing.T) {
for _, cache := range testDrivers() {
if err := cache.Touch("key1", 5); err == nil {
tests.FailMsg(t, cache, "Can't touch a non-existing key.")
}
cache.Set("key1", []byte("Hello world"), 0)
if err := cache.Touch("key1", 5); err != nil {
tests.FailMsg(t, cache, "Should be able to touch existing key.")
}
}
}
func testDrivers() []cacher.Cacher {
var drivers []cacher.Cacher
memoryCache := memory.New(0)
memoryCache.Flush()
drivers = append(drivers, memoryCache)
c, _ := redis.Dial("tcp", ":6379")
redisCache := rcache.New(c)
redisCache.Flush()
drivers = append(drivers, redisCache)
return drivers
}