Skip to content
GitLab
Menu
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
Jun 05, 2018
by
Erin Swenson-Healey
Browse files
add test which demonstrates how Mocknet latency works
parent
4b33a800
Changes
1
Hide whitespace changes
Inline
Side-by-side
p2p/net/mock/mock_test.go
View file @
bb9d55ac
...
@@ -582,3 +582,56 @@ func TestLimitedStreams(t *testing.T) {
...
@@ -582,3 +582,56 @@ func TestLimitedStreams(t *testing.T) {
t
.
Fatal
(
"Expected 2ish seconds but got "
,
time
.
Since
(
before
))
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
())
}
}
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