Skip to content

Commit 2058231

Browse files
committed
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 <[email protected]>
1 parent f19b59b commit 2058231

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

httptransport/instrumentation_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,26 @@ clair_http_test_request_total{code="500",handler="err",method="get"} 1
4545

4646
srv := httptest.NewUnstartedServer(m)
4747
srv.Config.BaseContext = func(_ net.Listener) context.Context { return ctx }
48-
srv.Start()
49-
defer srv.Close()
5048

51-
c := srv.Client()
52-
for _, p := range []string{"ok", "err", "err?panic=1"} {
53-
u := srv.URL + "/" + p
54-
t.Logf("making request: %q", u)
55-
res, err := c.Get(u)
56-
if err != nil {
57-
t.Error(err)
49+
// Create a scope for doing the actual requests.
50+
//
51+
// Doing this and having the server teardown run synchronously should be
52+
// enough time to ensure the metrics are actually collected.
53+
func() {
54+
srv.Start()
55+
defer srv.Close()
56+
57+
c := srv.Client()
58+
for _, p := range []string{"ok", "err", "err?panic=1"} {
59+
u := srv.URL + "/" + p
60+
t.Logf("making request: %q", u)
61+
res, err := c.Get(u)
62+
if err != nil {
63+
t.Error(err)
64+
}
65+
t.Logf("got status: %q", res.Status)
5866
}
59-
t.Logf("got status: %q", res.Status)
60-
}
67+
}()
6168

6269
if err := testutil.GatherAndCompare(reg, want, "clair_http_test_request_total"); err != nil {
6370
t.Error(err)

0 commit comments

Comments
 (0)