From: Changkun Ou Date: Mon, 27 Sep 2021 10:06:43 +0000 (+0200) Subject: testing: fix error message when a parallel Cleanup calls runtime.Goexit X-Git-Tag: go1.20rc1~290 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a650e399dfc2435eb18efc430a70cba9d87cec73;p=gostls13.git testing: fix error message when a parallel Cleanup calls runtime.Goexit Fixes #48502 Change-Id: I6054b043ebd2237e19897fdf1234b311d19facc7 Reviewed-on: https://go-review.googlesource.com/c/go/+/352350 Reviewed-by: Joedian Reid TryBot-Result: Gopher Robot Run-TryBot: Changkun Ou Reviewed-by: Bryan Mills Auto-Submit: Bryan Mills --- diff --git a/src/testing/panic_test.go b/src/testing/panic_test.go index 6b8b95391d..fafcff790e 100644 --- a/src/testing/panic_test.go +++ b/src/testing/panic_test.go @@ -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") +} diff --git a/src/testing/testing.go b/src/testing/testing.go index b2a65e95d3..8d3129fbcd 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -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 }