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
0a053f9e
Commit
0a053f9e
authored
Feb 03, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix streamtest tests
parent
226b12d1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
8 deletions
+80
-8
streamtest.go
pkg/p2p/streamtest/streamtest.go
+3
-1
streamtest_test.go
pkg/p2p/streamtest/streamtest_test.go
+77
-7
No files found.
pkg/p2p/streamtest/streamtest.go
View file @
0a053f9e
...
@@ -73,7 +73,9 @@ func (r *Recorder) NewStream(_ context.Context, addr swarm.Address, protocolName
...
@@ -73,7 +73,9 @@ func (r *Recorder) NewStream(_ context.Context, addr swarm.Address, protocolName
record
:=
&
Record
{
in
:
recordIn
,
out
:
recordOut
}
record
:=
&
Record
{
in
:
recordIn
,
out
:
recordOut
}
go
func
()
{
go
func
()
{
err
:=
handler
(
p2p
.
Peer
{
Address
:
addr
},
streamIn
)
err
:=
handler
(
p2p
.
Peer
{
Address
:
addr
},
streamIn
)
record
.
setErr
(
err
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
record
.
setErr
(
err
)
}
}()
}()
id
:=
addr
.
String
()
+
p2p
.
NewSwarmStreamName
(
protocolName
,
streamName
,
version
)
id
:=
addr
.
String
()
+
p2p
.
NewSwarmStreamName
(
protocolName
,
streamName
,
version
)
...
...
pkg/p2p/streamtest/streamtest_test.go
View file @
0a053f9e
...
@@ -108,7 +108,7 @@ func TestRecorder(t *testing.T) {
...
@@ -108,7 +108,7 @@ func TestRecorder(t *testing.T) {
"What is your name?
\n
What is your quest?
\n
What is your favorite color?
\n
"
,
"What is your name?
\n
What is your quest?
\n
What is your favorite color?
\n
"
,
"Sir Lancelot of Camelot
\n
To seek the Holy Grail.
\n
Blue.
\n
"
,
"Sir Lancelot of Camelot
\n
To seek the Holy Grail.
\n
Blue.
\n
"
,
},
},
})
}
,
nil
)
}
}
func
TestRecorder_errStreamNotSupported
(
t
*
testing
.
T
)
{
func
TestRecorder_errStreamNotSupported
(
t
*
testing
.
T
)
{
...
@@ -179,7 +179,7 @@ func TestRecorder_closeAfterPartialWrite(t *testing.T) {
...
@@ -179,7 +179,7 @@ func TestRecorder_closeAfterPartialWrite(t *testing.T) {
"unterminated message"
,
"unterminated message"
,
""
,
""
,
},
},
})
}
,
nil
)
}
}
func
TestRecorder_withMiddlewares
(
t
*
testing
.
T
)
{
func
TestRecorder_withMiddlewares
(
t
*
testing
.
T
)
{
...
@@ -199,7 +199,7 @@ func TestRecorder_withMiddlewares(t *testing.T) {
...
@@ -199,7 +199,7 @@ func TestRecorder_withMiddlewares(t *testing.T) {
return
err
return
err
}
}
return
stream
.
Close
()
return
nil
}),
}),
),
),
streamtest
.
WithMiddlewares
(
streamtest
.
WithMiddlewares
(
...
@@ -247,6 +247,16 @@ func TestRecorder_withMiddlewares(t *testing.T) {
...
@@ -247,6 +247,16 @@ func TestRecorder_withMiddlewares(t *testing.T) {
return
nil
return
nil
}
}
},
},
func
(
h
p2p
.
HandlerFunc
)
p2p
.
HandlerFunc
{
return
func
(
peer
p2p
.
Peer
,
stream
p2p
.
Stream
)
error
{
if
err
:=
h
(
peer
,
stream
);
err
!=
nil
{
return
err
}
// close stream after all previous middlewares wrote to it
// so that the receiving peer can get all the post messages
return
stream
.
Close
()
}
},
),
),
)
)
...
@@ -284,7 +294,65 @@ func TestRecorder_withMiddlewares(t *testing.T) {
...
@@ -284,7 +294,65 @@ func TestRecorder_withMiddlewares(t *testing.T) {
"test
\n
"
,
"test
\n
"
,
"pre 1, pre 2, pre 3, handler, post 3, post 2, post 1, "
,
"pre 1, pre 2, pre 3, handler, post 3, post 2, post 1, "
,
},
},
})
},
nil
)
}
func
TestRecorder_recordErr
(
t
*
testing
.
T
)
{
testErr
:=
errors
.
New
(
"test error"
)
recorder
:=
streamtest
.
New
(
streamtest
.
WithProtocols
(
newTestProtocol
(
func
(
peer
p2p
.
Peer
,
stream
p2p
.
Stream
)
error
{
rw
:=
bufio
.
NewReadWriter
(
bufio
.
NewReader
(
stream
),
bufio
.
NewWriter
(
stream
))
defer
stream
.
Close
()
if
_
,
err
:=
rw
.
ReadString
(
'\n'
);
err
!=
nil
{
return
err
}
if
_
,
err
:=
rw
.
WriteString
(
"resp
\n
"
);
err
!=
nil
{
return
err
}
if
err
:=
rw
.
Flush
();
err
!=
nil
{
return
err
}
return
testErr
}),
),
)
request
:=
func
(
ctx
context
.
Context
,
s
p2p
.
Streamer
,
address
swarm
.
Address
)
(
err
error
)
{
stream
,
err
:=
s
.
NewStream
(
ctx
,
address
,
testProtocolName
,
testStreamName
,
testStreamVersion
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"new stream: %w"
,
err
)
}
defer
stream
.
Close
()
if
_
,
err
=
stream
.
Write
([]
byte
(
"req
\n
"
));
err
!=
nil
{
return
err
}
_
,
err
=
ioutil
.
ReadAll
(
stream
)
return
err
}
err
:=
request
(
context
.
Background
(),
recorder
,
swarm
.
ZeroAddress
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
records
,
err
:=
recorder
.
Records
(
swarm
.
ZeroAddress
,
testProtocolName
,
testStreamName
,
testStreamVersion
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
testRecords
(
t
,
records
,
[][
2
]
string
{
{
"req
\n
"
,
"resp
\n
"
,
},
},
testErr
)
}
}
const
(
const
(
...
@@ -306,7 +374,9 @@ func newTestProtocol(h p2p.HandlerFunc) p2p.ProtocolSpec {
...
@@ -306,7 +374,9 @@ func newTestProtocol(h p2p.HandlerFunc) p2p.ProtocolSpec {
}
}
}
}
func
testRecords
(
t
*
testing
.
T
,
records
[]
*
streamtest
.
Record
,
want
[][
2
]
string
)
{
func
testRecords
(
t
*
testing
.
T
,
records
[]
*
streamtest
.
Record
,
want
[][
2
]
string
,
wantErr
error
)
{
t
.
Helper
()
lr
:=
len
(
records
)
lr
:=
len
(
records
)
lw
:=
len
(
want
)
lw
:=
len
(
want
)
if
lr
!=
lw
{
if
lr
!=
lw
{
...
@@ -316,8 +386,8 @@ func testRecords(t *testing.T, records []*streamtest.Record, want [][2]string) {
...
@@ -316,8 +386,8 @@ func testRecords(t *testing.T, records []*streamtest.Record, want [][2]string) {
for
i
:=
0
;
i
<
lr
;
i
++
{
for
i
:=
0
;
i
<
lr
;
i
++
{
record
:=
records
[
i
]
record
:=
records
[
i
]
if
err
:=
record
.
Err
();
err
!=
nil
{
if
err
:=
record
.
Err
();
err
!=
wantErr
{
t
.
Fatalf
(
"got error from record %v, want
nil"
,
e
rr
)
t
.
Fatalf
(
"got error from record %v, want
%v"
,
err
,
wantE
rr
)
}
}
w
:=
want
[
i
]
w
:=
want
[
i
]
...
...
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