@@ -334,7 +334,6 @@ func TestDoubleGet(t *testing.T) {
334
334
blocks := bg .Blocks (1 )
335
335
336
336
ctx1 , cancel1 := context .WithCancel (context .Background ())
337
-
338
337
blkch1 , err := instances [1 ].Exchange .GetBlocks (ctx1 , []key.Key {blocks [0 ].Key ()})
339
338
if err != nil {
340
339
t .Fatal (err )
@@ -362,11 +361,15 @@ func TestDoubleGet(t *testing.T) {
362
361
t .Fatal (err )
363
362
}
364
363
365
- blk , ok := <- blkch2
366
- if ! ok {
367
- t .Fatal ("expected to get the block here" )
364
+ select {
365
+ case blk , ok := <- blkch2 :
366
+ if ! ok {
367
+ t .Fatal ("expected to get the block here" )
368
+ }
369
+ t .Log (blk )
370
+ case <- time .After (time .Second * 5 ):
371
+ t .Fatal ("timed out waiting on block" )
368
372
}
369
- t .Log (blk )
370
373
371
374
for _ , inst := range instances {
372
375
err := inst .Exchange .Close ()
@@ -375,3 +378,68 @@ func TestDoubleGet(t *testing.T) {
375
378
}
376
379
}
377
380
}
381
+
382
+ func TestWantlistCleanup (t * testing.T ) {
383
+ net := tn .VirtualNetwork (mockrouting .NewServer (), delay .Fixed (kNetworkDelay ))
384
+ sg := NewTestSessionGenerator (net )
385
+ defer sg .Close ()
386
+ bg := blocksutil .NewBlockGenerator ()
387
+
388
+ instances := sg .Instances (1 )[0 ]
389
+ bswap := instances .Exchange
390
+ blocks := bg .Blocks (20 )
391
+
392
+ var keys []key.Key
393
+ for _ , b := range blocks {
394
+ keys = append (keys , b .Key ())
395
+ }
396
+
397
+ ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond * 50 )
398
+ defer cancel ()
399
+ _ , err := bswap .GetBlock (ctx , keys [0 ])
400
+ if err != context .DeadlineExceeded {
401
+ t .Fatal ("shouldnt have fetched any blocks" )
402
+ }
403
+
404
+ time .Sleep (time .Millisecond * 50 )
405
+
406
+ if len (bswap .GetWantlist ()) > 0 {
407
+ t .Fatal ("should not have anyting in wantlist" )
408
+ }
409
+
410
+ ctx , cancel = context .WithTimeout (context .Background (), time .Millisecond * 50 )
411
+ defer cancel ()
412
+ _ , err = bswap .GetBlocks (ctx , keys [:10 ])
413
+ if err != nil {
414
+ t .Fatal (err )
415
+ }
416
+
417
+ <- ctx .Done ()
418
+ time .Sleep (time .Millisecond * 50 )
419
+
420
+ if len (bswap .GetWantlist ()) > 0 {
421
+ t .Fatal ("should not have anyting in wantlist" )
422
+ }
423
+
424
+ _ , err = bswap .GetBlocks (context .Background (), keys [:1 ])
425
+ if err != nil {
426
+ t .Fatal (err )
427
+ }
428
+
429
+ ctx , cancel = context .WithCancel (context .Background ())
430
+ _ , err = bswap .GetBlocks (ctx , keys [10 :])
431
+ if err != nil {
432
+ t .Fatal (err )
433
+ }
434
+
435
+ time .Sleep (time .Millisecond * 50 )
436
+ if len (bswap .GetWantlist ()) != 11 {
437
+ t .Fatal ("should have 11 keys in wantlist" )
438
+ }
439
+
440
+ cancel ()
441
+ time .Sleep (time .Millisecond * 50 )
442
+ if ! (len (bswap .GetWantlist ()) == 1 && bswap .GetWantlist ()[0 ] == keys [0 ]) {
443
+ t .Fatal ("should only have keys[0] in wantlist" )
444
+ }
445
+ }
0 commit comments