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
3b7ae9b8
Commit
3b7ae9b8
authored
Nov 27, 2018
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test(messagequeue): Add test for messagequeue
parent
9532d009
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
195 additions
and
15 deletions
+195
-15
messagequeue/messagequeue.go
messagequeue/messagequeue.go
+18
-10
messagequeue/messagequeue_test.go
messagequeue/messagequeue_test.go
+161
-0
peermanager/peermanager_test.go
peermanager/peermanager_test.go
+3
-3
testutil/testutil.go
testutil/testutil.go
+13
-2
No files found.
messagequeue/messagequeue.go
View file @
3b7ae9b8
...
...
@@ -14,12 +14,17 @@ import (
var
log
=
logging
.
Logger
(
"bitswap"
)
type
MessageNetwork
interface
{
ConnectTo
(
context
.
Context
,
peer
.
ID
)
error
NewMessageSender
(
context
.
Context
,
peer
.
ID
)
(
bsnet
.
MessageSender
,
error
)
}
type
MessageQueue
struct
{
p
peer
.
ID
outlk
sync
.
Mutex
out
bsmsg
.
BitSwapMessage
network
bsnet
.
BitSwap
Network
network
Message
Network
wl
*
wantlist
.
ThreadSafe
sender
bsnet
.
MessageSender
...
...
@@ -30,7 +35,7 @@ type MessageQueue struct {
done
chan
struct
{}
}
func
New
(
p
peer
.
ID
,
network
bsnet
.
BitSwap
Network
)
*
MessageQueue
{
func
New
(
p
peer
.
ID
,
network
Message
Network
)
*
MessageQueue
{
return
&
MessageQueue
{
done
:
make
(
chan
struct
{}),
work
:
make
(
chan
struct
{},
1
),
...
...
@@ -90,22 +95,25 @@ func (mq *MessageQueue) AddMessage(entries []*bsmsg.Entry, ses uint64) {
func
(
mq
*
MessageQueue
)
Startup
(
ctx
context
.
Context
,
initialEntries
[]
*
wantlist
.
Entry
)
{
// new peer, we will want to give them our full wantlist
fullwantlist
:=
bsmsg
.
New
(
true
)
for
_
,
e
:=
range
initialEntries
{
for
k
:=
range
e
.
SesTrk
{
mq
.
wl
.
AddEntry
(
e
,
k
)
if
len
(
initialEntries
)
>
0
{
fullwantlist
:=
bsmsg
.
New
(
true
)
for
_
,
e
:=
range
initialEntries
{
for
k
:=
range
e
.
SesTrk
{
mq
.
wl
.
AddEntry
(
e
,
k
)
}
fullwantlist
.
AddEntry
(
e
.
Cid
,
e
.
Priority
)
}
fullwantlist
.
AddEntry
(
e
.
Cid
,
e
.
Priority
)
mq
.
out
=
fullwantlist
mq
.
work
<-
struct
{}{}
}
mq
.
out
=
fullwantlist
mq
.
work
<-
struct
{}{}
go
mq
.
runQueue
(
ctx
)
}
func
(
mq
*
MessageQueue
)
Shutdown
()
{
close
(
mq
.
done
)
}
func
(
mq
*
MessageQueue
)
runQueue
(
ctx
context
.
Context
)
{
for
{
select
{
...
...
messagequeue/messagequeue_test.go
0 → 100644
View file @
3b7ae9b8
package
messagequeue
import
(
"context"
"testing"
"time"
"github.com/ipfs/go-bitswap/testutil"
bsmsg
"github.com/ipfs/go-bitswap/message"
bsnet
"github.com/ipfs/go-bitswap/network"
peer
"github.com/libp2p/go-libp2p-peer"
)
type
fakeMessageNetwork
struct
{
connectError
error
messageSenderError
error
messageSender
bsnet
.
MessageSender
}
func
(
fmn
*
fakeMessageNetwork
)
ConnectTo
(
context
.
Context
,
peer
.
ID
)
error
{
return
fmn
.
connectError
}
func
(
fmn
*
fakeMessageNetwork
)
NewMessageSender
(
context
.
Context
,
peer
.
ID
)
(
bsnet
.
MessageSender
,
error
)
{
if
fmn
.
messageSenderError
==
nil
{
return
fmn
.
messageSender
,
nil
}
else
{
return
nil
,
fmn
.
messageSenderError
}
}
type
fakeMessageSender
struct
{
sendError
error
fullClosed
chan
<-
struct
{}
reset
chan
<-
struct
{}
messagesSent
chan
<-
bsmsg
.
BitSwapMessage
}
func
(
fms
*
fakeMessageSender
)
SendMsg
(
ctx
context
.
Context
,
msg
bsmsg
.
BitSwapMessage
)
error
{
fms
.
messagesSent
<-
msg
return
fms
.
sendError
}
func
(
fms
*
fakeMessageSender
)
Close
()
error
{
fms
.
fullClosed
<-
struct
{}{};
return
nil
}
func
(
fms
*
fakeMessageSender
)
Reset
()
error
{
fms
.
reset
<-
struct
{}{};
return
nil
}
func
collectMessages
(
ctx
context
.
Context
,
t
*
testing
.
T
,
messagesSent
<-
chan
bsmsg
.
BitSwapMessage
,
timeout
time
.
Duration
)
[]
bsmsg
.
BitSwapMessage
{
var
messagesReceived
[]
bsmsg
.
BitSwapMessage
timeoutctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
timeout
)
defer
cancel
()
for
{
select
{
case
messageReceived
:=
<-
messagesSent
:
messagesReceived
=
append
(
messagesReceived
,
messageReceived
)
case
<-
timeoutctx
.
Done
()
:
return
messagesReceived
}
}
}
func
totalEntriesLength
(
messages
[]
bsmsg
.
BitSwapMessage
)
int
{
totalLength
:=
0
for
_
,
messages
:=
range
messages
{
totalLength
+=
len
(
messages
.
Wantlist
())
}
return
totalLength
}
func
TestStartupAndShutdown
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
fakeSender
:=
&
fakeMessageSender
{
nil
,
fullClosedChan
,
resetChan
,
messagesSent
}
fakenet
:=
&
fakeMessageNetwork
{
nil
,
nil
,
fakeSender
}
peerID
:=
testutil
.
GeneratePeers
(
1
)[
0
]
messageQueue
:=
New
(
peerID
,
fakenet
)
ses
:=
testutil
.
GenerateSessionID
()
wl
:=
testutil
.
GenerateWantlist
(
10
,
ses
)
messageQueue
.
Startup
(
ctx
,
wl
.
Entries
())
messages
:=
collectMessages
(
ctx
,
t
,
messagesSent
,
10
*
time
.
Millisecond
)
if
len
(
messages
)
!=
1
{
t
.
Fatal
(
"wrong number of messages were sent for initial wants"
)
}
firstMessage
:=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
())
!=
wl
.
Len
()
{
t
.
Fatal
(
"did not add all wants to want list"
)
}
for
_
,
entry
:=
range
firstMessage
.
Wantlist
()
{
if
entry
.
Cancel
{
t
.
Fatal
(
"initial add sent cancel entry when it should not have"
)
}
}
messageQueue
.
Shutdown
()
timeoutctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
10
*
time
.
Millisecond
)
defer
cancel
()
select
{
case
<-
fullClosedChan
:
case
<-
resetChan
:
t
.
Fatal
(
"message sender should have been closed but was reset"
)
case
<-
timeoutctx
.
Done
()
:
t
.
Fatal
(
"message sender should have been closed but wasn't"
)
}
}
func
TestSendingMessagesDeduped
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
fakeSender
:=
&
fakeMessageSender
{
nil
,
fullClosedChan
,
resetChan
,
messagesSent
}
fakenet
:=
&
fakeMessageNetwork
{
nil
,
nil
,
fakeSender
}
peerID
:=
testutil
.
GeneratePeers
(
1
)[
0
]
messageQueue
:=
New
(
peerID
,
fakenet
)
ses1
:=
testutil
.
GenerateSessionID
()
ses2
:=
testutil
.
GenerateSessionID
()
entries
:=
testutil
.
GenerateMessageEntries
(
10
,
false
)
messageQueue
.
Startup
(
ctx
,
nil
)
messageQueue
.
AddMessage
(
entries
,
ses1
)
messageQueue
.
AddMessage
(
entries
,
ses2
)
messages
:=
collectMessages
(
ctx
,
t
,
messagesSent
,
10
*
time
.
Millisecond
)
if
totalEntriesLength
(
messages
)
!=
len
(
entries
)
{
t
.
Fatal
(
"Messages were not deduped"
)
}
}
func
TestSendingMessagesPartialDupe
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
fakeSender
:=
&
fakeMessageSender
{
nil
,
fullClosedChan
,
resetChan
,
messagesSent
}
fakenet
:=
&
fakeMessageNetwork
{
nil
,
nil
,
fakeSender
}
peerID
:=
testutil
.
GeneratePeers
(
1
)[
0
]
messageQueue
:=
New
(
peerID
,
fakenet
)
ses1
:=
testutil
.
GenerateSessionID
()
ses2
:=
testutil
.
GenerateSessionID
()
entries
:=
testutil
.
GenerateMessageEntries
(
10
,
false
)
moreEntries
:=
testutil
.
GenerateMessageEntries
(
5
,
false
)
secondEntries
:=
append
(
entries
[
5
:
],
moreEntries
...
)
messageQueue
.
Startup
(
ctx
,
nil
)
messageQueue
.
AddMessage
(
entries
,
ses1
)
messageQueue
.
AddMessage
(
secondEntries
,
ses2
)
messages
:=
collectMessages
(
ctx
,
t
,
messagesSent
,
20
*
time
.
Millisecond
)
if
totalEntriesLength
(
messages
)
!=
len
(
entries
)
+
len
(
moreEntries
)
{
t
.
Fatal
(
"messages were not correctly deduped"
)
}
}
peermanager/peermanager_test.go
View file @
3b7ae9b8
...
...
@@ -131,13 +131,13 @@ func TestSendingMessagesToPeers(t *testing.T) {
peerManager
.
Connected
(
peer2
,
nil
)
peerManager
.
Connected
(
peer3
,
nil
)
entries
:=
testutil
.
GenerateEntries
(
5
,
false
)
entries
:=
testutil
.
Generate
Message
Entries
(
5
,
false
)
ses
:=
testutil
.
GenerateSessionID
()
peerManager
.
SendMessage
(
entries
,
nil
,
ses
)
peersReceived
:=
collectAndCheckMessages
(
ctx
,
t
,
messagesSent
,
entries
,
ses
,
20
0
*
time
.
Millisecond
)
ctx
,
t
,
messagesSent
,
entries
,
ses
,
1
0
*
time
.
Millisecond
)
if
len
(
peersReceived
)
!=
3
{
t
.
Fatal
(
"Incorrect number of peers received messages"
)
}
...
...
@@ -157,7 +157,7 @@ func TestSendingMessagesToPeers(t *testing.T) {
peersToSendTo
=
append
(
peersToSendTo
,
peer1
,
peer3
,
peer4
)
peerManager
.
SendMessage
(
entries
,
peersToSendTo
,
ses
)
peersReceived
=
collectAndCheckMessages
(
ctx
,
t
,
messagesSent
,
entries
,
ses
,
20
0
*
time
.
Millisecond
)
ctx
,
t
,
messagesSent
,
entries
,
ses
,
1
0
*
time
.
Millisecond
)
if
len
(
peersReceived
)
!=
2
{
t
.
Fatal
(
"Incorrect number of peers received messages"
)
...
...
testutil/testutil.go
View file @
3b7ae9b8
...
...
@@ -21,8 +21,19 @@ func GenerateCids(n int) []cid.Cid {
return
cids
}
// GenerateEntries makes fake bitswap message entries
func
GenerateEntries
(
n
int
,
isCancel
bool
)
[]
*
bsmsg
.
Entry
{
// GenerateWantlist makes a populated wantlist
func
GenerateWantlist
(
n
int
,
ses
uint64
)
*
wantlist
.
ThreadSafe
{
wl
:=
wantlist
.
NewThreadSafe
()
for
i
:=
0
;
i
<
n
;
i
++
{
prioritySeq
++
entry
:=
wantlist
.
NewRefEntry
(
blockGenerator
.
Next
()
.
Cid
(),
prioritySeq
)
wl
.
AddEntry
(
entry
,
ses
)
}
return
wl
}
// GenerateMessageEntries makes fake bitswap message entries
func
GenerateMessageEntries
(
n
int
,
isCancel
bool
)
[]
*
bsmsg
.
Entry
{
bsmsgs
:=
make
([]
*
bsmsg
.
Entry
,
0
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
prioritySeq
++
...
...
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