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
9532d009
Commit
9532d009
authored
Nov 27, 2018
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(testing): extract common test utils
parent
9ed150a7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
102 deletions
+97
-102
peermanager/peermanager_test.go
peermanager/peermanager_test.go
+23
-66
testutil/testutil.go
testutil/testutil.go
+67
-0
wantmanager/wantmanager_test.go
wantmanager/wantmanager_test.go
+7
-36
No files found.
peermanager/peermanager_test.go
View file @
9532d009
...
...
@@ -6,47 +6,13 @@ import (
"testing"
"time"
"github.com/ipfs/go-bitswap/testutil"
bsmsg
"github.com/ipfs/go-bitswap/message"
wantlist
"github.com/ipfs/go-bitswap/wantlist"
"github.com/ipfs/go-ipfs-blocksutil"
"github.com/libp2p/go-libp2p-peer"
)
var
blockGenerator
=
blocksutil
.
NewBlockGenerator
()
var
prioritySeq
int
func
generateEntries
(
n
int
,
isCancel
bool
)
[]
*
bsmsg
.
Entry
{
bsmsgs
:=
make
([]
*
bsmsg
.
Entry
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
prioritySeq
++
msg
:=
&
bsmsg
.
Entry
{
Entry
:
wantlist
.
NewRefEntry
(
blockGenerator
.
Next
()
.
Cid
(),
prioritySeq
),
Cancel
:
isCancel
,
}
bsmsgs
=
append
(
bsmsgs
,
msg
)
}
return
bsmsgs
}
var
peerSeq
int
func
generatePeers
(
n
int
)
[]
peer
.
ID
{
peerIds
:=
make
([]
peer
.
ID
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
peerSeq
++
p
:=
peer
.
ID
(
peerSeq
)
peerIds
=
append
(
peerIds
,
p
)
}
return
peerIds
}
var
nextSession
uint64
func
generateSessionID
()
uint64
{
nextSession
++
return
uint64
(
nextSession
)
}
type
messageSent
struct
{
p
peer
.
ID
entries
[]
*
bsmsg
.
Entry
...
...
@@ -59,15 +25,6 @@ type fakePeer struct {
messagesSent
chan
messageSent
}
func
containsPeer
(
peers
[]
peer
.
ID
,
p
peer
.
ID
)
bool
{
for
_
,
n
:=
range
peers
{
if
p
==
n
{
return
true
}
}
return
false
}
func
(
fp
*
fakePeer
)
Startup
(
ctx
context
.
Context
,
initialEntries
[]
*
wantlist
.
Entry
)
{}
func
(
fp
*
fakePeer
)
Shutdown
()
{}
func
(
fp
*
fakePeer
)
RefIncrement
()
{
fp
.
refcnt
++
}
...
...
@@ -119,7 +76,7 @@ func TestAddingAndRemovingPeers(t *testing.T) {
ctx
:=
context
.
Background
()
peerQueueFactory
:=
makePeerQueueFactory
(
nil
)
tp
:=
g
eneratePeers
(
5
)
tp
:=
testutil
.
G
eneratePeers
(
5
)
peer1
,
peer2
,
peer3
,
peer4
,
peer5
:=
tp
[
0
],
tp
[
1
],
tp
[
2
],
tp
[
3
],
tp
[
4
]
peerManager
:=
New
(
ctx
,
peerQueueFactory
)
peerManager
.
Startup
()
...
...
@@ -130,14 +87,14 @@ func TestAddingAndRemovingPeers(t *testing.T) {
connectedPeers
:=
peerManager
.
ConnectedPeers
()
if
!
c
ontainsPeer
(
connectedPeers
,
peer1
)
||
!
c
ontainsPeer
(
connectedPeers
,
peer2
)
||
!
c
ontainsPeer
(
connectedPeers
,
peer3
)
{
if
!
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer1
)
||
!
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer2
)
||
!
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer3
)
{
t
.
Fatal
(
"Peers not connected that should be connected"
)
}
if
c
ontainsPeer
(
connectedPeers
,
peer4
)
||
c
ontainsPeer
(
connectedPeers
,
peer5
)
{
if
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer4
)
||
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer5
)
{
t
.
Fatal
(
"Peers connected that shouldn't be connected"
)
}
...
...
@@ -145,7 +102,7 @@ func TestAddingAndRemovingPeers(t *testing.T) {
peerManager
.
Disconnected
(
peer1
)
connectedPeers
=
peerManager
.
ConnectedPeers
()
if
c
ontainsPeer
(
connectedPeers
,
peer1
)
{
if
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer1
)
{
t
.
Fatal
(
"Peer should have been disconnected but was not"
)
}
...
...
@@ -154,7 +111,7 @@ func TestAddingAndRemovingPeers(t *testing.T) {
peerManager
.
Disconnected
(
peer2
)
connectedPeers
=
peerManager
.
ConnectedPeers
()
if
!
c
ontainsPeer
(
connectedPeers
,
peer2
)
{
if
!
testutil
.
C
ontainsPeer
(
connectedPeers
,
peer2
)
{
t
.
Fatal
(
"Peer was disconnected but should not have been"
)
}
}
...
...
@@ -164,7 +121,7 @@ func TestSendingMessagesToPeers(t *testing.T) {
messagesSent
:=
make
(
chan
messageSent
)
peerQueueFactory
:=
makePeerQueueFactory
(
messagesSent
)
tp
:=
g
eneratePeers
(
5
)
tp
:=
testutil
.
G
eneratePeers
(
5
)
peer1
,
peer2
,
peer3
,
peer4
,
peer5
:=
tp
[
0
],
tp
[
1
],
tp
[
2
],
tp
[
3
],
tp
[
4
]
peerManager
:=
New
(
ctx
,
peerQueueFactory
)
...
...
@@ -174,8 +131,8 @@ func TestSendingMessagesToPeers(t *testing.T) {
peerManager
.
Connected
(
peer2
,
nil
)
peerManager
.
Connected
(
peer3
,
nil
)
entries
:=
g
enerateEntries
(
5
,
false
)
ses
:=
g
enerateSessionID
()
entries
:=
testutil
.
G
enerateEntries
(
5
,
false
)
ses
:=
testutil
.
G
enerateSessionID
()
peerManager
.
SendMessage
(
entries
,
nil
,
ses
)
...
...
@@ -185,14 +142,14 @@ func TestSendingMessagesToPeers(t *testing.T) {
t
.
Fatal
(
"Incorrect number of peers received messages"
)
}
if
!
c
ontainsPeer
(
peersReceived
,
peer1
)
||
!
c
ontainsPeer
(
peersReceived
,
peer2
)
||
!
c
ontainsPeer
(
peersReceived
,
peer3
)
{
if
!
testutil
.
C
ontainsPeer
(
peersReceived
,
peer1
)
||
!
testutil
.
C
ontainsPeer
(
peersReceived
,
peer2
)
||
!
testutil
.
C
ontainsPeer
(
peersReceived
,
peer3
)
{
t
.
Fatal
(
"Peers should have received message but did not"
)
}
if
c
ontainsPeer
(
peersReceived
,
peer4
)
||
c
ontainsPeer
(
peersReceived
,
peer5
)
{
if
testutil
.
C
ontainsPeer
(
peersReceived
,
peer4
)
||
testutil
.
C
ontainsPeer
(
peersReceived
,
peer5
)
{
t
.
Fatal
(
"Peers received message but should not have"
)
}
...
...
@@ -206,17 +163,17 @@ func TestSendingMessagesToPeers(t *testing.T) {
t
.
Fatal
(
"Incorrect number of peers received messages"
)
}
if
!
c
ontainsPeer
(
peersReceived
,
peer1
)
||
!
c
ontainsPeer
(
peersReceived
,
peer3
)
{
if
!
testutil
.
C
ontainsPeer
(
peersReceived
,
peer1
)
||
!
testutil
.
C
ontainsPeer
(
peersReceived
,
peer3
)
{
t
.
Fatal
(
"Peers should have received message but did not"
)
}
if
c
ontainsPeer
(
peersReceived
,
peer2
)
||
c
ontainsPeer
(
peersReceived
,
peer5
)
{
if
testutil
.
C
ontainsPeer
(
peersReceived
,
peer2
)
||
testutil
.
C
ontainsPeer
(
peersReceived
,
peer5
)
{
t
.
Fatal
(
"Peers received message but should not have"
)
}
if
c
ontainsPeer
(
peersReceived
,
peer4
)
{
if
testutil
.
C
ontainsPeer
(
peersReceived
,
peer4
)
{
t
.
Fatal
(
"Peers targeted received message but was not connected"
)
}
}
testutil/testutil.go
0 → 100644
View file @
9532d009
package
testutil
import
(
bsmsg
"github.com/ipfs/go-bitswap/message"
"github.com/ipfs/go-bitswap/wantlist"
cid
"github.com/ipfs/go-cid"
blocksutil
"github.com/ipfs/go-ipfs-blocksutil"
peer
"github.com/libp2p/go-libp2p-peer"
)
var
blockGenerator
=
blocksutil
.
NewBlockGenerator
()
var
prioritySeq
int
// GenerateCids produces n content identifiers
func
GenerateCids
(
n
int
)
[]
cid
.
Cid
{
cids
:=
make
([]
cid
.
Cid
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
c
:=
blockGenerator
.
Next
()
.
Cid
()
cids
=
append
(
cids
,
c
)
}
return
cids
}
// GenerateEntries makes fake bitswap message entries
func
GenerateEntries
(
n
int
,
isCancel
bool
)
[]
*
bsmsg
.
Entry
{
bsmsgs
:=
make
([]
*
bsmsg
.
Entry
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
prioritySeq
++
msg
:=
&
bsmsg
.
Entry
{
Entry
:
wantlist
.
NewRefEntry
(
blockGenerator
.
Next
()
.
Cid
(),
prioritySeq
),
Cancel
:
isCancel
,
}
bsmsgs
=
append
(
bsmsgs
,
msg
)
}
return
bsmsgs
}
var
peerSeq
int
// GeneratePeers creates n peer ids
func
GeneratePeers
(
n
int
)
[]
peer
.
ID
{
peerIds
:=
make
([]
peer
.
ID
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
peerSeq
++
p
:=
peer
.
ID
(
peerSeq
)
peerIds
=
append
(
peerIds
,
p
)
}
return
peerIds
}
var
nextSession
uint64
// GenerateSessionID make a unit session identifier
func
GenerateSessionID
()
uint64
{
nextSession
++
return
uint64
(
nextSession
)
}
// ContainsPeer returns true if a peer is found n a list of peers
func
ContainsPeer
(
peers
[]
peer
.
ID
,
p
peer
.
ID
)
bool
{
for
_
,
n
:=
range
peers
{
if
p
==
n
{
return
true
}
}
return
false
}
wantmanager/wantmanager_test.go
View file @
9532d009
...
...
@@ -6,42 +6,13 @@ import (
"sync"
"testing"
"github.com/ipfs/go-bitswap/testutil"
bsmsg
"github.com/ipfs/go-bitswap/message"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-blocksutil"
"github.com/libp2p/go-libp2p-peer"
)
var
blockGenerator
=
blocksutil
.
NewBlockGenerator
()
func
generateCids
(
n
int
)
[]
cid
.
Cid
{
cids
:=
make
([]
cid
.
Cid
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
c
:=
blockGenerator
.
Next
()
.
Cid
()
cids
=
append
(
cids
,
c
)
}
return
cids
}
var
peerSeq
int
func
generatePeers
(
n
int
)
[]
peer
.
ID
{
peerIds
:=
make
([]
peer
.
ID
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
peerSeq
++
p
:=
peer
.
ID
(
peerSeq
)
peerIds
=
append
(
peerIds
,
p
)
}
return
peerIds
}
var
nextSession
uint64
func
generateSessionID
()
uint64
{
nextSession
++
return
uint64
(
nextSession
)
}
type
fakeWantSender
struct
{
lk
sync
.
RWMutex
lastWantSet
wantSet
...
...
@@ -66,11 +37,11 @@ func setupTestFixturesAndInitialWantList() (
// setup fixtures
wantSender
:=
&
fakeWantSender
{}
wantManager
:=
New
(
ctx
)
keys
:=
g
enerateCids
(
10
)
otherKeys
:=
g
enerateCids
(
5
)
peers
:=
g
eneratePeers
(
10
)
session
:=
g
enerateSessionID
()
otherSession
:=
g
enerateSessionID
()
keys
:=
testutil
.
G
enerateCids
(
10
)
otherKeys
:=
testutil
.
G
enerateCids
(
5
)
peers
:=
testutil
.
G
eneratePeers
(
10
)
session
:=
testutil
.
G
enerateSessionID
()
otherSession
:=
testutil
.
G
enerateSessionID
()
// startup wantManager
wantManager
.
SetDelegate
(
wantSender
)
...
...
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