Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-bitswap
Commits
e1a25234
Commit
e1a25234
authored
Nov 28, 2018
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test(sessionmanager): Add unit test
Add a unit test and do some additional decoupling
parent
d7a532d0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
197 additions
and
16 deletions
+197
-16
bitswap.go
bitswap.go
+10
-2
sessionmanager/sessionmanager.go
sessionmanager/sessionmanager.go
+24
-14
sessionmanager/sessionmanager_test.go
sessionmanager/sessionmanager_test.go
+163
-0
No files found.
bitswap.go
View file @
e1a25234
...
...
@@ -16,9 +16,10 @@ import (
bsnet
"github.com/ipfs/go-bitswap/network"
notifications
"github.com/ipfs/go-bitswap/notifications"
bspm
"github.com/ipfs/go-bitswap/peermanager"
bssession
"github.com/ipfs/go-bitswap/session"
bssm
"github.com/ipfs/go-bitswap/sessionmanager"
bsspm
"github.com/ipfs/go-bitswap/sessionpeermanager"
bswm
"github.com/ipfs/go-bitswap/wantmanager"
blocks
"github.com/ipfs/go-block-format"
cid
"github.com/ipfs/go-cid"
blockstore
"github.com/ipfs/go-ipfs-blockstore"
...
...
@@ -102,6 +103,13 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
}
wm
:=
bswm
.
New
(
ctx
)
sessionFactory
:=
func
(
ctx
context
.
Context
,
id
uint64
,
pm
bssession
.
PeerManager
)
bssm
.
Session
{
return
bssession
.
New
(
ctx
,
id
,
wm
,
pm
)
}
sessionPeerManagerFactory
:=
func
(
ctx
context
.
Context
,
id
uint64
)
bssession
.
PeerManager
{
return
bsspm
.
New
(
ctx
,
id
,
network
)
}
bs
:=
&
Bitswap
{
blockstore
:
bstore
,
notifications
:
notif
,
...
...
@@ -113,7 +121,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
provideKeys
:
make
(
chan
cid
.
Cid
,
provideKeysBufferSize
),
wm
:
wm
,
pm
:
bspm
.
New
(
ctx
,
peerQueueFactory
),
sm
:
bssm
.
New
(
ctx
,
wm
,
netw
or
k
),
sm
:
bssm
.
New
(
ctx
,
sessionFactory
,
sessionPeerManagerFact
or
y
),
counters
:
new
(
counters
),
dupMetric
:
dupHist
,
allMetric
:
allHist
,
...
...
sessionmanager/sessionmanager.go
View file @
e1a25234
...
...
@@ -8,22 +8,34 @@ import (
cid
"github.com/ipfs/go-cid"
bssession
"github.com/ipfs/go-bitswap/session"
bsspm
"github.com/ipfs/go-bitswap/sessionpeermanager"
exchange
"github.com/ipfs/go-ipfs-exchange-interface"
peer
"github.com/libp2p/go-libp2p-peer"
)
// Session is a session that is managed by the session manager
type
Session
interface
{
exchange
.
Fetcher
InterestedIn
(
cid
.
Cid
)
bool
ReceiveBlockFrom
(
peer
.
ID
,
blocks
.
Block
)
}
type
sesTrk
struct
{
session
*
bssession
.
Session
pm
*
bss
pm
.
S
essionPeerManager
session
Session
pm
bssession
.
PeerManager
}
// SessionFactory generates a new session for the SessionManager to track.
type
SessionFactory
func
(
ctx
context
.
Context
,
id
uint64
,
pm
bssession
.
PeerManager
)
Session
// PeerManagerFactory generates a new peer manager for a session.
type
PeerManagerFactory
func
(
ctx
context
.
Context
,
id
uint64
)
bssession
.
PeerManager
// SessionManager is responsible for creating, managing, and dispatching to
// sessions.
type
SessionManager
struct
{
wm
bssession
.
WantManager
network
bsspm
.
PeerNetwork
ctx
context
.
Context
sessionFactory
SessionFactory
peerManagerFactory
PeerManagerFactory
// Sessions
sessLk
sync
.
Mutex
sessions
[]
sesTrk
...
...
@@ -34,11 +46,11 @@ type SessionManager struct {
}
// New creates a new SessionManager.
func
New
(
ctx
context
.
Context
,
wm
bssession
.
WantManager
,
network
bsspm
.
PeerNetw
or
k
)
*
SessionManager
{
func
New
(
ctx
context
.
Context
,
sessionFactory
SessionFactory
,
peerManagerFactory
PeerManagerFact
or
y
)
*
SessionManager
{
return
&
SessionManager
{
ctx
:
ctx
,
wm
:
wm
,
netw
or
k
:
netw
or
k
,
sessionFactory
:
sessionFactory
,
peerManagerFact
or
y
:
peerManagerFact
or
y
,
}
}
...
...
@@ -48,8 +60,8 @@ func (sm *SessionManager) NewSession(ctx context.Context) exchange.Fetcher {
id
:=
sm
.
GetNextSessionID
()
sessionctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
pm
:=
bsspm
.
New
(
sessionctx
,
id
,
sm
.
network
)
session
:=
b
ssession
.
New
(
sessionctx
,
id
,
sm
.
wm
,
pm
)
pm
:=
sm
.
peerManagerFactory
(
sessionctx
,
id
)
session
:=
s
m
.
session
Factory
(
sessionctx
,
id
,
pm
)
tracked
:=
sesTrk
{
session
,
pm
}
sm
.
sessLk
.
Lock
()
sm
.
sessions
=
append
(
sm
.
sessions
,
tracked
)
...
...
@@ -94,11 +106,9 @@ func (sm *SessionManager) ReceiveBlockFrom(from peer.ID, blk blocks.Block) {
defer
sm
.
sessLk
.
Unlock
()
k
:=
blk
.
Cid
()
ks
:=
[]
cid
.
Cid
{
k
}
for
_
,
s
:=
range
sm
.
sessions
{
if
s
.
session
.
InterestedIn
(
k
)
{
s
.
session
.
ReceiveBlockFrom
(
from
,
blk
)
sm
.
wm
.
CancelWants
(
sm
.
ctx
,
ks
,
nil
,
s
.
session
.
ID
())
}
}
}
sessionmanager/sessionmanager_test.go
0 → 100644
View file @
e1a25234
package
sessionmanager
import
(
"context"
"testing"
"time"
bssession
"github.com/ipfs/go-bitswap/session"
blocks
"github.com/ipfs/go-block-format"
cid
"github.com/ipfs/go-cid"
peer
"github.com/libp2p/go-libp2p-peer"
)
type
fakeSession
struct
{
interested
bool
receivedBlock
bool
id
uint64
pm
*
fakePeerManager
}
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
}
func
(
fs
*
fakeSession
)
InterestedIn
(
cid
.
Cid
)
bool
{
return
fs
.
interested
}
func
(
fs
*
fakeSession
)
ReceiveBlockFrom
(
peer
.
ID
,
blocks
.
Block
)
{
fs
.
receivedBlock
=
true
}
type
fakePeerManager
struct
{
id
uint64
}
func
(
*
fakePeerManager
)
FindMorePeers
(
context
.
Context
,
cid
.
Cid
)
{}
func
(
*
fakePeerManager
)
GetOptimizedPeers
()
[]
peer
.
ID
{
return
nil
}
func
(
*
fakePeerManager
)
RecordPeerRequests
([]
peer
.
ID
,
[]
cid
.
Cid
)
{}
func
(
*
fakePeerManager
)
RecordPeerResponse
(
peer
.
ID
,
cid
.
Cid
)
{}
var
nextInterestedIn
bool
func
sessionFactory
(
ctx
context
.
Context
,
id
uint64
,
pm
bssession
.
PeerManager
)
Session
{
return
&
fakeSession
{
interested
:
nextInterestedIn
,
receivedBlock
:
false
,
id
:
id
,
pm
:
pm
.
(
*
fakePeerManager
),
}
}
func
peerManagerFactory
(
ctx
context
.
Context
,
id
uint64
)
bssession
.
PeerManager
{
return
&
fakePeerManager
{
id
}
}
func
TestAddingSessions
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
defer
cancel
()
sm
:=
New
(
ctx
,
sessionFactory
,
peerManagerFactory
)
p
:=
peer
.
ID
(
123
)
block
:=
blocks
.
NewBlock
([]
byte
(
"block"
))
// we'll be interested in all blocks for this test
nextInterestedIn
=
true
currentID
:=
sm
.
GetNextSessionID
()
firstSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
if
firstSession
.
id
!=
firstSession
.
pm
.
id
||
firstSession
.
id
!=
currentID
+
1
{
t
.
Fatal
(
"session does not have correct id set"
)
}
secondSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
if
secondSession
.
id
!=
secondSession
.
pm
.
id
||
secondSession
.
id
!=
firstSession
.
id
+
1
{
t
.
Fatal
(
"session does not have correct id set"
)
}
sm
.
GetNextSessionID
()
thirdSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
if
thirdSession
.
id
!=
thirdSession
.
pm
.
id
||
thirdSession
.
id
!=
secondSession
.
id
+
2
{
t
.
Fatal
(
"session does not have correct id set"
)
}
sm
.
ReceiveBlockFrom
(
p
,
block
)
if
!
firstSession
.
receivedBlock
||
!
secondSession
.
receivedBlock
||
!
thirdSession
.
receivedBlock
{
t
.
Fatal
(
"should have received blocks but didn't"
)
}
}
func
TestReceivingBlocksWhenNotInterested
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
defer
cancel
()
sm
:=
New
(
ctx
,
sessionFactory
,
peerManagerFactory
)
p
:=
peer
.
ID
(
123
)
block
:=
blocks
.
NewBlock
([]
byte
(
"block"
))
// we'll be interested in all blocks for this test
nextInterestedIn
=
false
firstSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
nextInterestedIn
=
true
secondSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
nextInterestedIn
=
false
thirdSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
sm
.
ReceiveBlockFrom
(
p
,
block
)
if
firstSession
.
receivedBlock
||
!
secondSession
.
receivedBlock
||
thirdSession
.
receivedBlock
{
t
.
Fatal
(
"did not receive blocks only for interested sessions"
)
}
}
func
TestRemovingPeersWhenManagerContextCancelled
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
sm
:=
New
(
ctx
,
sessionFactory
,
peerManagerFactory
)
p
:=
peer
.
ID
(
123
)
block
:=
blocks
.
NewBlock
([]
byte
(
"block"
))
// we'll be interested in all blocks for this test
nextInterestedIn
=
true
firstSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
secondSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
thirdSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
cancel
()
// wait for sessions to get removed
time
.
Sleep
(
10
*
time
.
Millisecond
)
sm
.
ReceiveBlockFrom
(
p
,
block
)
if
firstSession
.
receivedBlock
||
secondSession
.
receivedBlock
||
thirdSession
.
receivedBlock
{
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
()
sm
:=
New
(
ctx
,
sessionFactory
,
peerManagerFactory
)
p
:=
peer
.
ID
(
123
)
block
:=
blocks
.
NewBlock
([]
byte
(
"block"
))
// we'll be interested in all blocks for this test
nextInterestedIn
=
true
firstSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
sessionCtx
,
sessionCancel
:=
context
.
WithCancel
(
ctx
)
secondSession
:=
sm
.
NewSession
(
sessionCtx
)
.
(
*
fakeSession
)
thirdSession
:=
sm
.
NewSession
(
ctx
)
.
(
*
fakeSession
)
sessionCancel
()
// wait for sessions to get removed
time
.
Sleep
(
10
*
time
.
Millisecond
)
sm
.
ReceiveBlockFrom
(
p
,
block
)
if
!
firstSession
.
receivedBlock
||
secondSession
.
receivedBlock
||
!
thirdSession
.
receivedBlock
{
t
.
Fatal
(
"received blocks for sessions that are canceled"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment