...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package stacktrace
22
23 import (
24 "bytes"
25 "strings"
26 "testing"
27
28 "github.com/stretchr/testify/assert"
29 "github.com/stretchr/testify/require"
30 )
31
32 func TestTake(t *testing.T) {
33 trace := Take(0)
34 lines := strings.Split(trace, "\n")
35 require.NotEmpty(t, lines, "Expected stacktrace to have at least one frame.")
36 assert.Contains(
37 t,
38 lines[0],
39 "go.uber.org/zap/internal/stacktrace.TestTake",
40 "Expected stacktrace to start with the test.",
41 )
42 }
43
44 func TestTakeWithSkip(t *testing.T) {
45 trace := Take(1)
46 lines := strings.Split(trace, "\n")
47 require.NotEmpty(t, lines, "Expected stacktrace to have at least one frame.")
48 assert.Contains(
49 t,
50 lines[0],
51 "testing.",
52 "Expected stacktrace to start with the test runner (skipping our own frame).",
53 )
54 }
55
56 func TestTakeWithSkipInnerFunc(t *testing.T) {
57 var trace string
58 func() {
59 trace = Take(2)
60 }()
61 lines := strings.Split(trace, "\n")
62 require.NotEmpty(t, lines, "Expected stacktrace to have at least one frame.")
63 assert.Contains(
64 t,
65 lines[0],
66 "testing.",
67 "Expected stacktrace to start with the test function (skipping the test function).",
68 )
69 }
70
71 func TestTakeDeepStack(t *testing.T) {
72 const (
73 N = 500
74 withStackDepthName = "go.uber.org/zap/internal/stacktrace.withStackDepth"
75 )
76 withStackDepth(N, func() {
77 trace := Take(0)
78 for found := 0; found < N; found++ {
79 i := strings.Index(trace, withStackDepthName)
80 if i < 0 {
81 t.Fatalf(`expected %v occurrences of %q, found %d`,
82 N, withStackDepthName, found)
83 }
84 trace = trace[i+len(withStackDepthName):]
85 }
86 })
87 }
88
89 func BenchmarkTake(b *testing.B) {
90 for i := 0; i < b.N; i++ {
91 Take(0)
92 }
93 }
94
95 func withStackDepth(depth int, f func()) {
96 var recurse func(rune) rune
97 recurse = func(r rune) rune {
98 if r > 0 {
99 bytes.Map(recurse, []byte(string([]rune{r - 1})))
100 } else {
101 f()
102 }
103 return 0
104 }
105 recurse(rune(depth))
106 }
107
View as plain text