]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] testing: small cleanup to running targets
authorKatie Hockman <katie@golang.org>
Thu, 17 Sep 2020 14:28:18 +0000 (10:28 -0400)
committerFilippo Valsorda <filippo@golang.org>
Fri, 4 Dec 2020 18:17:29 +0000 (19:17 +0100)
Change-Id: Idcf90c5acbf7dbba2ea01d21d893214a5c2028c2
Reviewed-on: https://go-review.googlesource.com/c/go/+/255517
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.txt
src/testing/fuzz.go

index 68e50418220dadee67ae271e1f5ad0e3a9b75751..24350ee450834210811c43e08aea8ea26fb91c4c 100644 (file)
@@ -8,6 +8,16 @@ go test success_fuzz_test.go
 stdout ok
 ! stdout FAIL
 
+# Test that calling f.Fatal while fuzzing causes a non-zero exit status.
+! go test -fuzz Fuzz fail_fuzz_test.go
+! stdout ^ok
+stdout FAIL
+
+# Test that successful fuzzing exits cleanly.
+go test -fuzz Fuzz success_fuzz_test.go
+stdout ok
+! stdout FAIL
+
 [short] stop
 
 # Test that calling panic(nil) in a fuzz target causes a non-zero exit status.
index f5162115b423b394f108259c4c7cf2a232ea4d9d..ee7f68e5448873dac179b4f4d5c1648d94c47af1 100644 (file)
@@ -78,20 +78,16 @@ func (f *F) run(name string, fn func(f *F)) (ran, ok bool) {
                context: f.context,
        }
        if innerF.chatty != nil {
-               if f.fuzz {
-                       innerF.chatty.Updatef(name, "--- FUZZ: %s\n", name)
-               } else {
-                       innerF.chatty.Updatef(name, "=== RUN   %s\n", name)
-               }
+               innerF.chatty.Updatef(name, "=== RUN   %s\n", name)
        }
-       go runTarget(innerF, fn)
+       go innerF.runTarget(fn)
        <-innerF.signal
        return innerF.ran, !innerF.failed
 }
 
 // runTarget runs the given target, handling panics and exits
 // within the test, and reporting errors.
-func runTarget(f *F, fn func(f *F)) {
+func (f *F) runTarget(fn func(f *F)) {
        defer func() {
                err := recover()
                // If the function has recovered but the test hasn't finished,
@@ -202,7 +198,8 @@ func runFuzzing(matchString func(pat, str string) (bool, error), fuzzTargets []I
        }
        f := &F{
                common: common{
-                       w: os.Stdout,
+                       signal: make(chan bool),
+                       w:      os.Stdout,
                },
                context: ctx,
                fuzz:    true,
@@ -227,8 +224,11 @@ func runFuzzing(matchString func(pat, str string) (bool, error), fuzzTargets []I
        }
        if Verbose() {
                f.chatty = newChattyPrinter(f.w)
+               f.chatty.Updatef(f.name, "--- FUZZ: %s\n", f.name)
        }
-       return f.run(ft.Name, ft.Fn)
+       go f.runTarget(ft.Fn)
+       <-f.signal
+       return f.ran, !f.failed
 }
 
 // Fuzz runs a single fuzz target. It is useful for creating