diff --git a/test/clash_test.go b/test/clash_test.go index b3192283..e6cb14a1 100644 --- a/test/clash_test.go +++ b/test/clash_test.go @@ -210,7 +210,7 @@ func newLargeDataPair() (chan hashPair, chan hashPair, func(t *testing.T) error) func testPingPongWithSocksPort(t *testing.T, port int) { pingCh, pongCh, test := newPingPongPair() go func() { - l, err := net.Listen("tcp", ":10001") + l, err := Listen("tcp", ":10001") if err != nil { assert.FailNow(t, err.Error()) } @@ -259,7 +259,7 @@ func testPingPongWithSocksPort(t *testing.T, port int) { } func testPingPongWithConn(t *testing.T, c net.Conn) error { - l, err := net.Listen("tcp", ":10001") + l, err := Listen("tcp", ":10001") if err != nil { return err } @@ -300,7 +300,7 @@ func testPingPongWithConn(t *testing.T, c net.Conn) error { } func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error { - l, err := net.ListenPacket("udp", ":10001") + l, err := ListenPacket("udp", ":10001") if err != nil { return err } @@ -345,7 +345,7 @@ type hashPair struct { } func testLargeDataWithConn(t *testing.T, c net.Conn) error { - l, err := net.Listen("tcp", ":10001") + l, err := Listen("tcp", ":10001") if err != nil { return err } @@ -438,7 +438,7 @@ func testLargeDataWithConn(t *testing.T, c net.Conn) error { } func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { - l, err := net.ListenPacket("udp", ":10001") + l, err := ListenPacket("udp", ":10001") if err != nil { return err } @@ -622,7 +622,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { } func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { - l, err := net.Listen("tcp", ":10001") + l, err := Listen("tcp", ":10001") if err != nil { assert.FailNow(b, err.Error()) } @@ -639,6 +639,7 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { chunkSize := int64(16 * 1024) chunk := make([]byte, chunkSize) + rand.Read(chunk) conn, err := proxy.DialContext(context.Background(), &C.Metadata{ Host: localIP.String(), DstPort: "10001", diff --git a/test/util.go b/test/util.go new file mode 100644 index 00000000..f3325aeb --- /dev/null +++ b/test/util.go @@ -0,0 +1,37 @@ +package main + +import ( + "context" + "net" + "time" +) + +func Listen(network, address string) (net.Listener, error) { + lc := net.ListenConfig{} + + var lastErr error + for i := 0; i < 5; i++ { + l, err := lc.Listen(context.Background(), network, address) + if err == nil { + return l, nil + } + + lastErr = err + time.Sleep(time.Millisecond * 200) + } + return nil, lastErr +} + +func ListenPacket(network, address string) (net.PacketConn, error) { + var lastErr error + for i := 0; i < 5; i++ { + l, err := net.ListenPacket(network, address) + if err == nil { + return l, nil + } + + lastErr = err + time.Sleep(time.Millisecond * 200) + } + return nil, lastErr +} diff --git a/test/util_other_test.go b/test/util_other_test.go index e325bd8b..13ba87f3 100644 --- a/test/util_other_test.go +++ b/test/util_other_test.go @@ -1,3 +1,4 @@ +//go:build !darwin // +build !darwin package main