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
This commit is contained in:
tgiachi 2026-11-10 12:00:04 +01:00
commit 266bcba354

View file

@ -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 {