]> Cypherpunks.ru repositories - gostls13.git/commitdiff
testing: fix error message when a parallel Cleanup calls runtime.Goexit
authorChangkun Ou <hi@changkun.de>
Mon, 27 Sep 2021 10:06:43 +0000 (12:06 +0200)
committerGopher Robot <gobot@golang.org>
Mon, 14 Nov 2022 17:37:45 +0000 (17:37 +0000)
Fixes #48502

Change-Id: I6054b043ebd2237e19897fdf1234b311d19facc7
Reviewed-on: https://go-review.googlesource.com/c/go/+/352350
Reviewed-by: Joedian Reid <joedian@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Changkun Ou <mail@changkun.de>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>

src/testing/panic_test.go
src/testing/testing.go

index 6b8b95391df16e810e3d99b77253b8616277d17f..fafcff790e2f1500e168a8a4a4a686587421f05b 100644 (file)
@@ -11,6 +11,7 @@ import (
        "os"
        "os/exec"
        "regexp"
+       "runtime"
        "strings"
        "testing"
 )
@@ -208,3 +209,42 @@ func TestPanicHelper(t *testing.T) {
                })
        }
 }
+
+func TestMorePanic(t *testing.T) {
+       testenv.MustHaveExec(t)
+
+       testCases := []struct {
+               desc  string
+               flags []string
+               want  string
+       }{
+               {
+                       desc:  "Issue 48502: call runtime.Goexit in t.Cleanup after panic",
+                       flags: []string{"-test.run=TestGoexitInCleanupAfterPanicHelper"},
+                       want: `panic: die
+       panic: test executed panic(nil) or runtime.Goexit`,
+               },
+       }
+
+       for _, tc := range testCases {
+               cmd := exec.Command(os.Args[0], tc.flags...)
+               cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
+               b, _ := cmd.CombinedOutput()
+               got := string(b)
+               want := tc.want
+               re := makeRegexp(want)
+               if ok, err := regexp.MatchString(re, got); !ok || err != nil {
+                       t.Errorf("output:\ngot:\n%s\nwant:\n%s", got, want)
+               }
+       }
+}
+
+func TestGoexitInCleanupAfterPanicHelper(t *testing.T) {
+       if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
+               return
+       }
+
+       t.Cleanup(func() { runtime.Goexit() })
+       t.Parallel()
+       panic("die")
+}
index b2a65e95d3e9a2e8b9aff4b7c78d60382d374142..8d3129fbcdb1abff11488c5200cd207d9f2191e4 100644 (file)
@@ -1462,8 +1462,10 @@ func tRunner(t *T, fn func(t *T)) {
                                finished = p.finished
                                p.mu.RUnlock()
                                if finished {
-                                       t.Errorf("%v: subtest may have called FailNow on a parent test", err)
-                                       err = nil
+                                       if !t.isParallel {
+                                               t.Errorf("%v: subtest may have called FailNow on a parent test", err)
+                                               err = nil
+                                       }
                                        signal = false
                                        break
                                }