sessionmanager_test.go 7.17 KB
Newer Older
1 2 3 4 5 6 7
package sessionmanager

import (
	"context"
	"testing"
	"time"

8
	delay "github.com/ipfs/go-ipfs-delay"
9

10
	notifications "github.com/ipfs/go-bitswap/notifications"
11
	bssession "github.com/ipfs/go-bitswap/session"
12
	bssd "github.com/ipfs/go-bitswap/sessiondata"
13
	"github.com/ipfs/go-bitswap/testutil"
14 15 16

	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
Raúl Kripalani's avatar
Raúl Kripalani committed
17
	peer "github.com/libp2p/go-libp2p-core/peer"
18 19 20
)

type fakeSession struct {
Steven Allen's avatar
Steven Allen committed
21 22 23 24 25
	interested []cid.Cid
	blks       []blocks.Block
	id         uint64
	pm         *fakePeerManager
	srs        *fakeRequestSplitter
26
	notif      notifications.PubSub
27 28 29 30 31 32 33 34
}

func (*fakeSession) GetBlock(context.Context, cid.Cid) (blocks.Block, error) {
	return nil, nil
}
func (*fakeSession) GetBlocks(context.Context, []cid.Cid) (<-chan blocks.Block, error) {
	return nil, nil
}
35 36 37 38 39 40 41 42 43 44 45
func (fs *fakeSession) InterestedIn(c cid.Cid) bool {
	for _, ic := range fs.interested {
		if c == ic {
			return true
		}
	}
	return false
}
func (fs *fakeSession) ReceiveBlocksFrom(p peer.ID, blks []blocks.Block) {
	fs.blks = append(fs.blks, blks...)
}
46 47 48 49 50 51

type fakePeerManager struct {
	id uint64
}

func (*fakePeerManager) FindMorePeers(context.Context, cid.Cid)  {}
52
func (*fakePeerManager) GetOptimizedPeers() []bssd.OptimizedPeer { return nil }
53
func (*fakePeerManager) RecordPeerRequests([]peer.ID, []cid.Cid) {}
54 55
func (*fakePeerManager) RecordPeerResponse(peer.ID, []cid.Cid)   {}
func (*fakePeerManager) RecordCancels(c []cid.Cid)               {}
56

57 58 59
type fakeRequestSplitter struct {
}

60
func (frs *fakeRequestSplitter) SplitRequest(optimizedPeers []bssd.OptimizedPeer, keys []cid.Cid) []bssd.PartialRequest {
61 62 63 64 65
	return nil
}
func (frs *fakeRequestSplitter) RecordDuplicateBlock() {}
func (frs *fakeRequestSplitter) RecordUniqueBlock()    {}

66
var nextInterestedIn []cid.Cid
67

68 69 70 71
func sessionFactory(ctx context.Context,
	id uint64,
	pm bssession.PeerManager,
	srs bssession.RequestSplitter,
72
	notif notifications.PubSub,
73 74
	provSearchDelay time.Duration,
	rebroadcastDelay delay.D) Session {
75
	return &fakeSession{
76 77 78 79
		interested: nextInterestedIn,
		id:         id,
		pm:         pm.(*fakePeerManager),
		srs:        srs.(*fakeRequestSplitter),
80
		notif:      notif,
81 82 83 84 85 86 87
	}
}

func peerManagerFactory(ctx context.Context, id uint64) bssession.PeerManager {
	return &fakePeerManager{id}
}

