sessionwantsender_test.go 18 KB
Newer Older
dirkmc's avatar
dirkmc committed
1 2 3 4 5 6 7 8
package session

import (
	"context"
	"sync"
	"testing"
	"time"

9 10 11
	bsbpm "github.com/ipfs/go-bitswap/internal/blockpresencemanager"
	bspm "github.com/ipfs/go-bitswap/internal/peermanager"
	"github.com/ipfs/go-bitswap/internal/testutil"
dirkmc's avatar
dirkmc committed
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
	cid "github.com/ipfs/go-cid"
	peer "github.com/libp2p/go-libp2p-core/peer"
)

type sentWants struct {
	p          peer.ID
	wantHaves  *cid.Set
	wantBlocks *cid.Set
}

type mockPeerManager struct {
	peerSessions sync.Map
	peerSends    sync.Map
}

func newMockPeerManager() *mockPeerManager {
	return &mockPeerManager{}
}

func (pm *mockPeerManager) RegisterSession(p peer.ID, sess bspm.Session) bool {
	pm.peerSessions.Store(p, sess)
	return true
}

func (pm *mockPeerManager) UnregisterSession(sesid uint64) {
}

func (pm *mockPeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) {
	swi, _ := pm.peerSends.LoadOrStore(p, sentWants{p, cid.NewSet(), cid.NewSet()})
	sw := swi.(sentWants)
	for _, c := range wantBlocks {
		sw.wantBlocks.Add(c)
	}
	for _, c := range wantHaves {
		if !sw.wantBlocks.Has(c) {
			sw.wantHaves.Add(c)
		}
	}
}

func (pm *mockPeerManager) waitNextWants() map[peer.ID]sentWants {
	time.Sleep(5 * time.Millisecond)
	nw := make(map[peer.ID]sentWants)
	pm.peerSends.Range(func(k, v interface{}) bool {
		nw[k.(peer.ID)] = v.(sentWants)
		return true
	})
	return nw
}

func (pm *mockPeerManager) clearWants() {
	pm.peerSends.Range(func(k, v interface{}) bool {
		pm.peerSends.Delete(k)
		return true
	})
}

func TestSendWants(t *testing.T) {
	cids := testutil.GenerateCids(4)
	peers := testutil.GeneratePeers(1)
	peerA := peers[0]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
75
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
76 77 78
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
79
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
80 81 82 83 84 85 86

	go spm.Run()

	// add cid0, cid1
	blkCids0 := cids[0:2]
	spm.Add(blkCids0)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
87
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
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

	// Wait for processing to complete
	peerSends := pm.waitNextWants()

	// Should have sent
	// peerA: want-block cid0, cid1
	sw, ok := peerSends[peerA]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocks.Keys(), blkCids0) {
		t.Fatal("Wrong keys")
	}
	if sw.wantHaves.Len() > 0 {
		t.Fatal("Expecting no want-haves")
	}
}

func TestSendsWantBlockToOnePeerOnly(t *testing.T) {
	cids := testutil.GenerateCids(4)
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
113
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
114 115 116
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
117
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
118 119 120 121 122 123 124

	go spm.Run()

	// add cid0, cid1
	blkCids0 := cids[0:2]
	spm.Add(blkCids0)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
125
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	// Wait for processing to complete
	peerSends := pm.waitNextWants()

	// Should have sent
	// peerA: want-block cid0, cid1
	sw, ok := peerSends[peerA]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocks.Keys(), blkCids0) {
		t.Fatal("Wrong keys")
	}

	// Clear wants (makes keeping track of what's been sent easier)
	pm.clearWants()

	// peerB: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
144
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
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

	// Wait for processing to complete
	peerSends = pm.waitNextWants()

	// Have not received response from peerA, so should not send want-block to
	// peerB. Should have sent
	// peerB: want-have cid0, cid1
	sw, ok = peerSends[peerB]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	if sw.wantBlocks.Len() > 0 {
		t.Fatal("Expecting no want-blocks")
	}
	if !testutil.MatchKeysIgnoreOrder(sw.wantHaves.Keys(), blkCids0) {
		t.Fatal("Wrong keys")
	}
}

