Skip to content

Commit

Permalink
chore: add timeout to channel and HTTP read
Browse files Browse the repository at this point in the history
Fixes #1927
  • Loading branch information
enocom committed May 22, 2023
1 parent bf890c2 commit 1deb6df
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 1deb6df

Please sign in to comment.