-
Notifications
You must be signed in to change notification settings - Fork 3
/
memcache_test.go
444 lines (423 loc) · 13.8 KB
/
memcache_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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package gomemcache
import (
"bytes"
"fmt"
"os"
"testing"
"time"
)
var client, textClient *Client
func init() {
var err error
client, err = NewClient([]string{"127.0.0.1:11211", "127.0.0.1:11213"})
if err != nil {
os.Exit(1)
}
client.SetProtocol("binary")
textClient, err = NewClient([]string{"127.0.0.1:11211", "127.0.0.1:11213"})
if err != nil {
os.Exit(1)
}
}
type TestCase struct {
client *Client
protocol string
command string
}
func TestSet(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "set"},
{client: textClient, protocol: "text", command: "set"},
}
for _, testcase := range testcases {
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
if err := testcase.client.Set(&Item{Key: key, Value: value}); err != nil {
t.Fatalf("client %s set error: %v", testcase.protocol, err)
}
}
}
func TestGet(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "get"},
{client: textClient, protocol: "text", command: "get"},
}
for _, testcase := range testcases {
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
item := &Item{Key: key, Value: value}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("client %s set error: %v", testcase.protocol, err)
}
result, err := testcase.client.Get(key)
if err != nil {
t.Fatalf("client %s get error: %v", testcase.protocol, err)
}
if result == nil {
t.Fatalf("client %s TestGet value expect: %v but got nil", testcase.protocol, string(value))
}
if !bytes.Equal(value, result.Value) {
t.Fatalf("client %s TestGet value expect: %v but got: %v", testcase.protocol, string(value), string(result.Value))
}
}
}
func TestGetFlagItem(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "get_flag_item"},
{client: textClient, protocol: "text", command: "get_flag_item"},
}
for _, testcase := range testcases {
flags := uint32(1000)
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
item := &Item{Key: key, Value: value, Flags: flags}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("client %s set error: %v", testcase.protocol, err)
}
result, err := testcase.client.Get(key)
if err != nil {
t.Fatalf("client %s get error: %v", testcase.protocol, err)
}
if result == nil {
t.Fatalf("client %s TestGet value expect: %v but got nil", testcase.protocol, string(value))
}
if !bytes.Equal(value, result.Value) {
t.Fatalf("client %s TestGet value expect: %v but got: %v", testcase.protocol, string(value), string(result.Value))
}
if flags != result.Flags {
t.Fatalf("client %s TestGet flags expect: %v but got: %v", testcase.protocol, flags, result.Flags)
}
}
}
func TestGetExpireItem(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "get_expire_item"},
{client: textClient, protocol: "text", command: "get_expire_item"},
}
for _, testcase := range testcases {
expiration := 1
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
item := &Item{Key: key, Value: value, Expiration: uint32(expiration)}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("client %s set error: %v", testcase.protocol, err)
}
result, err := testcase.client.Get(key)
if err != nil {
t.Fatalf("client %s get error: %v", testcase.protocol, err)
}
if result == nil {
t.Fatalf("client %s TestGet value expect: %v but got nil", testcase.protocol, string(value))
}
time.Sleep(time.Duration(expiration+1) * time.Second)
result, err = testcase.client.Get(key)
if err != nil {
t.Fatalf("client %s get error: %v", testcase.protocol, err)
}
if result != nil {
t.Fatalf("client %s get expired item got: %v", testcase.protocol, result)
}
}
}
func TestGets(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "gets"},
{client: textClient, protocol: "text", command: "gets"},
}
for _, testcase := range testcases {
flags := uint32(1000)
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
item := &Item{Key: key, Value: value, Flags: flags}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("client %s set error: %v", testcase.protocol, err)
}
result, err := testcase.client.Gets(key)
if err != nil {
t.Fatalf("client %s get error: %v", testcase.protocol, err)
}
if result == nil {
t.Fatalf("client %s TestGets value expect: %v but got nil", testcase.protocol, string(value))
}
if !bytes.Equal(value, result.Value) {
t.Fatalf("client %s TestGets value expect: %v but got: %v", testcase.protocol, string(value), string(result.Value))
}
if flags != result.Flags {
t.Fatalf("client %s TestGets flags expect: %v but got: %v", testcase.protocol, flags, result.Flags)
}
if result.CAS == 0 {
t.Fatalf("client %s TestGets CAS shouldn't got: 0", testcase.protocol)
}
}
}
func TestAdd(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "add"},
{client: textClient, protocol: "text", command: "add"},
}
for _, testcase := range testcases {
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
client.SetNoreply(false)
if err := testcase.client.Delete(key); err != nil && err != ErrItemNotFound {
t.Fatalf("test %s add delete error: %v", testcase.protocol, err)
}
if err := testcase.client.Add(&Item{Key: key, Value: value}); err != nil {
t.Fatalf("test %s add error: %v", testcase.protocol, err)
}
err := testcase.client.Add(&Item{Key: key, Value: value})
if err == nil {
t.Fatalf("test %s add second execute should error", testcase.protocol)
}
}
}
func TestCAS(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "cas"},
{client: textClient, protocol: "text", command: "cas"},
}
for _, testcase := range testcases {
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
if err := testcase.client.Set(&Item{Key: key, Value: value}); err != nil {
t.Fatalf("cas %s first set error: %v", testcase.protocol, err)
}
item, err := testcase.client.Get(key)
if err != nil {
t.Fatalf("cas %s get error: %v", testcase.protocol, err)
}
if err = testcase.client.Set(&Item{Key: key, Value: []byte("test_value_cas_2")}); err != nil {
t.Fatalf("cas %s second set error: %v", testcase.protocol, err)
}
err = testcase.client.CAS(&Item{Key: key, Value: []byte("test_value_cas_3"), CAS: item.CAS})
if err == nil {
t.Fatalf("cas %s set expect fail, but successful", testcase.protocol)
}
}
}
func TestReplace(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "replace"},
{client: textClient, protocol: "text", command: "replace"},
}
for _, testcase := range testcases {
key := fmt.Sprintf("test_%s_%s_key", testcase.protocol, testcase.command)
value := []byte(fmt.Sprintf("test_%s_%s_value", testcase.protocol, testcase.command))
if err := testcase.client.Delete(key); err != nil && err != ErrItemNotFound {
t.Fatalf("test add delete error: %v", err)
}
if err := testcase.client.Replace(&Item{Key: key, Value: value}); err == nil {
t.Fatalf("test replace when key not exist must raise error")
}
if err := testcase.client.Set(&Item{Key: key, Value: []byte("test_replace_value")}); err != nil {
t.Fatalf("test replace set error: %v", err)
}
if err := testcase.client.Replace(&Item{Key: key, Value: value}); err != nil {
t.Fatalf("test replace error: %v", err)
}
item, err := testcase.client.Get(key)
if err != nil {
t.Fatalf("test replace get error: %v", err)
}
if !bytes.Equal(item.Value, value) {
t.Fatalf("test replace expect %v got %v", item.Value, value)
}
}
}
func TestMultiGet(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "multiGet"},
{client: textClient, protocol: "text", command: "multiGet"},
}
for _, testcase := range testcases {
num := 10
keys := make([]string, 0, num)
expect := make(map[string][]byte, num)
for i := 0; i < num; i++ {
key := fmt.Sprintf("test_%s_%s_key_%d", testcase.protocol, testcase.command, i)
value := []byte(fmt.Sprintf("test_%s_%s_value_%d", testcase.protocol, testcase.command, i))
item := &Item{Key: key, Value: value}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("set error: %v", err)
}
keys = append(keys, key)
expect[key] = value
}
items, err := testcase.client.MultiGet(keys)
if err != nil {
t.Fatalf("get error: %v", err)
}
result := make(map[string]*Item, len(items))
for _, item := range items {
result[item.Key] = item
}
for k, v := range expect {
if _, ok := result[k]; !ok {
t.Fatalf("MultiGet key: %s not exsist", k)
}
if !bytes.Equal(v, result[k].Value) {
t.Fatalf("MultiGet key: %s expect: %v got: %v", k, v, result[k])
}
}
}
}
func TestMultiGetWithRepeatKeys(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "multiGet"},
{client: textClient, protocol: "text", command: "multiGet"},
}
for _, testcase := range testcases {
num := 10
keys := make([]string, 0, num)
expect := make(map[string][]byte, num)
for i := 0; i < num; i++ {
key := fmt.Sprintf("test_%s_%s_rkey_%d", testcase.protocol, testcase.command, i)
value := []byte(fmt.Sprintf("test_%s_%s_rvalue_%d", testcase.protocol, testcase.command, i))
item := &Item{Key: key, Value: value}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("set error: %v", err)
}
keys = append(keys, key)
if i%3 == 0 {
keys = append(keys, key)
}
expect[key] = value
}
items, err := testcase.client.MultiGet(keys)
if err != nil {
t.Fatalf("get error: %v", err)
}
result := make(map[string]*Item, len(items))
for _, item := range items {
result[item.Key] = item
}
for k, v := range expect {
if _, ok := result[k]; !ok {
t.Fatalf("MultiGet %s key: %s not exsist", testcase.protocol, k)
}
if !bytes.Equal(v, result[k].Value) {
t.Fatalf("MultiGet %s key: %s expect: %v got: %v", testcase.protocol, k, v, result[k])
}
}
}
}
func TestDelete(t *testing.T) {
testcases := []TestCase{
{client: client, protocol: "binary", command: "delete"},
{client: textClient, protocol: "text", command: "delete"},
}
for i, testcase := range testcases {
key := fmt.Sprintf("test_%s_%s_key_%d", testcase.protocol, testcase.command, i)
value := []byte(fmt.Sprintf("test_%s_%s_value_%d", testcase.protocol, testcase.command, i))
item := &Item{Key: key, Value: value}
if err := testcase.client.Set(item); err != nil {
t.Fatalf("set error: %v", err)
}
if err := testcase.client.Delete(key); err != nil {
t.Fatalf("delete error: %v", err)
}
result, err := testcase.client.Get(key)
if err != nil {
t.Fatalf("get error: %v", err)
}
if result != nil {
t.Fatalf("TestDelete expect result: nil but got: %v", result)
}
}
}
func BenchmarkBinarySet(b *testing.B) {
item := &Item{Key: "bench_binary_set", Value: []byte("world")}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := client.Set(item); err != nil {
b.Fatalf("set error: %v", err)
}
}
}
func BenchmarkBinaryGet(b *testing.B) {
key := "bench_binary_get"
value := []byte("world")
item := &Item{Key: key, Value: value}
if err := client.Set(item); err != nil {
b.Fatalf("set error: %v", err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result, err := client.Get(key)
if err != nil {
b.Fatalf("get error: %v", err)
}
if !bytes.Equal(value, result.Value) {
b.Fatalf("TestGet expect: %v but got: %v", value, result)
}
}
}
func BenchmarkBinaryMultiGet(b *testing.B) {
value := []byte("world")
keys := make([]string, 0, 10)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("bench_binary_%d", i)
item := &Item{Key: key, Value: value}
if err := client.Set(item); err != nil {
b.Fatalf("set error: %v", err)
}
keys = append(keys, key)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := client.MultiGet(keys)
if err != nil {
b.Fatalf("get error: %v", err)
}
}
}
func BenchmarkTextSet(b *testing.B) {
item := &Item{Key: "bench_text_set", Value: []byte("world")}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := textClient.Set(item); err != nil {
b.Fatalf("set error: %v", err)
}
}
}
func BenchmarkTextGet(b *testing.B) {
key := "bench_text_get"
value := []byte("world")
item := &Item{Key: key, Value: value}
if err := textClient.Set(item); err != nil {
b.Fatalf("set error: %v", err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
result, err := textClient.Get(key)
if err != nil {
b.Fatalf("get error: %v", err)
}
if !bytes.Equal(value, result.Value) {
b.Fatalf("TestGet expect: %v but got: %v", value, result)
}
}
}
func BenchmarkTextMultiGet(b *testing.B) {
value := []byte("world")
keys := make([]string, 0, 10)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("bench_text_%d", i)
item := &Item{Key: key, Value: value}
if err := textClient.Set(item); err != nil {
b.Fatalf("set error: %v", err)
}
keys = append(keys, key)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := textClient.MultiGet(keys)
if err != nil {
b.Fatalf("get error: %v", err)
}
}
}