sessionwantsender_test.go 20 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
	cid "github.com/ipfs/go-cid"
	peer "github.com/libp2p/go-libp2p-core/peer"
)

type sentWants struct {
Dirk McCormick's avatar
Dirk McCormick committed
17
	sync.Mutex
dirkmc's avatar
dirkmc committed
18 19 20 21 22
	p          peer.ID
	wantHaves  *cid.Set
	wantBlocks *cid.Set
}

Dirk McCormick's avatar
Dirk McCormick committed
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
func (sw *sentWants) add(wantBlocks []cid.Cid, wantHaves []cid.Cid) {
	sw.Lock()
	defer sw.Unlock()

	for _, c := range wantBlocks {
		sw.wantBlocks.Add(c)
	}
	for _, c := range wantHaves {
		if !sw.wantBlocks.Has(c) {
			sw.wantHaves.Add(c)
		}
	}

}
func (sw *sentWants) wantHavesKeys() []cid.Cid {
	sw.Lock()
	defer sw.Unlock()
	return sw.wantHaves.Keys()
}
func (sw *sentWants) wantBlocksKeys() []cid.Cid {
	sw.Lock()
	defer sw.Unlock()
	return sw.wantBlocks.Keys()
}

dirkmc's avatar
dirkmc committed
48
type mockPeerManager struct {
Dirk McCormick's avatar
Dirk McCormick committed
49 50 51
	lk           sync.Mutex
	peerSessions map[peer.ID]bspm.Session
	peerSends    map[peer.ID]*sentWants
dirkmc's avatar
dirkmc committed
52 53 54
}

func newMockPeerManager() *mockPeerManager {
Dirk McCormick's avatar
Dirk McCormick committed
55 56 57 58
	return &mockPeerManager{
		peerSessions: make(map[peer.ID]bspm.Session),
		peerSends:    make(map[peer.ID]*sentWants),
	}
dirkmc's avatar
dirkmc committed
59 60 61
}

func (pm *mockPeerManager) RegisterSession(p peer.ID, sess bspm.Session) bool {
Dirk McCormick's avatar
Dirk McCormick committed
62 63 64 65
	pm.lk.Lock()
	defer pm.lk.Unlock()

	pm.peerSessions[p] = sess
dirkmc's avatar
dirkmc committed
66 67 68 69 70 71 72
	return true
}

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

func (pm *mockPeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) {
Dirk McCormick's avatar
Dirk McCormick committed
73 74 75 76 77 78 79
	pm.lk.Lock()
	defer pm.lk.Unlock()

	sw, ok := pm.peerSends[p]
	if !ok {
		sw = &sentWants{p: p, wantHaves: cid.NewSet(), wantBlocks: cid.NewSet()}
		pm.peerSends[p] = sw
dirkmc's avatar
dirkmc committed
80
	}
Dirk McCormick's avatar
Dirk McCormick committed
81
	sw.add(wantBlocks, wantHaves)
dirkmc's avatar
dirkmc committed
82 83
}

Dirk McCormick's avatar
Dirk McCormick committed
84
func (pm *mockPeerManager) waitNextWants() map[peer.ID]*sentWants {
dirkmc's avatar
dirkmc committed
85
	time.Sleep(5 * time.Millisecond)
Dirk McCormick's avatar
Dirk McCormick committed
86 87 88 89 90 91 92

	pm.lk.Lock()
	defer pm.lk.Unlock()
	nw := make(map[peer.ID]*sentWants)
	for p, sentWants := range pm.peerSends {
		nw[p] = sentWants
	}
dirkmc's avatar
dirkmc committed
93 94 95 96
	return nw
}

func (pm *mockPeerManager) clearWants() {
Dirk McCormick's avatar
Dirk McCormick committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	pm.lk.Lock()
	defer pm.lk.Unlock()

	for p := range pm.peerSends {
		delete(pm.peerSends, p)
	}
}

type exhaustedPeers struct {
	lk sync.Mutex
	ks []cid.Cid
}

func (ep *exhaustedPeers) onPeersExhausted(ks []cid.Cid) {
	ep.lk.Lock()
	defer ep.lk.Unlock()

	ep.ks = append(ep.ks, ks...)
}

func (ep *exhaustedPeers) clear() {
	ep.lk.Lock()
	defer ep.lk.Unlock()

	ep.ks = nil
}

