Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
bb9d55ac
Commit
bb9d55ac
authored
6 years ago
by
Erin Swenson-Healey
Browse files
Options
Download
Email Patches
Plain Diff
add test which demonstrates how Mocknet latency works
parent
4b33a800
master
2018-Q4-OKR
feat/protobuf
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-nza0mn
multistream-ping
punching
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
p2p/net/mock/mock_test.go
+53
-0
p2p/net/mock/mock_test.go
with
53 additions
and
0 deletions
+53
-0
p2p/net/mock/mock_test.go
View file @
bb9d55ac
...
...
@@ -582,3 +582,56 @@ func TestLimitedStreams(t *testing.T) {
t
.
Fatal
(
"Expected 2ish seconds but got "
,
time
.
Since
(
before
))
}
}
func
TestStreamsWithLatency
(
t
*
testing
.
T
)
{
latency
:=
time
.
Millisecond
*
500
mn
,
err
:=
WithNPeers
(
context
.
Background
(),
2
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// configure the Mocknet with some latency and link/connect its peers
mn
.
SetLinkDefaults
(
LinkOptions
{
Latency
:
latency
})
mn
.
LinkAll
()
mn
.
ConnectAllButSelf
()
msg
:=
[]
byte
(
"ping"
)
mln
:=
len
(
msg
)
var
wg
sync
.
WaitGroup
// we'll write once to a single stream
wg
.
Add
(
1
)
handler
:=
func
(
s
inet
.
Stream
)
{
b
:=
make
([]
byte
,
mln
)
if
_
,
err
:=
io
.
ReadFull
(
s
,
b
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
wg
.
Done
()
s
.
Close
()
}
mn
.
Hosts
()[
0
]
.
SetStreamHandler
(
protocol
.
TestingID
,
handler
)
mn
.
Hosts
()[
1
]
.
SetStreamHandler
(
protocol
.
TestingID
,
handler
)
s
,
err
:=
mn
.
Hosts
()[
0
]
.
NewStream
(
context
.
Background
(),
mn
.
Hosts
()[
1
]
.
ID
(),
protocol
.
TestingID
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// writing to the stream will be subject to our configured latency
checkpoint
:=
time
.
Now
()
if
_
,
err
:=
s
.
Write
(
msg
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
wg
.
Wait
()
delta
:=
time
.
Since
(
checkpoint
)
if
!
within
(
delta
,
latency
,
time
.
Millisecond
*
50
)
{
t
.
Fatalf
(
"Expected write to take ~%s, but took %s"
,
latency
.
String
(),
delta
.
String
())
}
}
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help