From bbb435b034fb5d3d41b4c59e859bc66a7009be7d Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 27 Oct 2026 18:36:51 +0000 Subject: [PATCH] Health check test uses addresses nothing can listen on (closes #158) TestHealthCheck closed the app's server, and later smallwebwaf, and expected connections to the freed ports to fail. Another test could take such a port in between, so the connection succeeded and the test failed. The failing app check now uses the multicast address 224.0.0.1, to which TCP cannot connect. The failing smallwebwaf check uses port 0, on which nothing listens. Neither result depends on other processes. The code under test is unchanged. Model: opus-5-5 --- internal/smallwebwaf/healthcheck_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/smallwebwaf/healthcheck_test.go b/internal/smallwebwaf/healthcheck_test.go index 3089f22..a316c6c 100644 --- a/internal/smallwebwaf/healthcheck_test.go +++ b/internal/smallwebwaf/healthcheck_test.go @@ -60,8 +60,13 @@ func TestHealthCheck(t *testing.T) { }, 0, "") } - app.Close() - wantHealthCheck(t, env, 1, "unhealthy: connect to the app: ") + // Another test can take a closed server's port at once, so the app's + // address here is one that nothing can listen on: TCP cannot connect + // to a multicast address such as 224.0.0.1. + wantHealthCheck(t, map[string]string{ + listenAddr: ":" + port, + upstreamURL: "http://224.0.0.1", + }, 1, "unhealthy: connect to the app: ") stop() @@ -71,7 +76,10 @@ func TestHealthCheck(t *testing.T) { t.Fatal("still running after being told to stop") } - wantHealthCheck(t, env, 1, "unhealthy: ask smallwebwaf: ") + // Listening on port 0 takes any free port, so nothing listens on port 0 + // itself. + wantHealthCheck(t, map[string]string{listenAddr: ":0"}, 1, + "unhealthy: ask smallwebwaf: ") wantHealthCheck(t, map[string]string{listenAddr: "8080"}, 1, "unhealthy: invalid setting: SWWAF_LISTEN_ADDR: ") }