-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathctx_test.go
86 lines (75 loc) · 1.86 KB
/
ctx_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
package monkit
import (
"context"
"testing"
"time"
)
// TestLateObserver checks that if you add an observer to a trace after it has
// begun, the original span that started the trace will also be observed
func TestLateObserver(t *testing.T) {
ctx := context.Background()
mon := Package()
mock := &mockSpanObserver{}
func() {
// Start the first span, this is the trace
defer mon.Task()(&ctx)(nil)
// Start a second span, this is a span on the trace, but still occurs
// before we register a span observer
defer mon.Task()(&ctx)(nil)
// Now start observing all new traces
mon.r.ObserveTraces(func(t *Trace) {
t.ObserveSpans(mock)
})
// Now go through all root spans, and observe the traces associated
// with those spans
mon.r.RootSpans(func(s *Span) {
s.Trace().ObserveSpans(mock)
})
// One more after-the-fact
defer mon.Task()(&ctx)(nil)
// Here is a new trace, to prove that is working
ctx2 := context.Background()
defer mon.Task()(&ctx2)(nil)
}()
expStarts := 2
expFinishes := 4
if mock.starts != expStarts {
t.Errorf("Expected %d, got %d", expStarts, mock.starts)
}
if mock.finishes != expFinishes {
t.Errorf("Expected %d, got %d", expFinishes, mock.finishes)
}
}
type mockSpanObserver struct {
starts, finishes int
}
func (m *mockSpanObserver) Start(s *Span) {
m.starts++
}
func (m *mockSpanObserver) Finish(s *Span, err error, panicked bool, finish time.Time) {
m.finishes++
}
func BenchmarkTask(b *testing.B) {
mon := Package()
pctx := context.Background()
for i := 0; i < b.N; i++ {
var err error
func() {
ctx := pctx
defer mon.Task()(&ctx)(&err)
}()
}
}
func BenchmarkTaskNested(b *testing.B) {
mon := Package()
pctx := context.Background()
var errout error
defer mon.Task()(&pctx)(&errout)
for i := 0; i < b.N; i++ {
var err error
func() {
ctx := pctx
defer mon.Task()(&ctx)(&err)
}()
}
}