-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_test.go
More file actions
1137 lines (996 loc) · 33.7 KB
/
Copy pathbuffer_test.go
File metadata and controls
1137 lines (996 loc) · 33.7 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 scopecache
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
func newItem(scope, id string, payload map[string]interface{}) Item {
if payload == nil {
payload = map[string]interface{}{"v": 1}
}
raw, err := json.Marshal(payload)
if err != nil {
panic(err)
}
return Item{Scope: scope, ID: id, Payload: raw}
}
// newTestBuffer returns a scopeBuffer wired to a permissive private
// store: byte / item caps come from the store, so per-scope cap
// behaviour stays observable in tests that previously relied on the
// orphan-buffer ctor. Byte and per-item caps are wide open (1<<62)
// so only the explicit item-count cap (maxItems) fires; tests that
// want to exercise byte cap behaviour build a store directly via
// newStore(Config{...}).
func newTestBuffer(maxItems int) *scopeBuffer {
s := newStore(Config{
ScopeMaxItems: maxItems,
MaxStoreBytes: 1 << 62,
MaxItemBytes: 1 << 62,
})
// Default scope "s" matches every newItem("s", …) call in tests; insert
// paths will canonicalise item.Scope = b.scope on the way in, so the
// buffer's scope must agree with the scope the tests construct items with.
return s.newscopeBuffer("s")
}
// --- scopeBuffer.appendItem ---------------------------------------------------
func TestAppendItem_AssignsSeqMonotonically(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 5; i++ {
it, err := buf.appendItem(newItem("s", "", nil))
if err != nil {
t.Fatalf("append %d: %v", i, err)
}
if it.Seq != uint64(i) {
t.Fatalf("append %d: seq=%d want %d", i, it.Seq, i)
}
}
}
func TestAppendItem_RejectsDuplicateID(t *testing.T) {
buf := newTestBuffer(10)
if _, err := buf.appendItem(newItem("s", "a", nil)); err != nil {
t.Fatalf("first append: %v", err)
}
if _, err := buf.appendItem(newItem("s", "a", nil)); err == nil {
t.Fatal("expected duplicate id rejection")
}
}
func TestAppendItem_AllowsMultipleEmptyIDs(t *testing.T) {
buf := newTestBuffer(10)
for i := 0; i < 3; i++ {
if _, err := buf.appendItem(newItem("s", "", nil)); err != nil {
t.Fatalf("append %d: %v", i, err)
}
}
if len(buf.items) != 3 {
t.Fatalf("len=%d want 3", len(buf.items))
}
}
// Capacity is a hard cap: a write that would push the buffer past maxItems
// is rejected with ScopeFullError. No eviction happens — state, seq cursor
// and byID index stay exactly as they were before the failed append.
func TestAppendItem_RejectsAtCapacity(t *testing.T) {
buf := newTestBuffer(3)
for i := 0; i < 3; i++ {
if _, err := buf.appendItem(newItem("s", "", nil)); err != nil {
t.Fatalf("pre-fill %d: %v", i, err)
}
}
_, err := buf.appendItem(newItem("s", "overflow", nil))
if err == nil {
t.Fatal("expected ScopeFullError when appending past cap")
}
var sfe *ScopeFullError
if !errors.As(err, &sfe) {
t.Fatalf("expected *ScopeFullError, got %T: %v", err, err)
}
if sfe.Count != 3 || sfe.Cap != 3 {
t.Fatalf("ScopeFullError{Count:%d Cap:%d}, want {3,3}", sfe.Count, sfe.Cap)
}
if len(buf.items) != 3 {
t.Fatalf("rejected write mutated buffer: len=%d want 3", len(buf.items))
}
if buf.lastSeq != 3 {
t.Fatalf("rejected write advanced lastSeq: got %d want 3", buf.lastSeq)
}
if _, ok := buf.byID["overflow"]; ok {
t.Fatal("rejected id 'overflow' leaked into byID index")
}
}
// --- scopeBuffer.replaceAll ---------------------------------------------------
func TestReplaceAll_AssignsFreshSeqFromOne(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "", nil))
_, _ = buf.appendItem(newItem("s", "", nil))
items := []Item{
newItem("s", "a", nil),
newItem("s", "b", nil),
}
_, err := buf.replaceAll(items)
if err != nil {
t.Fatal(err)
}
if buf.items[0].Seq != 1 || buf.items[1].Seq != 2 {
t.Fatalf("seq not reset: %+v", buf.items)
}
if buf.lastSeq != 2 {
t.Fatalf("lastSeq=%d want 2", buf.lastSeq)
}
}
// replaceAll rejects the whole batch when it exceeds the per-scope cap —
// no silent truncation. Pre-existing state must stay untouched since the
// buffer is the mutation target and the caller expects all-or-nothing.
func TestReplaceAll_RejectsOverCap(t *testing.T) {
buf := newTestBuffer(3)
_, _ = buf.appendItem(newItem("s", "keep", nil))
priorLen := len(buf.items)
items := []Item{
newItem("s", "a", nil),
newItem("s", "b", nil),
newItem("s", "c", nil),
newItem("s", "d", nil),
}
_, err := buf.replaceAll(items)
if err == nil {
t.Fatal("expected ScopeFullError when replacement exceeds cap")
}
var sfe *ScopeFullError
if !errors.As(err, &sfe) {
t.Fatalf("expected *ScopeFullError, got %T: %v", err, err)
}
if sfe.Count != 4 || sfe.Cap != 3 {
t.Fatalf("ScopeFullError{Count:%d Cap:%d}, want {4,3}", sfe.Count, sfe.Cap)
}
if len(buf.items) != priorLen {
t.Fatalf("rejected replaceAll mutated buffer: len=%d want %d", len(buf.items), priorLen)
}
}
func TestReplaceAll_RejectsDuplicateIDs(t *testing.T) {
buf := newTestBuffer(10)
items := []Item{
newItem("s", "a", nil),
newItem("s", "a", nil),
}
if _, err := buf.replaceAll(items); err == nil {
t.Fatal("expected duplicate id error")
}
}
func TestReplaceAll_EmptyItemsClearsScope(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "", nil))
if _, err := buf.replaceAll([]Item{}); err != nil {
t.Fatal(err)
}
if len(buf.items) != 0 {
t.Fatalf("expected empty buffer, got %d items", len(buf.items))
}
}
// --- scopeBuffer.updateByID ---------------------------------------------------
func TestUpdateByID_HitPreservesSeq(t *testing.T) {
buf := newTestBuffer(10)
original, _ := buf.appendItem(newItem("s", "a", map[string]interface{}{"v": 1}))
newPayload, _ := json.Marshal(map[string]interface{}{"v": 2})
n, err := buf.updateByID("a", newPayload, nil)
if err != nil {
t.Fatalf("updateByID: %v", err)
}
if n != 1 {
t.Fatalf("updated=%d want 1", n)
}
got, _ := buf.getByID("a")
if got.Seq != original.Seq {
t.Fatalf("seq changed: %d -> %d", original.Seq, got.Seq)
}
var decoded map[string]interface{}
if err := json.Unmarshal(got.Payload, &decoded); err != nil {
t.Fatalf("payload decode: %v", err)
}
if v, ok := decoded["v"].(float64); !ok || v != 2 {
t.Fatalf("payload not updated: %s", string(got.Payload))
}
}
func TestUpdateByID_Miss(t *testing.T) {
buf := newTestBuffer(10)
raw, _ := json.Marshal(map[string]interface{}{"v": 1})
n, err := buf.updateByID("missing", raw, nil)
if err != nil {
t.Fatalf("updateByID: %v", err)
}
if n != 0 {
t.Fatalf("updated=%d want 0", n)
}
}
// --- scopeBuffer.updateBySeq --------------------------------------------------
func TestUpdateBySeq_Hit(t *testing.T) {
buf := newTestBuffer(10)
it, _ := buf.appendItem(newItem("s", "", map[string]interface{}{"v": 1}))
newPayload, _ := json.Marshal(map[string]interface{}{"v": 2})
n, err := buf.updateBySeq(it.Seq, newPayload, nil)
if err != nil {
t.Fatalf("updateBySeq: %v", err)
}
if n != 1 {
t.Fatalf("updated=%d want 1", n)
}
got, _ := buf.getBySeq(it.Seq)
var decoded map[string]interface{}
if err := json.Unmarshal(got.Payload, &decoded); err != nil {
t.Fatalf("payload decode: %v", err)
}
if v, ok := decoded["v"].(float64); !ok || v != 2 {
t.Fatalf("payload not updated: %s", string(got.Payload))
}
}
func TestUpdateBySeq_KeepsByIDIndexInSync(t *testing.T) {
buf := newTestBuffer(10)
it, _ := buf.appendItem(newItem("s", "a", map[string]interface{}{"v": 1}))
newPayload, _ := json.Marshal(map[string]interface{}{"v": 42})
if _, err := buf.updateBySeq(it.Seq, newPayload, nil); err != nil {
t.Fatalf("updateBySeq: %v", err)
}
// byID index must reflect the new payload too, otherwise a /get by id
// would return the pre-update payload.
got, ok := buf.getByID("a")
if !ok {
t.Fatal("getByID missed after updateBySeq")
}
var decoded map[string]interface{}
_ = json.Unmarshal(got.Payload, &decoded)
if decoded["v"].(float64) != 42 {
t.Fatalf("byID stale payload: %s", string(got.Payload))
}
}
func TestUpdateBySeq_Miss(t *testing.T) {
buf := newTestBuffer(10)
raw, _ := json.Marshal(map[string]interface{}{"v": 1})
n, err := buf.updateBySeq(999, raw, nil)
if err != nil {
t.Fatalf("updateBySeq: %v", err)
}
if n != 0 {
t.Fatalf("updated=%d want 0", n)
}
}
// --- scopeBuffer.upsertByID ---------------------------------------------------
func TestUpsertByID_CreatesNewItem(t *testing.T) {
buf := newTestBuffer(10)
result, created, err := buf.upsertByID(newItem("s", "a", map[string]interface{}{"v": 1}))
if err != nil {
t.Fatalf("upsertByID: %v", err)
}
if !created {
t.Fatal("created=false on first upsert")
}
if result.Seq != 1 {
t.Fatalf("seq=%d want 1", result.Seq)
}
if _, ok := buf.byID["a"]; !ok {
t.Fatal("byID index missing new item")
}
if len(buf.items) != 1 {
t.Fatalf("items len=%d want 1", len(buf.items))
}
}
func TestUpsertByID_ReplacesPayloadAndPreservesSeq(t *testing.T) {
buf := newTestBuffer(10)
first, _, err := buf.upsertByID(newItem("s", "a", map[string]interface{}{"v": 1}))
if err != nil {
t.Fatalf("first upsert: %v", err)
}
second, created, err := buf.upsertByID(newItem("s", "a", map[string]interface{}{"v": 2}))
if err != nil {
t.Fatalf("second upsert: %v", err)
}
if created {
t.Fatal("created=true on replace")
}
if second.Seq != first.Seq {
t.Fatalf("seq changed: %d -> %d", first.Seq, second.Seq)
}
if len(buf.items) != 1 {
t.Fatalf("items len=%d want 1 (no duplicate inserted)", len(buf.items))
}
got, _ := buf.getByID("a")
var decoded map[string]interface{}
_ = json.Unmarshal(got.Payload, &decoded)
if decoded["v"].(float64) != 2 {
t.Fatalf("payload not replaced: %s", string(got.Payload))
}
}
func TestUpsertByID_CoexistsWithAppend(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
result, created, err := buf.upsertByID(newItem("s", "b", map[string]interface{}{"v": 9}))
if err != nil {
t.Fatalf("upsertByID: %v", err)
}
if !created {
t.Fatal("created=false for a fresh id")
}
if result.Seq != 2 {
t.Fatalf("seq=%d want 2 (continuous with prior append)", result.Seq)
}
}
func TestUpsertByID_RejectsAtCapacity(t *testing.T) {
buf := newTestBuffer(2)
_, _ = buf.appendItem(newItem("s", "a", nil))
_, _ = buf.appendItem(newItem("s", "b", nil))
_, _, err := buf.upsertByID(newItem("s", "c", nil))
if err == nil {
t.Fatal("expected ScopeFullError when upserting past cap")
}
var sfe *ScopeFullError
if !errors.As(err, &sfe) {
t.Fatalf("expected *ScopeFullError, got %T: %v", err, err)
}
// A replace must still succeed at capacity — only create hits the cap.
if _, _, err := buf.upsertByID(newItem("s", "a", map[string]interface{}{"v": 99})); err != nil {
t.Fatalf("replace at cap should succeed: %v", err)
}
}
// --- scopeBuffer.deleteByID ---------------------------------------------------
func TestDeleteByID_Hit(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
_, _ = buf.appendItem(newItem("s", "b", nil))
n, _ := buf.deleteByID("a")
if n != 1 {
t.Fatalf("deleted=%d want 1", n)
}
if _, ok := buf.byID["a"]; ok {
t.Fatal("id 'a' still in index")
}
if len(buf.items) != 1 {
t.Fatalf("len=%d want 1", len(buf.items))
}
}
func TestDeleteByID_Miss(t *testing.T) {
buf := newTestBuffer(10)
n, _ := buf.deleteByID("missing")
if n != 0 {
t.Fatalf("deleted=%d want 0", n)
}
}
func TestDeleteByID_DoesNotRollbackLastSeq(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
_, _ = buf.appendItem(newItem("s", "b", nil))
_, _ = buf.deleteByID("b")
next, _ := buf.appendItem(newItem("s", "c", nil))
if next.Seq != 3 {
t.Fatalf("seq=%d want 3 (no rollback)", next.Seq)
}
}
// Drain-to-empty via single-item deletes must release the high-
// watermark backing storage. Pre-fix the items slice's reslice kept
// cap pinned at the historical max, and Go maps never shrink their
// bucket arrays on delete() — a write-buffer scope that drained 1k
// items down to 0 leaked ~100 KiB of capacity until appendItem
// re-grew it. resetIfEmptyLocked nil's items + maps when len drops
// to zero so the GC can reclaim everything.
//
// Asserts the post-conditions resetIfEmptyLocked promises:
// - items slice is nil (cap == 0; no backing array retained)
// - byID map is nil
// - lastSeq is preserved (monotonic across drain/refill cycles)
// - subsequent appendItem still works (lazy-init verified)
func TestDeleteByID_DrainToEmptyReleasesBacking(t *testing.T) {
buf := newTestBuffer(10_000)
const N = 1000
for i := 0; i < N; i++ {
_, _ = buf.appendItem(newItem("s", fmt.Sprintf("id-%d", i), nil))
}
if cap(buf.items) < N {
t.Fatalf("setup: cap(items)=%d expected at least %d", cap(buf.items), N)
}
preDrainLastSeq := buf.lastSeq
for i := 0; i < N; i++ {
if n, _ := buf.deleteByID(fmt.Sprintf("id-%d", i)); n != 1 {
t.Fatalf("delete id-%d: n=%d want 1", i, n)
}
}
if cap(buf.items) != 0 {
t.Errorf("cap(items)=%d after drain-to-empty, want 0 (backing array not released)", cap(buf.items))
}
if buf.items != nil {
t.Errorf("items slice = %v, want nil", buf.items)
}
if buf.byID != nil {
t.Errorf("byID = %v, want nil (map buckets not released)", buf.byID)
}
if buf.lastSeq != preDrainLastSeq {
t.Errorf("lastSeq=%d, want %d (cursor must not regress on drain)", buf.lastSeq, preDrainLastSeq)
}
// appendItem must lazy-init the slice and maps after a reset.
next, err := buf.appendItem(newItem("s", "after-drain", nil))
if err != nil {
t.Fatalf("appendItem after drain: %v", err)
}
if next.Seq <= preDrainLastSeq {
t.Errorf("post-drain append seq=%d, want > %d (cursor regressed)", next.Seq, preDrainLastSeq)
}
}
// trimSeq draining everything must also nil the maps. The items
// slice is already replaced by `rest := make(...)` in the existing
// code; the map-side leak is what resetIfEmptyLocked closes here.
func TestTrimSeq_DrainToEmptyReleasesBacking(t *testing.T) {
buf := newTestBuffer(10_000)
const N = 1000
var lastSeq uint64
for i := 0; i < N; i++ {
it, _ := buf.appendItem(newItem("s", fmt.Sprintf("id-%d", i), nil))
lastSeq = it.Seq
}
if n, _ := buf.trimSeq(lastSeq); n != N {
t.Fatalf("trimSeq: n=%d want %d", n, N)
}
if cap(buf.items) != 0 {
t.Errorf("cap(items)=%d after drain, want 0", cap(buf.items))
}
if buf.byID != nil {
t.Errorf("byID = %v, want nil", buf.byID)
}
}
// --- scopeBuffer.deleteBySeq --------------------------------------------------
func TestDeleteBySeq_Hit(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
it2, _ := buf.appendItem(newItem("s", "b", nil))
_, _ = buf.appendItem(newItem("s", "c", nil))
n, _ := buf.deleteBySeq(it2.Seq)
if n != 1 {
t.Fatalf("deleted=%d want 1", n)
}
if _, ok := buf.indexBySeqLocked(it2.Seq); ok {
t.Fatal("seq still findable via indexBySeqLocked")
}
if _, ok := buf.byID["b"]; ok {
t.Fatal("id 'b' still in byID index")
}
if len(buf.items) != 2 {
t.Fatalf("len=%d want 2", len(buf.items))
}
}
func TestDeleteBySeq_Miss(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
if n, _ := buf.deleteBySeq(999); n != 0 {
t.Fatalf("deleted=%d want 0", n)
}
if len(buf.items) != 1 {
t.Fatalf("len=%d want 1", len(buf.items))
}
}
func TestDeleteBySeq_NoIDItem(t *testing.T) {
buf := newTestBuffer(10)
it, _ := buf.appendItem(newItem("s", "", nil))
if n, _ := buf.deleteBySeq(it.Seq); n != 1 {
t.Fatalf("deleted=%d want 1", n)
}
if len(buf.items) != 0 {
t.Fatalf("len=%d want 0", len(buf.items))
}
}
func TestDeleteBySeq_DoesNotRollbackLastSeq(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
it2, _ := buf.appendItem(newItem("s", "b", nil))
_, _ = buf.deleteBySeq(it2.Seq)
next, _ := buf.appendItem(newItem("s", "c", nil))
if next.Seq != 3 {
t.Fatalf("seq=%d want 3 (no rollback)", next.Seq)
}
}
// --- scopeBuffer.trimSeq ---------------------------------------------
func TestTrimSeq_RemovesPrefix(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 5; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
n, _ := buf.trimSeq(3)
if n != 3 {
t.Fatalf("deleted=%d want 3", n)
}
if len(buf.items) != 2 {
t.Fatalf("len=%d want 2", len(buf.items))
}
if buf.items[0].Seq != 4 || buf.items[1].Seq != 5 {
t.Fatalf("unexpected survivors: %+v", buf.items)
}
for seq := uint64(1); seq <= 3; seq++ {
if _, ok := buf.indexBySeqLocked(seq); ok {
t.Fatalf("seq %d should be gone from items index", seq)
}
}
}
func TestTrimSeq_RemovesIDsToo(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "a", nil))
_, _ = buf.appendItem(newItem("s", "b", nil))
_, _ = buf.appendItem(newItem("s", "c", nil))
_, _ = buf.trimSeq(2)
if _, ok := buf.byID["a"]; ok {
t.Fatal("id 'a' should have been removed from byID")
}
if _, ok := buf.byID["b"]; ok {
t.Fatal("id 'b' should have been removed from byID")
}
if _, ok := buf.byID["c"]; !ok {
t.Fatal("id 'c' should still exist")
}
}
func TestTrimSeq_NoOpBelowRange(t *testing.T) {
buf := newTestBuffer(5)
// Append 3 items, then delete through the prefix before seq 3's start.
// Nothing matches because no item has seq <= 0.
for i := 1; i <= 3; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
// Drop seqs 1..2 first to simulate a post-drain state, then ask to drop
// anything <= 2 again. The cut point is already past — expect no-op.
_, _ = buf.trimSeq(2)
n, _ := buf.trimSeq(2)
if n != 0 {
t.Fatalf("deleted=%d want 0 (no items at or below seq 2 remain)", n)
}
if len(buf.items) != 1 {
t.Fatalf("len=%d want 1", len(buf.items))
}
}
func TestTrimSeq_RemovesAllWhenMaxAtOrAboveLast(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 3; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
n, _ := buf.trimSeq(99)
if n != 3 {
t.Fatalf("deleted=%d want 3", n)
}
if len(buf.items) != 0 {
t.Fatalf("expected empty scope, got %d", len(buf.items))
}
}
func TestTrimSeq_DoesNotRollbackLastSeq(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 3; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
_, _ = buf.trimSeq(3)
next, _ := buf.appendItem(newItem("s", "", nil))
if next.Seq != 4 {
t.Fatalf("seq=%d want 4 (no rollback after draining)", next.Seq)
}
}
func TestTrimSeq_ReleasesBackingArray(t *testing.T) {
// Fill a scope well past its natural grow-cycle so the backing array
// has capacity noticeably larger than the survivors. Drain the prefix
// and assert the backing array was reallocated to match the remainder
// — that is the guarantee that frees the removed-payload memory for
// GC in the write-buffer drain-from-front pattern.
const fill = 1000
buf := newTestBuffer(fill * 2)
for i := 0; i < fill; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
preCap := cap(buf.items)
if preCap < fill {
t.Fatalf("sanity: pre-drain cap=%d want >= %d", preCap, fill)
}
drained, _ := buf.trimSeq(uint64(fill - 10))
if drained != fill-10 {
t.Fatalf("drained=%d want %d", drained, fill-10)
}
if len(buf.items) != 10 {
t.Fatalf("len=%d want 10", len(buf.items))
}
if cap(buf.items) != len(buf.items) {
t.Fatalf("backing array not released: cap=%d len=%d (pre-drain cap was %d)",
cap(buf.items), len(buf.items), preCap)
}
}
// --- scopeBuffer.tailOffset ---------------------------------------------------
func TestTailOffset_BasicAndEdges(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 5; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
tests := []struct {
limit, offset int
wantSeq []uint64
}{
{2, 0, []uint64{4, 5}},
{2, 2, []uint64{2, 3}},
{10, 0, []uint64{1, 2, 3, 4, 5}},
{2, 10, nil},
}
for _, tc := range tests {
got, _ := buf.tailOffset(nil, tc.limit, tc.offset)
if len(got) != len(tc.wantSeq) {
t.Errorf("tail(limit=%d offset=%d): len=%d want %d", tc.limit, tc.offset, len(got), len(tc.wantSeq))
continue
}
for i, seq := range tc.wantSeq {
if got[i].Seq != seq {
t.Errorf("tail(limit=%d offset=%d)[%d].seq=%d want %d", tc.limit, tc.offset, i, got[i].Seq, seq)
}
}
}
}
// --- scopeBuffer.sinceSeq -----------------------------------------------------
func TestSinceSeq_ReturnsItemsAfterCursor(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 5; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
// Limit chosen above the available count to confirm "all matching"
// behaviour without relying on the historical limit=0 → unlimited
// semantics (tightened to limit ≤ 0 → empty for cross-method
// uniformity; see sinceSeq's doc-comment).
got, _ := buf.sinceSeq(nil, 2, 100)
if len(got) != 3 {
t.Fatalf("len=%d want 3", len(got))
}
if got[0].Seq != 3 {
t.Fatalf("first.seq=%d want 3", got[0].Seq)
}
}
func TestSinceSeq_RespectsLimit(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 5; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
got, _ := buf.sinceSeq(nil, 0, 2)
if len(got) != 2 {
t.Fatalf("len=%d want 2", len(got))
}
}
func TestSinceSeq_EmptyWhenPastEnd(t *testing.T) {
buf := newTestBuffer(10)
_, _ = buf.appendItem(newItem("s", "", nil))
got, _ := buf.sinceSeq(nil, 100, 100)
if len(got) != 0 {
t.Fatalf("len=%d want 0", len(got))
}
}
// limit ≤ 0 returns an empty slice on every multi-item read
// (sinceSeq, tailOffset, scopeList). Pre-fix sinceSeq treated 0 as
// "no truncation" and returned every matching item — inconsistent
// with the other two methods, surprising for Go-API callers that
// passed an uninitialised int, and silently bypassing the
// HTTP-layer's normalizeLimit gate. The guard makes "give me ≤ 0
// items" answered uniformly with the empty result.
func TestSinceSeq_NonPositiveLimitReturnsEmpty(t *testing.T) {
buf := newTestBuffer(10)
for i := 1; i <= 5; i++ {
_, _ = buf.appendItem(newItem("s", "", nil))
}
for _, limit := range []int{0, -1, -1000} {
got, more := buf.sinceSeq(nil, 0, limit)
if len(got) != 0 {
t.Errorf("limit=%d: len=%d want 0", limit, len(got))
}
if more {
t.Errorf("limit=%d: more=true want false", limit)
}
}
}
// --- scopeBuffer.getByID / getBySeq -------------------------------------------
func TestGetByIDAndSeq(t *testing.T) {
buf := newTestBuffer(10)
it, _ := buf.appendItem(newItem("s", "a", nil))
if got, ok := buf.getByID("a"); !ok || got.Seq != it.Seq {
t.Fatalf("getByID: ok=%v seq=%d want %d", ok, got.Seq, it.Seq)
}
if got, ok := buf.getBySeq(it.Seq); !ok || got.ID != "a" {
t.Fatalf("getBySeq: ok=%v id=%q", ok, got.ID)
}
if _, ok := buf.getByID("missing"); ok {
t.Fatal("getByID('missing') should miss")
}
if _, ok := buf.getBySeq(999); ok {
t.Fatal("getBySeq(999) should miss")
}
}
// --- recordRead ---------------------------------------------------------------
// recordRead bumps two atomics: readCountTotal and lastAccessTS. The
// time-windowed aggregation that used to live here moved out of core
// — see buffer_heat.go.
func TestRecordRead_BumpsCountAndTimestamp(t *testing.T) {
buf := newTestBuffer(10)
if got := buf.readCountTotal.Load(); got != 0 {
t.Fatalf("pre-read readCountTotal=%d want 0", got)
}
if got := buf.lastAccessTS.Load(); got != 0 {
t.Fatalf("pre-read lastAccessTS=%d want 0", got)
}
now := nowUnixMicro()
buf.recordRead(now)
buf.recordRead(now + 1)
buf.recordRead(now + 2)
if got := buf.readCountTotal.Load(); got != 3 {
t.Errorf("readCountTotal=%d want 3", got)
}
if got := buf.lastAccessTS.Load(); got != now+2 {
t.Errorf("lastAccessTS=%d want %d (most-recent stamp wins)", got, now+2)
}
}
// --- approxSizeBytes ----------------------------------------------------------
func TestApproxSizeBytes_IgnoresReservedCapacity(t *testing.T) {
buf := newTestBuffer(10000)
size := buf.approxSizeBytes()
// Buggy code counted cap(items)*32 = 320KB for an empty scope.
if size > 2048 {
t.Fatalf("empty scope approx_scope_bytes=%d want < 2KB (should not count reserved capacity)", size)
}
}
func TestApproxSizeBytes_GrowsWithItems(t *testing.T) {
buf := newTestBuffer(10000)
before := buf.approxSizeBytes()
_, _ = buf.appendItem(newItem("s", "a", map[string]interface{}{"text": "hello world"}))
after := buf.approxSizeBytes()
if after <= before {
t.Fatalf("size did not grow after append: before=%d after=%d", before, after)
}
}
// TestDeleteByID_ClearsBackingSlot verifies the GC invariant for deleteByID:
// after the slice shift-and-shrink, the tail slot must be nil-ed so the
// removed *Item is eligible for GC. The backing array still exists at full
// capacity, so we reslice past the current length to peek at the vacated slot.
func TestDeleteByID_ClearsBackingSlot(t *testing.T) {
buf := newTestBuffer(8)
_, _ = buf.appendItem(newItem("s", "a", map[string]interface{}{"marker": "A"}))
_, _ = buf.appendItem(newItem("s", "b", map[string]interface{}{"marker": "B"}))
_, _ = buf.appendItem(newItem("s", "c", map[string]interface{}{"marker": "C"}))
if n, _ := buf.deleteByID("b"); n != 1 {
t.Fatalf("delete: n=%d want 1", n)
}
if len(buf.items) != 2 {
t.Fatalf("len=%d want 2 after delete", len(buf.items))
}
full := buf.items[:3]
if tail := full[2]; tail != nil {
t.Fatalf("tail slot not nil-ed in backing array: %+v", *tail)
}
}
// --- lastWriteTS --------------------------------------------------------------
//
// lastWriteTS advances on every path that mutates the scope (append,
// upsert, update, delete, trimSeq, replaceAll) and
// stays put on reads (recordRead). The "preCall <= lastWriteTS" bracket
// is the resilient assertion shape: clock resolution on Windows can be
// ~16ms, so a strictly-greater check against createdTS would be flaky;
// tests instead assert that lastWriteTS sits at or beyond a stamp
// captured immediately before the write call.
func TestLastWriteTS_FreshScopeEqualsCreatedTS(t *testing.T) {
buf := newTestBuffer(10)
if buf.lastWriteTS != buf.createdTS {
t.Fatalf("fresh scope: lastWriteTS=%d createdTS=%d (must be equal — both initialised from one nowUnixMicro() call)",
buf.lastWriteTS, buf.createdTS)
}
if buf.lastWriteTS == 0 {
t.Fatal("fresh scope: lastWriteTS=0 (must be initialised to a real timestamp, not left zero)")
}
}
func TestLastWriteTS_AdvancesOnAppend(t *testing.T) {
buf := newTestBuffer(10)
pre := nowUnixMicro()
it, err := buf.appendItem(newItem("s", "", nil))
if err != nil {
t.Fatalf("append: %v", err)
}
if buf.lastWriteTS < pre {
t.Errorf("lastWriteTS=%d pre=%d (must be >= pre-call stamp)", buf.lastWriteTS, pre)
}
if buf.lastWriteTS != it.Ts {
t.Errorf("lastWriteTS=%d item.Ts=%d (insertNewItemLocked must use one nowUs for both)",
buf.lastWriteTS, it.Ts)
}
}
func TestLastWriteTS_AdvancesOnUpsertReplace(t *testing.T) {
buf := newTestBuffer(10)
if _, _, err := buf.upsertByID(newItem("s", "a", nil)); err != nil {
t.Fatalf("upsert create: %v", err)
}
beforeReplace := buf.lastWriteTS
pre := nowUnixMicro()
if _, _, err := buf.upsertByID(newItem("s", "a", map[string]interface{}{"v": 2})); err != nil {
t.Fatalf("upsert replace: %v", err)
}
if buf.lastWriteTS < pre {
t.Errorf("lastWriteTS=%d pre=%d (replace path must stamp lastWriteTS)", buf.lastWriteTS, pre)
}
if buf.lastWriteTS < beforeReplace {
t.Errorf("lastWriteTS=%d went backwards from %d (replace must not regress the timestamp)",
buf.lastWriteTS, beforeReplace)
}
}
func TestLastWriteTS_AdvancesOnUpdate(t *testing.T) {
buf := newTestBuffer(10)
if _, err := buf.appendItem(newItem("s", "a", nil)); err != nil {
t.Fatalf("append: %v", err)
}
pre := nowUnixMicro()
if _, err := buf.updateByID("a", json.RawMessage(`{"v":2}`), nil); err != nil {
t.Fatalf("update: %v", err)
}
if buf.lastWriteTS < pre {
t.Errorf("lastWriteTS=%d pre=%d (replaceItemFieldsLocked must stamp lastWriteTS)",
buf.lastWriteTS, pre)
}
}
func TestLastWriteTS_AdvancesOnDelete(t *testing.T) {
buf := newTestBuffer(10)
for i := 0; i < 5; i++ {
if _, err := buf.appendItem(newItem("s", "", map[string]interface{}{"i": i})); err != nil {
t.Fatalf("append %d: %v", i, err)
}
}
preDelByID := nowUnixMicro()
if _, err := buf.deleteBySeq(2); err != nil {
t.Fatalf("deleteBySeq: %v", err)
}
if buf.lastWriteTS < preDelByID {
t.Errorf("after deleteBySeq: lastWriteTS=%d pre=%d", buf.lastWriteTS, preDelByID)
}
preDelUpTo := nowUnixMicro()
if _, err := buf.trimSeq(4); err != nil {
t.Fatalf("trimSeq: %v", err)
}
if buf.lastWriteTS < preDelUpTo {
t.Errorf("after trimSeq: lastWriteTS=%d pre=%d", buf.lastWriteTS, preDelUpTo)
}
}
func TestLastWriteTS_AdvancesOnReplaceAll(t *testing.T) {
buf := newTestBuffer(10)
if _, err := buf.appendItem(newItem("s", "a", nil)); err != nil {
t.Fatalf("append: %v", err)
}
pre := nowUnixMicro()
if _, err := buf.replaceAll([]Item{newItem("s", "x", nil), newItem("s", "y", nil)}); err != nil {
t.Fatalf("replaceAll: %v", err)
}
if buf.lastWriteTS < pre {
t.Errorf("after replaceAll (commitReplacement): lastWriteTS=%d pre=%d", buf.lastWriteTS, pre)
}
}
// recordRead is a read-path bookkeeping update; it must not advance
// lastWriteTS. lastAccessTS is the matching read-side counter.
func TestLastWriteTS_NotAffectedByReads(t *testing.T) {
buf := newTestBuffer(10)
if _, err := buf.appendItem(newItem("s", "a", nil)); err != nil {
t.Fatalf("append: %v", err)
}
beforeRead := buf.lastWriteTS
buf.recordRead(nowUnixMicro())
if buf.lastWriteTS != beforeRead {
t.Errorf("recordRead changed lastWriteTS: before=%d after=%d (reads must not bump write timestamp)",
beforeRead, buf.lastWriteTS)
}
}