forked from Cray-HPE/hms-hmetcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvs_etcd_test.go
726 lines (584 loc) · 17.5 KB
/
kvs_etcd_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
// MIT License
//
// (C) Copyright [2021] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package hmetcd
import (
"fmt"
"sort"
"testing"
"time"
)
func TestOpen(t *testing.T) {
var kvh Kvi
var err error
t.Logf("** TESTING OPEN/CLOSE **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED OPEN/CLOSE TEST\n")
}
func TestDistLock(t *testing.T) {
var kvh Kvi
var err error
t.Logf("** TESTING DIST LOCKS **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//This test is a little hokey in that we can't test multi-locking,
//since ETCD will let a process grab the lock multiple times. So,
//we'll just insure that lock/unlock works.
err = kvh.DistLock()
if err != nil {
t.Error("ERROR acquiring distributed lock:", err)
}
err = kvh.DistUnlock()
if err != nil {
t.Error("ERROR releasing distributed lock:", err)
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED DIST LOCK TEST\n")
}
func TestStore(t *testing.T) {
var kvh Kvi
var err error
var kkey, kval string
t.Logf("** RUNNING STORE OPERATIONS TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//Store 1000 keys
for i := 0; i < 1000; i += 1 {
kkey = fmt.Sprintf("key_%04d", i)
kval = fmt.Sprintf("val_%04d", i)
err = kvh.Store(kkey, kval)
if err != nil {
t.Error("ERROR storing key:", kkey, ":", err)
break
}
}
//We'll leave the values in place, but close the interface.
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED STORE OPERATIONS TEST\n")
}
func TestGet(t *testing.T) {
var kvh Kvi
var err error
var i int
var ok bool
var kkey, kval, ekval string
t.Logf("** RUNNING GET OPERATIONS TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//Write the KVs
for i = 0; i < 1000; i += 1 {
kkey = fmt.Sprintf("key_%04d", i)
kval = fmt.Sprintf("val_%04d", i)
err = kvh.Store(kkey, kval)
if err != nil {
t.Error("ERROR storing key:", kkey, ":", err)
break
}
}
//Read them back
for i = 0; i < 1000; i += 1 {
kkey = fmt.Sprintf("key_%04d", i)
kval, ok, err = kvh.Get(kkey)
if err != nil {
t.Error("ERROR fetching key:", kkey, ":", err)
break
}
if ok != true {
t.Error("ERROR, Get operation on known key says key doesn't exist.")
}
ekval = fmt.Sprintf("val_%04d", i)
if kval != ekval {
t.Error("ERROR, mismatch, expected:", ekval, ", got:", kval)
break
}
}
//Test reading a non-existent key
kval, ok, err = kvh.Get("xyzzy_xyzzy")
if err != nil {
t.Error("ERROR, reading empty key should not fail, got:", err)
}
if ok == true {
t.Error("ERROR, reading empty key should return a false key-exists flag.")
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED GET OPERATIONS TEST\n")
}
func TestGetRange(t *testing.T) {
var kvh Kvi
var err error
var i int
var kkey, kval string
var kvrange []Kvi_KV
t.Logf("** RUNNING GET RANGE TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//Write some KVs
for i = 0; i < 1000; i += 1 {
kkey = fmt.Sprintf("key_%04d", i)
kval = fmt.Sprintf("val_%04d", i)
err = kvh.Store(kkey, kval)
if err != nil {
t.Error("ERROR storing key:", kkey, ":", err)
break
}
}
//Get a range of them
kvrange, err = kvh.GetRange("key_0100", "key_0500")
if err != nil {
t.Error("ERROR getting range:", err)
}
//Sort keys lexicographically
sort.SliceStable(kvrange, func(i, j int) bool { return kvrange[i].Key < kvrange[j].Key })
//Verify
for i = 100; i <= 500; i += 1 {
z := i - 100
kkey = fmt.Sprintf("key_%04d", i)
kval = fmt.Sprintf("val_%04d", i)
if kkey != kvrange[z].Key {
t.Errorf("ERROR, key mismatch, expected '%s', got '%s'\n", kkey, kvrange[z].Key)
break
}
if kval != kvrange[z].Value {
t.Errorf("ERROR, value mismatch, expected '%s', got '%s'\n", kval, kvrange[z].Value)
break
}
}
//Test fetching a range with no keys
kvrange, err = kvh.GetRange("key_AAAA", "key_BBBB")
if err != nil {
t.Error("ERROR, GetRange returned error on empty set:", err)
}
if len(kvrange) != 0 {
t.Error("ERROR< GetRange on empty set returned keys!")
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED GET RANGE TEST\n")
}
func TestDelete(t *testing.T) {
var kvh Kvi
var err error
var i int
var ok bool
var kkey, kval string
t.Logf("** RUNNING DELETE OPERATIONS TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//Write the KVs
for i = 0; i < 100; i += 1 {
kkey = fmt.Sprintf("key_%04d", i)
kval = fmt.Sprintf("val_%04d", i)
err = kvh.Store(kkey, kval)
if err != nil {
t.Error("ERROR storing key:", kkey, ":", err)
break
}
}
// Delete one
err = kvh.Delete("key_0023")
if err != nil {
t.Error("ERROR deleting key:", err)
}
//Try to access the key, 'ok' should be false (key doesn't exist)
kval, ok, err = kvh.Get("key_0023")
if err != nil {
t.Error("ERROR reading a deleted key unexpected failure.")
}
if ok == true {
t.Error("ERROR, reading a deleted key showed that it still exists.")
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED DELETE OPERATIONS TEST\n")
}
type transdata struct {
ifkey string
inival string
op string
ifval string
thenkey string
thenval string
elsekey string
elseval string
isthen bool
}
type tasdata struct {
key string
inival string
testval string
setval string
shouldset bool
}
var trans = []transdata{
transdata{"x0001", "val0001", "=", "val0001", "x0002", "0002_then", "x0003", "0003_else", true},
transdata{"x0011", "val0011", "!=", "val0011", "x0012", "0012_then", "x0013", "0013_else", false},
transdata{"x0021", "val0021", "<", "val0021", "x0022", "0022_then", "x0023", "0023_else", false},
transdata{"x0031", "val0031", ">", "val0031", "x0032", "0032_then", "x0033", "0033_else", false},
transdata{"x0041", "val0041", "!=", "val004x", "x0042", "0042_then", "x0043", "0043_else", true},
transdata{"x0051", "val0051", "<", "val0052", "x0052", "0052_then", "x0053", "0053_else", true},
transdata{"x0061", "val0061", "<", "val0060", "x0062", "0062_then", "x0063", "0063_else", false},
transdata{"x0071", "val0071", ">", "val0070", "x0072", "0072_then", "x0073", "0073_else", true},
transdata{"x0081", "val0081", ">", "val0082", "x0082", "0082_then", "x0083", "0083_else", false},
}
var tass = []tasdata{
tasdata{"x0100", "val0100", "val0100", "val_0100_tasok", true},
tasdata{"x0110", "val0110", "val01x0", "val_0110_tasok", false},
}
func TestTransaction(t *testing.T) {
var kvh Kvi
var err error
var i int
var kkey, kval, rval string
var bval bool
t.Logf("** RUNNING TRANSACTION OPERATIONS TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
for i = 0; i < len(trans); i += 1 {
err = kvh.Store(trans[i].ifkey, trans[i].inival)
if err != nil {
t.Error("ERROR storing key:", err)
break
}
}
for i = 0; i < len(trans); i += 1 {
bval, err = kvh.Transaction(trans[i].ifkey, trans[i].op,
trans[i].ifval, trans[i].thenkey,
trans[i].thenval, trans[i].elsekey,
trans[i].elseval)
if err != nil {
t.Error("ERROR on transaction dataset ", i, ":", err)
}
if trans[i].isthen != bval {
t.Errorf("ERROR, transaction result mismatch on dataset %d\n", i)
}
}
//Double-verify using Get operations
for i = 0; i < len(trans); i += 1 {
if trans[i].isthen {
kkey = trans[i].thenkey
kval = trans[i].thenval
} else {
kkey = trans[i].elsekey
kval = trans[i].elseval
}
rval, _, err = kvh.Get(kkey)
if err != nil {
t.Error("ERROR fetching key:", kkey, ":", err)
}
if rval != kval {
t.Errorf("ERROR, double-check mismatch, dataset %d\n", i)
}
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED TRANSACTION OPERATIONS TEST\n")
}
func TestTAS(t *testing.T) {
var kvh Kvi
var err error
var i int
var bval bool
var kval string
t.Logf("** RUNNING TAS OPERATIONS TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
for i = 0; i < len(tass); i += 1 {
err = kvh.Store(tass[i].key, tass[i].inival)
if err != nil {
t.Error("ERROR storing key:", err)
break
}
}
//Do the TASs
for i = 0; i < len(tass); i += 1 {
bval, err = kvh.TAS(tass[i].key, tass[i].testval, tass[i].setval)
if err != nil {
t.Error("ERROR, TAS operation on dataset ", i, " failed:", err)
}
if tass[i].shouldset != bval {
t.Errorf("ERROR, mismatch on dataset %d, expected %t, got %t\n",
i, tass[i].shouldset, bval)
}
}
//Double check via Get operations
for i = 0; i < len(tass); i += 1 {
kval, _, err = kvh.Get(tass[i].key)
if tass[i].shouldset {
if kval != tass[i].setval {
t.Errorf("ERROR, mismatch during double-check, dataset %d, key '%s' set to '%s, expected '%s'\n",
i, tass[i].key, kval, tass[i].setval)
}
} else {
if kval != tass[i].inival {
t.Errorf("ERROR, mismatch during double-check, dataset %d, key '%s' set to '%s, expected '%s'\n",
i, tass[i].key, kval, tass[i].inival)
}
}
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED TAS OPERATIONS TEST\n")
}
type watchdata struct {
key string
inival string
changeval string
expop int
hit bool
actualop int
}
var watches = []watchdata{
watchdata{"x0200", "val_0200", "change_0200", KVC_KEYCHANGE_PUT, false, 0},
watchdata{"x0201", "", "change_0200", KVC_KEYCHANGE_PUT, false, 0},
watchdata{"x0202", "", "", KVC_KEYCHANGE_DELETE, false, 0},
}
func watchme(t *testing.T, kvh Kvi, wd *watchdata) {
_, op := kvh.Watch(wd.key)
wd.actualop = op
wd.hit = true
}
func watch_cb(key string, val string, op int, udata interface{}) bool {
wd := udata.(*watchdata)
wd.hit = true
wd.actualop = op
return true
}
func watch_cb_self_cancel(key string, val string, op int, udata interface{}) bool {
wd := udata.(*watchdata)
wd.hit = true
wd.actualop = op
return false
}
func TestWatch(t *testing.T) {
var kvh Kvi
var err error
t.Logf("** RUNNING WATCH TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//Set up a key
err = kvh.Store(watches[0].key, watches[0].inival)
if err != nil {
t.Error("ERROR setting up a key:", watches[0].key, ":", err)
}
//Set up a watch for that key
go watchme(t, kvh, &watches[0])
//Sleep a tick, then change the value
time.Sleep(500000 * time.Microsecond)
err = kvh.Store(watches[0].key, watches[0].changeval)
if err != nil {
t.Error("ERROR changing key after watch was set:", err)
}
time.Sleep(1 * time.Second)
//Verify that the watch caught it
if !watches[0].hit {
t.Errorf("ERROR, no watch was tripped for existing key '%s'\n", watches[0].key)
}
if watches[0].expop != watches[0].actualop {
t.Errorf("ERROR, watch trip for key '%s' unexpected operation, wanted %d, got %d\n",
watches[0].key, watches[0].expop, watches[0].actualop)
}
//Set up a watch for a non-existent key
go watchme(t, kvh, &watches[1])
//Sleep a tick, then set a value to that key
time.Sleep(500000 * time.Microsecond)
err = kvh.Store(watches[1].key, watches[1].changeval)
if err != nil {
t.Error("ERROR changing key after watch was set:", err)
}
time.Sleep(1 * time.Second)
//Verify that it got caught.
if !watches[1].hit {
t.Errorf("ERROR, no watch was tripped for formerly non-existent key '%s'\n",
watches[1].key)
}
if watches[1].expop != watches[1].actualop {
t.Errorf("ERROR, watch trip for key '%s' unexpected operation, wanted %d, got %d\n",
watches[1].key, watches[1].expop, watches[1].actualop)
}
//Set up a watch for deletion of a key
err = kvh.Store(watches[2].key, watches[2].inival)
if err != nil {
t.Error("ERROR setting up a key:", watches[0].key, ":", err)
}
time.Sleep(500000 * time.Microsecond)
go watchme(t, kvh, &watches[2])
//Sleep a tick, then delete the key
time.Sleep(500000 * time.Microsecond)
err = kvh.Delete(watches[2].key)
if err != nil {
t.Error("ERROR deleting key:", err)
}
time.Sleep(1 * time.Second)
//Verify that it got caught.
if !watches[2].hit {
t.Errorf("ERROR, no watch was tripped for deleted key '%s'\n",
watches[2].key)
}
if watches[2].expop != watches[2].actualop {
t.Errorf("ERROR, watch trip for key '%s' unexpected operation, wanted %d, got %d\n",
watches[2].key, watches[2].expop, watches[2].actualop)
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED WATCH TEST\n")
}
// Also tests WatchCBCancel
func TestWatchWithCB(t *testing.T) {
var kvh Kvi
var err error
var i int
var cbh WatchCBHandle
t.Logf("** RUNNING WATCH WITH CALLBACK TEST **\n")
kvh, err = Open("mem:", "")
if err != nil {
t.Error("ERROR opening memory-based KV interface:", err)
}
//clear all hit indicators from test data, all existing watch keys
for i = 0; i < len(watches); i += 1 {
watches[i].hit = false
watches[i].actualop = 0
_ = kvh.Delete(watches[i].key)
}
//Set up a key
err = kvh.Store(watches[0].key, watches[0].inival)
if err != nil {
t.Error("ERROR setting up a key:", watches[0].key, ":", err)
}
//Set up a watch for that key, with callback func
cbh, err = kvh.WatchWithCB(watches[0].key, KVC_KEYCHANGE_PUT, watch_cb,
&watches[0])
if err != nil {
t.Error("ERROR setting up watch with callback[0]:", err)
}
//Sleep a tick, then change the value
time.Sleep(500000 * time.Microsecond)
err = kvh.Store(watches[0].key, watches[0].changeval)
if err != nil {
t.Error("ERROR changing key after watch was set:", err)
}
time.Sleep(1 * time.Second)
//Verify that the watch caught it
if !watches[0].hit {
t.Errorf("ERROR, no watch callback was tripped for existing key '%s'\n", watches[0].key)
}
if watches[0].expop != watches[0].actualop {
t.Errorf("ERROR, watch trip for key '%s' unexpected operation, wanted %d, got %d\n",
watches[0].key, watches[0].expop, watches[0].actualop)
}
//Cancel callback
kvh.WatchCBCancel(cbh)
//Set up a watch for a non-existent key, self cancelling CB
cbh, err = kvh.WatchWithCB(watches[1].key, KVC_KEYCHANGE_PUT,
watch_cb_self_cancel, &watches[1])
if err != nil {
t.Error("ERROR setting up watch with callback[1]:", err)
}
//Sleep a tick, then set a value to that key
time.Sleep(500000 * time.Microsecond)
err = kvh.Store(watches[1].key, watches[1].changeval)
if err != nil {
t.Error("ERROR changing key after watch was set:", err)
}
time.Sleep(1 * time.Second)
//Verify that it got caught.
if !watches[1].hit {
t.Errorf("ERROR, no watch callback was tripped for formerly non-existent key '%s'\n",
watches[1].key)
}
if watches[1].expop != watches[1].actualop {
t.Errorf("ERROR, watch trip for key '%s' unexpected operation, wanted %d, got %d\n",
watches[1].key, watches[1].expop, watches[1].actualop)
}
//Set up a watch for deletion of a key
err = kvh.Store(watches[2].key, watches[2].inival)
if err != nil {
t.Error("ERROR setting up a key:", watches[0].key, ":", err)
}
time.Sleep(500000 * time.Microsecond)
cbh, err = kvh.WatchWithCB(watches[2].key, KVC_KEYCHANGE_DELETE,
watch_cb_self_cancel, &watches[2])
if err != nil {
t.Error("ERROR setting up watch with callback[2]:", err)
}
//Sleep a tick, then delete the key
time.Sleep(500000 * time.Microsecond)
err = kvh.Delete(watches[2].key)
if err != nil {
t.Error("ERROR deleting key:", err)
}
time.Sleep(1 * time.Second)
//Verify that it got caught.
if !watches[2].hit {
t.Errorf("ERROR, no watch was tripped for deleted key '%s'\n",
watches[2].key)
}
if watches[2].expop != watches[2].actualop {
t.Errorf("ERROR, watch trip for key '%s' unexpected operation, wanted %d, got %d\n",
watches[2].key, watches[2].expop, watches[2].actualop)
}
err = kvh.Close()
if err != nil {
t.Error("ERROR closing memory-based KV interface:", err)
}
t.Logf(" ==> FINISHED WATCH WITH CALLBACK TEST **\n")
}