Skip to content

Commit b894834

Browse files
committed
[dev.fuzz] testing: only let workers run fuzz targets
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 <[email protected]> Run-TryBot: Katie Hockman <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 354c77a commit b894834

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/cmd/go/testdata/script/test_fuzz_chatty.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ stdout PASS
3434
stdout 'all good here'
3535
! stdout FAIL
3636

37+
# Fuzz successful chatty fuzz target that includes a separate unit test.
38+
go test -v chatty_with_test_fuzz_test.go -fuzz=Fuzz -fuzztime=1s
39+
stdout ok
40+
stdout PASS
41+
! stdout FAIL
42+
# TODO: It's currently the case that it's logged twice. Fix that, and change
43+
# this check to verify it.
44+
stdout 'all good here'
45+
# Verify that the unit test is only run once.
46+
! stdout '(?s)logged foo.*logged foo'
47+
3748
-- chatty_error_fuzz_test.go --
3849
package chatty_error_fuzz
3950

@@ -79,3 +90,17 @@ func Fuzz(f *testing.F) {
7990
f.Log("all good here")
8091
f.Fuzz(func(*testing.T, []byte) {})
8192
}
93+
94+
-- chatty_with_test_fuzz_test.go --
95+
package chatty_with_test_fuzz
96+
97+
import "testing"
98+
99+
func TestFoo(t *testing.T) {
100+
t.Log("logged foo")
101+
}
102+
103+
func Fuzz(f *testing.F) {
104+
f.Log("all good here")
105+
f.Fuzz(func(*testing.T, []byte) {})
106+
}

src/testing/testing.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,26 +1439,30 @@ func (m *M) Run() (code int) {
14391439

14401440
m.before()
14411441
defer m.after()
1442-
deadline := m.startAlarm()
1443-
haveExamples = len(m.examples) > 0
1444-
testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline)
1445-
fuzzTargetsRan, fuzzTargetsOk := runFuzzTargets(m.deps, m.fuzzTargets)
1446-
exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
1447-
m.stopAlarm()
1448-
if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" {
1449-
fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
1450-
}
1451-
if !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 {
1452-
fmt.Println("FAIL")
1453-
m.exitCode = 1
1454-
return
1442+
if !*isFuzzWorker {
1443+
// The fuzzing coordinator will already run all tests, examples,
1444+
// and benchmarks. Don't make the workers do redundant work.
1445+
deadline := m.startAlarm()
1446+
haveExamples = len(m.examples) > 0
1447+
testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline)
1448+
fuzzTargetsRan, fuzzTargetsOk := runFuzzTargets(m.deps, m.fuzzTargets)
1449+
exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
1450+
m.stopAlarm()
1451+
if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" {
1452+
fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
1453+
}
1454+
if !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 {
1455+
fmt.Println("FAIL")
1456+
m.exitCode = 1
1457+
return
1458+
}
14551459
}
14561460

14571461
fuzzingRan, fuzzingOk := runFuzzing(m.deps, m.fuzzTargets)
14581462
if *matchFuzz != "" && !fuzzingRan {
14591463
fmt.Fprintln(os.Stderr, "testing: warning: no targets to fuzz")
14601464
}
1461-
if !fuzzingOk && !*isFuzzWorker {
1465+
if !*isFuzzWorker && !fuzzingOk {
14621466
fmt.Println("FAIL")
14631467
m.exitCode = 1
14641468
return

0 commit comments

Comments
 (0)