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
ba11ef59
Commit
ba11ef59
authored
Feb 20, 2019
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: fix race when counting tagged peers
parent
e6a2a408
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
2 deletions
+15
-2
sessionpeermanager/sessionpeermanager_test.go
sessionpeermanager/sessionpeermanager_test.go
+15
-2
No files found.
sessionpeermanager/sessionpeermanager_test.go
View file @
ba11ef59
...
...
@@ -41,18 +41,24 @@ func (fppf *fakePeerProviderFinder) FindProvidersAsync(ctx context.Context, c ci
}
type
fakePeerTagger
struct
{
lk
sync
.
Mutex
taggedPeers
[]
peer
.
ID
wait
sync
.
WaitGroup
}
func
(
fpt
*
fakePeerTagger
)
TagPeer
(
p
peer
.
ID
,
tag
string
,
n
int
)
{
fpt
.
wait
.
Add
(
1
)
fpt
.
lk
.
Lock
()
defer
fpt
.
lk
.
Unlock
()
fpt
.
taggedPeers
=
append
(
fpt
.
taggedPeers
,
p
)
}
func
(
fpt
*
fakePeerTagger
)
UntagPeer
(
p
peer
.
ID
,
tag
string
)
{
defer
fpt
.
wait
.
Done
()
fpt
.
lk
.
Lock
()
defer
fpt
.
lk
.
Unlock
()
for
i
:=
0
;
i
<
len
(
fpt
.
taggedPeers
);
i
++
{
if
fpt
.
taggedPeers
[
i
]
==
p
{
fpt
.
taggedPeers
[
i
]
=
fpt
.
taggedPeers
[
len
(
fpt
.
taggedPeers
)
-
1
]
...
...
@@ -62,6 +68,12 @@ func (fpt *fakePeerTagger) UntagPeer(p peer.ID, tag string) {
}
}
func
(
fpt
*
fakePeerTagger
)
count
()
int
{
fpt
.
lk
.
Lock
()
defer
fpt
.
lk
.
Unlock
()
return
len
(
fpt
.
taggedPeers
)
}
func
TestFindingMorePeers
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
...
...
@@ -195,6 +207,7 @@ func TestOrderingPeers(t *testing.T) {
t
.
Fatal
(
"should not return the same random peers each time"
)
}
}
func
TestUntaggingPeers
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
10
*
time
.
Millisecond
)
...
...
@@ -216,13 +229,13 @@ func TestUntaggingPeers(t *testing.T) {
}
time
.
Sleep
(
2
*
time
.
Millisecond
)
if
len
(
fpt
.
taggedPeers
)
!=
len
(
peers
)
{
if
fpt
.
count
(
)
!=
len
(
peers
)
{
t
.
Fatal
(
"Peers were not tagged!"
)
}
<-
ctx
.
Done
()
fpt
.
wait
.
Wait
()
if
len
(
fpt
.
taggedPeers
)
!=
0
{
if
fpt
.
count
(
)
!=
0
{
t
.
Fatal
(
"Peers were not untagged!"
)
}
}
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