-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathobserver_test.go
More file actions
270 lines (229 loc) · 6.55 KB
/
Copy pathobserver_test.go
File metadata and controls
270 lines (229 loc) · 6.55 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package gotaskflow
import (
"bytes"
"sync"
"testing"
"time"
)
// TestObserverOpenSpan tests that openSpan creates a span with correct attributes.
func TestObserverOpenSpan(t *testing.T) {
obs := newObserver()
obs.withProfiler(newProfiler())
node := &innerNode{
name: "test-task",
Typ: nodeStatic,
dependents: []*innerNode{{name: "dep1"}, {name: "dep2"}},
}
s := obs.openSpan(node, nil)
if s == nil {
t.Fatal("expected non-nil span")
}
if s.extra.name != "test-task" {
t.Errorf("expected name 'test-task', got %q", s.extra.name)
}
if s.extra.typ != nodeStatic {
t.Errorf("expected type nodeStatic, got %q", s.extra.typ)
}
if s.parent != nil {
t.Errorf("expected nil parent, got %v", s.parent)
}
if len(s.dependents) != 2 || s.dependents[0] != "dep1" || s.dependents[1] != "dep2" {
t.Errorf("expected dependents [dep1, dep2], got %v", s.dependents)
}
}
// TestObserverOpenSpanNil tests that openSpan returns nil when no profiler or tracer is set.
func TestObserverOpenSpanNil(t *testing.T) {
obs := newObserver()
node := &innerNode{
name: "test-task",
Typ: nodeStatic,
}
s := obs.openSpan(node, nil)
if s != nil {
t.Errorf("expected nil span when no profiler/tracer, got %v", s)
}
}
// TestObserverCloseSpan tests that closeSpan records the span to profiler.
func TestObserverCloseSpan(t *testing.T) {
obs := newObserver()
p := newProfiler()
obs.withProfiler(p)
node := &innerNode{
name: "test-task",
Typ: nodeStatic,
}
s := obs.openSpan(node, nil)
time.Sleep(1 * time.Millisecond) // ensure some duration
obs.closeSpan(s, true)
if len(p.spans) != 1 {
t.Errorf("expected 1 span in profiler, got %d", len(p.spans))
}
if s.cost <= 0 {
t.Errorf("expected positive cost, got %v", s.cost)
}
}
// TestObserverCloseSpanNotOk tests that closeSpan does not record span when ok=false (panic).
func TestObserverCloseSpanNotOk(t *testing.T) {
obs := newObserver()
p := newProfiler()
obs.withProfiler(p)
node := &innerNode{
name: "test-task",
Typ: nodeStatic,
}
s := obs.openSpan(node, nil)
time.Sleep(1 * time.Millisecond)
obs.closeSpan(s, false) // ok=false means panic occurred
if len(p.spans) != 0 {
t.Errorf("expected 0 spans in profiler (panicked task), got %d", len(p.spans))
}
}
// TestObserverCloseSpanNil tests that closeSpan handles nil span gracefully.
func TestObserverCloseSpanNil(t *testing.T) {
obs := newObserver()
obs.withProfiler(newProfiler())
// Should not panic
obs.closeSpan(nil, true)
}
// TestObserverWithProfiler tests that withProfiler sets the profiler correctly.
func TestObserverWithProfiler(t *testing.T) {
obs := newObserver()
if obs.profiler != nil {
t.Error("expected nil profiler initially")
}
p := newProfiler()
obs.withProfiler(p)
if obs.profiler != p {
t.Error("profiler not set correctly")
}
}
// TestObserverWithTracer tests that withTracer sets the tracer correctly.
func TestObserverWithTracer(t *testing.T) {
obs := newObserver()
if obs.tracer != nil {
t.Error("expected nil tracer initially")
}
tr := newTracer()
obs.withTracer(tr)
if obs.tracer != tr {
t.Error("tracer not set correctly")
}
}
// TestObserverSpanParentChain tests that span parent chain is preserved.
func TestObserverSpanParentChain(t *testing.T) {
obs := newObserver()
obs.withProfiler(newProfiler())
parentNode := &innerNode{name: "parent", Typ: nodeStatic}
childNode := &innerNode{name: "child", Typ: nodeStatic}
parentSpan := obs.openSpan(parentNode, nil)
childSpan := obs.openSpan(childNode, parentSpan)
if childSpan.parent != parentSpan {
t.Error("child span should have parent span set")
}
if parentSpan.parent != nil {
t.Error("parent span should have nil parent")
}
}
// TestObserverWithBothProfilerAndTracer tests that both profiler and tracer work together.
func TestObserverWithBothProfilerAndTracer(t *testing.T) {
obs := newObserver()
p := newProfiler()
tr := newTracer()
obs.withProfiler(p)
obs.withTracer(tr)
node := &innerNode{name: "test-task", Typ: nodeStatic}
s := obs.openSpan(node, nil)
time.Sleep(1 * time.Millisecond)
obs.closeSpan(s, true)
if len(p.spans) != 1 {
t.Errorf("expected 1 span in profiler, got %d", len(p.spans))
}
if len(tr.events) != 1 {
t.Errorf("expected 1 event in tracer, got %d", len(tr.events))
}
}
// TestObserverConcurrent tests concurrent use of observer.
func TestObserverConcurrent(t *testing.T) {
obs := newObserver()
p := newProfiler()
tr := newTracer()
obs.withProfiler(p)
obs.withTracer(tr)
var wg sync.WaitGroup
n := 100
wg.Add(n)
for i := 0; i < n; i++ {
go func(i int) {
defer wg.Done()
node := &innerNode{
name: "task",
Typ: nodeStatic,
}
s := obs.openSpan(node, nil)
time.Sleep(time.Microsecond)
obs.closeSpan(s, true)
}(i)
}
wg.Wait()
// Note: profiler merges by attr, so may have fewer than n spans
// But tracer should have all n events
if len(tr.events) != n {
t.Errorf("expected %d tracer events, got %d", n, len(tr.events))
}
}
// TestObserverIntegrationWithExecutor tests observer works correctly with executor.
func TestObserverIntegrationWithExecutor(t *testing.T) {
tf := NewTaskFlow("test-flow")
a := tf.NewTask("A", func() {})
b := tf.NewTask("B", func() {})
a.Precede(b)
exec := NewExecutor(4, WithProfiler())
exec.Run(tf).Wait()
var buf bytes.Buffer
err := exec.Profile(&buf)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if buf.Len() == 0 {
t.Error("expected profile output, got empty")
}
}
// TestObserverIntegrationWithTracer tests observer works correctly with tracer.
func TestObserverIntegrationWithTracer(t *testing.T) {
tf := NewTaskFlow("test-flow")
a := tf.NewTask("A", func() {})
b := tf.NewTask("B", func() {})
a.Precede(b)
exec := NewExecutor(4, WithTracer())
exec.Run(tf).Wait()
var buf bytes.Buffer
err := exec.Trace(&buf)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if buf.Len() == 0 {
t.Error("expected trace output, got empty")
}
}
// TestObserverIntegrationWithBoth tests observer with both profiler and tracer.
func TestObserverIntegrationWithBoth(t *testing.T) {
tf := NewTaskFlow("test-flow")
a := tf.NewTask("A", func() {})
b := tf.NewTask("B", func() {})
a.Precede(b)
exec := NewExecutor(4, WithProfiler(), WithTracer())
exec.Run(tf).Wait()
var profileBuf, traceBuf bytes.Buffer
if err := exec.Profile(&profileBuf); err != nil {
t.Errorf("unexpected profile error: %v", err)
}
if err := exec.Trace(&traceBuf); err != nil {
t.Errorf("unexpected trace error: %v", err)
}
if profileBuf.Len() == 0 {
t.Error("expected profile output")
}
if traceBuf.Len() == 0 {
t.Error("expected trace output")
}
}