-
Notifications
You must be signed in to change notification settings - Fork 10
/
client_test.go
1698 lines (1406 loc) · 53.6 KB
/
client_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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mc
import (
"fmt"
"math/rand"
"regexp"
"strconv"
"testing"
"time"
)
const (
mcAddr = "localhost:11289"
badAddr = "127.0.0.2:23111"
user = "user-1"
pass = "pass"
)
var mcNil error
// Some basic tests that functions work
func TestMCSimple(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Val1 = "bar"
Val2 = "bar-bad"
Val3 = "bar-good"
)
// fmt.Printf("test init: %v", c)
val, flags, cs, err := c.Get(Key1)
if err != ErrNotFound {
t.Errorf("val: %v, flags: %v, cas: %v", val, flags, cs)
t.Fatalf("expected missing key: %v", err)
}
// unconditional SET
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
cas, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
// make sure CAS works
_, err = c.Set(Key1, Val2, 0, 0, cas+1)
assertEqualf(t, ErrKeyExists, err, "expected CAS mismatch: %v", err)
// check SET actually set the correct value...
v, _, cas2, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, Val1, v, "wrong value: %s", v)
assertEqualf(t, cas, cas2, "CAS shouldn't have changed: %d, %d", cas, cas2)
// use correct CAS...
cas2, err = c.Set(Key1, Val3, 0, 0, cas)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, cas, cas2, "CAS should not be the same")
}
// Test GET, does it care about CAS?
// NOTE: No it shouldn't, memcached mainline doesn't...
func TestGet(t *testing.T) {
c := testInit(t)
const (
Key1 = "fab"
Val1 = "faz"
)
_, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
// retrieve value with 0 CAS...
v1, _, cas1, err := c.getCAS(Key1, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, Val1, v1, "wrong value: %s", v1)
// retrieve value with good CAS...
v2, _, cas2, err := c.getCAS(Key1, cas1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, v1, v2, "value changed when it shouldn't: %s, %s", v1, v2)
assertEqualf(t, cas1, cas2, "CAS changed when it shouldn't: %d, %d", cas1, cas2)
// retrieve value with bad CAS...
v3, _, cas1, err := c.getCAS(Key1, cas1+1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, v3, v2, "value changed when it shouldn't: %s, %s", v3, v2)
assertEqualf(t, cas1, cas2, "CAS changed when it shouldn't: %d, %d", cas1, cas2)
// really make sure CAS is bad (above could be an off by one bug...)
v4, _, cas1, err := c.getCAS(Key1, cas1+992313128)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, v4, v2, "value changed when it shouldn't: %s, %s", v4, v2)
assertEqualf(t, cas1, cas2, "CAS changed when it shouldn't: %d, %d", cas1, cas2)
}
// Test some edge cases of memcached. This was originally done to better
// understand the protocol but servers as a good test for the client and
// server...
// Test SET behaviour with CAS...
func TestSet(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Key2 = "goo"
Val1 = "bar"
Val2 = "zar"
)
cas1, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
v, _, cas2, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, Val1, v, "wrong value: %v", v)
assertEqualf(t, cas1, cas2, "CAS don't match: %d != %d", cas1, cas2)
// do two sets of same key, make sure CAS changes...
cas1, err = c.Set(Key2, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
cas2, err = c.Set(Key2, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertNotEqualf(t, cas1, cas2, "CAS don't match: %d == %d", cas1, cas2)
// get back the val from Key2...
v, _, cas2, err = c.Get(Key2)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, Val1, v, "wrong value: %v", v)
// make sure changing value works...
_, err = c.Set(Key1, Val2, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
v, _, cas1, err = c.Get(Key1)
assertEqualf(t, Val2, v, "wrong value: %s", v)
// Delete Key1 and check it worked, needed for next test...
err = c.Del(Key1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "wrong error: %v", err)
// What happens when I set a new key and specify a CAS?
// (should fail, bad CAS, can't specify a CAS for a non-existent key, it fails,
// doesn't just ignore the CAS...)
cas, err := c.Set(Key1, Val1, 0, 0, 1)
assertEqualf(t, ErrNotFound, err, "wrong error: %v", err)
assertEqualf(t, uint64(0), cas, "CAS should be nil: %d", cas)
// make sure it really didn't set it...
v, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "wrong error: %v", err)
// TODO: On errors a human readable error description should be returned. So
// could test that.
// Setting an existing value with bad CAS... should fail
_, err = c.Set(Key2, Val2, 0, 0, cas2+1)
assertEqualf(t, ErrKeyExists, err, "wrong error: %v", err)
v, _, cas1, err = c.Get(Key2)
assertEqualf(t, Val1, v, "value shouldn't have changed: %s", v)
assertEqualf(t, cas1, cas2, "CAS shouldn't have changed: %d, %d", cas1, cas2)
}
// Testing Max SIZE of values...
// Testing if when you set a key/value with a bad value (e.g > 1MB) does that
// remove the existing key/value still or leave it intact?
func TestSetBadRemovePrevious(t *testing.T) {
c := testInit(t)
const (
// Larger than this memcached doesn't like for key 'foo' (with defaults)
MaxValSize = 1024*1024 - 80
Key = "foo"
Val = "bar"
)
// check basic get/set works first
_, err := c.Set(Key, Val, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err := c.Get(Key)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, Val, v, "wrong value: %s", v)
// Max GOOD ValUE
// generate random bytes
data := make([]byte, MaxValSize)
for i := 0; i < MaxValSize; i++ {
data[i] = byte(rand.Int())
}
val := string(data)
_, err = c.Set(Key, val, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err = c.Get(Key)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, val, v, "wrong value: (too big to print)")
// Max GOOD ValUE * 2
// generate random bytes
data = make([]byte, 2*MaxValSize)
for i := 0; i < 2*MaxValSize; i++ {
data[i] = byte(rand.Int())
}
val2 := string(data)
_, err = c.Set(Key, val2, 0, 0, 0)
assertEqualf(t, ErrValueTooLarge, err, "expected too large error: %v", err)
v, _, _, err = c.Get(Key)
if err == mcNil {
fmt.Println("\tmemcached removes the old value... so expecting no key")
fmt.Println("\tnot an error but just a different semantics than memcached")
// well it should at least be the old value stil..
assertEqualf(t, val, v, "wrong value: (too big to print)")
} else {
assertEqualf(t, ErrNotFound, err, "expected no key: %v", err)
}
}
// Test ADD.
func TestAdd(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Val1 = "bar"
)
c.Del(Key1)
// check add works... (key not already present)
_, err := c.Add(Key1, Val1, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error adding key: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error getting key: %v", err)
assertEqualf(t, v, Val1, "unexpected value for key: %v", v)
// check add works... (key already present)
_, err = c.Add(Key1, Val1, 0, 0)
assertEqualf(t, ErrKeyExists, err,
"expected an error adding existing key: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error getting key: %v", err)
assertEqualf(t, v, Val1, "unexpected value for key: %v", v)
}
// Test Replace.
func TestReplace(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Val1 = "bar"
Val2 = "car"
)
c.Del(Key1)
// check replace works... (key not already present)
_, err := c.Replace(Key1, Val1, 0, 0, 0)
assertEqualf(t, ErrNotFound, err,
"expected an error replacing non-existent key: %v", err)
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "expected error getting key: %v", err)
// check replace works...(key already present)
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, Val1, v, "wrong value: %v", v)
_, err = c.Replace(Key1, Val2, 0, 0, 0)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, Val2, v, "wrong value: %v", v)
// check replace works [2nd take]... (key not already present)
c.Del(Key1)
_, err = c.Replace(Key1, Val1, 0, 0, 0)
assertEqualf(t, ErrNotFound, err,
"expected an error replacing non-existent key: %v", err)
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "expected error getting key: %v", err)
// What happens when I replace a value and give a good CAS?...
cas, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
cas, err = c.Replace(Key1, Val1, 0, 0, cas)
assertEqualf(t, mcNil, err, "replace with good CAS failed: %v", err)
// bad CAS
_, err = c.Replace(Key1, Val2, 0, 0, cas+1)
assertEqualf(t, ErrKeyExists, err, "replace with bad CAS failed: %v", err)
}
// Test Delete.
func TestDelete(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Val1 = "bar"
)
// delete existing key...
_, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
err = c.Del(Key1)
assertEqualf(t, mcNil, err, "error deleting key: %v", err)
// delete non-existent key...
err = c.Del(Key1)
assertEqualf(t, ErrNotFound, err,
"no error deleting non-existent key: %v", err)
// delete existing key with 0 CAS...
cas1, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
err = c.DelCAS(Key1, cas1+1)
assertEqualf(t, ErrKeyExists, err,
"expected an error for deleting key with wrong CAS: %v", err)
// confirm it isn't gone...
v, _, cas1, err := c.Get(Key1)
assertEqualf(t, mcNil, err,
"delete with wrong CAS seems to have succeeded: %v", err)
assertEqualf(t, v, Val1, "corrupted value in cache: %v", v)
// now delete with good CAS...
err = c.DelCAS(Key1, cas1)
assertEqualf(t, mcNil, err,
"unexpected error for deleting key with correct CAS: %v", err)
// delete existing key with good CAS...
cas1, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
err = c.DelCAS(Key1, cas1)
assertEqualf(t, mcNil, err,
"unexpected error for deleting key with correct CAS: %v", err)
v, _, cas1, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err,
"delete with wrong CAS seems to have succeeded: %v", err)
// delete existing key with 0 CAS...
cas1, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
err = c.DelCAS(Key1, 0)
assertEqualf(t, mcNil, err,
"unexpected error for deleting key with 0 CAS: %v", err)
v, _, cas1, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err,
"delete with wrong CAS seems to have succeeded: %v", err)
}
// Test behaviour of errors and cache removal.
// NOTE: calling incr/decr on a non-numeric returns an error BUT also seems to
//
// remove it from the cache...
//
// NOTE: I think above may have been a bug present in memcache 1.4.12 but is
//
// fixed in 1.4.13...
func TestIncrDecrNonNumeric(t *testing.T) {
c := testInit(t)
const (
Key1 = "n"
NStart uint64 = 10
NVal = defaultPort
Val = "nup"
)
_, err := c.Set(Key1, Val, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, v, Val, "wrong value: %v", v)
_, _, err = c.Incr(Key1, 1, NStart, 0, 0)
assertEqualf(t, ErrNonNumeric, err, "unexpected error: %v", err)
_, _, err = c.Decr(Key1, 1, NStart, 0, 0)
assertEqualf(t, ErrNonNumeric, err, "unexpected error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, v, Val, "wrong value: %v", v)
}
// Test Incr/Decr works...
func TestIncrDecr(t *testing.T) {
c := testInit(t)
const (
Key1 = "n"
NStart uint64 = 10
NVal = defaultPort
)
// check DEL of non-existing key fails...
err := c.Del(Key1)
if err != ErrNotFound {
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
}
err = c.Del(Key1)
assertEqualf(t, ErrNotFound, err, "expected missing key: %v", err)
// test INCR/DECR...
exp := NStart // track what we expect
n, cas, err := c.Incr(Key1, 1, NStart, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
exp = exp + 1
n, cas, err = c.Incr(Key1, 1, 99, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
exp = exp - 1
n, cas, err = c.Decr(Key1, 1, 97, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// test big addition
exp = exp + 1123139
n, cas, err = c.Incr(Key1, 1123139, 97, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// test zero addition
exp = exp + 0
n, cas, err = c.Incr(Key1, 0, 97, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// test CAS works... (should match)
exp = exp - 1
n, cas, err = c.Decr(Key1, 1, 93, 0, cas)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// test CAS works... (should fail, doesn't match)
exp = exp + 0
n, cas, err = c.Decr(Key1, 1, 87, 0, cas+97)
assertEqualf(t, ErrKeyExists, err, "expected CAS mismatch: %v", err)
assertEqualf(t, uint64(0), n, "expected 0 due to CAS mismatch: %d", n)
assertEqualf(t, uint64(0), cas, "expected 0 due to CAS mismatch: %d", cas)
// test that get on a counter works...
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
vn := strconv.FormatUint(exp, 10)
assertEqualf(t, vn, v, "wrong value: %d (expected %s)", n, vn)
// test that set on a counter works...
_, err = c.Set(Key1, NVal, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, NVal, v, "wrong value: %s (expected %s)", v, NVal)
exp, errNum := strconv.ParseUint(NVal, 10, 64)
assertEqualf(t, nil, errNum, "unexpected error: %v", errNum)
exp = exp + 1123139
n, cas, err = c.Incr(Key1, 1123139, 97, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertNotEqualf(t, 0, cas, "CAS should not be 0")
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
}
// Test expiration works...
func TestIncrTimeouts(t *testing.T) {
c := testInit(t)
const (
Key2 = "n"
NStart uint64 = 10
)
c.Del(Key2)
// Incr (key, delta, initial, ttl, cas)
exp := NStart
n, _, err := c.Incr(Key2, 1, NStart, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
time.Sleep(1200 * time.Millisecond)
// no delta_only set before, so should incr
exp = exp + 39
n, _, err = c.Incr(Key2, 39, NStart, 1, 0)
assertEqualf(t, mcNil, err, "%v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
}
// Test Incr/Decr expiration field.
// This is a stupid name for the field as it has nothing to do with expiration /
// ttl. Instead its used to indicate that the incr/decr should fail if the key
// doesn't already exist in the cache. (i.e., that is since the incr/decr
// command takes both an initial value and a delta, the expiration field allows
// us to say that only the delta should be applied and rather than use the
// initial value when the key doesn't exist, throw an error).
//
// Only the value 0xffffffff is used to indicate that only the delta should be
// applied, all other values for expiration allow either the initial or delta to
// be used.
func TestIncrExpiration(t *testing.T) {
c := testInit(t)
const (
Key1 = "n"
NStart uint64 = 10
OnlyDelta uint32 = 0xffffffff
)
// fail as we only allow applying the delta with that expiration value.
c.Del(Key1)
_, _, err := c.Incr(Key1, 10, NStart, OnlyDelta, 0)
assertEqualf(t, ErrNotFound, err, "unexpected error: %v", err)
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "key shouldn't exist in cache: %v", err)
// succeed this time. Any value but OnlyDelta should succeed.
exp := NStart
n, _, err := c.Incr(Key1, 10, NStart, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
c.Del(Key1)
// succeed this time. Any value but OnlyDelta should succeed.
exp = NStart
n, _, err = c.Incr(Key1, 10, NStart, 1, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
c.Del(Key1)
// succeed this time. Any value but OnlyDelta should succeed.
exp = NStart
n, _, err = c.Incr(Key1, 10, NStart, OnlyDelta-1, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
c.Del(Key1)
}
// Test Incr/Decr overflow...
func TestIncrDecrWrap(t *testing.T) {
c := testInit(t)
const (
Key1 = "n"
NStart uint64 = 10
Max1 uint64 = 0xfffffffffffffffe
Max uint64 = 0xffffffffffffffff
)
// setup...
exp := NStart
n, _, err := c.Decr(Key1, NStart+1, NStart, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// can't decr past 0...
exp = 0
n, _, err = c.Decr(Key1, NStart+1, NStart, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// test limit of incr...
exp = Max1
n, _, err = c.Incr(Key1, Max1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
exp = Max
n, _, err = c.Incr(Key1, 1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
// overflow... wrap around
exp = 0
n, _, err = c.Incr(Key1, 1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, n, "wrong value: %d (expected %d)", n, exp)
}
// Test Append works...
func TestAppend(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Key2 = "goo"
Val1 = "moo"
Val2 = "bar"
)
c.Del(Key1)
c.Del(Key2)
// normal append
exp := Val1
_, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = exp + Val2
_, err = c.Append(Key1, Val2, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
// append to non-existent value
exp = Val1
_, err = c.Append(Key2, Val1, 0)
if err != ErrValueNotStored {
t.Errorf("expected 'value not stored error', got: %v", err)
}
v, _, _, err = c.Get(Key2)
assertEqualf(t, ErrNotFound, err, "expected not found error: %v", err)
// check CAS works...
v, _, cas, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = v
_, err = c.Append(Key1, Val2, cas+1)
assertEqualf(t, ErrKeyExists, err, "expected key exists error: %v", err)
v, _, cas2, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
assertEqualf(t, cas, cas2, "CAS shouldn't have changed: %d != %d", cas, cas2)
exp = exp + Val2
_, err = c.Append(Key1, Val2, cas)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = exp + Val1
// check 0 CAS...
_, err = c.Append(Key1, Val1, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
}
// Test Prepend works...
func TestPrepend(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Key2 = "goo"
Val1 = "moo"
Val2 = "bar"
)
c.Del(Key1)
c.Del(Key2)
// normal append
exp := Val1
_, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = Val2 + exp
_, err = c.Prepend(Key1, Val2, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
// append to non-existent value
exp = Val1
_, err = c.Prepend(Key2, Val1, 0)
if err != ErrValueNotStored {
t.Errorf("expected 'value not stored error', got: %v", err)
}
v, _, _, err = c.Get(Key2)
assertEqualf(t, ErrNotFound, err, "expected not found error: %v", err)
// check CAS works...
v, _, cas, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = v
_, err = c.Prepend(Key1, Val2, cas+1)
assertEqualf(t, ErrKeyExists, err, "expected key exists error: %v", err)
v, _, cas2, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
assertEqualf(t, cas, cas2, "CAS shouldn't have changed: %d != %d", cas, cas2)
exp = Val2 + exp
_, err = c.Prepend(Key1, Val2, cas)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = Val1 + exp
// check 0 CAS...
_, err = c.Prepend(Key1, Val1, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
}
// Test NoOp works... (by putting NoOps all between the prepend tests)
func TestNoOp(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Key2 = "goo"
Val1 = "moo"
Val2 = "bar"
)
err := c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
err = c.NoOp()
err = c.NoOp()
err = c.NoOp()
err = c.NoOp()
err = c.NoOp()
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
c.Del(Key1)
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
c.Del(Key2)
// normal append
exp := Val1
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
exp = Val2 + exp
_, err = c.Prepend(Key1, Val2, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
// append to non-existent value
exp = Val1
_, err = c.Prepend(Key2, Val1, 0)
if err != ErrValueNotStored {
t.Errorf("expected 'value not stored error', got: %v", err)
}
v, _, _, err = c.Get(Key2)
assertEqualf(t, ErrNotFound, err, "expected not found error: %v", err)
// check CAS works...
err = c.NoOp()
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, cas, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = v
_, err = c.Prepend(Key1, Val2, cas+1)
assertEqualf(t, ErrKeyExists, err, "expected key exists error: %v", err)
err = c.NoOp()
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, cas2, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
assertEqualf(t, cas, cas2, "CAS shouldn't have changed: %d != %d", cas, cas2)
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
exp = Val2 + exp
_, err = c.Prepend(Key1, Val2, cas)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
exp = Val1 + exp
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
// check 0 CAS...
_, err = c.Prepend(Key1, Val1, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, exp, v, "wrong value: %s", v)
err = c.NoOp()
assertEqualf(t, mcNil, err, "noop unexpected error: %v", err)
}
// Test Flush behaviour
func TestFlush(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Key2 = "goo"
Key3 = "hoo"
Val1 = "bar"
Val2 = "zar"
Val3 = "gar"
)
err := c.Flush(0)
assertEqualf(t, mcNil, err, "flush produced error: %v", err)
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertEqualf(t, Val1, v, "wrong value: %v", v)
err = c.Flush(0)
assertEqualf(t, mcNil, err, "flush produced error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
// do two sets of same key, make sure CAS changes...
cas1, err := c.Set(Key2, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
cas2, err := c.Set(Key2, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
assertNotEqualf(t, cas1, cas2, "CAS don't match: %d == %d", cas1, cas2)
// try to get back the vals...
err = c.Flush(0)
assertEqualf(t, mcNil, err, "flush produced error: %v", err)
v, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
v, _, _, err = c.Get(Key2)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
err = c.Del(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
err = c.Del(Key2)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
// do two sets
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
_, err = c.Set(Key2, Val2, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
// flush in future!
err = c.Flush(3)
// set a key now, after sending flush in future command. Should this key be
// included in flush when it applies?
_, err = c.Set(Key3, Val3, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
// keys should still survive as the flush hasn't applied yet.
time.Sleep(900 * time.Millisecond)
_, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "should have found key as flushed in future!: %v", err)
time.Sleep(100 * time.Millisecond)
_, _, _, err = c.Get(Key2)
assertEqualf(t, mcNil, err, "should have found key as flushed in future!: %v", err)
// now keys should all be flushed
time.Sleep(2200 * time.Millisecond)
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
_, _, _, err = c.Get(Key2)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
_, _, _, err = c.Get(Key3)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
// do two sets
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
_, err = c.Set(Key2, Val2, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
// flush in future! (should overwrite old flush in futures...)
err = c.Flush(3)
time.Sleep(900 * time.Millisecond)
_, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "should have found key as flushed in future!: %v", err)
time.Sleep(100 * time.Millisecond)
_, _, _, err = c.Get(Key2)
assertEqualf(t, mcNil, err, "should have found key as flushed in future!: %v", err)
err = c.Flush(4)
time.Sleep(2000 * time.Millisecond)
_, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "should have found key as flushed in future!: %v", err)
_, _, _, err = c.Get(Key2)
assertEqualf(t, mcNil, err, "should have found key as flushed in future!: %v", err)
time.Sleep(2000 * time.Millisecond)
v, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
v, _, _, err = c.Get(Key2)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key as flushed: %v", err)
}
// Test flush, flush future.
func TestFlushFuture(t *testing.T) {
c := testInit(t)
const (
Key1 = "foo"
Key2 = "goo"
Val1 = "bar"
Val2 = "zar"
)
// clear cache
err := c.Flush(0)
assertEqualf(t, mcNil, err, "flush produced error: %v", err)
// set Key1, Key2
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
_, err = c.Set(Key2, Val2, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
// wait two seconds
time.Sleep(2000 * time.Millisecond)
// flush cache (Key1, Key2)
err = c.Flush(0)
assertEqualf(t, mcNil, err, "flush produced error: %v", err)
// get Key1 -> null
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key! err: %v", err)
// re-set Key1
_, err = c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "shouldn't be an error: %v", err)
// flush again, but in future
err = c.Flush(2)
// XXX: Memcached is broken for this.
// get Key2 -- memcached bug where flush in future can resurrect items
// _, _, _, err = c.Get(Key2)
// assertEqualf(t, ErrNotFound, err, "shouldn't have found key! err: %v", err)
// get Key1
_, _, _, err = c.Get(Key1)
assertEqualf(t, mcNil, err, "should have found Key1! err: %v", err)
// wait for flush to expire
time.Sleep(2500 * time.Millisecond)
_, _, _, err = c.Get(Key1)
assertEqualf(t, ErrNotFound, err, "shouldn't have found key! err: %v", err)
}
// Test the version command works...
func TestVersion(t *testing.T) {
c := testInit(t)
vers, err := c.Version()
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
good, errRegex := regexp.MatchString("[0-9]+\\.[0-9]+\\.[0-9]+", vers[mcAddr])
assertEqualf(t, nil, errRegex, "unexpected error: %v", errRegex)
assertEqualf(t, good, true, "version of unexcpected form: %s", vers[mcAddr])
}
// Test the quit command works...
func TestQuit(t *testing.T) {
c := testInit(t)
const (
Key1 = "fooz"
Val1 = "barz"
)
_, err := c.Set(Key1, Val1, 0, 0, 0)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
v, _, _, err := c.Get(Key1)
assertEqualf(t, mcNil, err, "unexpected error: %v", err)
assertEqualf(t, Val1, v, "wrong value: %s", v)
c.Quit()
_, _, _, err = c.Get(Key1)
assertNotEqualf(t, mcNil, err, "expected an error (closed connection)")
c.Quit() // should not panic
}
// Test expiration works...
// See Note [Expiration] in mc.go for details of how expiration works.
// NOTE: Can't really test long expirations properly...
func TestExpiration(t *testing.T) {
c := testInit(t)
const (
Key0 = "zoo"
Key1 = "foo"
Key2 = "goo"