From 266bcba354cca7538f36a61712bb7bc859fa18a7 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 12:00:04 +0100 Subject: [PATCH] test(app): use an HTTP client without keep-alive - the transport could dial a connection and never use it; the HTTP server's graceful shutdown waits 5 s for such a connection, which made the Close tests flaky under -race - the call and fetch helpers now open one connection per request --- internal/app/app_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 6c66ee3..b7fcf42 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -122,13 +122,17 @@ func testConfig(t *testing.T, relay string) config.Config { return c } +// client opens one connection per request: a pooled connection the transport dialed and never +// used would hold an HTTP server's graceful shutdown for 5 s. +var client = &http.Client{Transport: &http.Transport{DisableKeepAlives: true}} + func call(t *testing.T, a *App, method, path, body, token string) int { t.Helper() req, _ := http.NewRequest(method, "http://"+a.APIAddr+path, strings.NewReader(body)) if token != "" { req.Header.Set("Authorization", "Bearer "+token) } - resp, err := http.DefaultClient.Do(req) + resp, err := client.Do(req) if err != nil { t.Fatal(err) } @@ -139,7 +143,7 @@ func call(t *testing.T, a *App, method, path, body, token string) int { func fetch(t *testing.T, method, url string) (int, string) { t.Helper() req, _ := http.NewRequest(method, url, nil) - resp, err := http.DefaultClient.Do(req) + resp, err := client.Do(req) if err != nil { t.Fatal(err) } @@ -466,8 +470,6 @@ func TestCloseLetsAnLMTPDeliveryInFlightFinish(t *testing.T) { } call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org"}`, "tok") call(t, a, "POST", "/api/v1/lists/1/members", `{"address":"alice@example.com"}`, "tok") - // A connection the HTTP client opened and never used would hold the API's shutdown for 5 s. - http.DefaultClient.CloseIdleConnections() conn, err := net.Dial("tcp", a.LMTPAddr) if err != nil {