test: give the dynamic timeout tests a private random source

TestDynamicTimeoutAdjustExponential and TestDynamicTimeoutAdjustNormal
seeded the global generator and then drew from it while other tests in the
package may use the same generator, so the sample was not the one the seed
promised and the exponential case failed once in a full race run. A private
source makes both tests deterministic.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PvgysXDmhPBBimCReYtA8q
Signed-off-by: Feng Ruohang <rh@vonng.com>
This commit is contained in:
Feng Ruohang
2026-09-02 20:04:57 +08:00
parent 3f9c79e919
commit ebac0ca73b
+6 -4
View File
@@ -180,12 +180,14 @@ func testDynamicTimeoutAdjust(t *testing.T, timeout *dynamicTimeout, f func() fl
func TestDynamicTimeoutAdjustExponential(t *testing.T) { func TestDynamicTimeoutAdjustExponential(t *testing.T) {
timeout := newDynamicTimeout(time.Minute, time.Second) timeout := newDynamicTimeout(time.Minute, time.Second)
rand.Seed(0) // A private source keeps the sample independent of other tests that use
// the global generator concurrently.
rng := rand.New(rand.NewSource(0))
initial := timeout.Timeout() initial := timeout.Timeout()
for range 10 { for range 10 {
testDynamicTimeoutAdjust(t, timeout, rand.ExpFloat64) testDynamicTimeoutAdjust(t, timeout, rng.ExpFloat64)
} }
adjusted := timeout.Timeout() adjusted := timeout.Timeout()
@@ -197,13 +199,13 @@ func TestDynamicTimeoutAdjustExponential(t *testing.T) {
func TestDynamicTimeoutAdjustNormalized(t *testing.T) { func TestDynamicTimeoutAdjustNormalized(t *testing.T) {
timeout := newDynamicTimeout(time.Minute, time.Second) timeout := newDynamicTimeout(time.Minute, time.Second)
rand.Seed(0) rng := rand.New(rand.NewSource(0))
initial := timeout.Timeout() initial := timeout.Timeout()
for range 10 { for range 10 {
testDynamicTimeoutAdjust(t, timeout, func() float64 { testDynamicTimeoutAdjust(t, timeout, func() float64 {
return 1.0 + rand.NormFloat64() return 1.0 + rng.NormFloat64()
}) })
} }