-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
caller_test.go
62 lines (58 loc) · 1.59 KB
/
caller_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
package tracer_test
import (
"strings"
"testing"
. "github.com/kamilsk/tracer"
)
func TestCaller(t *testing.T) {
tests := []struct {
name string
caller func() CallerInfo
expected []string
}{
{"direct caller", callerA, []string{"github.com/kamilsk/tracer_test.callerA"}},
{"chain caller", callerB, []string{"github.com/kamilsk/tracer_test.callerA"}},
{"lambda caller", callerC, []string{
"github.com/kamilsk/tracer_test.callerC",
"github.com/kamilsk/tracer_test.callerC.func1", // Go 1.10, 1.11 - https://golang.org/doc/go1.12#runtime
}},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
var found bool
obtained := tc.caller().Name
for _, expected := range tc.expected {
if expected == obtained {
found = true
break
}
}
if !found {
t.Errorf("\n expected: %+#v \n obtained: %+#v", strings.Join(tc.expected, " or "), obtained)
}
})
}
}
// BenchmarkCaller/direct_caller-12 5000000 308 ns/op 0 B/op 0 allocs/op
// BenchmarkCaller/chain_caller-12 5000000 272 ns/op 0 B/op 0 allocs/op
// BenchmarkCaller/lambda_caller-12 5000000 365 ns/op 0 B/op 0 allocs/op
func BenchmarkCaller(b *testing.B) {
benchmarks := []struct {
name string
caller func() CallerInfo
}{
{"direct caller", callerA},
{"chain caller", callerB},
{"lambda caller", callerC},
}
for _, bm := range benchmarks {
tc := bm
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = tc.caller()
}
})
}
}