Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
e094e221
Commit
e094e221
authored
Jan 24, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add handler middleware for mock recorder and improve TestPing in windows
parent
1971bae2
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
60 additions
and
42 deletions
+60
-42
go.sum
go.sum
+0
-29
mock.go
pkg/p2p/mock/mock.go
+33
-7
p2p.go
pkg/p2p/p2p.go
+5
-1
pingpong.go
pkg/pingpong/pingpong.go
+0
-1
pingpong_test.go
pkg/pingpong/pingpong_test.go
+22
-4
No files found.
go.sum
View file @
e094e221
This diff is collapsed.
Click to expand it.
pkg/p2p/mock/mock.go
View file @
e094e221
...
...
@@ -14,16 +14,32 @@ import (
)
type
Recorder
struct
{
records
map
[
string
][]
Record
recordsMu
sync
.
Mutex
protocols
[]
p2p
.
ProtocolSpec
records
map
[
string
][]
Record
recordsMu
sync
.
Mutex
protocols
[]
p2p
.
ProtocolSpec
middlewares
[]
p2p
.
HandlerMiddleware
}
func
NewRecorder
(
protocols
...
p2p
.
ProtocolSpec
)
*
Recorder
{
return
&
Recorder
{
records
:
make
(
map
[
string
][]
Record
),
protocols
:
protocols
,
func
WithProtocols
(
protocols
...
p2p
.
ProtocolSpec
)
Option
{
return
optionFunc
(
func
(
r
*
Recorder
)
{
r
.
protocols
=
append
(
r
.
protocols
,
protocols
...
)
})
}
func
WithMiddlewares
(
middlewares
...
p2p
.
HandlerMiddleware
)
Option
{
return
optionFunc
(
func
(
r
*
Recorder
)
{
r
.
middlewares
=
append
(
r
.
middlewares
,
middlewares
...
)
})
}
func
NewRecorder
(
opts
...
Option
)
*
Recorder
{
r
:=
&
Recorder
{
records
:
make
(
map
[
string
][]
Record
),
}
for
_
,
o
:=
range
opts
{
o
.
apply
(
r
)
}
return
r
}
func
(
r
*
Recorder
)
NewStream
(
_
context
.
Context
,
overlay
,
protocolName
,
streamName
,
version
string
)
(
p2p
.
Stream
,
error
)
{
...
...
@@ -45,6 +61,9 @@ func (r *Recorder) NewStream(_ context.Context, overlay, protocolName, streamNam
if
handler
==
nil
{
return
nil
,
fmt
.
Errorf
(
"unsupported protocol stream %q %q %q"
,
protocolName
,
streamName
,
version
)
}
for
_
,
m
:=
range
r
.
middlewares
{
handler
=
m
(
handler
)
}
go
handler
(
p2p
.
Peer
{
Address
:
overlay
},
streamIn
)
id
:=
overlay
+
p2p
.
NewSwarmStreamName
(
protocolName
,
streamName
,
version
)
...
...
@@ -167,3 +186,10 @@ func (r *record) bytes() []byte {
return
r
.
b
}
type
Option
interface
{
apply
(
*
Recorder
)
}
type
optionFunc
func
(
*
Recorder
)
func
(
f
optionFunc
)
apply
(
r
*
Recorder
)
{
f
(
r
)
}
pkg/p2p/p2p.go
View file @
e094e221
...
...
@@ -34,9 +34,13 @@ type ProtocolSpec struct {
type
StreamSpec
struct
{
Name
string
Version
string
Handler
func
(
Peer
,
Stream
)
Handler
HandlerFunc
}
type
HandlerFunc
func
(
Peer
,
Stream
)
type
HandlerMiddleware
func
(
HandlerFunc
)
HandlerFunc
type
IncompatibleStreamError
struct
{
err
error
}
...
...
pkg/pingpong/pingpong.go
View file @
e094e221
...
...
@@ -94,7 +94,6 @@ func (s *Service) Handler(peer p2p.Peer, stream p2p.Stream) {
w
,
r
:=
protobuf
.
NewWriterAndReader
(
stream
)
defer
stream
.
Close
()
fmt
.
Printf
(
"Initiate pinpong for peer %s"
,
peer
)
var
ping
Ping
for
{
if
err
:=
r
.
ReadMsg
(
&
ping
);
err
!=
nil
{
...
...
pkg/pingpong/pingpong_test.go
View file @
e094e221
...
...
@@ -9,15 +9,17 @@ import (
"context"
"fmt"
"io/ioutil"
"runtime"
"testing"
"time"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/ethersphere/bee/pkg/p2p/protobuf"
"github.com/ethersphere/bee/pkg/pingpong"
)
func
TestPing
(
t
*
testing
.
T
)
{
logger
:=
logging
.
New
(
ioutil
.
Discard
)
...
...
@@ -27,7 +29,18 @@ func TestPing(t *testing.T) {
})
// setup the stream recorder to record stream data
recorder
:=
mock
.
NewRecorder
(
server
.
Protocol
())
recorder
:=
mock
.
NewRecorder
(
mock
.
WithProtocols
(
server
.
Protocol
()),
mock
.
WithMiddlewares
(
func
(
f
p2p
.
HandlerFunc
)
p2p
.
HandlerFunc
{
if
runtime
.
GOOS
==
"windows"
{
// windows has a bit lower time resolution
// so, slow down the handler with a middleware
// not to get 0s for rtt value
time
.
Sleep
(
100
*
time
.
Millisecond
)
}
return
f
}),
)
// create a pingpong client that will do pinging
client
:=
pingpong
.
New
(
pingpong
.
Options
{
...
...
@@ -36,13 +49,18 @@ func TestPing(t *testing.T) {
})
// ping
peerID
:=
"
/p2p/QmZt98UimwpW9ptJumKTq7B7t3FzNfyoWVNGcd8PFCd7XS
"
peerID
:=
"
124
"
greetings
:=
[]
string
{
"hey"
,
"there"
,
"fella"
}
_
,
err
:=
client
.
Ping
(
context
.
Background
(),
peerID
,
greetings
...
)
rtt
,
err
:=
client
.
Ping
(
context
.
Background
(),
peerID
,
greetings
...
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// check that RTT is a sane value
if
rtt
<=
0
{
t
.
Errorf
(
"invalid RTT value %v"
,
rtt
)
}
// get a record for this stream
records
,
err
:=
recorder
.
Records
(
peerID
,
"pingpong"
,
"pingpong"
,
"1.0.0"
)
if
err
!=
nil
{
...
...
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