@@ -346,3 +346,198 @@ func TestPeersExhausted(t *testing.T) {
346
346
t .Fatal ("Wrong keys" )
347
347
}
348
348
}
349
+
350
+ func TestConsecutiveDontHaveLimit (t * testing.T ) {
351
+ cids := testutil .GenerateCids (peerDontHaveLimit + 10 )
352
+ p := testutil .GeneratePeers (1 )[0 ]
353
+ sid := uint64 (1 )
354
+ pm := newMockPeerManager ()
355
+ bpm := bsbpm .New ()
356
+ onSend := func (peer.ID , []cid.Cid , []cid.Cid ) {}
357
+ onPeersExhausted := func ([]cid.Cid ) {}
358
+ spm := newSessionWantSender (context .Background (), sid , pm , bpm , onSend , onPeersExhausted )
359
+
360
+ go spm .Run ()
361
+
362
+ // Add all cids as wants
363
+ spm .Add (cids )
364
+
365
+ // Receive a HAVE from peer (adds it to the session)
366
+ bpm .ReceiveFrom (p , cids [:1 ], []cid.Cid {})
367
+ spm .Update (p , []cid.Cid {}, cids [:1 ], []cid.Cid {}, true )
368
+
369
+ // Wait for processing to complete
370
+ time .Sleep (5 * time .Millisecond )
371
+
372
+ // Peer should be available
373
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
374
+ t .Fatal ("Expected peer to be available" )
375
+ }
376
+
377
+ // Receive DONT_HAVEs from peer that do not exceed limit
378
+ for _ , c := range cids [1 :peerDontHaveLimit ] {
379
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
380
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
381
+ }
382
+
383
+ // Wait for processing to complete
384
+ time .Sleep (5 * time .Millisecond )
385
+
386
+ // Peer should be available
387
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
388
+ t .Fatal ("Expected peer to be available" )
389
+ }
390
+
391
+ // Receive DONT_HAVEs from peer that exceed limit
392
+ for _ , c := range cids [peerDontHaveLimit :] {
393
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
394
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
395
+ }
396
+
397
+ // Wait for processing to complete
398
+ time .Sleep (5 * time .Millisecond )
399
+
400
+ // Session should remove peer
401
+ if avail , _ := spm .isAvailable (p ); avail {
402
+ t .Fatal ("Expected peer not to be available" )
403
+ }
404
+ }
405
+
406
+ func TestConsecutiveDontHaveLimitInterrupted (t * testing.T ) {
407
+ cids := testutil .GenerateCids (peerDontHaveLimit + 10 )
408
+ p := testutil .GeneratePeers (1 )[0 ]
409
+ sid := uint64 (1 )
410
+ pm := newMockPeerManager ()
411
+ bpm := bsbpm .New ()
412
+ onSend := func (peer.ID , []cid.Cid , []cid.Cid ) {}
413
+ onPeersExhausted := func ([]cid.Cid ) {}
414
+ spm := newSessionWantSender (context .Background (), sid , pm , bpm , onSend , onPeersExhausted )
415
+
416
+ go spm .Run ()
417
+
418
+ // Add all cids as wants
419
+ spm .Add (cids )
420
+
421
+ // Receive a HAVE from peer (adds it to the session)
422
+ bpm .ReceiveFrom (p , cids [:1 ], []cid.Cid {})
423
+ spm .Update (p , []cid.Cid {}, cids [:1 ], []cid.Cid {}, true )
424
+
425
+ // Wait for processing to complete
426
+ time .Sleep (5 * time .Millisecond )
427
+
428
+ // Peer should be available
429
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
430
+ t .Fatal ("Expected peer to be available" )
431
+ }
432
+
433
+ // Receive DONT_HAVE then HAVE then DONT_HAVE from peer,
434
+ // where consecutive DONT_HAVEs would have exceeded limit
435
+ // (but they are not consecutive)
436
+ for _ , c := range cids [1 :peerDontHaveLimit ] {
437
+ // DONT_HAVEs
438
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
439
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
440
+ }
441
+ for _ , c := range cids [peerDontHaveLimit : peerDontHaveLimit + 1 ] {
442
+ // HAVEs
443
+ bpm .ReceiveFrom (p , []cid.Cid {c }, []cid.Cid {})
444
+ spm .Update (p , []cid.Cid {}, []cid.Cid {c }, []cid.Cid {}, false )
445
+ }
446
+ for _ , c := range cids [peerDontHaveLimit + 1 :] {
447
+ // DONT_HAVEs
448
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
449
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
450
+ }
451
+
452
+ // Wait for processing to complete
453
+ time .Sleep (5 * time .Millisecond )
454
+
455
+ // Peer should be available
456
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
457
+ t .Fatal ("Expected peer to be available" )
458
+ }
459
+ }
460
+
461
+ func TestConsecutiveDontHaveReinstateAfterRemoval (t * testing.T ) {
462
+ cids := testutil .GenerateCids (peerDontHaveLimit + 10 )
463
+ p := testutil .GeneratePeers (1 )[0 ]
464
+ sid := uint64 (1 )
465
+ pm := newMockPeerManager ()
466
+ bpm := bsbpm .New ()
467
+ onSend := func (peer.ID , []cid.Cid , []cid.Cid ) {}
468
+ onPeersExhausted := func ([]cid.Cid ) {}
469
+ spm := newSessionWantSender (context .Background (), sid , pm , bpm , onSend , onPeersExhausted )
470
+
471
+ go spm .Run ()
472
+
473
+ // Add all cids as wants
474
+ spm .Add (cids )
475
+
476
+ // Receive a HAVE from peer (adds it to the session)
477
+ bpm .ReceiveFrom (p , cids [:1 ], []cid.Cid {})
478
+ spm .Update (p , []cid.Cid {}, cids [:1 ], []cid.Cid {}, true )
479
+
480
+ // Wait for processing to complete
481
+ time .Sleep (5 * time .Millisecond )
482
+
483
+ // Peer should be available
484
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
485
+ t .Fatal ("Expected peer to be available" )
486
+ }
487
+
488
+ // Receive DONT_HAVEs from peer that exceed limit
489
+ for _ , c := range cids [1 : peerDontHaveLimit + 2 ] {
490
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
491
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
492
+ }
493
+
494
+ // Wait for processing to complete
495
+ time .Sleep (5 * time .Millisecond )
496
+
497
+ // Session should remove peer
498
+ if avail , _ := spm .isAvailable (p ); avail {
499
+ t .Fatal ("Expected peer not to be available" )
500
+ }
501
+
502
+ // Receive a HAVE from peer (adds it back into the session)
503
+ bpm .ReceiveFrom (p , cids [:1 ], []cid.Cid {})
504
+ spm .Update (p , []cid.Cid {}, cids [:1 ], []cid.Cid {}, true )
505
+
506
+ // Wait for processing to complete
507
+ time .Sleep (5 * time .Millisecond )
508
+
509
+ // Peer should be available
510
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
511
+ t .Fatal ("Expected peer to be available" )
512
+ }
513
+
514
+ cids2 := testutil .GenerateCids (peerDontHaveLimit + 10 )
515
+
516
+ // Receive DONT_HAVEs from peer that don't exceed limit
517
+ for _ , c := range cids2 [1 :peerDontHaveLimit ] {
518
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
519
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
520
+ }
521
+
522
+ // Wait for processing to complete
523
+ time .Sleep (5 * time .Millisecond )
524
+
525
+ // Peer should be available
526
+ if avail , ok := spm .isAvailable (p ); ! ok || ! avail {
527
+ t .Fatal ("Expected peer to be available" )
528
+ }
529
+
530
+ // Receive DONT_HAVEs from peer that exceed limit
531
+ for _ , c := range cids2 [peerDontHaveLimit :] {
532
+ bpm .ReceiveFrom (p , []cid.Cid {}, []cid.Cid {c })
533
+ spm .Update (p , []cid.Cid {}, []cid.Cid {}, []cid.Cid {c }, false )
534
+ }
535
+
536
+ // Wait for processing to complete
537
+ time .Sleep (5 * time .Millisecond )
538
+
539
+ // Session should remove peer
540
+ if avail , _ := spm .isAvailable (p ); avail {
541
+ t .Fatal ("Expected peer not to be available" )
542
+ }
543
+ }
0 commit comments