Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: BenchmarkParallel that calls b.Fatal can get a race detector failure #45526

Closed
ianlancetaylor opened this issue Apr 12, 2021 · 1 comment
Closed

Comments

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 12, 2021

Running this test with go test -race -bench=. will get race detector errors. The b.Fatal should be enough to cause a failure, we shouldn't get a race detector error report on top of that.

package main_test

import (
	"math/rand"
	"testing"
	"time"
)

func BenchmarkParallel(b *testing.B) {
	rand.Seed(time.Now().UnixNano())
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			if rand.Float64() < 0.1 {
				b.Fatal("fatal")
			}
		}
	})
}

Sample output:

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
BenchmarkParallel-8   	==================
WARNING: DATA RACE
Write at 0x00c0001602e1 by goroutine 26:
  testing.(*common).FailNow()
      /home/iant/go/src/testing/testing.go:741 +0x4f
  testing.(*common).Fatal()
      /home/iant/go/src/testing/testing.go:810 +0x89
  command-line-arguments_test.BenchmarkParallel.func1()
      /home/iant/foo_test.go:14 +0xb2
  testing.(*B).RunParallel.func1()
      /home/iant/go/src/testing/benchmark.go:779 +0x1a3

Previous write at 0x00c0001602e1 by goroutine 25:
  testing.(*common).FailNow()
      /home/iant/go/src/testing/testing.go:741 +0x4f
  testing.(*common).Fatal()
      /home/iant/go/src/testing/testing.go:810 +0x89
  command-line-arguments_test.BenchmarkParallel.func1()
      /home/iant/foo_test.go:14 +0xb2
  testing.(*B).RunParallel.func1()
      /home/iant/go/src/testing/benchmark.go:779 +0x1a3

Goroutine 26 (running) created at:
  testing.(*B).RunParallel()
      /home/iant/go/src/testing/benchmark.go:772 +0x355
  command-line-arguments_test.BenchmarkParallel()
      /home/iant/foo_test.go:11 +0x177
  testing.(*B).runN()
      /home/iant/go/src/testing/benchmark.go:192 +0x1c5
  testing.(*B).launch()
      /home/iant/go/src/testing/benchmark.go:325 +0x37e

Goroutine 25 (finished) created at:
  testing.(*B).RunParallel()
      /home/iant/go/src/testing/benchmark.go:772 +0x355
  command-line-arguments_test.BenchmarkParallel()
      /home/iant/foo_test.go:11 +0x177
  testing.(*B).runN()
      /home/iant/go/src/testing/benchmark.go:192 +0x1c5
  testing.(*B).launch()
      /home/iant/go/src/testing/benchmark.go:325 +0x37e
==================
--- FAIL: BenchmarkParallel-8
    foo_test.go:14: fatal
    foo_test.go:14: fatal
    foo_test.go:14: fatal
    foo_test.go:14: fatal
    foo_test.go:14: fatal
    foo_test.go:14: fatal
    benchmark.go:198: race detected during execution of benchmark
FAIL
exit status 1
FAIL	command-line-arguments	0.019s
FAIL
@gopherbot
Copy link

@gopherbot gopherbot commented Apr 13, 2021

Change https://golang.org/cl/309572 mentions this issue: testing: benchmark parallel tests can call Fatal and SkipNow

@gopherbot gopherbot closed this in bc5de81 Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants