@@ -19,14 +19,16 @@ import (
1919 blocks "github.com/ipfs/go-block-format"
2020 cid "github.com/ipfs/go-cid"
2121 detectrace "github.com/ipfs/go-detect-race"
22- blocksutil "github.com/ipfs/go-ipfs-blocksutil"
2322 delay "github.com/ipfs/go-ipfs-delay"
2423 ipld "github.com/ipfs/go-ipld-format"
24+ "github.com/ipfs/go-test/random"
2525 tu "github.com/libp2p/go-libp2p-testing/etc"
2626 p2ptestutil "github.com/libp2p/go-libp2p-testing/netutil"
2727 peer "github.com/libp2p/go-libp2p/core/peer"
2828)
2929
30+ const blockSize = 4
31+
3032func isCI () bool {
3133 // https://github.blog/changelog/2020-04-15-github-actions-sets-the-ci-environment-variable-to-true/
3234 return os .Getenv ("CI" ) != ""
@@ -52,9 +54,7 @@ func TestClose(t *testing.T) {
5254 vnet := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
5355 ig := testinstance .NewTestInstanceGenerator (vnet , nil , nil )
5456 defer ig .Close ()
55- bgen := blocksutil .NewBlockGenerator ()
56-
57- block := bgen .Next ()
57+ block := random .BlocksOfSize (1 , blockSize )[0 ]
5858 bitswap := ig .Next ()
5959
6060 bitswap .Exchange .Close ()
@@ -187,7 +187,6 @@ func TestUnwantedBlockNotAdded(t *testing.T) {
187187func TestPendingBlockAdded (t * testing.T ) {
188188 ctx := context .Background ()
189189 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
190- bg := blocksutil .NewBlockGenerator ()
191190 sessionBroadcastWantCapacity := 4
192191
193192 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
@@ -202,7 +201,7 @@ func TestPendingBlockAdded(t *testing.T) {
202201 // Request enough blocks to exceed the session's broadcast want list
203202 // capacity (by one block). The session will put the remaining block
204203 // into the "tofetch" queue
205- blks := bg . Blocks (sessionBroadcastWantCapacity + 1 )
204+ blks := random . BlocksOfSize (sessionBroadcastWantCapacity + 1 , blockSize )
206205 ks := make ([]cid.Cid , 0 , len (blks ))
207206 for _ , b := range blks {
208207 ks = append (ks , b .Cid ())
@@ -285,10 +284,9 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
285284 bitswap .MaxOutstandingBytesPerPeer (1 << 20 ),
286285 })
287286 defer ig .Close ()
288- bg := blocksutil .NewBlockGenerator ()
289287
290288 instances := ig .Instances (numInstances )
291- blocks := bg . Blocks (numBlocks )
289+ blocks := random . BlocksOfSize (numBlocks , blockSize )
292290
293291 t .Log ("Give the blocks to the first instance" )
294292
@@ -338,7 +336,6 @@ func TestSendToWantingPeer(t *testing.T) {
338336 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
339337 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
340338 defer ig .Close ()
341- bg := blocksutil .NewBlockGenerator ()
342339
343340 peers := ig .Instances (2 )
344341 peerA := peers [0 ]
@@ -349,7 +346,7 @@ func TestSendToWantingPeer(t *testing.T) {
349346
350347 waitTime := time .Second * 5
351348
352- alpha := bg . Next ()
349+ alpha := random . BlocksOfSize ( 1 , blockSize )[ 0 ]
353350 // peerA requests and waits for block alpha
354351 ctx , cancel := context .WithTimeout (context .Background (), waitTime )
355352 defer cancel ()
@@ -409,12 +406,11 @@ func TestBasicBitswap(t *testing.T) {
409406 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
410407 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
411408 defer ig .Close ()
412- bg := blocksutil .NewBlockGenerator ()
413409
414410 t .Log ("Test a one node trying to get one block from another" )
415411
416412 instances := ig .Instances (3 )
417- blocks := bg . Blocks ( 1 )
413+ blocks := random . BlocksOfSize ( 1 , blockSize )
418414
419415 // First peer has block
420416 addBlock (t , context .Background (), instances [0 ], blocks [0 ])
@@ -481,12 +477,11 @@ func TestDoubleGet(t *testing.T) {
481477 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
482478 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
483479 defer ig .Close ()
484- bg := blocksutil .NewBlockGenerator ()
485480
486481 t .Log ("Test a one node trying to get one block from another" )
487482
488483 instances := ig .Instances (2 )
489- blocks := bg . Blocks ( 1 )
484+ blocks := random . BlocksOfSize ( 1 , blockSize )
490485
491486 // NOTE: A race condition can happen here where these GetBlocks requests go
492487 // through before the peers even get connected. This is okay, bitswap
@@ -546,12 +541,11 @@ func TestWantlistCleanup(t *testing.T) {
546541 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
547542 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
548543 defer ig .Close ()
549- bg := blocksutil .NewBlockGenerator ()
550544
551545 instances := ig .Instances (2 )
552546 instance := instances [0 ]
553547 bswap := instance .Exchange
554- blocks := bg . Blocks (20 )
548+ blocks := random . BlocksOfSize (20 , blockSize )
555549
556550 var keys []cid.Cid
557551 for _ , b := range blocks {
@@ -668,12 +662,11 @@ func TestBitswapLedgerOneWay(t *testing.T) {
668662 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
669663 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
670664 defer ig .Close ()
671- bg := blocksutil .NewBlockGenerator ()
672665
673666 t .Log ("Test ledgers match when one peer sends block to another" )
674667
675668 instances := ig .Instances (2 )
676- blocks := bg . Blocks ( 1 )
669+ blocks := random . BlocksOfSize ( 1 , blockSize )
677670 addBlock (t , context .Background (), instances [0 ], blocks [0 ])
678671
679672 ctx , cancel := context .WithTimeout (context .Background (), time .Second * 5 )
@@ -693,12 +686,12 @@ func TestBitswapLedgerOneWay(t *testing.T) {
693686 }
694687
695688 // check that receipts have intended values
696- ratest := newReceipt (1 , 0 , 1 )
689+ ratest := newReceipt (blockSize , 0 , 1 )
697690 err = assertLedgerEqual (ratest , ra )
698691 if err != nil {
699692 t .Fatal (err )
700693 }
701- rbtest := newReceipt (0 , 1 , 1 )
694+ rbtest := newReceipt (0 , blockSize , 1 )
702695 err = assertLedgerEqual (rbtest , rb )
703696 if err != nil {
704697 t .Fatal (err )
@@ -717,12 +710,11 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
717710 net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
718711 ig := testinstance .NewTestInstanceGenerator (net , nil , nil )
719712 defer ig .Close ()
720- bg := blocksutil .NewBlockGenerator ()
721713
722714 t .Log ("Test ledgers match when two peers send one block to each other" )
723715
724716 instances := ig .Instances (2 )
725- blocks := bg . Blocks ( 2 )
717+ blocks := random . BlocksOfSize ( 2 , blockSize )
726718 addBlock (t , context .Background (), instances [0 ], blocks [0 ])
727719 addBlock (t , context .Background (), instances [1 ], blocks [1 ])
728720
@@ -750,7 +742,7 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
750742 }
751743
752744 // check that receipts have intended values
753- rtest := newReceipt (1 , 1 , 2 )
745+ rtest := newReceipt (blockSize , blockSize , 2 )
754746 err = assertLedgerEqual (rtest , ra )
755747 if err != nil {
756748 t .Fatal (err )
0 commit comments