Commit f735d2d9 authored by Russ Cox's avatar Russ Cox

testing: use runtime/debug to format panics

Among other things, this avoids putting a testing.go:nnn:
prefix on every line of the stack trace.

R=golang-dev, r, dsymonds, r
CC=golang-dev
https://golang.org/cl/5651081
parent 6a75ece0
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package debug package debug_test
import ( import (
. "runtime/debug"
"strings" "strings"
"testing" "testing"
) )
......
...@@ -71,6 +71,7 @@ import ( ...@@ -71,6 +71,7 @@ import (
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
"runtime/debug"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
"strings" "strings"
...@@ -225,19 +226,6 @@ func (c *common) Fatalf(format string, args ...interface{}) { ...@@ -225,19 +226,6 @@ func (c *common) Fatalf(format string, args ...interface{}) {
c.FailNow() c.FailNow()
} }
// TODO(dsymonds): Consider hooking into runtime·traceback instead.
func (c *common) stack() {
for i := 2; ; i++ { // Caller we care about is the user, 2 frames up
pc, file, line, ok := runtime.Caller(i)
f := runtime.FuncForPC(pc)
if !ok || f == nil {
break
}
c.Logf("%s:%d (0x%x)", file, line, pc)
c.Logf("\t%s", f.Name())
}
}
// Parallel signals that this test is to be run in parallel with (and only with) // Parallel signals that this test is to be run in parallel with (and only with)
// other parallel tests in this CPU group. // other parallel tests in this CPU group.
func (t *T) Parallel() { func (t *T) Parallel() {
...@@ -260,11 +248,12 @@ func tRunner(t *T, test *InternalTest) { ...@@ -260,11 +248,12 @@ func tRunner(t *T, test *InternalTest) {
// a call to runtime.Goexit, record the duration and send // a call to runtime.Goexit, record the duration and send
// a signal saying that the test is done. // a signal saying that the test is done.
defer func() { defer func() {
// Consider any uncaught panic a failure. if false {
if err := recover(); err != nil { // Log and recover from panic instead of aborting binary.
t.failed = true if err := recover(); err != nil {
t.Log(err) t.failed = true
t.stack() t.Logf("%s\n%s", err, debug.Stack())
}
} }
t.duration = time.Now().Sub(t.start) t.duration = time.Now().Sub(t.start)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment