From 89a44722ea37739a293faa3d0bd5daf2ae094e37 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 20 Mar 2019 15:57:26 +0100 Subject: [PATCH] Try to avoid flakiness on http helper timeout tests Block the response only during a time or till the conext is done. Close body if a request object is returned. --- metricbeat/helper/http_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/metricbeat/helper/http_test.go b/metricbeat/helper/http_test.go index aee80166fa2..aed5ad44031 100644 --- a/metricbeat/helper/http_test.go +++ b/metricbeat/helper/http_test.go @@ -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() @@ -118,8 +119,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{}{} }()