Skip to content

Commit 256ac64

Browse files
committed
fix: eliminate port race in setupFakeEnvoyStats test helper
net.Listen(":0") followed by l.Close() and a later ListenAndServe() on the resulting port number leaves a window where another process (or a concurrently running test) can grab that exact ephemeral port before the re-bind happens, causing TestGetTotalConnections to fail with connection errors on loaded machines. Serve on the already-bound listener directly via s.Serve(l) so the port is never released. Signed-off-by: Muhammad Waqar <mwaqar@confluent.io>
1 parent 8782e63 commit 256ac64

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

internal/cmd/envoy/shutdown_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121

2222
// setupFakeEnvoyStats set up an HTTP server return content
2323
func setupFakeEnvoyStats(t *testing.T, content string) *http.Server {
24+
// Reuse the bound listener instead of closing and re-binding the port.
2425
l, err := net.Listen("tcp", ":0") //nolint: gosec
2526
require.NoError(t, err)
26-
require.NoError(t, l.Close())
2727
mux := http.NewServeMux()
2828
mux.HandleFunc("/", func(writer http.ResponseWriter, _ *http.Request) {
2929
writer.Header().Set("Content-Type", "application/json")
@@ -39,7 +39,7 @@ func setupFakeEnvoyStats(t *testing.T, content string) *http.Server {
3939
}
4040
t.Logf("start to listen at %s ", addr)
4141
go func() {
42-
if err := s.ListenAndServe(); err != nil {
42+
if err := s.Serve(l); err != nil {
4343
fmt.Println("fail to listen: ", err)
4444
}
4545
}()

0 commit comments

Comments
 (0)