func TestReceiveBlock(t *testing.T) {
	cids := testutil.GenerateCids(2)
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
171
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
172 173 174
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
175
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
176 177 178 179 180 181

	go spm.Run()

	// add cid0, cid1
	spm.Add(cids)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
182
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

	// Wait for processing to complete
	peerSends := pm.waitNextWants()

	// Should have sent
	// peerA: want-block cid0, cid1
	sw, ok := peerSends[peerA]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocks.Keys(), cids) {
		t.Fatal("Wrong keys")
	}

	// Clear wants (makes keeping track of what's been sent easier)
	pm.clearWants()

	// peerA: block cid0, DONT_HAVE cid1
	bpm.ReceiveFrom(peerA, []cid.Cid{}, []cid.Cid{cids[1]})
Dirk McCormick's avatar
Dirk McCormick committed
202
	spm.Update(peerA, []cid.Cid{cids[0]}, []cid.Cid{}, []cid.Cid{cids[1]})
dirkmc's avatar
dirkmc committed
203 204
	// peerB: HAVE cid0, cid1
	bpm.ReceiveFrom(peerB, cids, []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
205
	spm.Update(peerB, []cid.Cid{}, cids, []cid.Cid{})
dirkmc's avatar
dirkmc committed
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

	// Wait for processing to complete
	peerSends = pm.waitNextWants()

	// Should have sent
	// peerB: want-block cid1
	// (should not have sent want-block for cid0 because block0 has already
	// been received)
	sw, ok = peerSends[peerB]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	wb := sw.wantBlocks.Keys()
	if len(wb) != 1 || !wb[0].Equals(cids[1]) {
		t.Fatal("Wrong keys", wb)
	}
}

func TestPeerUnavailable(t *testing.T) {
	cids := testutil.GenerateCids(2)
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
231
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
232 233 234
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
235
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
236 237 238 239 240 241

	go spm.Run()

	// add cid0, cid1
	spm.Add(cids)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
242
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

	// Wait for processing to complete
	peerSends := pm.waitNextWants()

	// Should have sent
	// peerA: want-block cid0, cid1
	sw, ok := peerSends[peerA]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocks.Keys(), cids) {
		t.Fatal("Wrong keys")
	}

	// Clear wants (makes keeping track of what's been sent easier)
	pm.clearWants()

	// peerB: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
261
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
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

	// Wait for processing to complete
	peerSends = pm.waitNextWants()

	// Should not have sent anything because want-blocks were already sent to
	// peer A
	sw, ok = peerSends[peerB]
	if ok && sw.wantBlocks.Len() > 0 {
		t.Fatal("Expected no wants sent to peer")
	}

	// peerA becomes unavailable
	spm.SignalAvailability(peerA, false)

	// Wait for processing to complete
	peerSends = pm.waitNextWants()

	// Should now have sent want-block cid0, cid1 to peerB
	sw, ok = peerSends[peerB]
	if !ok {
		t.Fatal("Nothing sent to peer")
	}
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocks.Keys(), cids) {
		t.Fatal("Wrong keys")
	}
}

func TestPeersExhausted(t *testing.T) {
Dirk McCormick's avatar
Dirk McCormick committed
290
	cids := testutil.GenerateCids(3)
dirkmc's avatar
dirkmc committed
291 292 293 294 295
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
296
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
297 298 299 300 301 302 303
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}

	var exhausted []cid.Cid
	onPeersExhausted := func(ks []cid.Cid) {
		exhausted = append(exhausted, ks...)
	}
Dirk McCormick's avatar
Dirk McCormick committed
304
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
305 306 307 308 309 310

	go spm.Run()

	// add cid0, cid1
	spm.Add(cids)

Dirk McCormick's avatar
Dirk McCormick committed
311 312
	// peerA: HAVE cid0
	bpm.ReceiveFrom(peerA, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
313
	// Note: this also registers peer A as being available
Dirk McCormick's avatar
Dirk McCormick committed
314 315 316 317 318
	spm.Update(peerA, []cid.Cid{cids[0]}, []cid.Cid{}, []cid.Cid{})

	// peerA: DONT_HAVE cid1
	bpm.ReceiveFrom(peerA, []cid.Cid{}, []cid.Cid{cids[1]})
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{}, []cid.Cid{cids[1]})
dirkmc's avatar
dirkmc committed
319 320 321

	time.Sleep(5 * time.Millisecond)

