bitswap: add better tests around wantlist clearing
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Sep 2, 2016 at 15:28 UTC
feb653b4434d5b2911ec32e7d8796db161caaa96
1 file changed
+73
-5
exchange/bitswap/bitswap_test.go
+73
-5
@@ -334,7 +334,6 @@ func TestDoubleGet(t *testing.T) {
334
blocks := bg.Blocks(1)
335
336
ctx1, cancel1 := context.WithCancel(context.Background())
337
-
337
blkch1, err := instances[1].Exchange.GetBlocks(ctx1, []key.Key{blocks[0].Key()})
338
if err != nil {
339
t.Fatal(err)
@@ -362,11 +361,15 @@ func TestDoubleGet(t *testing.T) {
361
t.Fatal(err)
362
}
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")
372
}
369
- t.Log(blk)
373
374
for _, inst := range instances {
375
err := inst.Exchange.Close()
@@ -375,3 +378,68 @@ func TestDoubleGet(t *testing.T) {
378
}
379
}
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
+}