Skip to content

Commit

Permalink
Try to avoid flakiness on http helper timeout tests (elastic#11345)
Browse files Browse the repository at this point in the history
Block the response only during a time or till the conext is done.
Close body if a request object is returned.
  • Loading branch information
jsoriano committed Mar 21, 2019
1 parent eb4d918 commit 33e356b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions metricbeat/helper/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func TestGetAuthHeaderFromTokenNoFile(t *testing.T) {
}

func TestTimeout(t *testing.T) {
c := make(chan struct{})
defer close(c)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-c
select {
case <-time.After(5 * time.Nanosecond):
case <-r.Context().Done():
}
}))
defer ts.Close()

Expand Down Expand Up @@ -160,8 +161,11 @@ func checkTimeout(t *testing.T, h *HTTP) {

done := make(chan struct{})
go func() {
_, err := h.FetchResponse()
response, err := h.FetchResponse()
assert.Error(t, err)
if response != nil {
response.Body.Close()
}
done <- struct{}{}
}()

Expand Down

0 comments on commit 33e356b

Please sign in to comment.