Skip to content

Commit 867825e

Browse files
committed
fixed the issue of duplicate ports being acquired concurrently in the test
1 parent 70b93e3 commit 867825e

3 files changed

Lines changed: 27 additions & 19 deletions

File tree

application_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,15 +842,18 @@ func testSOCKS5Proxy(ts *TestSuite, proxyAddr string, expectSocksErr string) {
842842
func testSOCKS5ProxyWithAuth(ts *TestSuite, proxyAddr string, auth *proxy.Auth, iterations int, expectSocksErr string) {
843843
// setup mock server
844844
expectedBody := strings.Repeat("test text", 10_000)
845-
addr := pickFreeAddr(ts.t)
845+
l, err := net.Listen("tcp", "127.0.0.1:0")
846+
ts.NoError(err)
847+
addr := l.Addr().String()
848+
846849
mux := http.NewServeMux()
847850
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
848851
_, _ = fmt.Fprint(w, expectedBody)
849852
})
850853
//nolint
851-
httpServer := &http.Server{Addr: addr, Handler: mux}
854+
httpServer := &http.Server{Handler: mux}
852855
go func() {
853-
_ = httpServer.ListenAndServe()
856+
_ = httpServer.Serve(l)
854857
}()
855858
defer func() {
856859
httpServer.Shutdown(context.Background())

socks5/server_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"sync"
11+
"sync/atomic"
1112
"testing"
1213

1314
"github.com/stretchr/testify/require"
@@ -116,32 +117,36 @@ func TestProxyWithAuthRejection(t *testing.T) {
116117
}
117118
}
118119

120+
var testPortCounter int32 = 50000
121+
119122
func pickFreeAddr(t testing.TB) string {
120-
l, err := net.Listen("tcp", "127.0.0.1:0")
121-
if err != nil {
122-
t.Fatal(err)
123+
port := atomic.AddInt32(&testPortCounter, 1)
124+
if port < testPortCounter {
125+
t.Fatalf("port counter overflow: %d", port)
123126
}
124-
defer l.Close()
125-
126-
return l.Addr().String()
127+
return fmt.Sprintf("127.0.0.1:%d", port)
127128
}
128129

129130
// startUpstreamServer starts an HTTP server that responds with "test text" on /test.
130131
func startUpstreamServer(t testing.TB) string {
131-
addr := pickFreeAddr(t)
132+
l, err := net.Listen("tcp", "127.0.0.1:0")
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
132137
mux := http.NewServeMux()
133138
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
134139
_, _ = fmt.Fprintf(w, "test text")
135140
})
136141
//nolint
137-
httpServer := &http.Server{Addr: addr, Handler: mux}
142+
httpServer := &http.Server{Handler: mux}
138143
go func() {
139-
_ = httpServer.ListenAndServe()
144+
_ = httpServer.Serve(l)
140145
}()
141146
t.Cleanup(func() {
142147
httpServer.Shutdown(context.Background())
143148
})
144-
return addr
149+
return l.Addr().String()
145150
}
146151

147152
// newSOCKS5HttpClient creates an HTTP client that routes through a SOCKS5 proxy.

test_suite_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,14 @@ func (t *testTun) Close() error {
536536
return nil
537537
}
538538

539+
var testPortCounter int32 = 40000
540+
539541
func pickFreeAddr(t testing.TB) string {
540-
l, err := net.Listen("tcp", "127.0.0.1:0")
541-
if err != nil {
542-
t.Fatal(err)
542+
port := atomic.AddInt32(&testPortCounter, 1)
543+
if port < testPortCounter {
544+
t.Fatalf("port counter overflow: %d", port)
543545
}
544-
defer l.Close()
545-
546-
return l.Addr().String()
546+
return fmt.Sprintf("127.0.0.1:%d", port)
547547
}
548548

549549
func testPacket(length int) []byte {

0 commit comments

Comments
 (0)