func (ep *exhaustedPeers) exhausted() []cid.Cid {
	ep.lk.Lock()
	defer ep.lk.Unlock()

	return append([]cid.Cid{}, ep.ks...)
dirkmc's avatar
dirkmc committed
129 130 131 132 133 134 135 136
}

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
137
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
138 139 140
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
141
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
142 143 144 145 146 147 148

	go spm.Run()

	// add cid0, cid1
	blkCids0 := cids[0:2]
	spm.Add(blkCids0)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
149
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
150 151 152 153 154 155 156 157 158 159

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
160
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocksKeys(), blkCids0) {
dirkmc's avatar
dirkmc committed
161 162
		t.Fatal("Wrong keys")
	}
Dirk McCormick's avatar
Dirk McCormick committed
163
	if len(sw.wantHavesKeys()) > 0 {
dirkmc's avatar
dirkmc committed
164 165 166 167 168 169 170 171 172 173 174
		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
175
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
176 177 178
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
179
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
180 181 182 183 184 185 186

	go spm.Run()

	// add cid0, cid1
	blkCids0 := cids[0:2]
	spm.Add(blkCids0)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
187
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
188 189 190 191 192 193 194 195 196 197

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
198
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocksKeys(), blkCids0) {
dirkmc's avatar
dirkmc committed
199 200 201 202 203 204 205
		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
206
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
221
	if !testutil.MatchKeysIgnoreOrder(sw.wantHavesKeys(), blkCids0) {
dirkmc's avatar
dirkmc committed
222 223 224 225 226 227 228 229 230 231 232
		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
233
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
234 235 236
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
237
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
238 239 240 241 242 243

	go spm.Run()

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

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
255
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocksKeys(), cids) {
dirkmc's avatar
dirkmc committed
256 257 258 259 260 261 262 263
		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
264
	spm.Update(peerA, []cid.Cid{cids[0]}, []cid.Cid{}, []cid.Cid{cids[1]})
dirkmc's avatar
dirkmc committed
265 266
	// peerB: HAVE cid0, cid1
	bpm.ReceiveFrom(peerB, cids, []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
267
	spm.Update(peerB, []cid.Cid{}, cids, []cid.Cid{})
dirkmc's avatar
dirkmc committed
268 269 270 271 272 273 274 275 276 277 278 279

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
280
	wb := sw.wantBlocksKeys()
dirkmc's avatar
dirkmc committed
281 282 283 284 285 286 287 288 289 290 291 292
	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
293
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
294 295 296
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
297
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
dirkmc's avatar
dirkmc committed
298 299 300 301 302 303

	go spm.Run()

	// add cid0, cid1
	spm.Add(cids)
	// peerA: HAVE cid0
Dirk McCormick's avatar
Dirk McCormick committed
304
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
305 306 307 308 309 310 311 312 313 314

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
315
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocksKeys(), cids) {
dirkmc's avatar
dirkmc committed
316 317 318 319 320 321 322
		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
323
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

	// 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")
	}
Dirk McCormick's avatar
Dirk McCormick committed
346
	if !testutil.MatchKeysIgnoreOrder(sw.wantBlocksKeys(), cids) {
dirkmc's avatar
dirkmc committed
347 348 349 350 351
		t.Fatal("Wrong keys")
	}
}

func TestPeersExhausted(t *testing.T) {
Dirk McCormick's avatar
Dirk McCormick committed
352
	cids := testutil.GenerateCids(3)
dirkmc's avatar
dirkmc committed
353 354 355 356 357
	peers := testutil.GeneratePeers(2)
	peerA := peers[0]
	peerB := peers[1]
	sid := uint64(1)
	pm := newMockPeerManager()
Dirk McCormick's avatar
Dirk McCormick committed
358
	fpm := newFakeSessionPeerManager()
dirkmc's avatar
dirkmc committed
359 360 361
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}

Dirk McCormick's avatar
Dirk McCormick committed
362 363
	ep := exhaustedPeers{}
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, ep.onPeersExhausted)
dirkmc's avatar
dirkmc committed
364 365 366 367 368 369

	go spm.Run()

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

Dirk McCormick's avatar
Dirk McCormick committed
370 371
	// peerA: HAVE cid0
	bpm.ReceiveFrom(peerA, []cid.Cid{cids[0]}, []cid.Cid{})
dirkmc's avatar
dirkmc committed
372
	// Note: this also registers peer A as being available
Dirk McCormick's avatar
Dirk McCormick committed
373 374 375 376 377
	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
378 379 380

	time.Sleep(5 * time.Millisecond)

