Merge pull request #16170 from prometheus/fix-test-logging-race

Fix test race by not calling t.Log() after test completion
This commit is contained in:
Julius Volz 2025-03-05 14:10:46 +01:00 committed by GitHub
commit 677efa4678
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,6 +26,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"testing" "testing"
"time" "time"
@ -246,7 +247,12 @@ func TestWALSegmentSizeBounds(t *testing.T) {
// Log stderr in case of failure. // Log stderr in case of failure.
stderr, err := prom.StderrPipe() stderr, err := prom.StderrPipe()
require.NoError(t, err) 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() { go func() {
defer wg.Done()
slurp, _ := io.ReadAll(stderr) slurp, _ := io.ReadAll(stderr)
t.Log(string(slurp)) t.Log(string(slurp))
}() }()
@ -264,6 +270,7 @@ func TestWALSegmentSizeBounds(t *testing.T) {
prom.Process.Kill() prom.Process.Kill()
<-done <-done
} }
wg.Wait()
return return
} }
@ -273,6 +280,8 @@ func TestWALSegmentSizeBounds(t *testing.T) {
require.ErrorAs(t, err, &exitError) require.ErrorAs(t, err, &exitError)
status := exitError.Sys().(syscall.WaitStatus) status := exitError.Sys().(syscall.WaitStatus)
require.Equal(t, tc.exitCode, status.ExitStatus()) require.Equal(t, tc.exitCode, status.ExitStatus())
wg.Wait()
}) })
} }
} }
@ -497,7 +506,12 @@ func TestModeSpecificFlags(t *testing.T) {
// Log stderr in case of failure. // Log stderr in case of failure.
stderr, err := prom.StderrPipe() stderr, err := prom.StderrPipe()
require.NoError(t, err) 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() { go func() {
defer wg.Done()
slurp, _ := io.ReadAll(stderr) slurp, _ := io.ReadAll(stderr)
t.Log(string(slurp)) t.Log(string(slurp))
}() }()
@ -515,6 +529,7 @@ func TestModeSpecificFlags(t *testing.T) {
prom.Process.Kill() prom.Process.Kill()
<-done <-done
} }
wg.Wait()
return return
} }
@ -527,6 +542,8 @@ func TestModeSpecificFlags(t *testing.T) {
} else { } else {
t.Errorf("unable to retrieve the exit status for prometheus: %v", err) t.Errorf("unable to retrieve the exit status for prometheus: %v", err)
} }
wg.Wait()
}) })
} }
} }