-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkvs_test.go
More file actions
2319 lines (1988 loc) · 59.9 KB
/
kvs_test.go
File metadata and controls
2319 lines (1988 loc) · 59.9 KB
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 kvs
import (
"encoding/gob"
"fmt"
"os"
"testing"
"time"
)
func init() {
// Register types for gob encoding (needed for snapshots)
gob.Register(IntValue(0))
gob.Register(Person{})
}
type IntValue int
func (iv IntValue) Clone() Value {
return iv
}
type Person struct {
Name string
Age int
}
func (p Person) Clone() Value {
return Person{
Name: p.Name,
Age: p.Age,
}
}
func TestSet(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
err = store.Set("person", value)
if err != nil {
t.Errorf("Set returned an error: %v", err)
}
val, err := store.Get("person")
if err != nil {
t.Errorf("Get returned an error: %v", err)
}
if val == nil {
t.Error("Get returned nil value")
}
}
func TestGet(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
if err := store.Set("person", value); err != nil {
t.Errorf("Set returned an error: %v", err)
}
val, err := store.Get("person")
if err != nil {
t.Errorf("Get returned an error: %v", err)
}
if val == nil {
t.Error("Get returned nil value")
}
}
func TestDelete(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
if err := store.Set("person", value); err != nil {
t.Errorf("Set returned an error: %v", err)
}
err = store.Delete("person")
if err != nil {
t.Errorf("Delete returned an error: %v", err)
}
val, err := store.Get("person")
if err == nil {
t.Errorf("Get did not return an error for deleted key")
}
if val != nil {
t.Error("Get returned non-nil value for deleted key")
}
}
func TestKeys(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
if err := store.Set("person", value); err != nil {
t.Errorf("Set returned an error: %v", err)
}
keys, err := store.Keys()
if err != nil {
t.Errorf("Keys returned an error: %v", err)
}
if len(keys) != 1 || keys[0] != "person" {
t.Errorf("Keys returned unexpected result: %v", keys)
}
}
func TestKeyValueStore(t *testing.T) {
t.Run("Set", TestSet)
t.Run("Get", TestGet)
t.Run("Delete", TestDelete)
t.Run("Keys", TestKeys)
}
func TestKeyValueStore_Concurrent(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Errorf("NewKeyValueStore returned an error: %v", err)
}
// Set up a channel to communicate between goroutines
done := make(chan bool)
// Use multiple goroutines to write to the key-value store
for i := 0; i < 10; i++ {
go func(j int) {
for k := 0; k < 1000; k++ {
key := fmt.Sprintf("key-%d-%d", j, k)
err := store.Set(key, IntValue(j))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
done <- true
}(i)
}
// Wait for all goroutines to finish writing to the key-value store
for i := 0; i < 10; i++ {
<-done
}
// Use multiple goroutines to read from the key-value store
for i := 0; i < 10; i++ {
go func(j int) {
keys, err := store.Keys()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
for k := 0; k < 1000 && k < len(keys); k++ {
key := fmt.Sprintf("key-%d-%d", j, k)
val, err := store.Get(key)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if iv, ok := val.(IntValue); !ok || iv != IntValue(j) {
t.Errorf("Expected IntValue(%d), got %v", j, val)
}
}
done <- true
}(i)
}
}
func TestKeyValueStore_Struct(t *testing.T) {
store, err := NewKeyValueStore(5)
if err != nil {
t.Errorf("NewKeyValueStore returned an error: %v", err)
}
// Add some people to the store
if err := store.Set("john", Person{Name: "John Doe", Age: 42}); err != nil {
t.Errorf("Expected no error, got %v", err)
}
if err := store.Set("jane", Person{Name: "Jane Doe", Age: 36}); err != nil {
t.Errorf("Expected no error, got %v", err)
}
if err := store.Set("bob", Person{Name: "Bob Smith", Age: 27}); err != nil {
t.Errorf("Expected no error, got %v", err)
}
// Retrieve a person from the store
if val, err := store.Get("john"); err != nil {
t.Errorf("Expected no error, got %v", err)
} else if p, ok := val.(Person); !ok {
t.Errorf("Expected a Person value, got %v", val)
} else if p.Name != "John Doe" || p.Age != 42 {
t.Errorf("Expected Person{Name: 'John Doe', Age: 42}, got %v", p)
}
// Update a person in the store
if err := store.Set("john", Person{Name: "John Smith", Age: 43}); err != nil {
t.Errorf("Expected no error, got %v", err)
}
// Delete a person from the store
if err := store.Delete("bob"); err != nil {
t.Errorf("Expected no error, got %v", err)
}
// Check that the correct people are in the store
expected := []Person{
{Name: "John Smith", Age: 43},
{Name: "Jane Doe", Age: 36},
}
var actual []Person
keys, err := store.Keys()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
for _, key := range keys {
if val, err := store.Get(key); err != nil {
t.Errorf("Expected no error, got %v", err)
} else if p, ok := val.(Person); !ok {
t.Errorf("Expected a Person value, got %v", val)
} else {
actual = append(actual, p)
}
}
if len(actual) != len(expected) {
t.Errorf("Expected %d people, got %d", len(expected), len(actual))
}
for _, p := range expected {
if !contains(actual, p) {
t.Errorf("Expected %v, got %v", p, actual)
}
}
}
func contains(persons []Person, p Person) bool {
for _, person := range persons {
if person == p {
return true
}
}
return false
}
func BenchmarkSet(b *testing.B) {
store, err := NewKeyValueStore(10)
if err != nil {
b.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := store.Set("person", value); err != nil {
b.Errorf("Expected no error, got %v", err)
}
}
}
func BenchmarkGet(b *testing.B) {
store, err := NewKeyValueStore(10)
if err != nil {
b.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
if err := store.Set("person", value); err != nil {
b.Errorf("Expected no error, got %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := store.Get("person"); err != nil {
b.Errorf("Expected no error, got %v", err)
}
}
}
func BenchmarkDelete(b *testing.B) {
store, err := NewKeyValueStore(10)
if err != nil {
b.Errorf("NewKeyValueStore returned an error: %v", err)
}
value := &Person{
Name: "Alice",
Age: 30,
}
if err := store.Set("person", value); err != nil {
b.Errorf("Expected no error, got %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := store.Get("person")
if err == nil {
if err := store.Delete("person"); err != nil {
b.Errorf("Expected no error, got %v", err)
}
}
}
}
// TestExists_ExistingKey tests that Exists returns true for keys that are present in the store.
// Covers: T010 - Test Exists() returns true for existing keys
func TestExists_ExistingKey(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Set a key-value pair
err = store.Set("testkey", IntValue(42))
if err != nil {
t.Fatalf("Set returned an error: %v", err)
}
// Check that Exists returns true
exists := store.Exists("testkey")
if !exists {
t.Errorf("Exists returned false for existing key 'testkey'")
}
}
// TestExists_NonExistentKey tests that Exists returns false for keys that are not in the store.
// Covers: T011 - Test Exists() returns false for non-existent keys
func TestExists_NonExistentKey(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Check that Exists returns false for a key that was never set
exists := store.Exists("nonexistent")
if exists {
t.Errorf("Exists returned true for non-existent key 'nonexistent'")
}
// Set and then delete a key
if err := store.Set("deleted", IntValue(1)); err != nil {
t.Fatalf("Set returned an error: %v", err)
}
if err := store.Delete("deleted"); err != nil {
t.Fatalf("Delete returned an error: %v", err)
}
// Check that Exists returns false for the deleted key
exists = store.Exists("deleted")
if exists {
t.Errorf("Exists returned true for deleted key 'deleted'")
}
}
// TestExists_Concurrent tests that Exists works correctly with concurrent Set/Delete operations.
// Covers: T012 - Test Exists() with concurrent operations
func TestExists_Concurrent(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
done := make(chan bool)
// Goroutines writing keys
for i := 0; i < 5; i++ {
go func(j int) {
for k := 0; k < 100; k++ {
key := fmt.Sprintf("key-%d-%d", j, k)
if err := store.Set(key, IntValue(j)); err != nil {
t.Errorf("Set failed: %v", err)
}
}
done <- true
}(i)
}
// Goroutines checking existence
for i := 0; i < 5; i++ {
go func(j int) {
for k := 0; k < 100; k++ {
key := fmt.Sprintf("key-%d-%d", j, k)
// Just check that Exists doesn't panic or race
_ = store.Exists(key)
}
done <- true
}(i)
}
// Wait for all goroutines
for i := 0; i < 10; i++ {
<-done
}
// Verify that all keys exist
for i := 0; i < 5; i++ {
for k := 0; k < 100; k++ {
key := fmt.Sprintf("key-%d-%d", i, k)
if !store.Exists(key) {
t.Errorf("Exists returned false for key %s that should exist", key)
}
}
}
}
// TestLen_EmptyStore tests that Len returns 0 for an empty store.
// Covers: T014 - Test Len() returns 0 for empty store
func TestLen_EmptyStore(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
length := store.Len()
if length != 0 {
t.Errorf("Len returned %d for empty store, expected 0", length)
}
}
// TestLen_SetDelete tests that Len is correctly maintained during Set and Delete operations.
// Covers: T015 - Test Len() correctly tracks Set/Delete operations
func TestLen_SetDelete(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Start with 0
if length := store.Len(); length != 0 {
t.Errorf("Initial Len = %d, expected 0", length)
}
// Add 3 keys
if err := store.Set("key1", IntValue(1)); err != nil {
t.Fatalf("Set key1 failed: %v", err)
}
if err := store.Set("key2", IntValue(2)); err != nil {
t.Fatalf("Set key2 failed: %v", err)
}
if err := store.Set("key3", IntValue(3)); err != nil {
t.Fatalf("Set key3 failed: %v", err)
}
if length := store.Len(); length != 3 {
t.Errorf("After 3 Sets, Len = %d, expected 3", length)
}
// Update existing key (should not change count)
if err := store.Set("key1", IntValue(10)); err != nil {
t.Fatalf("Set key1 update failed: %v", err)
}
if length := store.Len(); length != 3 {
t.Errorf("After updating key1, Len = %d, expected 3", length)
}
// Delete one key
if err := store.Delete("key2"); err != nil {
t.Fatalf("Delete key2 failed: %v", err)
}
if length := store.Len(); length != 2 {
t.Errorf("After deleting key2, Len = %d, expected 2", length)
}
// Delete another key
if err := store.Delete("key1"); err != nil {
t.Fatalf("Delete key1 failed: %v", err)
}
if length := store.Len(); length != 1 {
t.Errorf("After deleting key1, Len = %d, expected 1", length)
}
// Delete last key
if err := store.Delete("key3"); err != nil {
t.Fatalf("Delete key3 failed: %v", err)
}
if length := store.Len(); length != 0 {
t.Errorf("After deleting all keys, Len = %d, expected 0", length)
}
}
// TestLen_Concurrent tests that Len correctly tracks concurrent operations.
// Covers: T016 - Test Len() with concurrent Set/Delete operations
func TestLen_Concurrent(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
done := make(chan bool)
numGoroutines := 10
keysPerGoroutine := 100
// Concurrent writes
for i := 0; i < numGoroutines; i++ {
go func(j int) {
for k := 0; k < keysPerGoroutine; k++ {
key := fmt.Sprintf("key-%d-%d", j, k)
if err := store.Set(key, IntValue(j)); err != nil {
t.Errorf("Set failed: %v", err)
}
}
done <- true
}(i)
}
// Wait for all writes
for i := 0; i < numGoroutines; i++ {
<-done
}
// Check that Len matches expected count
expectedLen := numGoroutines * keysPerGoroutine
if length := store.Len(); length != expectedLen {
t.Errorf("After concurrent writes, Len = %d, expected %d", length, expectedLen)
}
// Concurrent deletes (delete half the keys)
for i := 0; i < numGoroutines/2; i++ {
go func(j int) {
for k := 0; k < keysPerGoroutine; k++ {
key := fmt.Sprintf("key-%d-%d", j, k)
if err := store.Delete(key); err != nil {
t.Errorf("Delete failed: %v", err)
}
}
done <- true
}(i)
}
// Wait for deletes
for i := 0; i < numGoroutines/2; i++ {
<-done
}
// Check that Len is updated correctly
expectedLen = (numGoroutines / 2) * keysPerGoroutine
if length := store.Len(); length != expectedLen {
t.Errorf("After concurrent deletes, Len = %d, expected %d", length, expectedLen)
}
}
// TestBatchGet_AllExisting tests BatchGet with all keys existing in the store.
// Covers: T021 - Test BatchGet with all existing keys
func TestBatchGet_AllExisting(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Set up test data
if err := store.Set("key1", IntValue(1)); err != nil {
t.Fatalf("Set key1 failed: %v", err)
}
if err := store.Set("key2", IntValue(2)); err != nil {
t.Fatalf("Set key2 failed: %v", err)
}
if err := store.Set("key3", IntValue(3)); err != nil {
t.Fatalf("Set key3 failed: %v", err)
}
// Batch get all keys
result, err := store.BatchGet([]string{"key1", "key2", "key3"})
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
// Check that all keys were retrieved successfully
if len(result.Values) != 3 {
t.Errorf("Expected 3 values, got %d", len(result.Values))
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
// Verify values
if v, ok := result.Values["key1"].(IntValue); !ok || v != 1 {
t.Errorf("Expected key1=1, got %v", result.Values["key1"])
}
if v, ok := result.Values["key2"].(IntValue); !ok || v != 2 {
t.Errorf("Expected key2=2, got %v", result.Values["key2"])
}
if v, ok := result.Values["key3"].(IntValue); !ok || v != 3 {
t.Errorf("Expected key3=3, got %v", result.Values["key3"])
}
}
// TestBatchGet_MixedKeys tests BatchGet with both existing and non-existent keys.
// Covers: T022 - Test BatchGet with mixed existing/non-existent keys
func TestBatchGet_MixedKeys(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Set up test data (only key1 and key3 exist)
if err := store.Set("key1", IntValue(1)); err != nil {
t.Fatalf("Set key1 failed: %v", err)
}
if err := store.Set("key3", IntValue(3)); err != nil {
t.Fatalf("Set key3 failed: %v", err)
}
// Batch get with mixed keys
result, err := store.BatchGet([]string{"key1", "key2", "key3", "key4"})
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
// Check partial success
if len(result.Values) != 2 {
t.Errorf("Expected 2 successful values, got %d", len(result.Values))
}
if len(result.Errors) != 2 {
t.Errorf("Expected 2 errors, got %d", len(result.Errors))
}
// Verify successful retrievals
if _, ok := result.Values["key1"]; !ok {
t.Errorf("Expected key1 in Values")
}
if _, ok := result.Values["key3"]; !ok {
t.Errorf("Expected key3 in Values")
}
// Verify errors for non-existent keys
if _, ok := result.Errors["key2"]; !ok {
t.Errorf("Expected key2 in Errors")
}
if _, ok := result.Errors["key4"]; !ok {
t.Errorf("Expected key4 in Errors")
}
}
// TestBatchGet_AllNonExistent tests BatchGet with all keys missing from the store.
// Covers: T023 - Test BatchGet with all non-existent keys
func TestBatchGet_AllNonExistent(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Batch get with all non-existent keys
result, err := store.BatchGet([]string{"missing1", "missing2", "missing3"})
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
// Check that all keys returned errors
if len(result.Values) != 0 {
t.Errorf("Expected 0 values, got %d", len(result.Values))
}
if len(result.Errors) != 3 {
t.Errorf("Expected 3 errors, got %d", len(result.Errors))
}
// Verify all errors are ErrNotFound
for key, err := range result.Errors {
if err != ErrNotFound {
t.Errorf("Expected ErrNotFound for key %s, got %v", key, err)
}
}
}
// TestBatchGet_EmptyInput tests BatchGet with an empty key list.
// Covers: T024 - Test BatchGet with empty input
func TestBatchGet_EmptyInput(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Batch get with empty list
result, err := store.BatchGet([]string{})
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
// Check that result is empty
if len(result.Values) != 0 {
t.Errorf("Expected 0 values, got %d", len(result.Values))
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
}
// TestBatchGet_DuplicateKeys tests BatchGet with duplicate keys in the input.
// Covers: T025 - Test BatchGet with duplicate keys
func TestBatchGet_DuplicateKeys(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
if err := store.Set("key1", IntValue(42)); err != nil {
t.Fatalf("Set key1 failed: %v", err)
}
// Batch get with duplicate keys
result, err := store.BatchGet([]string{"key1", "key1", "key1"})
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
// Should handle duplicates gracefully (only one entry in result)
if len(result.Values) != 1 {
t.Errorf("Expected 1 value for duplicate keys, got %d", len(result.Values))
}
if v, ok := result.Values["key1"].(IntValue); !ok || v != 42 {
t.Errorf("Expected key1=42, got %v", result.Values["key1"])
}
}
// TestBatchGet_SingleKey tests BatchGet with a single key.
// Covers: T026 - Test BatchGet with single key
func TestBatchGet_SingleKey(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
if err := store.Set("single", IntValue(100)); err != nil {
t.Fatalf("Set single failed: %v", err)
}
// Batch get with single key
result, err := store.BatchGet([]string{"single"})
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
if len(result.Values) != 1 {
t.Errorf("Expected 1 value, got %d", len(result.Values))
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
}
// TestBatchGet_MultipleShards tests BatchGet with keys distributed across shards.
// Covers: T027 - Test BatchGet with keys across multiple shards
func TestBatchGet_MultipleShards(t *testing.T) {
store, err := NewKeyValueStore(4)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Add many keys to ensure they span multiple shards
keys := []string{}
for i := 0; i < 20; i++ {
key := fmt.Sprintf("key%d", i)
keys = append(keys, key)
if err := store.Set(key, IntValue(i)); err != nil {
t.Fatalf("Set %s failed: %v", key, err)
}
}
// Batch get all keys
result, err := store.BatchGet(keys)
if err != nil {
t.Fatalf("BatchGet returned an error: %v", err)
}
// All keys should be retrieved successfully
if len(result.Values) != 20 {
t.Errorf("Expected 20 values, got %d", len(result.Values))
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
}
// TestBatchGet_Concurrent tests BatchGet with concurrent operations.
// Covers: T028 - Test BatchGet with concurrent reads/writes
func TestBatchGet_Concurrent(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Pre-populate store
for i := 0; i < 100; i++ {
if err := store.Set(fmt.Sprintf("key%d", i), IntValue(i)); err != nil {
t.Fatalf("Set key%d failed: %v", i, err)
}
}
done := make(chan bool)
// Concurrent BatchGet operations
for i := 0; i < 10; i++ {
go func() {
keys := []string{"key10", "key20", "key30", "key40", "key50"}
result, err := store.BatchGet(keys)
if err != nil {
t.Errorf("BatchGet failed: %v", err)
}
if len(result.Values) != 5 {
t.Errorf("Expected 5 values, got %d", len(result.Values))
}
done <- true
}()
}
// Concurrent Set operations
for i := 0; i < 5; i++ {
go func(j int) {
for k := 0; k < 20; k++ {
key := fmt.Sprintf("newkey%d-%d", j, k)
if err := store.Set(key, IntValue(j*100+k)); err != nil {
t.Errorf("Set %s failed: %v", key, err)
}
}
done <- true
}(i)
}
// Wait for all goroutines
for i := 0; i < 15; i++ {
<-done
}
}
// TestBatchSet_NewKeys tests BatchSet with multiple new keys.
// Covers: T038 - Test BatchSet with multiple new keys
func TestBatchSet_NewKeys(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
items := map[string]Value{
"key1": IntValue(1),
"key2": IntValue(2),
"key3": IntValue(3),
}
result, err := store.BatchSet(items)
if err != nil {
t.Fatalf("BatchSet returned an error: %v", err)
}
// All should succeed
if result.Successful != 3 {
t.Errorf("Expected 3 successful sets, got %d", result.Successful)
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
// Verify all keys were set
for key, expectedVal := range items {
val, err := store.Get(key)
if err != nil {
t.Errorf("Get(%s) failed: %v", key, err)
}
if val != expectedVal {
t.Errorf("Expected %v for %s, got %v", expectedVal, key, val)
}
}
}
// TestBatchSet_MixedKeys tests BatchSet with both new and existing keys.
// Covers: T039 - Test BatchSet with mixed new/existing keys
func TestBatchSet_MixedKeys(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Pre-set some keys
if err := store.Set("key1", IntValue(100)); err != nil {
t.Fatalf("Set key1 failed: %v", err)
}
if err := store.Set("key2", IntValue(200)); err != nil {
t.Fatalf("Set key2 failed: %v", err)
}
// Batch set with mixed keys (update key1, key2; add key3, key4)
items := map[string]Value{
"key1": IntValue(1), // update
"key2": IntValue(2), // update
"key3": IntValue(3), // new
"key4": IntValue(4), // new
}
result, err := store.BatchSet(items)
if err != nil {
t.Fatalf("BatchSet returned an error: %v", err)
}
// All should succeed
if result.Successful != 4 {
t.Errorf("Expected 4 successful sets, got %d", result.Successful)
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
// Verify values were updated/added
if val, _ := store.Get("key1"); val.(IntValue) != 1 {
t.Errorf("key1 not updated, got %v", val)
}
if val, _ := store.Get("key2"); val.(IntValue) != 2 {
t.Errorf("key2 not updated, got %v", val)
}
}
// TestBatchSet_EmptyInput tests BatchSet with an empty map.
// Covers: T040 - Test BatchSet with empty input
func TestBatchSet_EmptyInput(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
result, err := store.BatchSet(map[string]Value{})
if err != nil {
t.Fatalf("BatchSet returned an error: %v", err)
}
if result.Successful != 0 {
t.Errorf("Expected 0 successful sets, got %d", result.Successful)
}
if len(result.Errors) != 0 {
t.Errorf("Expected 0 errors, got %d", len(result.Errors))
}
}
// TestBatchSet_UpdatesLen tests that BatchSet correctly updates Len().
// Covers: T042 - Test BatchSet correctly updates Len()
func TestBatchSet_UpdatesLen(t *testing.T) {
store, err := NewKeyValueStore(10)
if err != nil {
t.Fatalf("NewKeyValueStore returned an error: %v", err)
}
// Initial length is 0
if store.Len() != 0 {
t.Errorf("Initial Len = %d, expected 0", store.Len())
}
// Batch set 5 keys
items := map[string]Value{
"k1": IntValue(1),
"k2": IntValue(2),
"k3": IntValue(3),
"k4": IntValue(4),
"k5": IntValue(5),
}
if _, err := store.BatchSet(items); err != nil {
t.Fatalf("BatchSet failed: %v", err)
}
if store.Len() != 5 {
t.Errorf("After BatchSet, Len = %d, expected 5", store.Len())
}
// Update 2 existing keys (should not change Len)
updates := map[string]Value{