test(caddy): fix flaky tests on macOS

This commit is contained in:
Kévin Dunglas
2026-02-18 15:26:31 +01:00
parent abba64b0ff
commit 151ea1c87d

View File

@@ -11,6 +11,7 @@ import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddytest"
@@ -19,6 +20,26 @@ import (
"github.com/stretchr/testify/require"
)
// waitForServerReady polls the server with retries until it responds to HTTP requests.
// This handles a race condition during Caddy config reload on macOS where SO_REUSEPORT
// can briefly route connections to the old listener being shut down,
// resulting in "connection reset by peer".
func waitForServerReady(t *testing.T, url string) {
t.Helper()
client := &http.Client{Timeout: 1 * time.Second}
for range 10 {
resp, err := client.Get(url)
if err == nil {
require.NoError(t, resp.Body.Close())
return
}
time.Sleep(100 * time.Millisecond)
}
}
var testPort = "9080"
func TestPHP(t *testing.T) {
@@ -406,6 +427,7 @@ func TestPHPServerDirective(t *testing.T) {
}
`, "caddyfile")
waitForServerReady(t, "http://localhost:"+testPort)
tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "I am by birth a Genevese (i not set)")
tester.AssertGetResponse("http://localhost:"+testPort+"/hello.txt", http.StatusOK, "Hello\n")
tester.AssertGetResponse("http://localhost:"+testPort+"/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
@@ -431,6 +453,7 @@ func TestPHPServerDirectiveDisableFileServer(t *testing.T) {
}
`, "caddyfile")
waitForServerReady(t, "http://localhost:"+testPort)
tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "I am by birth a Genevese (i not set)")
tester.AssertGetResponse("http://localhost:"+testPort+"/not-found.txt", http.StatusOK, "I am by birth a Genevese (i not set)")
}