88 89 90 91
func requestSplitterFactory(ctx context.Context) bssession.RequestSplitter {
	return &fakeRequestSplitter{}
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
func cmpSessionCids(s *fakeSession, cids []cid.Cid) bool {
	return cmpBlockCids(s.blks, cids)
}

func cmpBlockCids(blks []blocks.Block, cids []cid.Cid) bool {
	if len(blks) != len(cids) {
		return false
	}
	for _, b := range blks {
		has := false
		for _, c := range cids {
			if c == b.Cid() {
				has = true
			}
		}
		if !has {
			return false
		}
	}
	return true
}

114 115 116 117
func TestAddingSessions(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
118 119 120
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
121 122 123 124

	p := peer.ID(123)
	block := blocks.NewBlock([]byte("block"))
	// we'll be interested in all blocks for this test
125
	nextInterestedIn = []cid.Cid{block.Cid()}
126 127

	currentID := sm.GetNextSessionID()
128
	firstSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
129 130 131 132
	if firstSession.id != firstSession.pm.id ||
		firstSession.id != currentID+1 {
		t.Fatal("session does not have correct id set")
	}
133
	secondSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
134 135 136 137 138
	if secondSession.id != secondSession.pm.id ||
		secondSession.id != firstSession.id+1 {
		t.Fatal("session does not have correct id set")
	}
	sm.GetNextSessionID()
139
	thirdSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
140 141 142 143
	if thirdSession.id != thirdSession.pm.id ||
		thirdSession.id != secondSession.id+2 {
		t.Fatal("session does not have correct id set")
	}
144 145 146 147
	sm.ReceiveBlocksFrom(p, []blocks.Block{block})
	if len(firstSession.blks) == 0 ||
		len(secondSession.blks) == 0 ||
		len(thirdSession.blks) == 0 {
148 149 150 151 152 153 154 155
		t.Fatal("should have received blocks but didn't")
	}
}

func TestReceivingBlocksWhenNotInterested(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
156 157 158
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
159 160

	p := peer.ID(123)
161 162 163 164 165 166 167
	blks := testutil.GenerateBlocksOfSize(3, 1024)
	var cids []cid.Cid
	for _, b := range blks {
		cids = append(cids, b.Cid())
	}

	nextInterestedIn = []cid.Cid{cids[0], cids[1]}
168
	firstSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
169
	nextInterestedIn = []cid.Cid{cids[0]}
170
	secondSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
171
	nextInterestedIn = []cid.Cid{}
172
	thirdSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
173

174 175 176 177 178 179
	sm.ReceiveBlocksFrom(p, []blocks.Block{blks[0], blks[1]})

	if !cmpSessionCids(firstSession, []cid.Cid{cids[0], cids[1]}) ||
		!cmpSessionCids(secondSession, []cid.Cid{cids[0]}) ||
		!cmpSessionCids(thirdSession, []cid.Cid{}) {
		t.Fatal("did not receive correct blocks for sessions")
180 181 182 183 184 185
	}
}

func TestRemovingPeersWhenManagerContextCancelled(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
186 187 188
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
189 190 191 192

	p := peer.ID(123)
	block := blocks.NewBlock([]byte("block"))
	// we'll be interested in all blocks for this test
193
	nextInterestedIn = []cid.Cid{block.Cid()}
194 195 196
	firstSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
	secondSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
	thirdSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
197 198 199 200

	cancel()
	// wait for sessions to get removed
	time.Sleep(10 * time.Millisecond)
201 202 203 204
	sm.ReceiveBlocksFrom(p, []blocks.Block{block})
	if len(firstSession.blks) > 0 ||
		len(secondSession.blks) > 0 ||
		len(thirdSession.blks) > 0 {
205 206 207 208 209 210 211 212
		t.Fatal("received blocks for sessions after manager is shutdown")
	}
}

func TestRemovingPeersWhenSessionContextCancelled(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
213 214 215
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
216 217 218 219

	p := peer.ID(123)
	block := blocks.NewBlock([]byte("block"))
	// we'll be interested in all blocks for this test
220
	nextInterestedIn = []cid.Cid{block.Cid()}
221
	firstSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
222
	sessionCtx, sessionCancel := context.WithCancel(ctx)
223 224
	secondSession := sm.NewSession(sessionCtx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
	thirdSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
225 226 227 228

	sessionCancel()
	// wait for sessions to get removed
	time.Sleep(10 * time.Millisecond)
229 230 231 232
	sm.ReceiveBlocksFrom(p, []blocks.Block{block})
	if len(firstSession.blks) == 0 ||
		len(secondSession.blks) > 0 ||
		len(thirdSession.blks) == 0 {
233 234 235
		t.Fatal("received blocks for sessions that are canceled")
	}
}