Dirk McCormick's avatar
Dirk McCormick committed
381 382
	// All available peers (peer A) have sent us a DONT_HAVE for cid1,
	// so expect that onPeersExhausted() will be called with cid1
Dirk McCormick's avatar
Dirk McCormick committed
383
	if !testutil.MatchKeysIgnoreOrder(ep.exhausted(), []cid.Cid{cids[1]}) {
dirkmc's avatar
dirkmc committed
384 385 386 387
		t.Fatal("Wrong keys")
	}

	// Clear exhausted cids
Dirk McCormick's avatar
Dirk McCormick committed
388
	ep.clear()
dirkmc's avatar
dirkmc committed
389

Dirk McCormick's avatar
Dirk McCormick committed
390 391 392 393 394 395 396 397
	// 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
398 399 400 401 402

	// 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
403
	// for cid1, but we already called onPeersExhausted with cid1, so it
dirkmc's avatar
dirkmc committed
404
	// should not be called again
Dirk McCormick's avatar
Dirk McCormick committed
405
	if len(ep.exhausted()) > 0 {
dirkmc's avatar
dirkmc committed
406 407 408
		t.Fatal("Wrong keys")
	}

Dirk McCormick's avatar
Dirk McCormick committed
409 410 411
	// 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
412 413 414 415 416

	// 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
417
	// cid2, so expect that onPeersExhausted() will be called with cid2
Dirk McCormick's avatar
Dirk McCormick committed
418
	if !testutil.MatchKeysIgnoreOrder(ep.exhausted(), []cid.Cid{cids[2]}) {
dirkmc's avatar
dirkmc committed
419 420 421
		t.Fatal("Wrong keys")
	}
}
422

423 424 425 426 427 428 429 430 431 432 433
// 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
434
	fpm := newFakeSessionPeerManager()
435 436 437
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}

Dirk McCormick's avatar
Dirk McCormick committed
438 439
	ep := exhaustedPeers{}
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, ep.onPeersExhausted)
440 441 442 443 444 445 446 447 448

	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
449
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
450 451 452
	// 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
453
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
454 455 456

	// peerA: DONT_HAVE cid1
	bpm.ReceiveFrom(peerA, []cid.Cid{}, []cid.Cid{cids[1]})
Dirk McCormick's avatar
Dirk McCormick committed
457
	spm.Update(peerA, []cid.Cid{}, []cid.Cid{}, []cid.Cid{cids[0]})
458 459 460 461 462 463 464 465 466 467

	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
Dirk McCormick's avatar
Dirk McCormick committed
468
	if !testutil.MatchKeysIgnoreOrder(ep.exhausted(), []cid.Cid{cids[1]}) {
469 470 471 472 473 474 475 476 477 478 479 480 481
		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
482
	fpm := newFakeSessionPeerManager()
483 484 485
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}

Dirk McCormick's avatar
Dirk McCormick committed
486 487
	ep := exhaustedPeers{}
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, ep.onPeersExhausted)
488 489 490 491 492 493 494

	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
495
	spm.Update(peerA, []cid.Cid{cids[0]}, []cid.Cid{}, []cid.Cid{})
496 497 498
	// 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
499
	spm.Update(peerB, []cid.Cid{}, []cid.Cid{cids[0]}, []cid.Cid{})
500 501 502 503 504 505 506 507 508 509 510

	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
Dirk McCormick's avatar
Dirk McCormick committed
511
	if !testutil.MatchKeysIgnoreOrder(ep.exhausted(), []cid.Cid{cids[1], cids[2]}) {
512 513 514 515
		t.Fatal("Wrong keys")
	}
}

516 517 518 519 520
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
521
	fpm := newFakeSessionPeerManager()
522 523 524
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
525
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
526 527 528 529 530 531

	go spm.Run()

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

532 533
	// Receive a block from peer (adds it to the session)
	spm.Update(p, cids[:1], []cid.Cid{}, []cid.Cid{})
534 535

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
536
	time.Sleep(10 * time.Millisecond)
