Commit 8785e160 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 887c6a2e
...@@ -21,11 +21,73 @@ package lonet ...@@ -21,11 +21,73 @@ package lonet
import ( import (
"context" "context"
"fmt"
"io"
"net"
"reflect"
"testing" "testing"
"golang.org/x/sync/errgroup"
"lab.nexedi.com/kirr/go123/exc" "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:\nhave: %v\nwant: %v\n", a, b)
t.Errorf("not equal:\nhave: %v\nwant: %v", a, b)
exc.Raise(0)
}
}
// ---- 8< ----
// TODO test go-go // TODO test go-go
// TODO test go-py // TODO test go-py
...@@ -37,6 +99,8 @@ import ( ...@@ -37,6 +99,8 @@ import (
func TestLonet(t *testing.T) { func TestLonet(t *testing.T) {
// FIXME ~100% dup from TestPipeNet
X := exc.Raiseif X := exc.Raiseif
ctx := context.Background() ctx := context.Background()
...@@ -44,9 +108,65 @@ func TestLonet(t *testing.T) { ...@@ -44,9 +108,65 @@ func TestLonet(t *testing.T) {
X(err) X(err)
// XXX defer shutdown/rm this lonet fully? // XXX defer shutdown/rm this lonet fully?
xaddr := func(addr string) *Addr {
a, err := subnet.parseAddr(addr)
X(err)
return a
}
, err := subnet.NewHost(ctx, "α") , err := subnet.NewHost(ctx, "α")
X(err) X(err)
, err := subnet.NewHost(ctx, "β") , err := subnet.NewHost(ctx, "β")
X(err) X(err)
assertEq(t, .Network(), subnet.Network())
assertEq(t, .Network(), subnet.Network())
assertEq(t, .Name(), "α")
assertEq(t, .Name(), "β")
l1, err := .Listen("")
X(err)
assertEq(t, l1.Addr(), xaddr("α:1"))
// zero port always stays unused even after autobind
_, err = .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(, "α:1")
assertEq(t, c1c.LocalAddr(), xaddr("β:1"))
assertEq(t, c1c.RemoteAddr(), xaddr("α:2"))
xwrite(c1c, "ping")
assertEq(t, xread(c1c), "pong")
c2c := xdial(, "α: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(, ":0") // autobind again
assertEq(t, l2.Addr(), xaddr("α:4"))
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment