Fix test race by not calling t.Log() after test completion

Fixes https://github.com/prometheus/prometheus/issues/16169

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2025-03-05 12:11:01 +01:00
parent 9e5d59b777
commit 6054e843fe

View file

@ -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()
})
}
}