Skip to content

Commit befcdef

Browse files
David KushnerChrisHines
authored andcommitted
Implement panic stack trace test.
1 parent a4d942e commit befcdef

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

stack_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,7 @@ func TestTrimRuntime(t *testing.T) {
224224
}
225225
}
226226

227-
func TestPanic(t *testing.T) {
228-
defer func() {
229-
if recover() != nil {
230-
trace := stack.Trace().TrimRuntime()
231-
fmt.Println(trace)
232-
}
233-
}()
234227

235-
var x *uintptr
236-
_ = *x
237-
}
238228

239229
func BenchmarkCallVFmt(b *testing.B) {
240230
c := stack.Caller(0)

stackinternal_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package stack
33
import (
44
"runtime"
55
"testing"
6+
"github.com/dkushner/stack"
7+
"strings"
8+
"strconv"
69
)
710

811
func TestCaller(t *testing.T) {
@@ -47,11 +50,53 @@ func TestTrace(t *testing.T) {
4750

4851
cs := fh.labyrinth()
4952

50-
lines := []int{43, 33, 48}
53+
lines := []int{46, 36, 51}
5154

5255
for i, line := range lines {
5356
if got, want := cs[i].line(), line; got != want {
5457
t.Errorf("got line[%d] == %v, want line[%d] == %v", i, got, i, want)
5558
}
5659
}
5760
}
61+
62+
// Test stack handling originating from a sigpanic.
63+
func TestTracePanic(t *testing.T) {
64+
t.Parallel()
65+
66+
defer func() {
67+
if recover() != nil {
68+
trace := stack.Trace().TrimRuntime()
69+
70+
if len(trace) != 6 {
71+
t.Errorf("got len(trace) == %v, want %v", len(trace), 6)
72+
}
73+
74+
// Check frames in this file, the interceding frames are somewhat
75+
// platform-dependent.
76+
lines := []int64{68, 101}
77+
78+
var local []int64
79+
for _, call := range trace {
80+
parts := strings.Split(call.String(), ":")
81+
if parts[0] == "stackinternal_test.go" {
82+
line, _ := strconv.ParseInt(parts[1], 10, 32)
83+
local = append(local, line)
84+
}
85+
}
86+
87+
if len(local) != 2 {
88+
t.Errorf("expected %v local frames but got %v", 2, len(local))
89+
}
90+
91+
for i, line := range lines {
92+
if got, want := local[i], line; got != want {
93+
t.Errorf("got line[%d] == %v, want line[%d] == %v", i, got, i, want)
94+
}
95+
}
96+
}
97+
}()
98+
99+
// Initiate a sigpanic.
100+
var x *uintptr
101+
_ = *x
102+
}

0 commit comments

Comments
 (0)