2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-10 11:41:52 +08:00

let the kernel assign ports

This commit is contained in:
Tamir Duberstein 2016-02-20 15:43:18 -05:00
parent ebea4cb52a
commit 90e60a4b6d

View File

@ -14,20 +14,16 @@ const (
rpcVal = 1234 rpcVal = 1234
) )
var testPort = 5125 func testListener(t *testing.T) (net.Listener, func()) {
l, err := net.Listen("tcp", ":0")
func testAddr() string {
testPort++
return fmt.Sprintf("127.0.0.1:%d", testPort)
}
func testListener(t *testing.T) (net.Listener, string) {
addr := testAddr()
l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
return l, addr return l, func() {
if err := l.Close(); err != nil {
t.Error(err)
}
}
} }
type testHTTP1Handler struct{} type testHTTP1Handler struct{}
@ -40,14 +36,14 @@ func runTestHTTPServer(t *testing.T, l net.Listener) {
s := &http.Server{ s := &http.Server{
Handler: &testHTTP1Handler{}, Handler: &testHTTP1Handler{},
} }
if err := s.Serve(l); err != nil { if err := s.Serve(l); err != nil && err != ErrListenerClosed {
t.Log(err) t.Log(err)
} }
} }
func runTestHTTP1Client(t *testing.T, addr string) { func runTestHTTP1Client(t *testing.T, addr net.Addr) {
var r *http.Response var r *http.Response
if resp, err := http.Get("http://" + addr); err != nil { if resp, err := http.Get("http://" + addr.String()); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
r = resp r = resp
@ -90,8 +86,8 @@ func runTestRPCServer(t *testing.T, l net.Listener) {
} }
} }
func runTestRPCClient(t *testing.T, addr string) { func runTestRPCClient(t *testing.T, addr net.Addr) {
c, err := rpc.Dial("tcp", addr) c, err := rpc.Dial(addr.Network(), addr.String())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -109,12 +105,8 @@ func runTestRPCClient(t *testing.T, addr string) {
} }
func TestAny(t *testing.T) { func TestAny(t *testing.T) {
l, addr := testListener(t) l, cleanup := testListener(t)
defer func() { defer cleanup()
if err := l.Close(); err != nil {
t.Log(err)
}
}()
muxl := New(l) muxl := New(l)
httpl := muxl.Match(Any()) httpl := muxl.Match(Any())
@ -127,7 +119,7 @@ func TestAny(t *testing.T) {
}() }()
var r *http.Response var r *http.Response
if resp, err := http.Get("http://" + addr); err != nil { if resp, err := http.Get("http://" + l.Addr().String()); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
r = resp r = resp
@ -149,12 +141,8 @@ func TestAny(t *testing.T) {
} }
func TestHTTPGoRPC(t *testing.T) { func TestHTTPGoRPC(t *testing.T) {
l, addr := testListener(t) l, cleanup := testListener(t)
defer func() { defer cleanup()
if err := l.Close(); err != nil {
t.Log(err)
}
}()
muxl := New(l) muxl := New(l)
httpl := muxl.Match(HTTP2(), HTTP1Fast()) httpl := muxl.Match(HTTP2(), HTTP1Fast())
@ -168,17 +156,13 @@ func TestHTTPGoRPC(t *testing.T) {
} }
}() }()
runTestHTTP1Client(t, addr) runTestHTTP1Client(t, l.Addr())
runTestRPCClient(t, addr) runTestRPCClient(t, l.Addr())
} }
func TestErrorHandler(t *testing.T) { func TestErrorHandler(t *testing.T) {
l, addr := testListener(t) l, cleanup := testListener(t)
defer func() { defer cleanup()
if err := l.Close(); err != nil {
t.Log(err)
}
}()
muxl := New(l) muxl := New(l)
httpl := muxl.Match(HTTP2(), HTTP1Fast()) httpl := muxl.Match(HTTP2(), HTTP1Fast())
@ -202,7 +186,8 @@ func TestErrorHandler(t *testing.T) {
return true return true
}) })
c, err := rpc.Dial("tcp", addr) addr := l.Addr()
c, err := rpc.Dial(addr.Network(), addr.String())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }