-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paths3fifo_test.go
More file actions
2965 lines (2465 loc) · 78.1 KB
/
s3fifo_test.go
File metadata and controls
2965 lines (2465 loc) · 78.1 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 fido
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestS3FIFO_BasicOperations(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 100})
// Test set and get
cache.set("key1", 42, 0)
if val, ok := cache.get("key1"); !ok || val != 42 {
t.Errorf("get(key1) = %v, %v; want 42, true", val, ok)
}
// Test missing key
if val, ok := cache.get("missing"); ok {
t.Errorf("get(missing) = %v, %v; want _, false", val, ok)
}
// Test update
cache.set("key1", 100, 0)
if val, ok := cache.get("key1"); !ok || val != 100 {
t.Errorf("get(key1) after update = %v, %v; want 100, true", val, ok)
}
// Test delete
cache.del("key1")
if val, ok := cache.get("key1"); ok {
t.Errorf("get(key1) after delete = %v, %v; want _, false", val, ok)
}
}
func TestS3FIFO_Capacity(t *testing.T) {
cache := newS3FIFO[int, string](&config{size: 20000})
// Fill cache well beyond capacity
for i := range 30000 {
cache.set(i, "value", 0)
}
// Cache should be at or near requested capacity
// Allow up to 10% variance due to shard rounding
if cache.len() < 18000 || cache.len() > 22000 {
t.Errorf("cache length = %d; want ~20000 (±10%%)", cache.len())
}
}
// TestS3FIFO_CapacityAccuracy verifies that cache capacity is accurate across sizes.
// This is a regression test for the bug where small caches were inflated to numShards.
func TestS3FIFO_CapacityAccuracy(t *testing.T) {
testCases := []struct {
requested int
maxActual int // Allow some overhead for shard rounding
}{
{100, 128}, // Small cache
{500, 512}, // Very small cache (was inflated to 2048 before fix)
{1000, 1024}, // Medium-small cache
{10000, 10240}, // Medium cache
{100000, 102400}, // Large cache
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("capacity_%d", tc.requested), func(t *testing.T) {
cache := newS3FIFO[int, int](&config{size: tc.requested})
// Insert many more items than capacity
for i := range tc.requested * 3 {
cache.set(i, i, 0)
}
actual := cache.len()
if actual > tc.maxActual {
t.Errorf("requested %d, got %d items (max expected %d)",
tc.requested, actual, tc.maxActual)
}
// Should be at least 80% of requested to ensure we're not under-sizing
minExpected := tc.requested * 80 / 100
if actual < minExpected {
t.Errorf("requested %d, got only %d items (min expected %d)",
tc.requested, actual, minExpected)
}
})
}
}
func TestS3FIFO_Eviction(t *testing.T) {
// Use 65536 to get proper S3-FIFO behavior (4096 entries per shard)
cache := newS3FIFO[int, int](&config{size: 65536})
// Fill cache to capacity
for i := range 65536 {
cache.set(i, i, 0)
}
// Access items 0-99 multiple times to mark them as hot
for range 3 {
for i := range 100 {
cache.get(i)
}
}
// Add more items to trigger evictions - hot items should survive
for i := 65536; i < 130000; i++ {
cache.set(i, i, 0)
}
// Count how many hot items survived
hotSurvived := 0
for i := range 100 {
if _, ok := cache.get(i); ok {
hotSurvived++
}
}
// Most hot items should survive (at least 75%)
if hotSurvived < 75 {
t.Errorf("only %d/100 hot items survived; want >= 75", hotSurvived)
}
// Should be near capacity (allow 10% variance)
if cache.len() < 58000 || cache.len() > 72000 {
t.Errorf("cache length = %d; want ~65536", cache.len())
}
}
func TestS3FIFO_GhostQueue(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 12})
// Fill one shard's worth
cache.set("a", 1, 0)
cache.set("b", 2, 0)
cache.set("c", 3, 0)
// Evict "a" by adding "d" (assuming same shard)
cache.set("d", 4, 0)
// Re-add "a" - should go directly to main queue if it was in ghost
cache.set("a", 10, 0)
// Verify "a" is retrievable with updated value
if val, ok := cache.get("a"); !ok || val != 10 {
t.Errorf("get(a) = %v, %v; want 10, true", val, ok)
}
}
func TestS3FIFO_TTL(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 10})
// Set item with past expiry
past := uint32(time.Now().Add(-1 * time.Second).Unix())
cache.set("expired", 42, past)
// Should not be retrievable
if val, ok := cache.get("expired"); ok {
t.Errorf("get(expired) = %v, %v; want _, false", val, ok)
}
// Set item with future expiry
future := uint32(time.Now().Add(1 * time.Hour).Unix())
cache.set("valid", 100, future)
// Should be retrievable
if val, ok := cache.get("valid"); !ok || val != 100 {
t.Errorf("get(valid) = %v, %v; want 100, true", val, ok)
}
}
func TestS3FIFO_Concurrent(t *testing.T) {
cache := newS3FIFO[int, int](&config{size: 1000})
var wg sync.WaitGroup
// Concurrent writers
for i := range 10 {
wg.Add(1)
go func(offset int) {
defer wg.Done()
for j := range 100 {
cache.set(offset*100+j, j, 0)
}
}(i)
}
// Concurrent readers
for range 10 {
wg.Go(func() {
for j := range 100 {
cache.get(j)
}
})
}
wg.Wait()
// Cache should be at or below requested capacity (with some shard rounding tolerance)
if cache.len() > 1100 {
t.Errorf("cache length = %d; want <= ~1000", cache.len())
}
}
func TestS3FIFO_FrequencyPromotion(t *testing.T) {
// Use 65536 to get proper S3-FIFO behavior (4096 entries per shard)
cache := newS3FIFO[int, int](&config{size: 65536})
// Fill cache with items using int keys for predictable sharding
for i := range 65536 {
cache.set(i, i, 0)
}
// Access first 1000 keys multiple times to mark them as hot
for range 3 {
for i := range 1000 {
cache.get(i)
}
}
// Add more items to trigger significant evictions (2x capacity)
for i := 65536; i < 130000; i++ {
cache.set(i, i, 0)
}
// Count how many hot items survived vs cold items
hotSurvived := 0
coldSurvived := 0
for i := range 65536 {
if _, ok := cache.get(i); ok {
if i < 1000 {
hotSurvived++
} else {
coldSurvived++
}
}
}
// Calculate survival rates
hotRate := float64(hotSurvived) / 1000.0
coldRate := float64(coldSurvived) / 64536.0
t.Logf("Hot survived: %d/1000 (%.1f%%), Cold survived: %d/64536 (%.1f%%)",
hotSurvived, hotRate*100, coldSurvived, coldRate*100)
// Hot items should survive at higher rate than cold items
// This verifies the frequency promotion mechanism works
if hotRate <= coldRate {
t.Errorf("hot item survival rate (%.1f%%) should exceed cold item rate (%.1f%%)",
hotRate*100, coldRate*100)
}
}
func TestS3FIFO_SmallCapacity(t *testing.T) {
// Test with capacity of 12 (3 per shard)
cache := newS3FIFO[string, int](&config{size: 12})
// Fill to capacity
cache.set("a", 1, 0)
cache.set("b", 2, 0)
cache.set("c", 3, 0)
initialLen := cache.len()
// Adding fourth item should trigger eviction in its shard
cache.set("d", 4, 0)
// Should still be at or near capacity
if cache.len() > 12 {
t.Errorf("cache length after eviction = %d; want <= 12", cache.len())
}
// Newest item should exist
if val, ok := cache.get("d"); !ok || val != 4 {
t.Errorf("get(d) = %v, %v; want 4, true", val, ok)
}
t.Logf("Initial len: %d, Final len: %d", initialLen, cache.len())
}
func BenchmarkS3FIFO_Set(b *testing.B) {
cache := newS3FIFO[int, int](&config{size: 10000})
b.ResetTimer()
for i := range b.N {
cache.set(i%10000, i, 0)
}
}
func BenchmarkS3FIFO_Get(b *testing.B) {
cache := newS3FIFO[int, int](&config{size: 10000})
for i := range 10000 {
cache.set(i, i, 0)
}
b.ResetTimer()
for i := range b.N {
cache.get(i % 10000)
}
}
func BenchmarkS3FIFO_GetParallel(b *testing.B) {
cache := newS3FIFO[int, int](&config{size: 10000})
for i := range 10000 {
cache.set(i, i, 0)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
cache.get(i % 10000)
i++
}
})
}
func BenchmarkS3FIFO_Mixed(b *testing.B) {
cache := newS3FIFO[int, int](&config{size: 10000})
b.ResetTimer()
for i := range b.N {
if i%2 == 0 {
cache.set(i%10000, i, 0)
} else {
cache.get(i % 10000)
}
}
}
// BenchmarkS3FIFO_SetEvict benchmarks Set with eviction (unique keys after warmup).
func BenchmarkS3FIFO_SetEvict(b *testing.B) {
cache := newS3FIFO[int, int](&config{size: 10000})
// Warmup: fill cache to capacity
for i := range 10000 {
cache.set(i, i, 0)
}
b.ResetTimer()
// Each set uses a unique key, forcing eviction
for i := range b.N {
cache.set(10000+i, i, 0)
}
}
// BenchmarkS3FIFO_SetEvictString benchmarks Set with eviction using string keys.
func BenchmarkS3FIFO_SetEvictString(b *testing.B) {
cache := newS3FIFO[string, int](&config{size: 10000})
// Pre-generate keys to avoid allocation in benchmark loop
warmupKeys := make([]string, 10000)
for i := range 10000 {
warmupKeys[i] = fmt.Sprintf("warmup-key-%d", i)
}
benchKeys := make([]string, b.N)
for i := range b.N {
benchKeys[i] = fmt.Sprintf("bench-key-%d", i)
}
// Warmup: fill cache to capacity
for i := range 10000 {
cache.set(warmupKeys[i], i, 0)
}
b.ResetTimer()
// Each set uses a unique key, forcing eviction
for i := range b.N {
cache.set(benchKeys[i], i, 0)
}
}
// Test S3-FIFO behavior: hot items survive one-hit wonder floods
func TestS3FIFOBehavior(t *testing.T) {
// Use larger capacity for meaningful per-shard sizes with 2048 shards
cache := New[int, int](Size(10000))
// Insert hot items that will be accessed multiple times
for i := range 5000 {
cache.Set(i, i)
}
// Access hot items once (marks them for promotion)
for i := range 5000 {
cache.Get(i)
}
// Insert one-hit wonders (should be evicted before hot items)
for i := 20000; i < 26000; i++ {
cache.Set(i, i)
}
// Check if hot items survived
hotItemsFound := 0
for i := range 5000 {
if _, found := cache.Get(i); found {
hotItemsFound++
}
}
// Hot items should mostly survive - S3-FIFO protects frequently accessed items
if hotItemsFound < 4000 {
t.Errorf("Expected most hot items to survive, got %d/5000", hotItemsFound)
}
}
// Test eviction order: accessed items survive new insertions
func TestS3FIFOEvictionOrder(t *testing.T) {
cache := New[int, int](Size(40))
// Fill cache with items
for i := range 40 {
cache.Set(i, i)
}
// Access first 20 items (marks them for promotion)
for i := range 20 {
cache.Get(i)
}
// Insert new items (should evict unaccessed items first)
for i := 100; i < 120; i++ {
cache.Set(i, i)
}
// Verify accessed items survived
accessedFound := 0
for i := range 20 {
if _, found := cache.Get(i); found {
accessedFound++
}
}
t.Logf("Accessed items found: %d/20", accessedFound)
}
// Test S3-FIFO vs LRU: hot items survive, cold items evicted
func TestS3FIFODetailed(t *testing.T) {
// Use 65536 capacity for proper S3-FIFO behavior with tiered sharding
const cacheSize = 65536
cache := New[int, int](Size(cacheSize))
// Insert items 1-cacheSize into cache
for i := 1; i <= cacheSize; i++ {
cache.Set(i, i*100)
}
// Access items 1-1000 multiple times (marks them as hot)
const hotItems = 1000
for range 3 {
for i := 1; i <= hotItems; i++ {
cache.Get(i)
}
}
// Insert one-hit wonders to trigger eviction
for i := cacheSize + 1; i <= cacheSize+20000; i++ {
cache.Set(i, i*100)
}
// Check which hot items survived
hotSurvived := 0
for i := 1; i <= hotItems; i++ {
if _, found := cache.Get(i); found {
hotSurvived++
}
}
// Check which cold items survived (items that were never accessed)
coldSurvived := 0
for i := hotItems + 1; i <= hotItems+1000; i++ {
if _, found := cache.Get(i); found {
coldSurvived++
}
}
t.Logf("Hot items found: %d/%d, Cold items found: %d/1000", hotSurvived, hotItems, coldSurvived)
// Verify expected behavior - hot items should mostly survive
// With proper S3-FIFO, at least 75% of hot items should survive
if hotSurvived < hotItems*3/4 {
t.Errorf("Expected most hot items to survive, got %d/%d", hotSurvived, hotItems)
}
}
func TestS3FIFO_Flush(t *testing.T) {
// Use int keys for predictable sharding, large capacity to avoid evictions
cache := newS3FIFO[int, int](&config{size: 10000})
// Add some items (fewer than capacity to avoid eviction)
for i := range 100 {
cache.set(i, i, 0)
}
// Access some to promote to main queue
for i := range 20 {
cache.get(i)
}
if cache.len() != 100 {
t.Errorf("cache length = %d; want 100", cache.len())
}
// Flush
removed := cache.flush()
if removed != 100 {
t.Errorf("flushMemory removed %d items; want 100", removed)
}
// Cache should be empty
if cache.len() != 0 {
t.Errorf("cache length after flush = %d; want 0", cache.len())
}
// All keys should be gone
for i := range 100 {
if _, ok := cache.get(i); ok {
t.Errorf("key%d should not be found after flush", i)
}
}
// Can add new items after flush
cache.set(999, 999, 0)
if val, ok := cache.get(999); !ok || val != 999 {
t.Errorf("get(999) = %v, %v; want 999, true", val, ok)
}
}
func TestS3FIFO_FlushEmpty(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 100})
// Flush empty cache
removed := cache.flush()
if removed != 0 {
t.Errorf("flushMemory removed %d items; want 0", removed)
}
}
// stringerKey implements fmt.Stringer for testing the Stringer fast path.
type stringerKey struct {
id int
}
func (k stringerKey) String() string {
return fmt.Sprintf("stringer-%d", k.id)
}
// plainKey is a struct without String() method for testing the fallback path.
type plainKey struct {
a int
b string
}
//nolint:gocognit // Test function intentionally exercises many code paths via subtests
func TestS3FIFO_VariousKeyTypes(t *testing.T) {
// Test that various key types work correctly with the sharding logic.
// This exercises different code paths in shard/shardIndexSlow.
t.Run("int", func(t *testing.T) {
cache := newS3FIFO[int, string](&config{size: 100})
cache.set(42, "forty-two", 0)
cache.set(-1, "negative", 0)
cache.set(0, "zero", 0)
if v, ok := cache.get(42); !ok || v != "forty-two" {
t.Errorf("int key 42: got %v, %v", v, ok)
}
if v, ok := cache.get(-1); !ok || v != "negative" {
t.Errorf("int key -1: got %v, %v", v, ok)
}
if v, ok := cache.get(0); !ok || v != "zero" {
t.Errorf("int key 0: got %v, %v", v, ok)
}
})
t.Run("int64", func(t *testing.T) {
cache := newS3FIFO[int64, string](&config{size: 100})
cache.set(int64(1<<62), "large", 0)
cache.set(int64(-1), "negative", 0)
if v, ok := cache.get(int64(1 << 62)); !ok || v != "large" {
t.Errorf("int64 large key: got %v, %v", v, ok)
}
if v, ok := cache.get(int64(-1)); !ok || v != "negative" {
t.Errorf("int64 -1 key: got %v, %v", v, ok)
}
})
t.Run("uint", func(t *testing.T) {
cache := newS3FIFO[uint, string](&config{size: 100})
cache.set(uint(0), "zero", 0)
cache.set(uint(100), "hundred", 0)
if v, ok := cache.get(uint(0)); !ok || v != "zero" {
t.Errorf("uint 0: got %v, %v", v, ok)
}
if v, ok := cache.get(uint(100)); !ok || v != "hundred" {
t.Errorf("uint 100: got %v, %v", v, ok)
}
})
t.Run("uint64", func(t *testing.T) {
// Use larger size to ensure per-shard capacity > 1 (2048 shards)
cache := newS3FIFO[uint64, string](&config{size: 10000})
cache.set(uint64(1<<63), "large", 0)
cache.set(uint64(0), "zero", 0)
if v, ok := cache.get(uint64(1 << 63)); !ok || v != "large" {
t.Errorf("uint64 large: got %v, %v", v, ok)
}
if v, ok := cache.get(uint64(0)); !ok || v != "zero" {
t.Errorf("uint64 0: got %v, %v", v, ok)
}
})
t.Run("string", func(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 100})
cache.set("hello", 1, 0)
cache.set("", 2, 0) // empty string is valid
unicode := "unicode-\u65e5\u672c\u8a9e"
cache.set(unicode, 3, 0)
if v, ok := cache.get("hello"); !ok || v != 1 {
t.Errorf("string hello: got %v, %v", v, ok)
}
if v, ok := cache.get(""); !ok || v != 2 {
t.Errorf("empty string: got %v, %v", v, ok)
}
if v, ok := cache.get(unicode); !ok || v != 3 {
t.Errorf("unicode string: got %v, %v", v, ok)
}
})
t.Run("fmt.Stringer", func(t *testing.T) {
// Tests the fmt.Stringer fast path in shardIndexSlow
cache := newS3FIFO[stringerKey, string](&config{size: 100})
k1 := stringerKey{id: 1}
k2 := stringerKey{id: 2}
k3 := stringerKey{id: 999}
cache.set(k1, "one", 0)
cache.set(k2, "two", 0)
cache.set(k3, "many", 0)
if v, ok := cache.get(k1); !ok || v != "one" {
t.Errorf("stringer k1: got %v, %v", v, ok)
}
if v, ok := cache.get(k2); !ok || v != "two" {
t.Errorf("stringer k2: got %v, %v", v, ok)
}
if v, ok := cache.get(k3); !ok || v != "many" {
t.Errorf("stringer k3: got %v, %v", v, ok)
}
// Verify delete works
cache.del(k2)
if _, ok := cache.get(k2); ok {
t.Error("stringer k2 should be deleted")
}
})
t.Run("plain struct", func(t *testing.T) {
// Tests the fmt.Sprintf fallback in shardIndexSlow.
// This is not fast, but should be reliable.
cache := newS3FIFO[plainKey, string](&config{size: 100})
k1 := plainKey{a: 1, b: "one"}
k2 := plainKey{a: 2, b: "two"}
k3 := plainKey{a: 1, b: "one"} // Same as k1
cache.set(k1, "first", 0)
cache.set(k2, "second", 0)
if v, ok := cache.get(k1); !ok || v != "first" {
t.Errorf("plain k1: got %v, %v", v, ok)
}
if v, ok := cache.get(k2); !ok || v != "second" {
t.Errorf("plain k2: got %v, %v", v, ok)
}
// k3 is equal to k1, should get the same value
if v, ok := cache.get(k3); !ok || v != "first" {
t.Errorf("plain k3 (same as k1): got %v, %v", v, ok)
}
// Update via equal key
cache.set(k3, "updated", 0)
if v, ok := cache.get(k1); !ok || v != "updated" {
t.Errorf("plain k1 after k3 update: got %v, %v", v, ok)
}
})
}
// TestS3FIFO_FrequencyCapAt7 tests that frequency counter is capped at 7.
// This is a critical S3-FIFO parameter that affects promotion behavior.
func TestS3FIFO_FrequencyCapAt7(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 100})
// Insert a key
cache.set("hot", 1, 0)
// Access it many times (well beyond 7)
for range 20 {
cache.get("hot")
}
// Access the internal entry to check frequency
ent, ok := cache.getEntry("hot")
if !ok {
t.Fatal("entry not found")
}
freq := ent.freq()
if freq > 7 {
t.Errorf("frequency = %d; want <= 7 (should be capped at 7)", freq)
}
if freq != 7 {
t.Logf("frequency = %d (expected 7, but may vary due to timing)", freq)
}
}
// TestS3FIFO_SetIncrementsFrequency tests that updating an existing key increments frequency.
// This matches reference s3-fifo behavior where Set increments frequency.
func TestS3FIFO_SetIncrementsFrequency(t *testing.T) {
cache := newS3FIFO[string, int](&config{size: 100})
// Insert a key
cache.set("key", 1, 0)
// Check initial frequency (should be 0 for new entries)
ent, _ := cache.getEntry("key")
initialFreq := ent.freq()
if initialFreq != 0 {
t.Errorf("initial frequency = %d; want 0", initialFreq)
}
// Update the key several times
for i := 2; i <= 4; i++ {
cache.set("key", i, 0)
}
// Check that frequency increased due to updates
ent, _ = cache.getEntry("key")
finalFreq := ent.freq()
if finalFreq == 0 {
t.Error("frequency should have increased after updates, but is still 0")
}
if finalFreq > 3 {
t.Errorf("frequency = %d; want <= 3 (should be capped)", finalFreq)
}
}
// TestS3FIFO_CascadingEviction tests that promoting from small to main triggers
// eviction from main when main exceeds 90% capacity.
func TestS3FIFO_CascadingEviction(t *testing.T) {
// Use a small cache to make test fast
capacity := 1000
cache := newS3FIFO[int, int](&config{size: capacity})
// Fill the cache completely
for i := range capacity {
cache.set(i, i, 0)
}
// Now access items that are likely in the small queue to trigger promotions
// The first 10% should be in small queue (100 items)
// Access them twice to trigger promotion (freq > 1)
for i := range 100 {
cache.get(i) // First access
cache.get(i) // Second access - should trigger promotion
}
// Add more items to trigger evictions with promotions
for i := capacity; i < capacity+200; i++ {
cache.set(i, i, 0)
// This should trigger cascading eviction when small promotes to main
}
// Cache should still be at capacity (not exceeding it)
actualLen := cache.len()
if actualLen > capacity*11/10 { // Allow 10% variance for shard rounding
t.Errorf("cache length = %d; should be near capacity %d (got %.1f%% over)",
actualLen, capacity, float64(actualLen-capacity)/float64(capacity)*100)
}
}
// TestS3FIFO_GhostQueueSize tests that ghost queue is sized correctly.
func TestS3FIFO_GhostQueueSize(t *testing.T) {
capacity := 1000
cache := newS3FIFO[int, int](&config{size: capacity})
// Ghost capacity varies by cache size (see ghostRatio function).
want := capacity * ghostRatio(capacity) / 1000
if cache.ghostCap != want {
t.Errorf("ghost capacity = %d; want %d", cache.ghostCap, want)
}
}
// TestS3FIFO_EvictionTriggerBoundary tests the eviction trigger at exactly 10% boundary.
// We use > instead of >= for the small queue size check.
func TestS3FIFO_EvictionTriggerBoundary(t *testing.T) {
// Use capacity of 100 for easy math (10% = 10 items)
capacity := 100
cache := newS3FIFO[int, int](&config{size: capacity})
// Fill cache to capacity
for i := range capacity {
cache.set(i, i, 0)
}
// Insert one more item - this should trigger eviction
// The eviction logic should use `small.len > capacity/10`
// which means it only evicts from small when small has MORE than 10% (> 10 items)
cache.set(capacity, capacity, 0)
// Cache should still be at or near capacity
actualLen := cache.len()
if actualLen > capacity*11/10 { // Allow 10% variance
t.Errorf("cache length = %d; should be near capacity %d", actualLen, capacity)
}
}
// TestS3FIFO_GhostQueuePromotion tests that items in ghost queue are promoted to main.
func TestS3FIFO_GhostQueuePromotion(t *testing.T) {
// Use larger capacity so ghost doesn't rotate too quickly
capacity := 1000
cache := newS3FIFO[int, int](&config{size: capacity})
// Fill cache (don't access key 0 - we want it evicted with freq=0)
for i := range capacity {
cache.set(i, i, 0)
}
// Add just enough items to evict key 0 (50% of capacity to avoid ghost rotation)
for i := capacity; i < capacity+capacity/2; i++ {
cache.set(i, i, 0)
}
// Key 0 should be evicted and in ghost queue
if _, ok := cache.get(0); ok {
t.Log("key 0 still in cache (may not have been evicted yet)")
}
// Re-insert key 0 - it should go to main queue (not small) because it's in ghost
cache.set(0, 9999, 0)
// Check that key 0 is in main queue by inspecting internal state
ent, ok := cache.getEntry(0)
inSmall := false
if ok {
inSmall = ent.inSmall()
}
if !ok {
t.Fatal("key 0 not found after re-insertion")
}
if inSmall {
t.Error("key 0 should be in main queue (not small) after re-insertion from ghost")
}
}
// TestS3FIFO_SmallQueuePromotion tests the promotion logic from small to main.
// Items with freq > 1 should be promoted when evicting from small.
func TestS3FIFO_SmallQueuePromotion(t *testing.T) {
capacity := 1000
cache := newS3FIFO[int, int](&config{size: capacity})
// Fill cache
for i := range capacity {
cache.set(i, i, 0)
}
// Access first 50 items twice (they should be in small queue initially)
for i := range 50 {
cache.get(i) // First access
cache.get(i) // Second access - freq > 1
}
// Add more items to trigger eviction from small
for i := capacity; i < capacity+100; i++ {
cache.set(i, i, 0)
}
// The accessed items (0-49) should still be in cache because they were promoted
// due to freq > 1
survivedCount := 0
for i := range 50 {
if _, ok := cache.get(i); ok {
survivedCount++
}
}
// At least some of them should have survived (been promoted to main)
if survivedCount < 40 {
t.Errorf("only %d/50 accessed items survived; expected most to be promoted to main",
survivedCount)
}
}
// TestS3FIFO_MainQueueReinsertion tests the reinsertion logic in main queue.
// Items with freq > 0 should be reinserted to back of main with decremented freq.
func TestS3FIFO_MainQueueReinsertion(t *testing.T) {
capacity := 1000
cache := newS3FIFO[int, int](&config{size: capacity})
// Fill cache
for i := range capacity {
cache.set(i, i, 0)
}
// Access items to promote them to main
for i := range 100 {
cache.get(i)
cache.get(i)
}
// Add more items to fill small queue and trigger promotions to main
for i := capacity; i < capacity+200; i++ {
cache.set(i, i, 0)
}
// Continue accessing the same items
for range 3 {
for i := range 100 {
cache.get(i)
}
}
// Add even more items to force main queue evictions
for i := capacity + 200; i < capacity+400; i++ {
cache.set(i, i, 0)
}
// The frequently accessed items should mostly survive due to reinsertion
survivedCount := 0
for i := range 100 {
if _, ok := cache.get(i); ok {
survivedCount++
}
}
// Most should survive due to main queue reinsertion with freq > 0
if survivedCount < 90 {
t.Errorf("only %d/100 hot items survived; expected most to survive due to reinsertion",
survivedCount)
}
}
// TestS3FIFO_NeverExceedsCapacity verifies that the cache never exceeds its capacity
// under various workloads and sizes. This is a critical invariant.
func TestS3FIFO_NeverExceedsCapacity(t *testing.T) {
testCases := []struct {
name string
capacity int
insertions int
}{
{"small_2x", 100, 200},
{"small_5x", 100, 500},
{"small_10x", 100, 1000},
{"medium_2x", 1000, 2000},
{"medium_5x", 1000, 5000},
{"large_3x", 10000, 30000},
{"large_5x", 10000, 50000},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cache := newS3FIFO[int, int](&config{size: tc.capacity})
maxSeen := 0
for i := range tc.insertions {
cache.set(i, i, 0)
// Check capacity after every insertion
current := cache.len()
if current > maxSeen {
maxSeen = current
}
// Capacity should never exceed the configured limit
// Allow small overhead for concurrent operations (10%)
maxAllowed := tc.capacity * 11 / 10
if current > maxAllowed {
t.Fatalf("capacity exceeded at insertion %d: got %d, max allowed %d",
i, current, maxAllowed)
}
}
// Final check: cache should be at or near capacity
final := cache.len()
minExpected := tc.capacity * 80 / 100
if final < minExpected {
t.Errorf("cache under-utilized: got %d, want at least %d", final, minExpected)
}
t.Logf("capacity=%d, insertions=%d, max_seen=%d, final=%d",
tc.capacity, tc.insertions, maxSeen, final)
})
}
}