-
Notifications
You must be signed in to change notification settings - Fork 339
/
reserve_test.go
798 lines (689 loc) · 20 KB
/
reserve_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package localstore
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/ethersphere/bee/pkg/postage"
postagetesting "github.com/ethersphere/bee/pkg/postage/testing"
"github.com/ethersphere/bee/pkg/shed"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
// TestDB_ReserveGC_AllOutOfRadius tests that when all chunks fall outside of
// batch radius, all end up in the cache and that gc size eventually
// converges to the correct value.
func TestDB_ReserveGC_AllOutOfRadius(t *testing.T) {
chunkCount := 150
var closed chan struct{}
testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
select {
case testHookCollectGarbageChan <- collectedCount:
case <-closed:
}
}))
t.Cleanup(setWithinRadiusFunc(func(*DB, shed.Item) bool { return false }))
db := newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 200,
})
closed = db.close
addrs := make([]swarm.Address, 0)
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(5, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutRequest, ch)
if err != nil {
t.Fatal(err)
}
addrs = append(addrs, ch.Address())
}
gcTarget := db.gcTarget()
for {
select {
case <-testHookCollectGarbageChan:
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
gcSize, err := db.gcSize.Get()
if err != nil {
t.Fatal(err)
}
if gcSize == gcTarget {
break
}
}
t.Run("pull index count", newItemsCountTest(db.pullIndex, 0))
t.Run("postage chunks index count", newItemsCountTest(db.postageChunksIndex, int(gcTarget)))
// postageRadiusIndex gets removed only when the batches are called with evict on MaxPO+1
// therefore, the expected index count here is larger than one would expect.
t.Run("postage radius index count", newItemsCountTest(db.postageRadiusIndex, 0))
t.Run("gc index count", newItemsCountTest(db.gcIndex, int(gcTarget)))
t.Run("gc size", newIndexGCSizeTest(db))
t.Run("reserve size", reserveSizeTest(db, 0, 0))
// the first synced chunk should be removed
t.Run("get the first synced chunk", func(t *testing.T) {
_, err := db.Get(context.Background(), storage.ModeGetRequest, addrs[0])
if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("got error %v, want %v", err, storage.ErrNotFound)
}
})
t.Run("only first inserted chunks should be removed", func(t *testing.T) {
for i := 0; i < (chunkCount - int(gcTarget)); i++ {
_, err := db.Get(context.Background(), storage.ModeGetRequest, addrs[i])
if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("got error %v, want %v", err, storage.ErrNotFound)
}
}
})
// last synced chunk should not be removed
t.Run("get most recent synced chunk", func(t *testing.T) {
_, err := db.Get(context.Background(), storage.ModeGetRequest, addrs[len(addrs)-1])
if err != nil {
t.Fatal(err)
}
})
}
// TestDB_ReserveGC_AllWithinRadius tests that when all chunks fall within
// batch radius, none get collected.
func TestDB_ReserveGC_AllWithinRadius(t *testing.T) {
chunkCount := 150
var closed chan struct{}
testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
select {
case testHookCollectGarbageChan <- collectedCount:
case <-closed:
}
}))
var (
batchIDs [][]byte
unreserveCalled bool
mtx sync.Mutex
)
unres := func(f postage.UnreserveIteratorFn) error {
mtx.Lock()
defer mtx.Unlock()
unreserveCalled = true
for i := 0; i < len(batchIDs); i++ {
// pop an element from batchIDs, call the Unreserve
item := batchIDs[i]
// here we mock the behavior of the batchstore
// that would call the localstore back with the
// batch IDs and the radiuses from the FIFO queue
stop, err := f(item, 4)
if err != nil {
return err
}
if stop {
return nil
}
}
return nil
}
db := newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 151,
UnreserveFunc: unres,
})
closed = db.close
addrs := make([]swarm.Address, 0)
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
mtx.Lock()
addrs = append(addrs, ch.Address())
batchIDs = append(batchIDs, ch.Stamp().BatchID())
mtx.Unlock()
}
select {
case <-testHookCollectGarbageChan:
t.Fatal("gc ran but shouldnt have")
case <-time.After(1 * time.Second):
}
t.Run("pull index count", newItemsCountTest(db.pullIndex, chunkCount))
t.Run("postage chunks index count", newItemsCountTest(db.postageChunksIndex, chunkCount))
t.Run("postage radius index count", newItemsCountTest(db.postageRadiusIndex, 0))
t.Run("gc index count", newItemsCountTest(db.gcIndex, 0))
t.Run("gc size", newIndexGCSizeTest(db))
t.Run("reserve size", reserveSizeTest(db, 150, 2))
t.Run("all chunks should be accessible", func(t *testing.T) {
for _, a := range addrs {
_, err := db.Get(context.Background(), storage.ModeGetRequest, a)
if err != nil {
t.Errorf("got error %v, want none", err)
}
}
})
mtx.Lock()
defer mtx.Unlock()
if unreserveCalled {
t.Fatal("unreserveCalled but should not have")
}
}
// TestDB_ReserveGC_Unreserve tests that after calling UnreserveBatch
// with a certain radius change, the correct chunks get put into the
// GC index and eventually get garbage collected.
func TestDB_ReserveGC_Unreserve(t *testing.T) {
chunkCount := 100
var closed chan struct{}
testHookCollectGarbageChan := make(chan uint64)
testHookEvictChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
select {
case testHookCollectGarbageChan <- collectedCount:
case <-closed:
}
}))
t.Cleanup(setTestHookEviction(func(collectedCount uint64) {
select {
case testHookEvictChan <- collectedCount:
case <-closed:
}
}))
var (
mtx sync.Mutex
batchIDs [][]byte
addrs []swarm.Address
)
unres := func(f postage.UnreserveIteratorFn) error {
mtx.Lock()
defer mtx.Unlock()
for i := 0; i < len(batchIDs); i++ {
// pop an element from batchIDs, call the Unreserve
item := batchIDs[i]
// here we mock the behavior of the batchstore
// that would call the localstore back with the
// batch IDs and the radiuses from the FIFO queue
stop, err := f(item, 2)
if err != nil {
return err
}
if stop {
return nil
}
stop, err = f(item, 4)
if err != nil {
return err
}
if stop {
return nil
}
}
batchIDs = nil
return nil
}
db := newTestDB(t, &Options{
Capacity: 100,
// once reaching 150 in the reserve, we will evict
// half the size of the cache from the reserve, so 50 chunks
ReserveCapacity: 90,
UnreserveFunc: unres,
})
closed = db.close
// put chunksCount chunks within radius. this
// will cause reserve eviction of 10 chunks into
// the cache. gc of the cache is still not triggered
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
mtx.Lock()
batchIDs = append(batchIDs, ch.Stamp().BatchID())
addrs = append(addrs, ch.Address())
mtx.Unlock()
}
t.Run("reserve size", reserveSizeTest(db, uint64(chunkCount), 2))
err := db.SetReserveSize(uint64(chunkCount))
if err != nil {
t.Fatal(err)
}
var evicted uint64
for {
select {
case c := <-testHookEvictChan:
evicted += c
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
if evicted == 10 {
break
}
}
// insert another 90, this will trigger gc
for i := 0; i < 90; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
mtx.Lock()
batchIDs = append(batchIDs, ch.Stamp().BatchID())
addrs = append(addrs, ch.Address())
mtx.Unlock()
}
t.Run("reserve size", reserveSizeTest(db, 180, 2))
err = db.SetReserveSize(uint64(180))
if err != nil {
t.Fatal(err)
}
evicted = 0
for {
select {
case c := <-testHookEvictChan:
evicted += c
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
if evicted == 90 {
break
}
}
gcTarget := db.gcTarget()
for {
select {
case <-testHookCollectGarbageChan:
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
gcSize, err := db.gcSize.Get()
if err != nil {
t.Fatal(err)
}
if gcSize == gcTarget {
break
}
}
// pullIndex count should be equal to the reserve
t.Run("pull index count", newItemsCountTest(db.pullIndex, 90))
t.Run("postage chunks index count", newItemsCountTest(db.postageChunksIndex, chunkCount+90-10))
// postageRadiusIndex gets removed only when the batches are called with evict on MaxPO+1
// therefore, the expected index count here is larger than one would expect.
t.Run("postage radius index count", newItemsCountTest(db.postageRadiusIndex, chunkCount))
t.Run("gc index count", newItemsCountTest(db.gcIndex, 90))
t.Run("gc size", newIndexGCSizeTest(db))
t.Run("reserve size", reserveSizeTest(db, 90, 2))
t.Run("first ten unreserved chunks should not be accessible", func(t *testing.T) {
for _, a := range addrs[:10] {
_, err := db.Get(context.Background(), storage.ModeGetRequest, a)
if err == nil {
t.Error("got no error, want NotFound")
}
}
})
t.Run("the rest should be accessible", func(t *testing.T) {
for _, a := range addrs[10:] {
_, err := db.Get(context.Background(), storage.ModeGetRequest, a)
if err != nil {
t.Errorf("got error %v but want none", err)
}
}
})
}
// TestDB_ReserveGC_EvictMaxPO tests that when unreserving a batch at
// swarm.MaxPO+1 results in the correct behaviour.
func TestDB_ReserveGC_EvictMaxPO(t *testing.T) {
var (
mtx sync.Mutex
batchIDs [][]byte
addrs []swarm.Address
chunkCount = 100
testHookCollectGarbageChan = make(chan uint64)
testHookEvictChan = make(chan uint64)
closed chan struct{}
)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
if collectedCount == 0 {
return
}
select {
case testHookCollectGarbageChan <- collectedCount:
case <-closed:
}
}))
t.Cleanup(setTestHookEviction(func(collectedCount uint64) {
if collectedCount == 0 {
return
}
select {
case testHookEvictChan <- collectedCount:
case <-closed:
}
}))
unres := func(f postage.UnreserveIteratorFn) error {
mtx.Lock()
defer mtx.Unlock()
i := 0
defer func() { batchIDs = batchIDs[i:] }()
for i = 0; i < len(batchIDs); i++ {
// pop an element from batchIDs, call the Unreserve
item := batchIDs[i]
// here we mock the behavior of the batchstore
// that would call the localstore back with the
// batch IDs and the radiuses from the FIFO queue
stop, err := f(item, 2)
if err != nil {
return err
}
if stop {
return nil
}
stop, err = f(item, swarm.MaxPO+1)
if err != nil {
return err
}
if stop {
return nil
}
}
return nil
}
db := newTestDB(t, &Options{
Capacity: 100,
// once reaching 100 in the reserve, we will evict
// half the size of the cache from the reserve, so 50 chunks
ReserveCapacity: 90,
UnreserveFunc: unres,
})
closed = db.close
// put the first chunkCount chunks within radius
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
mtx.Lock()
batchIDs = append(batchIDs, ch.Stamp().BatchID())
addrs = append(addrs, ch.Address())
mtx.Unlock()
}
t.Run("reserve size", reserveSizeTest(db, 100, 2))
err := db.SetReserveSize(uint64(100))
if err != nil {
t.Fatal(err)
}
var evicted uint64
for {
select {
case c := <-testHookEvictChan:
evicted += c
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
if evicted == 10 {
break
}
}
// this is zero because we call eviction with max PO on the first 10 batches
// but the next 90 batches were not called with unreserve yet. this means that
// although the next 90 chunks exist in the store, their according batch radius
// still isn't persisted, since the localstore still is not aware of their
// batch radiuses. the same goes for the check after the gc actually evicts the
// ten chunks out of the cache (we still expect a zero for postage radius for the
// same reason)
t.Run("postage radius index count", newItemsCountTest(db.postageRadiusIndex, 0))
for i := 0; i < 90; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
mtx.Lock()
batchIDs = append(batchIDs, ch.Stamp().BatchID())
addrs = append(addrs, ch.Address())
mtx.Unlock()
}
t.Run("reserve size", reserveSizeTest(db, 180, 2))
err = db.SetReserveSize(uint64(180))
if err != nil {
t.Fatal(err)
}
evicted = 0
for {
select {
case c := <-testHookEvictChan:
evicted += c
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
if evicted == 90 {
break
}
}
gcTarget := db.gcTarget()
for {
select {
case <-testHookCollectGarbageChan:
case <-time.After(10 * time.Second):
t.Fatal("collect garbage timeout")
}
gcSize, err := db.gcSize.Get()
if err != nil {
t.Fatal(err)
}
if gcSize == gcTarget {
break
}
}
t.Run("pull index count", newItemsCountTest(db.pullIndex, 90))
t.Run("postage chunks index count", newItemsCountTest(db.postageChunksIndex, chunkCount+90-10))
t.Run("postage radius index count", newItemsCountTest(db.postageRadiusIndex, 0))
t.Run("gc index count", newItemsCountTest(db.gcIndex, 90))
t.Run("gc size", newIndexGCSizeTest(db))
t.Run("reserve size", reserveSizeTest(db, 90, 0))
t.Run("first ten unreserved chunks should not be accessible", func(t *testing.T) {
for _, a := range addrs[:10] {
_, err := db.Get(context.Background(), storage.ModeGetRequest, a)
if err == nil {
t.Error("got no error, want NotFound")
}
}
})
t.Run("the rest should be accessible", func(t *testing.T) {
for _, a := range addrs[10:] {
_, err := db.Get(context.Background(), storage.ModeGetRequest, a)
if err != nil {
t.Errorf("got error %v but want none", err)
}
}
})
}
func TestReserveSize(t *testing.T) {
var (
chunkCount = 10
)
t.Run("variadic put sync", func(t *testing.T) {
var (
db = newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 100,
})
chs []swarm.Chunk
)
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
chs = append(chs, ch)
}
_, err := db.Put(context.Background(), storage.ModePutSync, chs...)
if err != nil {
t.Fatal(err)
}
t.Run("reserve size", reserveSizeTest(db, 10, 0))
})
t.Run("variadic put upload then set sync", func(t *testing.T) {
var (
db = newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 100,
})
chs []swarm.Chunk
addrs []swarm.Address
)
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
chs = append(chs, ch)
addrs = append(addrs, ch.Address())
}
_, err := db.Put(context.Background(), storage.ModePutUpload, chs...)
if err != nil {
t.Fatal(err)
}
t.Run("reserve size", reserveSizeTest(db, 0, 0))
err = db.Set(context.Background(), storage.ModeSetSync, addrs...)
if err != nil {
t.Fatal(err)
}
t.Run("reserve size", reserveSizeTest(db, 0, 0))
})
t.Run("sequencial put sync", func(t *testing.T) {
var (
db = newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 100,
})
)
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
}
t.Run("reserve size", reserveSizeTest(db, 10, 0))
})
t.Run("sequencial put request", func(t *testing.T) {
t.Cleanup(setWithinRadiusFunc(func(*DB, shed.Item) bool { return true }))
var (
db = newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 100,
})
chs []swarm.Chunk
)
for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2).WithBatch(2, 3, 2, false)
chs = append(chs, ch)
_, err := db.Put(context.Background(), storage.ModePutRequest, ch)
if err != nil {
t.Fatal(err)
}
}
t.Run("reserve size", reserveSizeTest(db, 10, 0))
})
}
func TestComputeReserveSize(t *testing.T) {
t.Parallel()
const chunkCountPerPO = 10
const maxPO = 10
var chs []swarm.Chunk
db := newTestDB(t, &Options{
Capacity: 1000,
ReserveCapacity: 1000,
})
for po := 0; po < maxPO; po++ {
for i := 0; i < chunkCountPerPO; i++ {
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), po).WithBatch(0, 3, 2, false)
chs = append(chs, ch)
}
}
_, err := db.Put(context.Background(), storage.ModePutSync, chs...)
if err != nil {
t.Fatal(err)
}
t.Run("reserve size", reserveSizeTest(db, chunkCountPerPO*maxPO, 0))
for po := 0; po < maxPO; po++ {
got, err := db.ComputeReserveSize(uint8(po))
if err != nil {
t.Fatal(err)
}
want := (maxPO - po) * chunkCountPerPO
if got != uint64(want) {
t.Fatalf("compute reserve size mismatch, po %d, got %d, want %d", po, got, want)
}
}
}
func TestDB_ReserveGC_BatchedUnreserve(t *testing.T) {
chunkCount := 100
var closed chan struct{}
testHookEvictChan := make(chan uint64)
t.Cleanup(setTestHookEviction(func(collectedCount uint64) {
select {
case testHookEvictChan <- collectedCount:
case <-closed:
}
}))
testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
if collectedCount == 0 {
return
}
select {
case testHookCollectGarbageChan <- collectedCount:
case <-closed:
}
}))
stamp := postagetesting.MustNewStamp()
unres := func(f postage.UnreserveIteratorFn) error {
_, err := f(stamp.BatchID(), 3)
if err != nil {
return err
}
return nil
}
// override the batchSize for the test and reset it when done
oldUnpinBatchSize := unpinBatchSize
unpinBatchSize = 10
defer func() { unpinBatchSize = oldUnpinBatchSize }()
db := newTestDB(t, &Options{
Capacity: 100,
ReserveCapacity: 50,
UnreserveFunc: unres,
})
closed = db.close
// generate chunks with the same batch and depth to trigger larger eviction
genChunk := func() swarm.Chunk {
newStamp := postagetesting.MustNewBatchStamp(stamp.BatchID())
ch := generateTestRandomChunkAt(swarm.NewAddress(db.baseKey), 2)
return ch.WithBatch(2, 3, 2, false).WithStamp(newStamp)
}
for i := 0; i < chunkCount; i++ {
ch := genChunk()
_, err := db.Put(context.Background(), storage.ModePutSync, ch)
if err != nil {
t.Fatal(err)
}
}
t.Run("reserve size", reserveSizeTest(db, 0, 100))
err := db.SetReserveSize(100)
if err != nil {
t.Fatal(err)
}
select {
case <-testHookEvictChan:
case <-time.After(10 * time.Second):
t.Fatal("eviction timeout")
}
select {
case <-testHookCollectGarbageChan:
case <-time.After(10 * time.Second):
t.Fatal("gc timeout")
}
t.Run("reserve size", reserveSizeTest(db, 0, 0))
t.Run("pull index count", newItemsCountTest(db.pullIndex, 0))
t.Run("postage chunks index count", newItemsCountTest(db.postageChunksIndex, 90))
// we use the same batch for all chunks
t.Run("postage radius index count", newItemsCountTest(db.postageRadiusIndex, 1))
// all chunks would land into the gcIndex
t.Run("gc index count", newItemsCountTest(db.gcIndex, 90))
t.Run("gc size", newIndexGCSizeTest(db))
}