-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime_test.go
104 lines (76 loc) · 1.7 KB
/
runtime_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package loc
import (
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRuntimeCaller(t *testing.T) {
rpc, rfile, rline, ok := runtime.Caller(0)
pc := Caller(0)
f := runtime.FuncForPC(rpc)
rname := f.Name()
name, file, line := pc.NameFileLine()
require.True(t, ok)
assert.Equal(t, rname, name)
assert.True(t, strings.HasSuffix(rfile, file))
assert.Equal(t, rline, line-2)
assert.Equal(t, f.Entry(), uintptr(pc.FuncEntry()))
assert.Equal(t, f.Entry(), uintptr(FuncEntryFromFunc(TestRuntimeCaller)))
}
func TestRuntimeCallers(t *testing.T) {
var rpcs [2]uintptr
n := runtime.Callers(1, rpcs[:])
pcs := Callers(0, 2)
var rsum string
frames := runtime.CallersFrames(rpcs[:n])
i := 0
for {
fr, ok := frames.Next()
name, file, line := pcs[i].NameFileLine()
rline := fr.Line
if i != 0 {
rsum += " at "
} else {
rline += 2 // we called them from different lines
}
rsum += filepath.Base(fr.File) + ":" + strconv.Itoa(rline)
assert.Equal(t, fr.Function, name)
assert.True(t, strings.HasSuffix(fr.File, file))
assert.Equal(t, rline, line)
i++
if !ok {
break
}
}
if t.Failed() {
t.Logf("callers: %v", pcs)
t.Logf("runtime: %v", rsum)
}
}
func BenchmarkRuntimeCallerNameFileLine(b *testing.B) {
b.ReportAllocs()
var pc uintptr
var ok bool
for i := 0; i < b.N; i++ {
pc, _, _, ok = runtime.Caller(0)
f := runtime.FuncForPC(pc)
_ = f.Name()
}
if !ok {
b.Errorf("not ok")
}
}
func BenchmarkRuntimeCallerFileLine(b *testing.B) {
b.ReportAllocs()
var ok bool
for i := 0; i < b.N; i++ {
_, _, _, ok = runtime.Caller(0)
}
if !ok {
b.Errorf("not ok")
}
}