From b383e39c4a22d89f627f7e8b347e7b01cfa1850a Mon Sep 17 00:00:00 2001 From: Joway Date: Mon, 10 Jun 2024 13:44:06 +0800 Subject: [PATCH] chore: use 127.0.0.1 and unique port for all test listener (#336) --- connection_test.go | 30 ++++++++++++++++++------------ mux/shard_queue_test.go | 4 ++-- net_dialer.go | 2 +- net_dialer_test.go | 31 ++++++++++++++++++------------- net_listener_test.go | 2 +- net_polldesc_test.go | 5 +++-- netpoll_test.go | 32 ++++++++++++++++++++------------ 7 files changed, 63 insertions(+), 43 deletions(-) diff --git a/connection_test.go b/connection_test.go index f79b2484..890c3239 100644 --- a/connection_test.go +++ b/connection_test.go @@ -212,7 +212,8 @@ func writeAll(fd int, buf []byte) error { // Large packet write test. The socket buffer is 2MB by default, here to verify // whether Connection.Close can be executed normally after socket output buffer is full. func TestLargeBufferWrite(t *testing.T) { - ln, err := createTestListener("tcp", ":12345") + address := getTestAddress() + ln, err := createTestListener("tcp", address) MustNil(t, err) trigger := make(chan int) @@ -231,7 +232,7 @@ func TestLargeBufferWrite(t *testing.T) { } }() - conn, err := DialConnection("tcp", ":12345", time.Second) + conn, err := DialConnection("tcp", address, time.Second) MustNil(t, err) rfd := <-trigger @@ -267,7 +268,8 @@ func TestLargeBufferWrite(t *testing.T) { } func TestWriteTimeout(t *testing.T) { - ln, err := createTestListener("tcp", ":1234") + address := getTestAddress() + ln, err := createTestListener("tcp", address) MustNil(t, err) interval := time.Millisecond * 100 @@ -296,7 +298,7 @@ func TestWriteTimeout(t *testing.T) { } }() - conn, err := DialConnection("tcp", ":1234", time.Second) + conn, err := DialConnection("tcp", address, time.Second) MustNil(t, err) _, err = conn.Writer().Malloc(1024) @@ -440,7 +442,8 @@ func TestBookSizeLargerThanMaxSize(t *testing.T) { } func TestConnDetach(t *testing.T) { - ln, err := createTestListener("tcp", ":1234") + address := getTestAddress() + ln, err := createTestListener("tcp", address) MustNil(t, err) go func() { @@ -470,7 +473,7 @@ func TestConnDetach(t *testing.T) { } }() - c, err := DialConnection("tcp", ":1234", time.Second) + c, err := DialConnection("tcp", address, time.Second) MustNil(t, err) conn := c.(*TCPConnection) @@ -497,7 +500,8 @@ func TestConnDetach(t *testing.T) { } func TestParallelShortConnection(t *testing.T) { - ln, err := createTestListener("tcp", ":12345") + address := getTestAddress() + ln, err := createTestListener("tcp", address) MustNil(t, err) defer ln.Close() @@ -523,7 +527,7 @@ func TestParallelShortConnection(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - conn, err := DialConnection("tcp", ":12345", time.Second) + conn, err := DialConnection("tcp", address, time.Second) MustNil(t, err) n, err := conn.Writer().WriteBinary(make([]byte, sizePerConn)) MustNil(t, err) @@ -546,7 +550,8 @@ func TestParallelShortConnection(t *testing.T) { } func TestConnectionServerClose(t *testing.T) { - ln, err := createTestListener("tcp", ":12345") + address := getTestAddress() + ln, err := createTestListener("tcp", address) MustNil(t, err) defer ln.Close() @@ -628,7 +633,7 @@ func TestConnectionServerClose(t *testing.T) { wg.Add(conns * 6) for i := 0; i < conns; i++ { go func() { - conn, err := DialConnection("tcp", ":12345", time.Second) + conn, err := DialConnection("tcp", address, time.Second) MustNil(t, err) err = conn.SetOnRequest(clientOnRequest) MustNil(t, err) @@ -644,7 +649,8 @@ func TestConnectionServerClose(t *testing.T) { } func TestConnectionDailTimeoutAndClose(t *testing.T) { - ln, err := createTestListener("tcp", ":12345") + address := getTestAddress() + ln, err := createTestListener("tcp", address) MustNil(t, err) defer ln.Close() @@ -670,7 +676,7 @@ func TestConnectionDailTimeoutAndClose(t *testing.T) { for i := 0; i < conns; i++ { go func() { defer wg.Done() - conn, err := DialConnection("tcp", ":12345", time.Nanosecond) + conn, err := DialConnection("tcp", address, time.Nanosecond) Assert(t, err == nil || strings.Contains(err.Error(), "i/o timeout")) _ = conn }() diff --git a/mux/shard_queue_test.go b/mux/shard_queue_test.go index 7a595d21..e5384a24 100644 --- a/mux/shard_queue_test.go +++ b/mux/shard_queue_test.go @@ -29,8 +29,8 @@ func TestShardQueue(t *testing.T) { var svrConn net.Conn accepted := make(chan struct{}) - network, address := "tcp", ":18888" - ln, err := net.Listen("tcp", ":18888") + network, address := "tcp", "localhost:12345" + ln, err := net.Listen("tcp", address) MustNil(t, err) stop := make(chan int, 1) defer close(stop) diff --git a/net_dialer.go b/net_dialer.go index f39245ff..85f1b7c6 100644 --- a/net_dialer.go +++ b/net_dialer.go @@ -75,7 +75,7 @@ func (d *dialer) dialTCP(ctx context.Context, network, address string) (connecti return nil, err } var ipaddrs []net.IPAddr - // host maybe empty if address is ":1234" + // host maybe empty if address is :12345 if host == "" { ipaddrs = []net.IPAddr{{}} } else { diff --git a/net_dialer_test.go b/net_dialer_test.go index 7383fd0d..64fa50cf 100644 --- a/net_dialer_test.go +++ b/net_dialer_test.go @@ -31,11 +31,12 @@ import ( func TestDialerTCP(t *testing.T) { dialer := NewDialer() - conn, err := dialer.DialTimeout("tcp", ":1234", time.Second) + address := getTestAddress() + conn, err := dialer.DialTimeout("tcp", address, time.Second) MustTrue(t, err != nil) MustTrue(t, conn.(*TCPConnection) == nil) - ln, err := CreateListener("tcp", ":1234") + ln, err := CreateListener("tcp", address) MustNil(t, err) stop := make(chan int, 1) @@ -57,10 +58,10 @@ func TestDialerTCP(t *testing.T) { } }() - conn, err = dialer.DialTimeout("tcp", ":1234", time.Second) + conn, err = dialer.DialTimeout("tcp", address, time.Second) MustNil(t, err) MustTrue(t, strings.HasPrefix(conn.LocalAddr().String(), "127.0.0.1:")) - Equal(t, conn.RemoteAddr().String(), "127.0.0.1:1234") + Equal(t, conn.RemoteAddr().String(), address) } func TestDialerUnix(t *testing.T) { @@ -106,7 +107,8 @@ func TestDialerUnix(t *testing.T) { } func TestDialerFdAlloc(t *testing.T) { - ln, err := CreateListener("tcp", ":1234") + address := getTestAddress() + ln, err := CreateListener("tcp", address) MustNil(t, err) defer ln.Close() el1, _ := NewEventLoop(func(ctx context.Context, connection Connection) error { @@ -121,7 +123,7 @@ func TestDialerFdAlloc(t *testing.T) { defer el1.Shutdown(ctx1) for i := 0; i < 100; i++ { - conn, err := DialConnection("tcp", ":1234", time.Second) + conn, err := DialConnection("tcp", address, time.Second) MustNil(t, err) fd := conn.(*TCPConnection).fd conn.Write([]byte("hello world")) @@ -134,7 +136,8 @@ func TestDialerFdAlloc(t *testing.T) { } func TestFDClose(t *testing.T) { - ln, err := CreateListener("tcp", ":1234") + address := getTestAddress() + ln, err := CreateListener("tcp", address) MustNil(t, err) defer ln.Close() el1, _ := NewEventLoop(func(ctx context.Context, connection Connection) error { @@ -150,13 +153,13 @@ func TestFDClose(t *testing.T) { var fd int var conn Connection - conn, err = DialConnection("tcp", ":1234", time.Second) + conn, err = DialConnection("tcp", address, time.Second) MustNil(t, err) fd = conn.(*TCPConnection).fd syscall.SetNonblock(fd, true) conn.Close() - conn, err = DialConnection("tcp", ":1234", time.Second) + conn, err = DialConnection("tcp", address, time.Second) MustNil(t, err) fd = conn.(*TCPConnection).fd syscall.SetNonblock(fd, true) @@ -166,8 +169,10 @@ func TestFDClose(t *testing.T) { // fd data package race test, use two servers and two dialers. func TestDialerThenClose(t *testing.T) { + address1 := getTestAddress() + address2 := getTestAddress() // server 1 - ln1, _ := createTestListener("tcp", ":1231") + ln1, _ := createTestListener("tcp", address1) el1 := mockDialerEventLoop(1) go func() { el1.Serve(ln1) @@ -177,7 +182,7 @@ func TestDialerThenClose(t *testing.T) { defer el1.Shutdown(ctx1) // server 2 - ln2, _ := createTestListener("tcp", ":1232") + ln2, _ := createTestListener("tcp", address2) el2 := mockDialerEventLoop(2) go func() { el2.Serve(ln2) @@ -194,12 +199,12 @@ func TestDialerThenClose(t *testing.T) { defer wg.Done() for i := 0; i < 50; i++ { // send server 1 - conn, err := DialConnection("tcp", ":1231", time.Second) + conn, err := DialConnection("tcp", address1, time.Second) if err == nil { mockDialerSend(1, &conn.(*TCPConnection).connection) } // send server 2 - conn, err = DialConnection("tcp", ":1232", time.Second) + conn, err = DialConnection("tcp", address2, time.Second) if err == nil { mockDialerSend(2, &conn.(*TCPConnection).connection) } diff --git a/net_listener_test.go b/net_listener_test.go index 9516f8e5..71983028 100644 --- a/net_listener_test.go +++ b/net_listener_test.go @@ -27,7 +27,7 @@ import ( func TestListenerDialer(t *testing.T) { network := "tcp" - addr := ":1234" + addr := getTestAddress() ln, err := CreateListener(network, addr) MustNil(t, err) defer ln.Close() diff --git a/net_polldesc_test.go b/net_polldesc_test.go index 40804b62..9850a39c 100644 --- a/net_polldesc_test.go +++ b/net_polldesc_test.go @@ -27,7 +27,8 @@ func TestZeroTimer(t *testing.T) { } func TestRuntimePoll(t *testing.T) { - ln, err := CreateListener("tcp", ":1234") + address := getTestAddress() + ln, err := CreateListener("tcp", address) MustNil(t, err) stop := make(chan int, 1) @@ -50,7 +51,7 @@ func TestRuntimePoll(t *testing.T) { }() for i := 0; i < 10; i++ { - conn, err := DialConnection("tcp", ":1234", time.Second) + conn, err := DialConnection("tcp", address, time.Second) MustNil(t, err) conn.Close() } diff --git a/netpoll_test.go b/netpoll_test.go index 843be8cd..4156a0b8 100644 --- a/netpoll_test.go +++ b/netpoll_test.go @@ -20,6 +20,7 @@ package netpoll import ( "context" "errors" + "fmt" "math/rand" "os" "runtime" @@ -64,6 +65,13 @@ func Assert(t *testing.T, cond bool, val ...interface{}) { } } +var testPort int32 = 10000 + +// getTestAddress return a unique port for every tests, so all tests will not share a same listerner +func getTestAddress() string { + return fmt.Sprintf("127.0.0.1:%d", atomic.AddInt32(&testPort, 1)) +} + func TestEqual(t *testing.T) { var err error MustNil(t, err) @@ -73,7 +81,7 @@ func TestEqual(t *testing.T) { } func TestOnConnect(t *testing.T) { - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() req, resp := "ping", "pong" var loop = newTestEventLoop(network, address, func(ctx context.Context, connection Connection) error { @@ -117,7 +125,7 @@ func TestOnConnect(t *testing.T) { } func TestOnConnectWrite(t *testing.T) { - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() var loop = newTestEventLoop(network, address, func(ctx context.Context, connection Connection) error { return nil @@ -140,7 +148,7 @@ func TestOnConnectWrite(t *testing.T) { func TestOnDisconnect(t *testing.T) { type ctxKey struct{} - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() var canceled, closed int32 var conns int32 = 100 req := "ping" @@ -201,7 +209,7 @@ func TestOnDisconnect(t *testing.T) { func TestOnDisconnectWhenOnConnect(t *testing.T) { type ctxPrepareKey struct{} type ctxConnectKey struct{} - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() var conns int32 = 100 var wg sync.WaitGroup wg.Add(int(conns) * 3) @@ -249,7 +257,7 @@ func TestOnDisconnectWhenOnConnect(t *testing.T) { } func TestGracefulExit(t *testing.T) { - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() // exit without processing connections var eventLoop1 = newTestEventLoop(network, address, @@ -306,7 +314,7 @@ func TestGracefulExit(t *testing.T) { } func TestCloseCallbackWhenOnRequest(t *testing.T) { - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() var requested, closed = make(chan struct{}), make(chan struct{}) var loop = newTestEventLoop(network, address, func(ctx context.Context, connection Connection) error { @@ -337,7 +345,7 @@ func TestCloseCallbackWhenOnRequest(t *testing.T) { } func TestCloseCallbackWhenOnConnect(t *testing.T) { - var network, address = "tcp", ":8888" + var network, address = "tcp", getTestAddress() var connected, closed = make(chan struct{}), make(chan struct{}) var loop = newTestEventLoop(network, address, nil, @@ -364,7 +372,7 @@ func TestCloseCallbackWhenOnConnect(t *testing.T) { } func TestCloseConnWhenOnConnect(t *testing.T) { - var network, address = "tcp", ":8888" + var network, address = "tcp", "localhost:8888" conns := 10 var wg sync.WaitGroup wg.Add(conns) @@ -399,7 +407,7 @@ func TestCloseConnWhenOnConnect(t *testing.T) { } func TestServerReadAndClose(t *testing.T) { - var network, address = "tcp", ":18888" + var network, address = "tcp", getTestAddress() var sendMsg = []byte("hello") var closed int32 var loop = newTestEventLoop(network, address, @@ -435,7 +443,7 @@ func TestServerReadAndClose(t *testing.T) { } func TestServerPanicAndClose(t *testing.T) { - var network, address = "tcp", ":18888" + var network, address = "tcp", getTestAddress() var sendMsg = []byte("hello") var paniced int32 var loop = newTestEventLoop(network, address, @@ -467,7 +475,7 @@ func TestServerPanicAndClose(t *testing.T) { func TestClientWriteAndClose(t *testing.T) { var ( - network, address = "tcp", ":18889" + network, address = "tcp", getTestAddress() connnum = 10 packetsize, packetnum = 1000 * 5, 1 recvbytes int32 = 0 @@ -531,7 +539,7 @@ func TestServerAcceptWhenTooManyOpenFiles(t *testing.T) { MustNil(t, err) }() - var network, address = "tcp", ":18888" + var network, address = "tcp", getTestAddress() var connected int32 var loop = newTestEventLoop(network, address, func(ctx context.Context, connection Connection) error {