-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace_test.go
222 lines (193 loc) · 5.09 KB
/
trace_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package trc_test
import (
"context"
"fmt"
"io"
"math/rand"
"reflect"
"sync"
"testing"
"github.com/peterbourgon/trc"
)
// TraceTest performs basic validation of trace implementations.
func TraceTest(t *testing.T, constructor trc.NewTraceFunc) {
t.Helper()
ctx := context.Background()
t.Run("Unique ID", func(t *testing.T) {
index := map[string]bool{}
for i := 0; i < 10; i++ {
_, tr := constructor(ctx, "src", "foo")
if _, ok := index[tr.ID()]; ok {
t.Errorf("duplicate ID %s", tr.ID())
}
index[tr.ID()] = true
}
})
t.Run("Errored", func(t *testing.T) {
_, tr := constructor(ctx, "src", "foo")
if want, have := false, tr.Errored(); want != have {
t.Errorf("Trace was marked Errored without error event")
}
tr.Errorf("err")
if want, have := true, tr.Errored(); want != have {
t.Errorf("Errorf didn't mark Errored immediately")
}
tr.Finish()
if want, have := true, tr.Errored(); want != have {
t.Errorf("Errorf didn't mark Errored after finish")
}
})
t.Run("Finish prevents updates", func(t *testing.T) {
_, tr := constructor(ctx, "src", "foo")
tr.Tracef("first")
tr.LazyTracef("second")
f1 := tr.Finished()
e1 := tr.Events()
tr.Finish()
f2 := tr.Finished()
d1 := tr.Duration()
tr.Tracef("should be no-op")
tr.LazyTracef("should be no-op")
tr.Errorf("should not error")
tr.LazyErrorf("should not error")
d2 := tr.Duration()
e2 := tr.Events()
if f1 == true {
t.Errorf("Finished returned true before Finish was called")
}
if f2 == false {
t.Errorf("Finished returned false after Finish was called")
}
if d1 != d2 {
t.Errorf("Duration changed after Finish was called")
}
if want, have := false, tr.Errored(); want != have {
t.Errorf("Errored was changed after Finish was called")
}
if !reflect.DeepEqual(e1, e2) {
t.Errorf("Events changed after Finish was called")
}
})
t.Run("Normal events", func(t *testing.T) {
_, tr := constructor(ctx, "src", "foo")
a := []int{1, 2, 3}
tr.Tracef("a=%v", a)
tr.Finish()
a[0] = 0
if want, have := "a=[1 2 3]", tr.Events()[0].What; want != have {
t.Errorf("want %s, have %s", want, have)
}
})
t.Run("Lazy events", func(t *testing.T) {
_, tr := constructor(ctx, "src", "foo")
a := []int{1, 2, 3}
tr.LazyTracef("a=%v", a)
tr.Finish()
a[0] = 0
if want, have := "a=[0 2 3]", tr.Events()[0].What; want != have {
t.Errorf("want %s, have %s", want, have)
}
})
t.Run("Error event", func(t *testing.T) {
_, tr := constructor(ctx, "src", "foo")
tr.Errorf("this is an error")
tr.Finish()
AssertEqual(t, true, tr.Errored())
})
t.Run("optional SetMaxEvents", func(t *testing.T) {
_, tr := constructor(ctx, "src", "foo")
defer tr.Finish()
m, ok := tr.(interface{ SetMaxEvents(int) })
if !ok {
t.Skipf("%T doesn't have a SetMaxEvents method", tr)
}
max, extra := 32, 17
m.SetMaxEvents(max)
for i := 0; i < max+extra; i++ {
tr.Tracef("event %d", i+1)
}
events := tr.Events()
have := len(events)
want1, want2 := max, max+1 // we can have an extra "truncated" event
if !(have == want1 || have == want2) {
t.Errorf("events: want either %d or %d, have %d", want1, want2, have)
}
})
t.Run("Concurrency", func(t *testing.T) {
t.Parallel()
workers := 100
_, tr := constructor(ctx, "src", "foo")
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
switch r := rand.Float64(); {
case r < 0.05:
tr.Errorf("trace event %d", rand.Intn(100))
case r < 0.10:
tr.LazyErrorf("trace event %d", rand.Intn(100))
case r < 0.75:
tr.Tracef("trace event %d", rand.Intn(100))
default:
tr.LazyTracef("trace event %d", rand.Intn(100))
}
_ = tr.ID()
_ = tr.Source()
_ = tr.Category()
_ = tr.Started()
_ = tr.Finished()
_ = tr.Errored()
_ = tr.Duration()
_ = tr.Events()
}()
}
wg.Wait()
tr.Finish()
AssertEqual(t, workers, len(tr.Events()))
})
}
func TestCoreTrace(t *testing.T) {
t.Parallel()
TraceTest(t, trc.New)
}
func TestTraceContext(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
ctx0 := context.Background()
if _, ok := trc.MaybeGet(ctx0); ok {
t.Fatalf("unexpectedly got trace from fresh context")
}
})
t.Run("same", func(t *testing.T) {
ctx, tr1 := trc.New(context.Background(), "src", "1")
tr2, ok := trc.MaybeGet(ctx)
if !ok {
t.Fatalf("MaybeFromContext failed to return a trace")
}
if want, have := tr1.ID(), tr2.ID(); want != have {
t.Fatalf("ID: want %s, have %s", want, have)
}
})
t.Run("scoping", func(t *testing.T) {
ctx, tr0 := trc.New(context.Background(), "src", "a")
func(ctx context.Context) {
ctx, tr1 := trc.New(ctx, "src", "b")
if tr0.ID() == tr1.ID() {
t.Errorf("tr0 %s == tr1 %s, bad", tr0.ID(), tr1.ID())
}
tr2 := trc.Get(ctx)
if tr1.ID() != tr2.ID() {
t.Errorf("tr1 %s != tr2 %s, bad", tr1.ID(), tr2.ID())
}
{
ctx, tr3 := trc.New(ctx, "src", "c")
fmt.Fprint(io.Discard, ctx, tr3)
}
tr4 := trc.Get(ctx)
if tr4.ID() != tr2.ID() {
t.Errorf("tr4 %s != tr2 %s, bad", tr4.ID(), tr2.ID())
}
}(ctx)
})
}