From 5918101d673d601c26f5de880b1fa2c6564fb745 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 20 Mar 2025 12:16:17 -0400 Subject: testing: detect a stopped timer in B.Loop Currently, if the user stops the timer in a B.Loop benchmark loop, the benchmark will run until it hits the timeout and fails. Fix this by detecting that the timer is stopped and failing the benchmark right away. We avoid making the fast path more expensive for this check by "poisoning" the B.Loop iteration counter when the timer is stopped so that it falls back to the slow path, which can check the timer. This causes b to escape from B.Loop, which is totally harmless because it was already definitely heap-allocated. But it causes the test/inline_testingbloop.go errorcheck test to fail. I don't think the escape messages actually mattered to that test, they just had to be matched. To fix this, we drop the debug level to -m=1, since -m=2 prints a lot of extra information for escaping parameters that we don't want to deal with, and change one error check to allow b to escape. Fixes #72971. Change-Id: I7d4abbb1ec1e096685514536f91ba0d581cca6b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/659657 Auto-Submit: Austin Clements Reviewed-by: Junyang Shao LUCI-TryBot-Result: Go LUCI --- src/testing/loop_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/testing/loop_test.go') diff --git a/src/testing/loop_test.go b/src/testing/loop_test.go index 423094fbbd..743cbe64f0 100644 --- a/src/testing/loop_test.go +++ b/src/testing/loop_test.go @@ -129,3 +129,26 @@ func TestBenchmarkBLoopError(t *T) { t.Errorf("want N == 0, got %d", bRet.N) } } + +func TestBenchmarkBLoopStop(t *T) { + var bState *B + var bLog bytes.Buffer + bRet := Benchmark(func(b *B) { + bState = b + b.common.w = &bLog + + for i := 0; b.Loop(); i++ { + b.StopTimer() + } + }) + if !bState.failed { + t.Errorf("benchmark should have failed") + } + const wantLog = "B.Loop called with timer stopped" + if log := bLog.String(); !strings.Contains(log, wantLog) { + t.Errorf("missing error %q in output:\n%s", wantLog, log) + } + if bRet.N != 0 { + t.Errorf("want N == 0, got %d", bRet.N) + } +} -- cgit v1.3-5-g9baa