-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode_stacktrace_test.go
More file actions
59 lines (49 loc) · 1.06 KB
/
encode_stacktrace_test.go
File metadata and controls
59 lines (49 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package zlog
import (
"bytes"
"runtime"
"sync"
"testing"
"github.com/icefed/zlog/buffer"
)
var (
wantPCFunction string
wantPCFile string
wantPCLine int
getPC = sync.OnceValue(func() uintptr {
pcs := make([]uintptr, 1)
runtime.Callers(1, pcs)
sfs := runtime.CallersFrames(pcs)
sf, _ := sfs.Next()
wantPCFunction = sf.Function
wantPCFile = sf.File
wantPCLine = sf.Line
return pcs[0]
})
)
func stacktraceCaller2(buf *buffer.Buffer) {
pcs := make([]uintptr, 1)
runtime.Callers(2, pcs)
formatStacktrace(buf, pcs[0])
}
func stacktraceCaller1(buf *buffer.Buffer) {
stacktraceCaller2(buf)
}
func TestFormatStacktrace(t *testing.T) {
buf := buffer.New()
defer buf.Free()
stacktraceCaller1(buf)
lines := bytes.Split(buf.Bytes(), []byte{'\n'})
if len(lines) != 8 {
t.Errorf("got %v, want %v", len(lines), 8)
}
buf.Reset()
// not found
pcs := make([]uintptr, 1)
runtime.Callers(1, pcs)
formatStacktrace(buf, pcs[0])
lines = bytes.Split(buf.Bytes(), []byte{'\n'})
if len(lines) != 2 {
t.Errorf("got %v, want %v", len(lines), 2)
}
}