Dirk McCormick's avatar
Dirk McCormick committed
322 323 324
	// All available peers (peer A) have sent us a DONT_HAVE for cid1,
	// so expect that onPeersExhausted() will be called with cid1
	if !testutil.MatchKeysIgnoreOrder(exhausted, []cid.Cid{cids[1]}) {
dirkmc's avatar
dirkmc committed
325 326 327 328 329 330
		t.Fatal("Wrong keys")
	}

	// Clear exhausted cids
	exhausted = []cid.Cid{}

Dirk McCormick's avatar
Dirk McCormick committed
331 332 333 334 335 336 337 338
	// peerB: HAVE cid0
	bpm.ReceiveFrom(peerB, []cid.Cid{cids[0]}, []cid.Cid{})
	// Note: this also registers peer B as being available
	spm.Update(peerB, []cid.Cid{cids[0]}, []cid.Cid{}, []cid.Cid{})

	// peerB: DONT_HAVE cid1, cid2
	bpm.ReceiveFrom(peerB, []cid.Cid{}, []cid.Cid{cids[1], cids[2]})
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{}, []cid.Cid{cids[1], cids[2]})
dirkmc's avatar
dirkmc committed
339 340 341 342 343

	// Wait for processing to complete
	pm.waitNextWants()

	// All available peers (peer A and peer B) have sent us a DONT_HAVE
Dirk McCormick's avatar
Dirk McCormick committed
344
	// for cid1, but we already called onPeersExhausted with cid1, so it
dirkmc's avatar
dirkmc committed
345 346 347 348 349
	// should not be called again
	if len(exhausted) > 0 {
		t.Fatal("Wrong keys")
	}

Dirk McCormick's avatar
Dirk McCormick committed
350 351 352
	// peerA: DONT_HAVE cid2
	bpm.ReceiveFrom(peerA, []cid.Cid{}, []cid.Cid{cids[2]})
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{}, []cid.Cid{cids[2]})
dirkmc's avatar
dirkmc committed
353 354 355 356 357

	// Wait for processing to complete
	pm.waitNextWants()

	// All available peers (peer A and peer B) have sent us a DONT_HAVE for
Dirk McCormick's avatar
Dirk McCormick committed
358 359
	// cid2, so expect that onPeersExhausted() will be called with cid2
	if !testutil.MatchKeysIgnoreOrder(exhausted, []cid.Cid{cids[2]}) {
dirkmc's avatar
dirkmc committed
360 361 362
		t.Fatal("Wrong keys")
	}
}
363

364 365 366 367 368 369 370 371 372 373 374
// Tests that when
// - all the peers except one have sent a DONT_HAVE for a CID
// - the remaining peer becomes unavailable
// onPeersExhausted should be sent for that CID
func TestPeersExhaustedLastWaitingPeerUnavailable(t *testing.T) {
	cids := testutil.GenerateCids(2)
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
375
	fpm := newFakeSessionPeerManager()
376 377 378 379 380 381 382
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}

	var exhausted []cid.Cid
	onPeersExhausted := func(ks []cid.Cid) {
		exhausted = append(exhausted, ks...)
	}
Dirk McCormick's avatar
Dirk McCormick committed
383
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
384 385 386 387 388 389 390 391 392

	go spm.Run()

	// add cid0, cid1
	spm.Add(cids)

	// peerA: HAVE cid0
	bpm.ReceiveFrom(peerA, []cid.Cid{cids[0]}, []cid.Cid{})
	// Note: this also registers peer A as being available
Dirk McCormick's avatar
Dirk McCormick committed
393
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
394 395 396
	// peerB: HAVE cid0
	bpm.ReceiveFrom(peerB, []cid.Cid{cids[0]}, []cid.Cid{})
	// Note: this also registers peer B as being available
Dirk McCormick's avatar
Dirk McCormick committed
397
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
398 399 400

	// peerA: DONT_HAVE cid1
	bpm.ReceiveFrom(peerA, []cid.Cid{}, []cid.Cid{cids[1]})
