|
8 | 8 | "net/http" |
9 | 9 | "net/url" |
10 | 10 | "sync" |
| 11 | + "sync/atomic" |
11 | 12 | "testing" |
12 | 13 |
|
13 | 14 | "github.com/stretchr/testify/require" |
@@ -116,32 +117,36 @@ func TestProxyWithAuthRejection(t *testing.T) { |
116 | 117 | } |
117 | 118 | } |
118 | 119 |
|
| 120 | +var testPortCounter int32 = 50000 |
| 121 | + |
119 | 122 | 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) |
123 | 126 | } |
124 | | - defer l.Close() |
125 | | - |
126 | | - return l.Addr().String() |
| 127 | + return fmt.Sprintf("127.0.0.1:%d", port) |
127 | 128 | } |
128 | 129 |
|
129 | 130 | // startUpstreamServer starts an HTTP server that responds with "test text" on /test. |
130 | 131 | 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 | + |
132 | 137 | mux := http.NewServeMux() |
133 | 138 | mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { |
134 | 139 | _, _ = fmt.Fprintf(w, "test text") |
135 | 140 | }) |
136 | 141 | //nolint |
137 | | - httpServer := &http.Server{Addr: addr, Handler: mux} |
| 142 | + httpServer := &http.Server{Handler: mux} |
138 | 143 | go func() { |
139 | | - _ = httpServer.ListenAndServe() |
| 144 | + _ = httpServer.Serve(l) |
140 | 145 | }() |
141 | 146 | t.Cleanup(func() { |
142 | 147 | httpServer.Shutdown(context.Background()) |
143 | 148 | }) |
144 | | - return addr |
| 149 | + return l.Addr().String() |
145 | 150 | } |
146 | 151 |
|
147 | 152 | // newSOCKS5HttpClient creates an HTTP client that routes through a SOCKS5 proxy. |
|
0 commit comments