From b11defeaed46a56e2d8a32f16ce3e22a93c3a825 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Tue, 31 Oct 2023 09:54:54 -0700 Subject: [PATCH] runtime: make select fairness test less picky Allow up to 10 standard deviations from the mean, instead of ~5 that the current test allows. 10 standard deviations allows up to a 4500/5500 split. Fixes #52465 Change-Id: Icb21c1d31fafbcf4723b75435ba5e98863e812c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/538815 Auto-Submit: Keith Randall Reviewed-by: Bryan Mills Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- src/runtime/chan_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/runtime/chan_test.go b/src/runtime/chan_test.go index 256f97676e..526d45bb43 100644 --- a/src/runtime/chan_test.go +++ b/src/runtime/chan_test.go @@ -481,12 +481,13 @@ func TestSelectFairness(t *testing.T) { } // If the select in the goroutine is fair, // cnt1 and cnt2 should be about the same value. - // With 10,000 trials, the expected margin of error at - // a confidence level of six nines is 4.891676 / (2 * Sqrt(10000)). - r := float64(cnt1) / trials - e := math.Abs(r - 0.5) - t.Log(cnt1, cnt2, r, e) - if e > 4.891676/(2*math.Sqrt(trials)) { + // See if we're more than 10 sigma away from the expected value. + // 10 sigma is a lot, but we're ok with some systematic bias as + // long as it isn't too severe. + const mean = trials * 0.5 + const variance = trials * 0.5 * (1 - 0.5) + stddev := math.Sqrt(variance) + if math.Abs(float64(cnt1-mean)) > 10*stddev { t.Errorf("unfair select: in %d trials, results were %d, %d", trials, cnt1, cnt2) } close(done) -- 2.50.0