]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] testing: only let workers run fuzz targets
authorKatie Hockman <katie@golang.org>
Thu, 4 Mar 2021 18:02:24 +0000 (13:02 -0500)
committerKatie Hockman <katie@golang.org>
Thu, 4 Mar 2021 18:46:53 +0000 (18:46 +0000)
Previously, ever worker would run all of the unit tests, benchmarks, and
examples. Only the single coordinator needs to do this.

Change-Id: I0dfa7f79b390b6c3220d8ea646e2d2312eee6bb1
Reviewed-on: https://go-review.googlesource.com/c/go/+/298809
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/testdata/script/test_fuzz_chatty.txt
src/testing/testing.go

index aaf385f293b2238b970a875f40447d4a19896b39..ea81bc331da429a3a929cabef58143571647bc10 100644 (file)
@@ -34,6 +34,17 @@ stdout PASS
 stdout 'all good here'
 ! stdout FAIL
 
+# Fuzz successful chatty fuzz target that includes a separate unit test.
+go test -v chatty_with_test_fuzz_test.go -fuzz=Fuzz -fuzztime=1s
+stdout ok
+stdout PASS
+! stdout FAIL
+# TODO: It's currently the case that it's logged twice. Fix that, and change
+# this check to verify it.
+stdout 'all good here'
+# Verify that the unit test is only run once.
+! stdout '(?s)logged foo.*logged foo'
+
 -- chatty_error_fuzz_test.go --
 package chatty_error_fuzz
 
@@ -79,3 +90,17 @@ func Fuzz(f *testing.F) {
     f.Log("all good here")
     f.Fuzz(func(*testing.T, []byte) {})
 }
+
+-- chatty_with_test_fuzz_test.go --
+package chatty_with_test_fuzz
+
+import "testing"
+
+func TestFoo(t *testing.T) {
+    t.Log("logged foo")
+}
+
+func Fuzz(f *testing.F) {
+    f.Log("all good here")
+    f.Fuzz(func(*testing.T, []byte) {})
+}
index 152483d8ffd2a5cb0859c0381ddfff04ff74799a..7ce794c5a89847e1172acbe412544afd5af39098 100644 (file)
@@ -1439,26 +1439,30 @@ func (m *M) Run() (code int) {
 
        m.before()
        defer m.after()
-       deadline := m.startAlarm()
-       haveExamples = len(m.examples) > 0
-       testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline)
-       fuzzTargetsRan, fuzzTargetsOk := runFuzzTargets(m.deps, m.fuzzTargets)
-       exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
-       m.stopAlarm()
-       if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" {
-               fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
-       }
-       if !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 {
-               fmt.Println("FAIL")
-               m.exitCode = 1
-               return
+       if !*isFuzzWorker {
+               // The fuzzing coordinator will already run all tests, examples,
+               // and benchmarks. Don't make the workers do redundant work.
+               deadline := m.startAlarm()
+               haveExamples = len(m.examples) > 0
+               testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline)
+               fuzzTargetsRan, fuzzTargetsOk := runFuzzTargets(m.deps, m.fuzzTargets)
+               exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
+               m.stopAlarm()
+               if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" {
+                       fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
+               }
+               if !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 {
+                       fmt.Println("FAIL")
+                       m.exitCode = 1
+                       return
+               }
        }
 
        fuzzingRan, fuzzingOk := runFuzzing(m.deps, m.fuzzTargets)
        if *matchFuzz != "" && !fuzzingRan {
                fmt.Fprintln(os.Stderr, "testing: warning: no targets to fuzz")
        }
-       if !fuzzingOk && !*isFuzzWorker {
+       if !*isFuzzWorker && !fuzzingOk {
                fmt.Println("FAIL")
                m.exitCode = 1
                return