-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathexecutor_tracer_test.go
More file actions
198 lines (166 loc) · 6.25 KB
/
executor_tracer_test.go
File metadata and controls
198 lines (166 loc) · 6.25 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
package gotaskflow_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"
"time"
gotaskflow "github.com/noneback/go-taskflow"
)
func TestExecutorWithTracer(t *testing.T) {
executor := gotaskflow.NewExecutor(4, gotaskflow.WithTracer())
tf := gotaskflow.NewTaskFlow("G")
A, B, C :=
tf.NewTask("A", func() { fmt.Println("A") }),
tf.NewTask("B", func() { fmt.Println("B") }),
tf.NewTask("C", func() { fmt.Println("C") })
A.Precede(B)
C.Precede(B)
executor.Run(tf).Wait()
var buf bytes.Buffer
if err := executor.Trace(&buf); err != nil {
t.Fatalf("Trace error: %v", err)
}
var events []map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &events); err != nil {
t.Fatalf("Trace output is not valid JSON: %v", err)
}
if len(events) != 3 {
t.Errorf("expected 3 trace events, got %d", len(events))
}
t.Logf(buf.String())
}
func TestExecutorWithoutTracer(t *testing.T) {
executor := gotaskflow.NewExecutor(4)
tf := gotaskflow.NewTaskFlow("G")
tf.NewTask("A", func() { fmt.Println("A") })
executor.Run(tf).Wait()
// Trace should return nil when tracer is not enabled
if err := executor.Trace(os.Stdout); err != nil {
t.Fatalf("Trace should return nil when disabled, got: %v", err)
}
}
func TestExecutorWithProfiler(t *testing.T) {
executor := gotaskflow.NewExecutor(4, gotaskflow.WithProfiler())
tf := gotaskflow.NewTaskFlow("G")
tf.NewTask("A", func() { fmt.Println("A") })
tf.NewTask("B", func() { fmt.Println("B") })
executor.Run(tf).Wait()
var buf bytes.Buffer
if err := executor.Profile(&buf); err != nil {
t.Fatalf("Profile error: %v", err)
}
if buf.Len() == 0 {
t.Error("expected profiler output, got empty")
}
}
func TestExecutorWithoutProfiler(t *testing.T) {
executor := gotaskflow.NewExecutor(4)
tf := gotaskflow.NewTaskFlow("G")
tf.NewTask("A", func() { fmt.Println("A") })
executor.Run(tf).Wait()
// Profile should return nil when profiler is not enabled
if err := executor.Profile(os.Stdout); err != nil {
t.Fatalf("Profile should return nil when disabled, got: %v", err)
}
}
func TestExecutorTracePrint(t *testing.T) {
executor := gotaskflow.NewExecutor(4, gotaskflow.WithTracer())
tf := gotaskflow.NewTaskFlow("pipeline")
A, B, C, D :=
tf.NewTask("fetch", func() { time.Sleep(10 * time.Millisecond) }),
tf.NewTask("parse", func() { time.Sleep(5 * time.Millisecond) }),
tf.NewTask("process", func() { time.Sleep(8 * time.Millisecond) }),
tf.NewTask("output", func() { time.Sleep(3 * time.Millisecond) })
A.Precede(B)
B.Precede(C)
C.Precede(D)
executor.Run(tf).Wait()
t.Log("=== Chrome Trace JSON (paste into chrome://tracing or https://ui.perfetto.dev) ===")
if err := executor.Trace(os.Stdout); err != nil {
t.Fatalf("Trace error: %v", err)
}
t.Log("=== End of Trace ===")
}
// TestExecutorTraceComplex simulates a data-processing pipeline:
//
// prepare
// ├─ read_config ─┐
// └─ load_data ─┴─ validate ─ check(cond)
// └─0─ sub_process (subflow)
// ├─ transform ─┐
// └─ enrich ─┴─ aggregate ─ report
func TestExecutorTraceComplex(t *testing.T) {
executor := gotaskflow.NewExecutor(8, gotaskflow.WithTracer())
tf := gotaskflow.NewTaskFlow("data-pipeline")
prepare := tf.NewTask("prepare", func() { time.Sleep(5 * time.Millisecond) })
// Parallel stage: read config and load data concurrently.
readConfig := tf.NewTask("read_config", func() { time.Sleep(8 * time.Millisecond) })
loadData := tf.NewTask("load_data", func() { time.Sleep(12 * time.Millisecond) })
// Merge: validate after both parallel tasks complete.
validate := tf.NewTask("validate", func() { time.Sleep(4 * time.Millisecond) })
// Condition: always takes the normal processing path (index 0).
check := tf.NewCondition("check_quality", func() uint { return 0 })
// Normal path: subflow runs transform and enrich in parallel, then aggregates.
subProcess := tf.NewSubflow("sub_process", func(sf *gotaskflow.Subflow) {
transform := sf.NewTask("transform", func() { time.Sleep(10 * time.Millisecond) })
enrich := sf.NewTask("enrich", func() { time.Sleep(7 * time.Millisecond) })
aggregate := sf.NewTask("aggregate", func() { time.Sleep(5 * time.Millisecond) })
transform.Precede(aggregate)
enrich.Precede(aggregate)
})
// Fallback path (not executed in this test).
fallback := tf.NewTask("fallback", func() { time.Sleep(2 * time.Millisecond) })
report := tf.NewTask("report", func() { time.Sleep(3 * time.Millisecond) })
// Build dependencies.
prepare.Precede(readConfig, loadData)
readConfig.Precede(validate)
loadData.Precede(validate)
validate.Precede(check)
check.Precede(subProcess, fallback) // 0 → subProcess, 1 → fallback
subProcess.Precede(report)
executor.Run(tf).Wait()
var buf bytes.Buffer
if err := executor.Trace(&buf); err != nil {
t.Fatalf("Trace error: %v", err)
}
var events []map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &events); err != nil {
t.Fatalf("Trace output is not valid JSON: %v", err)
}
// prepare + read_config + load_data + validate + check + sub_process + transform + enrich + aggregate + report = 10
if len(events) != 10 {
t.Errorf("expected 10 trace events, got %d", len(events))
}
t.Log("=== Complex Pipeline Chrome Trace JSON ===")
t.Log(buf.String())
t.Log("=== Paste into https://ui.perfetto.dev to visualize ===")
}
func TestExecutorWithBothProfilerAndTracer(t *testing.T) {
executor := gotaskflow.NewExecutor(4, gotaskflow.WithProfiler(), gotaskflow.WithTracer())
tf := gotaskflow.NewTaskFlow("G")
A, B :=
tf.NewTask("A", func() { fmt.Println("A") }),
tf.NewTask("B", func() { fmt.Println("B") })
A.Precede(B)
executor.Run(tf).Wait()
var profBuf bytes.Buffer
if err := executor.Profile(&profBuf); err != nil {
t.Fatalf("Profile error: %v", err)
}
if profBuf.Len() == 0 {
t.Error("expected profiler output, got empty")
}
var traceBuf bytes.Buffer
if err := executor.Trace(&traceBuf); err != nil {
t.Fatalf("Trace error: %v", err)
}
var events []map[string]interface{}
if err := json.Unmarshal(traceBuf.Bytes(), &events); err != nil {
t.Fatalf("Trace output is not valid JSON: %v", err)
}
if len(events) != 2 {
t.Errorf("expected 2 trace events, got %d", len(events))
}
}