Test: fix direct listen fail

This commit is contained in:
Dreamacro 2021-09-04 22:44:18 +08:00
parent 661c417fce
commit ff56e5c5de
3 changed files with 45 additions and 6 deletions

View file

@ -210,7 +210,7 @@ func newLargeDataPair() (chan hashPair, chan hashPair, func(t *testing.T) error)
func testPingPongWithSocksPort(t *testing.T, port int) { func testPingPongWithSocksPort(t *testing.T, port int) {
pingCh, pongCh, test := newPingPongPair() pingCh, pongCh, test := newPingPongPair()
go func() { go func() {
l, err := net.Listen("tcp", ":10001") l, err := Listen("tcp", ":10001")
if err != nil { if err != nil {
assert.FailNow(t, err.Error()) 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 { func testPingPongWithConn(t *testing.T, c net.Conn) error {
l, err := net.Listen("tcp", ":10001") l, err := Listen("tcp", ":10001")
if err != nil { if err != nil {
return err return err
} }
@ -300,7 +300,7 @@ func testPingPongWithConn(t *testing.T, c net.Conn) error {
} }
func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error { func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error {
l, err := net.ListenPacket("udp", ":10001") l, err := ListenPacket("udp", ":10001")
if err != nil { if err != nil {
return err return err
} }
@ -345,7 +345,7 @@ type hashPair struct {
} }
func testLargeDataWithConn(t *testing.T, c net.Conn) error { func testLargeDataWithConn(t *testing.T, c net.Conn) error {
l, err := net.Listen("tcp", ":10001") l, err := Listen("tcp", ":10001")
if err != nil { if err != nil {
return err return err
} }
@ -438,7 +438,7 @@ func testLargeDataWithConn(t *testing.T, c net.Conn) error {
} }
func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error {
l, err := net.ListenPacket("udp", ":10001") l, err := ListenPacket("udp", ":10001")
if err != nil { if err != nil {
return err return err
} }
@ -622,7 +622,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
} }
func benchmarkProxy(b *testing.B, 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 { if err != nil {
assert.FailNow(b, err.Error()) assert.FailNow(b, err.Error())
} }
@ -639,6 +639,7 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) {
chunkSize := int64(16 * 1024) chunkSize := int64(16 * 1024)
chunk := make([]byte, chunkSize) chunk := make([]byte, chunkSize)
rand.Read(chunk)
conn, err := proxy.DialContext(context.Background(), &C.Metadata{ conn, err := proxy.DialContext(context.Background(), &C.Metadata{
Host: localIP.String(), Host: localIP.String(),
DstPort: "10001", DstPort: "10001",

37
test/util.go Normal file
View file

@ -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
}

View file

@ -1,3 +1,4 @@
//go:build !darwin
// +build !darwin // +build !darwin
package main package main