Dirk McCormick's avatar
Dirk McCormick committed
401
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{}, []cid.Cid{cids[0]})
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

	time.Sleep(5 * time.Millisecond)

	// peerB: becomes unavailable
	spm.SignalAvailability(peerB, false)

	time.Sleep(5 * time.Millisecond)

	// All remaining peers (peer A) have sent us a DONT_HAVE for cid1,
	// so expect that onPeersExhausted() will be called with cid1
	if !testutil.MatchKeysIgnoreOrder(exhausted, []cid.Cid{cids[1]}) {
		t.Fatal("Wrong keys")
	}
}

// Tests that when all the peers are removed from the session
// onPeersExhausted should be called with all outstanding CIDs
func TestPeersExhaustedAllPeersUnavailable(t *testing.T) {
	cids := testutil.GenerateCids(3)
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
426
	fpm := newFakeSessionPeerManager()
427 428 429 430 431 432 433
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}

	var exhausted []cid.Cid
	onPeersExhausted := func(ks []cid.Cid) {
		exhausted = append(exhausted, ks...)
	}
Dirk McCormick's avatar
Dirk McCormick committed
434
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
435 436 437 438 439 440 441

	go spm.Run()

	// add cid0, cid1, cid2
	spm.Add(cids)

	// peerA: receive block for cid0 (and register peer A with sessionWantSender)
Dirk McCormick's avatar
Dirk McCormick committed
442
	spm.Update(peerA, []cid.Cid{cids[0]}, []cid.Cid{}, []cid.Cid{})
443 444 445
	// peerB: HAVE cid1
	bpm.ReceiveFrom(peerB, []cid.Cid{cids[0]}, []cid.Cid{})
	// Note: this also registers peer B as being available
Dirk McCormick's avatar
Dirk McCormick committed
446
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

	time.Sleep(5 * time.Millisecond)

	// peerA and peerB: become unavailable
	spm.SignalAvailability(peerA, false)
	spm.SignalAvailability(peerB, false)

	time.Sleep(5 * time.Millisecond)

	// Expect that onPeersExhausted() will be called with all cids for blocks
	// that have not been received
	if !testutil.MatchKeysIgnoreOrder(exhausted, []cid.Cid{cids[1], cids[2]}) {
		t.Fatal("Wrong keys")
	}
}

