sessionmanager_test.go 7.85 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
	interested []cid.Cid
22
	ks         []cid.Cid
Steven Allen's avatar
Steven Allen committed
23 24 25
	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
func (fs *fakeSession) InterestedIn(c cid.Cid) bool {
	for _, ic := range fs.interested {
		if c == ic {
			return true
		}
	}
	return false
}
43
func (fs *fakeSession) ReceiveFrom(p peer.ID, ks []cid.Cid) {
44
	fs.ks = append(fs.ks, ks...)
45
}
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
func cmpSessionCids(s *fakeSession, cids []cid.Cid) bool {
93
	if len(s.ks) != len(cids) {
94 95
		return false
	}
96
	for _, bk := range s.ks {
97 98
		has := false
		for _, c := range cids {
99
			if c == bk {
100 101 102 103 104 105 106 107 108 109
				has = true
			}
		}
		if !has {
			return false
		}
	}
	return true
}

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

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

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

func TestReceivingBlocksWhenNotInterested(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
152 153 154
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
155 156

	p := peer.ID(123)
157 158 159 160 161 162 163
	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]}
164
	firstSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
165
	nextInterestedIn = []cid.Cid{cids[0]}
166
	secondSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
167
	nextInterestedIn = []cid.Cid{}
168
	thirdSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
169

170
	sm.ReceiveFrom(p, []cid.Cid{blks[0].Cid(), blks[1].Cid()})
171 172 173 174 175

	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")
176 177 178
	}
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
func TestInterestedIn(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory)

	blks := testutil.GenerateBlocksOfSize(4, 1024)
	var cids []cid.Cid
	for _, b := range blks {
		cids = append(cids, b.Cid())
	}

	nextInterestedIn = []cid.Cid{cids[0], cids[1]}
	_ = sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
	nextInterestedIn = []cid.Cid{cids[0], cids[2]}
	_ = sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)

	if !sm.InterestedIn(cids[0]) ||
		!sm.InterestedIn(cids[1]) ||
		!sm.InterestedIn(cids[2]) {
		t.Fatal("expected interest but session manager was not interested")
	}
	if sm.InterestedIn(cids[3]) {
		t.Fatal("expected no interest but session manager was interested")
	}
}

206 207 208
func TestRemovingPeersWhenManagerContextCancelled(t *testing.T) {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
209 210 211
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
212 213 214 215

	p := peer.ID(123)
	block := blocks.NewBlock([]byte("block"))
	// we'll be interested in all blocks for this test
216
	nextInterestedIn = []cid.Cid{block.Cid()}
217 218 219
	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)
220 221 222 223

	cancel()
	// wait for sessions to get removed
	time.Sleep(10 * time.Millisecond)
224
	sm.ReceiveFrom(p, []cid.Cid{block.Cid()})
225 226 227
	if len(firstSession.ks) > 0 ||
		len(secondSession.ks) > 0 ||
		len(thirdSession.ks) > 0 {
228 229 230 231 232 233 234 235
		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()
236 237 238
	notif := notifications.New()
	defer notif.Shutdown()
	sm := New(ctx, sessionFactory, peerManagerFactory, requestSplitterFactory, notif)
239 240 241 242

	p := peer.ID(123)
	block := blocks.NewBlock([]byte("block"))
	// we'll be interested in all blocks for this test
243
	nextInterestedIn = []cid.Cid{block.Cid()}
244
	firstSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
245
	sessionCtx, sessionCancel := context.WithCancel(ctx)
246 247
	secondSession := sm.NewSession(sessionCtx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
	thirdSession := sm.NewSession(ctx, time.Second, delay.Fixed(time.Minute)).(*fakeSession)
248 249 250 251

	sessionCancel()
	// wait for sessions to get removed
	time.Sleep(10 * time.Millisecond)
252
	sm.ReceiveFrom(p, []cid.Cid{block.Cid()})
253 254 255
	if len(firstSession.ks) == 0 ||
		len(secondSession.ks) > 0 ||
		len(thirdSession.ks) == 0 {
256 257 258
		t.Fatal("received blocks for sessions that are canceled")
	}
}