Commit 1e98227e authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 341b2124
...@@ -40,10 +40,11 @@ package pipenet ...@@ -40,10 +40,11 @@ package pipenet
import ( import (
"context" "context"
"errors" "errors"
//"fmt"
"net" "net"
"strconv" "strconv"
"sync" "sync"
"../../../xcommon/xnet"
) )
const NetPrefix = "pipe" // pipenet package creates only "pipe*" networks const NetPrefix = "pipe" // pipenet package creates only "pipe*" networks
...@@ -85,6 +86,8 @@ type Host struct { ...@@ -85,6 +86,8 @@ type Host struct {
socketv []*socket // port -> listener | conn socketv []*socket // port -> listener | conn
} }
var _ xnet.Networker = (*Host)(nil)
// socket represents one endpoint entry on Network // socket represents one endpoint entry on Network
// it can be either already connected or listening // it can be either already connected or listening
type socket struct { type socket struct {
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"strconv"
"reflect" "reflect"
"testing" "testing"
...@@ -86,63 +87,64 @@ func assertEq(t *testing.T, a, b interface{}) { ...@@ -86,63 +87,64 @@ func assertEq(t *testing.T, a, b interface{}) {
func TestPipeNet(t *testing.T) { func TestPipeNet(t *testing.T) {
pnet := New("α") pnet := New("t")
addr := func(hostport string) *Addr { // XXX -> Network.ParseAddr ?
addrtestv := []struct {port, endpoint int; want string} { host, portstr, err := net.SplitHostPort(hostport)
{0, -1, "0"}, if err != nil {
{1, 0, "1c"}, t.Fatal(err)
{2, 1, "2s"},
}
for _, tt := range addrtestv {
addr := &Addr{Net: "pipeβ", Port: tt.port, Endpoint: tt.endpoint}
have := addr.String()
if have != tt.want {
t.Errorf("%#v -> %q ; want %q", addr, have, tt.want)
} }
port, err := strconv.Atoi(portstr)
if err != nil {
t.Fatal(err)
}
return &Addr{Net: pnet.Network(), Host: host, Port: port}
} }
_, err := pnet.Dial(context.Background(), "0") := pnet.Host("α")
assertEq(t, err, &net.OpError{Op: "dial", Net: "pipeα", Addr: &Addr{"pipeα", 0, -1}, Err: errConnRefused}) := pnet.Host("β")
_, err := .Dial(context.Background(), ":0")
assertEq(t, err, &net.OpError{Op: "dial", Net: "pipet", Addr: addr("α:0"), Err: errConnRefused})
l1 := xlisten(pnet, "") l1 := xlisten(, "")
assertEq(t, l1.Addr(), &Addr{"pipeα", 0, -1}) assertEq(t, l1.Addr(), addr("α:0"))
// XXX -> use workGroup (in connection_test.go) // XXX -> use workGroup (in connection_test.go)
wg := &errgroup.Group{} wg := &errgroup.Group{}
wg.Go(func() error { wg.Go(func() error {
return exc.Runx(func() { return exc.Runx(func() {
c1s := xaccept(l1) c1s := xaccept(l1)
assertEq(t, c1s.LocalAddr(), &Addr{"pipeα", 1, 1}) assertEq(t, c1s.LocalAddr(), addr("α:1"))
assertEq(t, c1s.RemoteAddr(), &Addr{"pipeα", 1, 0}) assertEq(t, c1s.RemoteAddr(), addr("β:0"))
assertEq(t, xread(c1s), "ping") assertEq(t, xread(c1s), "ping")
xwrite(c1s, "pong") xwrite(c1s, "pong")
c2s := xaccept(l1) c2s := xaccept(l1)
assertEq(t, c2s.LocalAddr(), &Addr{"pipeα", 2, 1}) assertEq(t, c2s.LocalAddr(), addr("α:2"))
assertEq(t, c2s.RemoteAddr(), &Addr{"pipeα", 2, 0}) assertEq(t, c2s.RemoteAddr(), addr("β:1"))
assertEq(t, xread(c2s), "hello") assertEq(t, xread(c2s), "hello")
xwrite(c2s, "world") xwrite(c2s, "world")
}) })
}) })
c1c := xdial(pnet, "0") c1c := xdial(, "α:0")
assertEq(t, c1c.LocalAddr(), &Addr{"pipeα", 1, 0}) assertEq(t, c1c.LocalAddr(), addr("β:0"))
assertEq(t, c1c.RemoteAddr(), &Addr{"pipeα", 1, 1}) assertEq(t, c1c.RemoteAddr(), addr("α:1"))
xwrite(c1c, "ping") xwrite(c1c, "ping")
assertEq(t, xread(c1c), "pong") assertEq(t, xread(c1c), "pong")
c2c := xdial(pnet, "0") c2c := xdial(, "α:0")
assertEq(t, c2c.LocalAddr(), &Addr{"pipeα", 2, 0}) assertEq(t, c2c.LocalAddr(), addr("β:1"))
assertEq(t, c2c.RemoteAddr(), &Addr{"pipeα", 2, 1}) assertEq(t, c2c.RemoteAddr(), addr("α:2"))
xwrite(c2c, "hello") xwrite(c2c, "hello")
assertEq(t, xread(c2c), "world") assertEq(t, xread(c2c), "world")
xwait(wg) xwait(wg)
l2 := xlisten(pnet, "") l2 := xlisten(, "")
assertEq(t, l2.Addr(), &Addr{"pipeα", 3, -1}) assertEq(t, l2.Addr(), addr("α:3"))
} }
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