diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index 4bd1c71b2d..6f9b8a2e41 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -26,6 +26,7 @@ import ( "runtime" "strconv" "strings" + "sync" "syscall" "testing" "time" @@ -246,7 +247,12 @@ func TestWALSegmentSizeBounds(t *testing.T) { // Log stderr in case of failure. stderr, err := prom.StderrPipe() require.NoError(t, err) + + // WaitGroup is used to ensure that we don't call t.Log() after the test has finished. + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() slurp, _ := io.ReadAll(stderr) t.Log(string(slurp)) }() @@ -264,6 +270,7 @@ func TestWALSegmentSizeBounds(t *testing.T) { prom.Process.Kill() <-done } + wg.Wait() return } @@ -273,6 +280,8 @@ func TestWALSegmentSizeBounds(t *testing.T) { require.ErrorAs(t, err, &exitError) status := exitError.Sys().(syscall.WaitStatus) require.Equal(t, tc.exitCode, status.ExitStatus()) + + wg.Wait() }) } } @@ -497,7 +506,12 @@ func TestModeSpecificFlags(t *testing.T) { // Log stderr in case of failure. stderr, err := prom.StderrPipe() require.NoError(t, err) + + // WaitGroup is used to ensure that we don't call t.Log() after the test has finished. + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() slurp, _ := io.ReadAll(stderr) t.Log(string(slurp)) }() @@ -515,6 +529,7 @@ func TestModeSpecificFlags(t *testing.T) { prom.Process.Kill() <-done } + wg.Wait() return } @@ -527,6 +542,8 @@ func TestModeSpecificFlags(t *testing.T) { } else { t.Errorf("unable to retrieve the exit status for prometheus: %v", err) } + + wg.Wait() }) } }