From 1deb6df1b131af7e5c425737d47a4bb829ad1112 Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Mon, 22 May 2023 13:30:00 -0600 Subject: [PATCH] chore: add timeout to channel and HTTP read Fixes #1927 --- cmd/root_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 540a88fe4..312071d41 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1128,11 +1128,13 @@ func tryDial(method, addr string) (*http.Response, error) { return nil, err } req := &http.Request{Method: method, URL: u} + // Never wait longer than 30 seconds for an HTTP response. + cl := &http.Client{Timeout: 30 * time.Second} for { if attempts > 10 { return resp, err } - resp, err = http.DefaultClient.Do(req) + resp, err = cl.Do(req) if err != nil { attempts++ time.Sleep(time.Second) @@ -1203,15 +1205,24 @@ func TestQuitQuitQuit(t *testing.T) { if resp.StatusCode != http.StatusBadRequest { t.Fatalf("expected a 400 status, got = %v", resp.StatusCode) } - resp, err = http.Post("http://localhost:9192/quitquitquit", "", nil) + resp, err = tryDial("POST", "http://localhost:9192/quitquitquit") if err != nil { t.Fatalf("failed to dial endpoint: %v", err) } if resp.StatusCode != http.StatusOK { t.Fatalf("expected a 200 status, got = %v", resp.StatusCode) } - if want, got := errQuitQuitQuit, <-errCh; !errors.Is(got, want) { - t.Fatalf("want = %v, got = %v", want, got) + + var gotErr error + select { + case err := <-errCh: + gotErr = err + case <-time.After(30 * time.Second): + t.Fatal("timeout waiting for error") + } + + if !errors.Is(gotErr, errQuitQuitQuit) { + t.Fatalf("want = %v, got = %v", errQuitQuitQuit, gotErr) } }