From 08e7875dc5df1f5caf5431199e02391dfa58bfd4 Mon Sep 17 00:00:00 2001 From: Hank Donnay Date: Tue, 9 Apr 2024 17:40:20 -0500 Subject: [PATCH] httptransport: fix test flake Saw this test flake in CI and managed to reproduce locally. I haven't been able to reproduce with this patch, but don't have a great explanation as to why it's not consistent. Signed-off-by: Hank Donnay --- httptransport/instrumentation_test.go | 29 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/httptransport/instrumentation_test.go b/httptransport/instrumentation_test.go index 1a65e6db18..f3ae4d84da 100644 --- a/httptransport/instrumentation_test.go +++ b/httptransport/instrumentation_test.go @@ -45,19 +45,26 @@ clair_http_test_request_total{code="500",handler="err",method="get"} 1 srv := httptest.NewUnstartedServer(m) srv.Config.BaseContext = func(_ net.Listener) context.Context { return ctx } - srv.Start() - defer srv.Close() - c := srv.Client() - for _, p := range []string{"ok", "err", "err?panic=1"} { - u := srv.URL + "/" + p - t.Logf("making request: %q", u) - res, err := c.Get(u) - if err != nil { - t.Error(err) + // Create a scope for doing the actual requests. + // + // Doing this and having the server teardown run synchronously should be + // enough time to ensure the metrics are actually collected. + func() { + srv.Start() + defer srv.Close() + + c := srv.Client() + for _, p := range []string{"ok", "err", "err?panic=1"} { + u := srv.URL + "/" + p + t.Logf("making request: %q", u) + res, err := c.Get(u) + if err != nil { + t.Error(err) + } + t.Logf("got status: %q", res.Status) } - t.Logf("got status: %q", res.Status) - } + }() if err := testutil.GatherAndCompare(reg, want, "clair_http_test_request_total"); err != nil { t.Error(err)