463 464 465 466 467
func TestConsecutiveDontHaveLimit(t *testing.T) {
	cids := testutil.GenerateCids(peerDontHaveLimit + 10)
	p := testutil.GeneratePeers(1)[0]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
468
	fpm := newFakeSessionPeerManager()
469 470 471
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
472
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
473 474 475 476 477 478 479 480

	go spm.Run()

	// Add all cids as wants
	spm.Add(cids)

	// Receive a HAVE from peer (adds it to the session)
	bpm.ReceiveFrom(p, cids[:1], []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
481
	spm.Update(p, []cid.Cid{}, cids[:1], []cid.Cid{})
482 483

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
484
	time.Sleep(10 * time.Millisecond)
485 486

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
487
	if has := fpm.HasPeer(p); !has {
488 489 490 491 492 493
		t.Fatal("Expected peer to be available")
	}

	// Receive DONT_HAVEs from peer that do not exceed limit
	for _, c := range cids[1:peerDontHaveLimit] {
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
494
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
495 496 497
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
498
	time.Sleep(20 * time.Millisecond)
499 500

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
501
	if has := fpm.HasPeer(p); !has {
502 503 504 505 506 507
		t.Fatal("Expected peer to be available")
	}

	// Receive DONT_HAVEs from peer that exceed limit
	for _, c := range cids[peerDontHaveLimit:] {
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
508
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
509 510 511
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
512
	time.Sleep(20 * time.Millisecond)
513 514

	// Session should remove peer
Dirk McCormick's avatar
Dirk McCormick committed
515
	if has := fpm.HasPeer(p); has {
516 517 518 519 520 521 522 523 524
		t.Fatal("Expected peer not to be available")
	}
}

func TestConsecutiveDontHaveLimitInterrupted(t *testing.T) {
	cids := testutil.GenerateCids(peerDontHaveLimit + 10)
	p := testutil.GeneratePeers(1)[0]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
525
	fpm := newFakeSessionPeerManager()
526 527 528
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
529
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
530 531 532 533 534 535 536 537

	go spm.Run()

	// Add all cids as wants
	spm.Add(cids)

	// Receive a HAVE from peer (adds it to the session)
	bpm.ReceiveFrom(p, cids[:1], []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
538
	spm.Update(p, []cid.Cid{}, cids[:1], []cid.Cid{})
539 540 541 542 543

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
544
	if has := fpm.HasPeer(p); !has {
545 546 547 548 549 550 551 552 553
		t.Fatal("Expected peer to be available")
	}

	// Receive DONT_HAVE then HAVE then DONT_HAVE from peer,
	// where consecutive DONT_HAVEs would have exceeded limit
	// (but they are not consecutive)
	for _, c := range cids[1:peerDontHaveLimit] {
		// DONT_HAVEs
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
554
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
555 556 557 558
	}
	for _, c := range cids[peerDontHaveLimit : peerDontHaveLimit+1] {
		// HAVEs
		bpm.ReceiveFrom(p, []cid.Cid{c}, []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
559
		spm.Update(p, []cid.Cid{}, []cid.Cid{c}, []cid.Cid{})
560 561 562 563
	}
	for _, c := range cids[peerDontHaveLimit+1:] {
		// DONT_HAVEs
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
564
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
565 566 567 568 569 570
	}

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
571
	if has := fpm.HasPeer(p); !has {
572 573 574 575 576 577 578 579 580
		t.Fatal("Expected peer to be available")
	}
}

func TestConsecutiveDontHaveReinstateAfterRemoval(t *testing.T) {
	cids := testutil.GenerateCids(peerDontHaveLimit + 10)
	p := testutil.GeneratePeers(1)[0]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
581
	fpm := newFakeSessionPeerManager()
582 583 584
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
585
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
586 587 588 589 590 591 592 593

	go spm.Run()

	// Add all cids as wants
	spm.Add(cids)

	// Receive a HAVE from peer (adds it to the session)
	bpm.ReceiveFrom(p, cids[:1], []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
594
	spm.Update(p, []cid.Cid{}, cids[:1], []cid.Cid{})
595 596 597 598 599

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
600
	if has := fpm.HasPeer(p); !has {
601 602 603 604 605 606
		t.Fatal("Expected peer to be available")
	}

	// Receive DONT_HAVEs from peer that exceed limit
	for _, c := range cids[1 : peerDontHaveLimit+2] {
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
607
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
608 609 610 611 612 613
	}

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Session should remove peer
Dirk McCormick's avatar
Dirk McCormick committed
614
	if has := fpm.HasPeer(p); has {
615 616 617 618 619
		t.Fatal("Expected peer not to be available")
	}

	// Receive a HAVE from peer (adds it back into the session)
	bpm.ReceiveFrom(p, cids[:1], []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
620
	spm.Update(p, []cid.Cid{}, cids[:1], []cid.Cid{})
621 622 623 624 625

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
626
	if has := fpm.HasPeer(p); !has {
627 628 629 630 631 632 633 634
		t.Fatal("Expected peer to be available")
	}

	cids2 := testutil.GenerateCids(peerDontHaveLimit + 10)

	// Receive DONT_HAVEs from peer that don't exceed limit
	for _, c := range cids2[1:peerDontHaveLimit] {
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
635
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
636 637 638 639 640 641
	}

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
642
	if has := fpm.HasPeer(p); !has {
643 644 645 646 647 648
		t.Fatal("Expected peer to be available")
	}

	// Receive DONT_HAVEs from peer that exceed limit
	for _, c := range cids2[peerDontHaveLimit:] {
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
649
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
650 651 652 653 654 655
	}

	// Wait for processing to complete
	time.Sleep(5 * time.Millisecond)

	// Session should remove peer
Dirk McCormick's avatar
Dirk McCormick committed
656
	if has := fpm.HasPeer(p); has {
657 658 659
		t.Fatal("Expected peer not to be available")
	}
}