537 538

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
539
	if has := fpm.HasPeer(p); !has {
540 541 542 543 544 545
		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
546
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
547 548 549
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
550
	time.Sleep(20 * time.Millisecond)
551 552

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
553
	if has := fpm.HasPeer(p); !has {
554 555 556 557 558 559
		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
560
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
561 562 563
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
564
	time.Sleep(20 * time.Millisecond)
565 566

	// Session should remove peer
Dirk McCormick's avatar
Dirk McCormick committed
567
	if has := fpm.HasPeer(p); has {
568 569 570 571 572 573 574 575 576
		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
577
	fpm := newFakeSessionPeerManager()
578 579 580
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
581
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
582 583 584 585 586 587

	go spm.Run()

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

588 589
	// Receive a block from peer (adds it to the session)
	spm.Update(p, cids[:1], []cid.Cid{}, []cid.Cid{})
590 591 592 593 594

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

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
595
	if has := fpm.HasPeer(p); !has {
596 597 598 599 600 601 602 603 604
		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
605
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
606 607 608 609
	}
	for _, c := range cids[peerDontHaveLimit : peerDontHaveLimit+1] {
		// HAVEs
		bpm.ReceiveFrom(p, []cid.Cid{c}, []cid.Cid{})
Dirk McCormick's avatar
Dirk McCormick committed
610
		spm.Update(p, []cid.Cid{}, []cid.Cid{c}, []cid.Cid{})
611 612 613 614
	}
	for _, c := range cids[peerDontHaveLimit+1:] {
		// DONT_HAVEs
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
Dirk McCormick's avatar
Dirk McCormick committed
615
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
616 617 618 619 620 621
	}

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

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
622
	if has := fpm.HasPeer(p); !has {
623 624 625 626 627 628 629 630 631
		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
632
	fpm := newFakeSessionPeerManager()
633 634 635
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
Dirk McCormick's avatar
Dirk McCormick committed
636
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)
637 638 639 640 641 642

	go spm.Run()

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

643 644
	// Receive a block from peer (adds it to the session)
	spm.Update(p, cids[:1], []cid.Cid{}, []cid.Cid{})
645 646 647 648 649

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

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
650
	if has := fpm.HasPeer(p); !has {
651 652 653 654 655 656
		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
657
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
658 659 660
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
661
	time.Sleep(10 * time.Millisecond)
662 663

	// Session should remove peer
Dirk McCormick's avatar
Dirk McCormick committed
664
	if has := fpm.HasPeer(p); has {
665 666 667 668 669
		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
670
	spm.Update(p, []cid.Cid{}, cids[:1], []cid.Cid{})
671 672

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
673
	time.Sleep(10 * time.Millisecond)
674 675

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
676
	if has := fpm.HasPeer(p); !has {
677 678 679 680 681 682 683 684
		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
685
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
686 687 688
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
689
	time.Sleep(10 * time.Millisecond)
690 691

	// Peer should be available
Dirk McCormick's avatar
Dirk McCormick committed
692
	if has := fpm.HasPeer(p); !has {
693 694 695 696 697 698
		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
699
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
700 701 702
	}

	// Wait for processing to complete
Dirk McCormick's avatar
Dirk McCormick committed
703
	time.Sleep(10 * time.Millisecond)
704 705

	// Session should remove peer
Dirk McCormick's avatar
Dirk McCormick committed
706
	if has := fpm.HasPeer(p); has {
707 708 709
		t.Fatal("Expected peer not to be available")
	}
}
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

func TestConsecutiveDontHaveDontRemoveIfHasWantedBlock(t *testing.T) {
	cids := testutil.GenerateCids(peerDontHaveLimit + 10)
	p := testutil.GeneratePeers(1)[0]
	sid := uint64(1)
	pm := newMockPeerManager()
	fpm := newFakeSessionPeerManager()
	bpm := bsbpm.New()
	onSend := func(peer.ID, []cid.Cid, []cid.Cid) {}
	onPeersExhausted := func([]cid.Cid) {}
	spm := newSessionWantSender(context.Background(), sid, pm, fpm, bpm, onSend, onPeersExhausted)

	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{})
	spm.Update(p, []cid.Cid{}, cids[:1], []cid.Cid{})

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

	// Peer should be available
	if has := fpm.HasPeer(p); !has {
		t.Fatal("Expected peer to be available")
	}

	// Receive DONT_HAVEs from peer that exceed limit
	for _, c := range cids[1 : peerDontHaveLimit+5] {
		bpm.ReceiveFrom(p, []cid.Cid{}, []cid.Cid{c})
		spm.Update(p, []cid.Cid{}, []cid.Cid{}, []cid.Cid{c})
	}

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

	// Peer should still be available because it has a block that we want.
	// (We received a HAVE for cid 0 but didn't yet receive the block)
	if has := fpm.HasPeer(p); !has {
		t.Fatal("Expected peer to be available")
	}
}