Skip to content

Commit

Permalink
chore: use 127.0.0.1 and unique port for all test listener (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
joway authored Jun 10, 2024
1 parent c4ec256 commit b383e39
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 43 deletions.
30 changes: 18 additions & 12 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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
}()
Expand Down
4 changes: 2 additions & 2 deletions mux/shard_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion net_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 18 additions & 13 deletions net_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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"))
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion net_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions net_polldesc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
Expand Down
Loading

0 comments on commit b383e39

Please sign in to comment.