From 47d9d3b86fbf23084c2b97e1f3ab40ca796a165e Mon Sep 17 00:00:00 2001 From: zhushunjia Date: Tue, 12 Nov 2024 14:10:17 +0800 Subject: [PATCH] wait until all request are handled Change-Id: Ia81b85668d6f9966fa26e5b744bf5498a3f34768 --- plugins/flusher/prometheus/flusher_prometheus_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/flusher/prometheus/flusher_prometheus_test.go b/plugins/flusher/prometheus/flusher_prometheus_test.go index 797352a7d4..98ae4cbad1 100644 --- a/plugins/flusher/prometheus/flusher_prometheus_test.go +++ b/plugins/flusher/prometheus/flusher_prometheus_test.go @@ -22,6 +22,7 @@ import ( "net/http" "sort" "strings" + "sync" "testing" "time" @@ -117,8 +118,10 @@ func TestPrometheusFlusher_ShouldWriteToRemoteStorageSuccess_GivenCorrectDataWit httpmock.Activate() defer httpmock.DeactivateAndReset() + var wg sync.WaitGroup httpmock.RegisterResponder("POST", endpoint, func(req *http.Request) (*http.Response, error) { + defer wg.Done() username, password, err := parseBasicAuth(req.Header.Get("Authorization")) if err != nil { return httpmock.NewStringResponse(http.StatusUnauthorized, "Invalid Authorization"), fmt.Errorf("invalid authentication: %w", err) @@ -238,10 +241,12 @@ func TestPrometheusFlusher_ShouldWriteToRemoteStorageSuccess_GivenCorrectDataWit } expectedWriteRequest = sortPromLabelsInWriteRequest(expectedWriteRequest) httpmock.ZeroCallCounters() + wg.Add(2) err := flusher.Export(groupEventsSlice, nil) So(err, ShouldBeNil) time.Sleep(1 * time.Second) // guarantee that all http requests are handled + wg.Wait() err = flusher.Stop() So(err, ShouldBeNil)