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
2b839164
Commit
2b839164
authored
Mar 19, 2020
by
Dirk McCormick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: race in tests
parent
e9862947
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
36 deletions
+36
-36
internal/messagequeue/messagequeue_test.go
internal/messagequeue/messagequeue_test.go
+36
-36
No files found.
internal/messagequeue/messagequeue_test.go
View file @
2b839164
...
...
@@ -86,13 +86,13 @@ type fakeMessageSender struct {
sendError
error
fullClosed
chan
<-
struct
{}
reset
chan
<-
struct
{}
messagesSent
chan
<-
bsmsg
.
BitSwapMessage
messagesSent
chan
<-
[]
bsmsg
.
Entry
sendErrors
chan
<-
error
supportsHave
bool
}
func
newFakeMessageSender
(
sendError
error
,
fullClosed
chan
<-
struct
{},
reset
chan
<-
struct
{},
messagesSent
chan
<-
bsmsg
.
BitSwapMessage
,
sendErrors
chan
<-
error
,
supportsHave
bool
)
*
fakeMessageSender
{
messagesSent
chan
<-
[]
bsmsg
.
Entry
,
sendErrors
chan
<-
error
,
supportsHave
bool
)
*
fakeMessageSender
{
return
&
fakeMessageSender
{
sendError
:
sendError
,
...
...
@@ -112,7 +112,7 @@ func (fms *fakeMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMess
fms
.
sendErrors
<-
fms
.
sendError
return
fms
.
sendError
}
fms
.
messagesSent
<-
msg
fms
.
messagesSent
<-
msg
.
Wantlist
()
return
nil
}
func
(
fms
*
fakeMessageSender
)
clearSendError
()
{
...
...
@@ -129,9 +129,9 @@ func mockTimeoutCb(peer.ID, []cid.Cid) {}
func
collectMessages
(
ctx
context
.
Context
,
t
*
testing
.
T
,
messagesSent
<-
chan
bsmsg
.
BitSwapMessage
,
timeout
time
.
Duration
)
[]
bsmsg
.
BitSwapMessage
{
var
messagesReceived
[]
bsmsg
.
BitSwapMessage
messagesSent
<-
chan
[]
bsmsg
.
Entry
,
timeout
time
.
Duration
)
[]
[]
bsmsg
.
Entry
{
var
messagesReceived
[]
[]
bsmsg
.
Entry
timeoutctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
timeout
)
defer
cancel
()
for
{
...
...
@@ -144,17 +144,17 @@ func collectMessages(ctx context.Context,
}
}
func
totalEntriesLength
(
messages
[]
bsmsg
.
BitSwapMessage
)
int
{
func
totalEntriesLength
(
messages
[]
[]
bsmsg
.
Entry
)
int
{
totalLength
:=
0
for
_
,
m
essages
:=
range
messages
{
totalLength
+=
len
(
m
essages
.
Wantlist
()
)
for
_
,
m
:=
range
messages
{
totalLength
+=
len
(
m
)
}
return
totalLength
}
func
TestStartupAndShutdown
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -172,10 +172,10 @@ func TestStartupAndShutdown(t *testing.T) {
}
firstMessage
:=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
()
)
!=
len
(
bcstwh
)
{
if
len
(
firstMessage
)
!=
len
(
bcstwh
)
{
t
.
Fatal
(
"did not add all wants to want list"
)
}
for
_
,
entry
:=
range
firstMessage
.
Wantlist
()
{
for
_
,
entry
:=
range
firstMessage
{
if
entry
.
Cancel
{
t
.
Fatal
(
"initial add sent cancel entry when it should not have"
)
}
...
...
@@ -196,7 +196,7 @@ func TestStartupAndShutdown(t *testing.T) {
func
TestSendingMessagesDeduped
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -219,7 +219,7 @@ func TestSendingMessagesDeduped(t *testing.T) {
func
TestSendingMessagesPartialDupe
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -242,7 +242,7 @@ func TestSendingMessagesPartialDupe(t *testing.T) {
func
TestSendingMessagesPriority
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -266,7 +266,7 @@ func TestSendingMessagesPriority(t *testing.T) {
t
.
Fatal
(
"wrong number of wants"
)
}
byCid
:=
make
(
map
[
cid
.
Cid
]
message
.
Entry
)
for
_
,
entry
:=
range
messages
[
0
]
.
Wantlist
()
{
for
_
,
entry
:=
range
messages
[
0
]
{
byCid
[
entry
.
Cid
]
=
entry
}
...
...
@@ -311,7 +311,7 @@ func TestSendingMessagesPriority(t *testing.T) {
func
TestCancelOverridesPendingWants
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -331,7 +331,7 @@ func TestCancelOverridesPendingWants(t *testing.T) {
t
.
Fatal
(
"Wrong message count"
)
}
wb
,
wh
,
cl
:=
filterWantTypes
(
messages
[
0
]
.
Wantlist
()
)
wb
,
wh
,
cl
:=
filterWantTypes
(
messages
[
0
])
if
len
(
wb
)
!=
1
||
!
wb
[
0
]
.
Equals
(
wantBlocks
[
1
])
{
t
.
Fatal
(
"Expected 1 want-block"
)
}
...
...
@@ -345,7 +345,7 @@ func TestCancelOverridesPendingWants(t *testing.T) {
func
TestWantOverridesPendingCancels
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -364,7 +364,7 @@ func TestWantOverridesPendingCancels(t *testing.T) {
t
.
Fatal
(
"Wrong message count"
)
}
wb
,
wh
,
cl
:=
filterWantTypes
(
messages
[
0
]
.
Wantlist
()
)
wb
,
wh
,
cl
:=
filterWantTypes
(
messages
[
0
])
if
len
(
wb
)
!=
1
||
!
wb
[
0
]
.
Equals
(
cancels
[
0
])
{
t
.
Fatal
(
"Expected 1 want-block"
)
}
...
...
@@ -378,7 +378,7 @@ func TestWantOverridesPendingCancels(t *testing.T) {
func
TestWantlistRebroadcast
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -400,7 +400,7 @@ func TestWantlistRebroadcast(t *testing.T) {
// All broadcast want-haves should have been sent
firstMessage
:=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
()
)
!=
len
(
bcstwh
)
{
if
len
(
firstMessage
)
!=
len
(
bcstwh
)
{
t
.
Fatal
(
"wrong number of wants"
)
}
...
...
@@ -413,7 +413,7 @@ func TestWantlistRebroadcast(t *testing.T) {
// All the want-haves should have been rebroadcast
firstMessage
=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
()
)
!=
len
(
bcstwh
)
{
if
len
(
firstMessage
)
!=
len
(
bcstwh
)
{
t
.
Fatal
(
"did not rebroadcast all wants"
)
}
...
...
@@ -429,7 +429,7 @@ func TestWantlistRebroadcast(t *testing.T) {
// All new wants should have been sent
firstMessage
=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
()
)
!=
len
(
wantHaves
)
+
len
(
wantBlocks
)
{
if
len
(
firstMessage
)
!=
len
(
wantHaves
)
+
len
(
wantBlocks
)
{
t
.
Fatal
(
"wrong number of wants"
)
}
...
...
@@ -440,7 +440,7 @@ func TestWantlistRebroadcast(t *testing.T) {
// Both original and new wants should have been rebroadcast
totalWants
:=
len
(
bcstwh
)
+
len
(
wantHaves
)
+
len
(
wantBlocks
)
if
len
(
firstMessage
.
Wantlist
()
)
!=
totalWants
{
if
len
(
firstMessage
)
!=
totalWants
{
t
.
Fatal
(
"did not rebroadcast all wants"
)
}
...
...
@@ -455,10 +455,10 @@ func TestWantlistRebroadcast(t *testing.T) {
// Cancels for each want should have been sent
firstMessage
=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
()
)
!=
len
(
cancels
)
{
if
len
(
firstMessage
)
!=
len
(
cancels
)
{
t
.
Fatal
(
"wrong number of cancels"
)
}
for
_
,
entry
:=
range
firstMessage
.
Wantlist
()
{
for
_
,
entry
:=
range
firstMessage
{
if
!
entry
.
Cancel
{
t
.
Fatal
(
"expected cancels"
)
}
...
...
@@ -468,14 +468,14 @@ func TestWantlistRebroadcast(t *testing.T) {
messageQueue
.
SetRebroadcastInterval
(
10
*
time
.
Millisecond
)
messages
=
collectMessages
(
ctx
,
t
,
messagesSent
,
15
*
time
.
Millisecond
)
firstMessage
=
messages
[
0
]
if
len
(
firstMessage
.
Wantlist
()
)
!=
totalWants
-
len
(
cancels
)
{
if
len
(
firstMessage
)
!=
totalWants
-
len
(
cancels
)
{
t
.
Fatal
(
"did not rebroadcast all wants"
)
}
}
func
TestSendingLargeMessages
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -506,7 +506,7 @@ func TestSendingLargeMessages(t *testing.T) {
func
TestSendToPeerThatDoesntSupportHave
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -530,7 +530,7 @@ func TestSendToPeerThatDoesntSupportHave(t *testing.T) {
if
len
(
messages
)
!=
1
{
t
.
Fatal
(
"wrong number of messages were sent"
,
len
(
messages
))
}
wl
:=
messages
[
0
]
.
Wantlist
()
wl
:=
messages
[
0
]
if
len
(
wl
)
!=
len
(
bcwh
)
{
t
.
Fatal
(
"wrong number of entries in wantlist"
,
len
(
wl
))
}
...
...
@@ -549,7 +549,7 @@ func TestSendToPeerThatDoesntSupportHave(t *testing.T) {
if
len
(
messages
)
!=
1
{
t
.
Fatal
(
"wrong number of messages were sent"
,
len
(
messages
))
}
wl
=
messages
[
0
]
.
Wantlist
()
wl
=
messages
[
0
]
if
len
(
wl
)
!=
len
(
wbs
)
{
t
.
Fatal
(
"should only send want-blocks (no want-haves)"
,
len
(
wl
))
}
...
...
@@ -562,7 +562,7 @@ func TestSendToPeerThatDoesntSupportHave(t *testing.T) {
func
TestSendToPeerThatDoesntSupportHaveMonitorsTimeouts
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -595,7 +595,7 @@ func TestSendToPeerThatDoesntSupportHaveMonitorsTimeouts(t *testing.T) {
func
TestResendAfterError
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -634,7 +634,7 @@ func TestResendAfterError(t *testing.T) {
func
TestResendAfterMaxRetries
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
maxRetries
*
2
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
@@ -713,7 +713,7 @@ func BenchmarkMessageQueue(b *testing.B) {
ctx
:=
context
.
Background
()
createQueue
:=
func
()
*
MessageQueue
{
messagesSent
:=
make
(
chan
bsmsg
.
BitSwapMessage
)
messagesSent
:=
make
(
chan
[]
bsmsg
.
Entry
)
sendErrors
:=
make
(
chan
error
)
resetChan
:=
make
(
chan
struct
{},
1
)
fullClosedChan
:=
make
(
chan
struct
{},
1
)
...
...
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