Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
8785e160
Commit
8785e160
authored
May 15, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
887c6a2e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
0 deletions
+120
-0
go/xcommon/xnet/lonet/lonet_test.go
go/xcommon/xnet/lonet/lonet_test.go
+120
-0
No files found.
go/xcommon/xnet/lonet/lonet_test.go
View file @
8785e160
...
...
@@ -21,11 +21,73 @@ package lonet
import
(
"context"
"fmt"
"io"
"net"
"reflect"
"testing"
"golang.org/x/sync/errgroup"
"lab.nexedi.com/kirr/go123/exc"
)
// FIXME dup from pipenet:
// ---- 8< ----
type
mklistener
interface
{
Listen
(
string
)
(
net
.
Listener
,
error
)
}
func
xlisten
(
n
mklistener
,
laddr
string
)
net
.
Listener
{
l
,
err
:=
n
.
Listen
(
laddr
)
exc
.
Raiseif
(
err
)
return
l
}
func
xaccept
(
l
net
.
Listener
)
net
.
Conn
{
c
,
err
:=
l
.
Accept
()
exc
.
Raiseif
(
err
)
return
c
}
type
dialer
interface
{
Dial
(
context
.
Context
,
string
)
(
net
.
Conn
,
error
)
}
func
xdial
(
n
dialer
,
addr
string
)
net
.
Conn
{
c
,
err
:=
n
.
Dial
(
context
.
Background
(),
addr
)
exc
.
Raiseif
(
err
)
return
c
}
func
xread
(
r
io
.
Reader
)
string
{
buf
:=
make
([]
byte
,
4096
)
n
,
err
:=
r
.
Read
(
buf
)
exc
.
Raiseif
(
err
)
return
string
(
buf
[
:
n
])
}
func
xwrite
(
w
io
.
Writer
,
data
string
)
{
_
,
err
:=
w
.
Write
([]
byte
(
data
))
exc
.
Raiseif
(
err
)
}
func
xwait
(
w
interface
{
Wait
()
error
})
{
err
:=
w
.
Wait
()
exc
.
Raiseif
(
err
)
}
func
assertEq
(
t
*
testing
.
T
,
a
,
b
interface
{})
{
t
.
Helper
()
if
!
reflect
.
DeepEqual
(
a
,
b
)
{
fmt
.
Printf
(
"not equal:
\n
have: %v
\n
want: %v
\n
"
,
a
,
b
)
t
.
Errorf
(
"not equal:
\n
have: %v
\n
want: %v"
,
a
,
b
)
exc
.
Raise
(
0
)
}
}
// ---- 8< ----
// TODO test go-go
// TODO test go-py
...
...
@@ -37,6 +99,8 @@ import (
func
TestLonet
(
t
*
testing
.
T
)
{
// FIXME ~100% dup from TestPipeNet
X
:=
exc
.
Raiseif
ctx
:=
context
.
Background
()
...
...
@@ -44,9 +108,65 @@ func TestLonet(t *testing.T) {
X
(
err
)
// XXX defer shutdown/rm this lonet fully?
xaddr
:=
func
(
addr
string
)
*
Addr
{
a
,
err
:=
subnet
.
parseAddr
(
addr
)
X
(
err
)
return
a
}
hα
,
err
:=
subnet
.
NewHost
(
ctx
,
"α"
)
X
(
err
)
hβ
,
err
:=
subnet
.
NewHost
(
ctx
,
"β"
)
X
(
err
)
assertEq
(
t
,
hα
.
Network
(),
subnet
.
Network
())
assertEq
(
t
,
hβ
.
Network
(),
subnet
.
Network
())
assertEq
(
t
,
hα
.
Name
(),
"α"
)
assertEq
(
t
,
hβ
.
Name
(),
"β"
)
l1
,
err
:=
hα
.
Listen
(
""
)
X
(
err
)
assertEq
(
t
,
l1
.
Addr
(),
xaddr
(
"α:1"
))
// zero port always stays unused even after autobind
_
,
err
=
hα
.
Dial
(
ctx
,
":0"
)
assertEq
(
t
,
err
,
&
net
.
OpError
{
Op
:
"dial"
,
Net
:
subnet
.
Network
(),
Addr
:
xaddr
(
"α:0"
),
Err
:
errConnRefused
})
wg
:=
&
errgroup
.
Group
{}
wg
.
Go
(
exc
.
Funcx
(
func
()
{
c1s
:=
xaccept
(
l1
)
assertEq
(
t
,
c1s
.
LocalAddr
(),
xaddr
(
"α:2"
))
assertEq
(
t
,
c1s
.
RemoteAddr
(),
xaddr
(
"β:1"
))
assertEq
(
t
,
xread
(
c1s
),
"ping"
)
xwrite
(
c1s
,
"pong"
)
c2s
:=
xaccept
(
l1
)
assertEq
(
t
,
c2s
.
LocalAddr
(),
xaddr
(
"α:3"
))
assertEq
(
t
,
c2s
.
RemoteAddr
(),
xaddr
(
"β:2"
))
assertEq
(
t
,
xread
(
c2s
),
"hello"
)
xwrite
(
c2s
,
"world"
)
}))
c1c
:=
xdial
(
hβ
,
"α:1"
)
assertEq
(
t
,
c1c
.
LocalAddr
(),
xaddr
(
"β:1"
))
assertEq
(
t
,
c1c
.
RemoteAddr
(),
xaddr
(
"α:2"
))
xwrite
(
c1c
,
"ping"
)
assertEq
(
t
,
xread
(
c1c
),
"pong"
)
c2c
:=
xdial
(
hβ
,
"α:1"
)
assertEq
(
t
,
c2c
.
LocalAddr
(),
xaddr
(
"β:2"
))
assertEq
(
t
,
c2c
.
RemoteAddr
(),
xaddr
(
"α:3"
))
xwrite
(
c2c
,
"hello"
)
assertEq
(
t
,
xread
(
c2c
),
"world"
)
xwait
(
wg
)
l2
:=
xlisten
(
hα
,
":0"
)
// autobind again
assertEq
(
t
,
l2
.
Addr
(),
xaddr
(
"α:4"
))
}
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