-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathvalidator_test.go
More file actions
328 lines (269 loc) · 9.2 KB
/
validator_test.go
File metadata and controls
328 lines (269 loc) · 9.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package gotaskflow
import (
"strings"
"testing"
)
func TestValidatorSimpleSerial(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("serial")
A := tf.NewTask("A", func() {})
B := tf.NewTask("B", func() {})
C := tf.NewTask("C", func() {})
A.Precede(B)
B.Precede(C)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid, got: %s", result.String())
}
}
func TestValidatorParallel(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("parallel")
// Fan-out: A -> B, C in parallel -> D (fan-in)
A := tf.NewTask("A", func() {})
B := tf.NewTask("B", func() {})
C := tf.NewTask("C", func() {})
D := tf.NewTask("D", func() {})
A.Precede(B, C)
B.Precede(D)
C.Precede(D)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid, got: %s", result.String())
}
}
func TestValidatorSubflow(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("with_subflow")
A := tf.NewTask("A", func() {})
sub := tf.NewSubflow("sub", func(sf *Subflow) {
S1 := sf.NewTask("S1", func() {})
S2 := sf.NewTask("S2", func() {})
S1.Precede(S2)
})
B := tf.NewTask("B", func() {})
A.Precede(sub)
sub.Precede(B)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid, got: %s", result.String())
}
}
func TestValidatorConditionBranch(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("condition")
A := tf.NewTask("A", func() {})
cond := tf.NewCondition("cond", func() uint { return 0 }) // always choose branch 0
B := tf.NewTask("B", func() {}) // branch 0 - will execute
C := tf.NewTask("C", func() {}) // branch 1 - will skip
D := tf.NewTask("D", func() {})
A.Precede(cond)
cond.Precede(B, C) // 0 -> B, 1 -> C
B.Precede(D)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid (C should be skipped branch), got: %s", result.String())
}
// C should be in skipped branches
if !containsStr(result.skippedBranches, "C") {
t.Errorf("expected C in skipped branches, got: %v", result.skippedBranches)
}
}
func TestValidatorWithoutTracer(t *testing.T) {
executor := NewExecutor(4) // no tracer
tf := NewTaskFlow("no_tracer")
A := tf.NewTask("A", func() {})
B := tf.NewTask("B", func() {})
A.Precede(B)
executor.Run(tf).Wait()
// nil record (no tracer) should be treated as always valid
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid (no tracer = nil record), got: %s", result.String())
}
}
func TestValidatorComplexPipeline(t *testing.T) {
executor := NewExecutor(8, WithTracer())
tf := NewTaskFlow("complex")
// prepare -> (read_config || load_data) -> validate -> check(cond) -> sub_process -> report
prepare := tf.NewTask("prepare", func() {})
readConfig := tf.NewTask("read_config", func() {})
loadData := tf.NewTask("load_data", func() {})
validateTask := tf.NewTask("validate", func() {})
check := tf.NewCondition("check", func() uint { return 0 })
subProcess := tf.NewSubflow("sub_process", func(sf *Subflow) {
transform := sf.NewTask("transform", func() {})
enrich := sf.NewTask("enrich", func() {})
aggregate := sf.NewTask("aggregate", func() {})
transform.Precede(aggregate)
enrich.Precede(aggregate)
})
fallback := tf.NewTask("fallback", func() {}) // skipped
report := tf.NewTask("report", func() {})
prepare.Precede(readConfig, loadData)
readConfig.Precede(validateTask)
loadData.Precede(validateTask)
validateTask.Precede(check)
check.Precede(subProcess, fallback)
subProcess.Precede(report)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid, got: %s", result.String())
}
// fallback should be skipped
if !containsStr(result.skippedBranches, "fallback") {
t.Errorf("expected fallback in skipped branches, got: %v", result.skippedBranches)
}
}
// TestValidatorConditionBranch1 verifies that branch 1 executes and branch 0 is skipped.
func TestValidatorConditionBranch1(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("cond_branch1")
cond := tf.NewCondition("cond", func() uint { return 1 }) // always branch 1
branch0 := tf.NewTask("branch0", func() {}) // skipped
branch1 := tf.NewTask("branch1", func() {}) // executed
cond.Precede(branch0, branch1)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid, got: %s", result.String())
}
if !containsStr(result.skippedBranches, "branch0") {
t.Errorf("expected branch0 in skipped branches, got: %v", result.skippedBranches)
}
if containsStr(result.skippedBranches, "branch1") {
t.Errorf("branch1 should have executed, not skipped")
}
}
// TestValidatorNestedSubflow verifies validation with a subflow nested inside another subflow.
func TestValidatorNestedSubflow(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("nested_subflow")
outer := tf.NewSubflow("outer", func(sf *Subflow) {
inner := sf.NewSubflow("inner", func(sf2 *Subflow) {
x := sf2.NewTask("X", func() {})
y := sf2.NewTask("Y", func() {})
x.Precede(y)
})
z := sf.NewTask("Z", func() {})
inner.Precede(z)
})
end := tf.NewTask("end", func() {})
outer.Precede(end)
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid for nested subflow, got: %s", result.String())
}
}
// TestValidatorIndependentTasks verifies that fully independent tasks (no edges) are all validated.
func TestValidatorIndependentTasks(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf := NewTaskFlow("independent")
tf.NewTask("T1", func() {})
tf.NewTask("T2", func() {})
tf.NewTask("T3", func() {})
executor.Run(tf).Wait()
result := validate(mustSnapshot(executor), tf)
if !result.valid {
t.Errorf("expected valid, got: %s", result.String())
}
if len(result.missingTasks) > 0 {
t.Errorf("expected no missing tasks, got: %v", result.missingTasks)
}
}
// TestValidatorMissingTask verifies that tasks defined but never executed appear in missingTasks.
// We run tf1 (A->B), then validate against tf2 (A->B->C). C is never executed → missing.
func TestValidatorMissingTask(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf1 := NewTaskFlow("run")
a1 := tf1.NewTask("A", func() {})
b1 := tf1.NewTask("B", func() {})
a1.Precede(b1)
executor.Run(tf1).Wait()
// Validate against a larger DAG that expects C (never ran)
tf2 := NewTaskFlow("expected")
a2 := tf2.NewTask("A", func() {})
b2 := tf2.NewTask("B", func() {})
c2 := tf2.NewTask("C", func() {})
a2.Precede(b2)
b2.Precede(c2)
result := validate(mustSnapshot(executor), tf2)
if result.valid {
t.Error("expected invalid: C was defined but not executed")
}
if !containsStr(result.missingTasks, "C") {
t.Errorf("expected C in missing tasks, got: %v", result.missingTasks)
}
}
// TestValidatorUnexpectedTask verifies that tasks executed but not defined appear in unexpectedTasks.
// We run tf1 (A->B->C), then validate against tf2 (A->B only). C is unexpected.
func TestValidatorUnexpectedTask(t *testing.T) {
executor := NewExecutor(4, WithTracer())
tf1 := NewTaskFlow("run")
a1 := tf1.NewTask("A", func() {})
b1 := tf1.NewTask("B", func() {})
c1 := tf1.NewTask("C", func() {})
a1.Precede(b1)
b1.Precede(c1)
executor.Run(tf1).Wait()
// Validate against a smaller DAG that doesn't know about C
tf2 := NewTaskFlow("expected")
a2 := tf2.NewTask("A", func() {})
b2 := tf2.NewTask("B", func() {})
a2.Precede(b2)
result := validate(mustSnapshot(executor), tf2)
if result.valid {
t.Error("expected invalid: C was executed but not defined")
}
if !containsStr(result.unexpectedTasks, "C") {
t.Errorf("expected C in unexpected tasks, got: %v", result.unexpectedTasks)
}
}
// TestValidatorResultString verifies the String() output format.
func TestValidatorResultString(t *testing.T) {
r := &validationResult{
valid: false,
missingTasks: []string{"X"},
unexpectedTasks: []string{"Y"},
dependencyErrors: []dependencyError{{task: "Z", expected: []string{"A"}, actual: []string{"B"}}},
skippedBranches: []string{"W"},
}
s := r.String()
for _, want := range []string{"X", "Y", "Z", "W", "validation failed"} {
if !containsSubstr(s, want) {
t.Errorf("expected %q in String() output, got:\n%s", want, s)
}
}
// Valid result should return short string
valid := &validationResult{valid: true}
if valid.String() != "validation passed" {
t.Errorf("unexpected valid string: %q", valid.String())
}
}
// ---- helpers ----
// mustSnapshot extracts a traceRecord from an executor.
// Returns nil if the executor was not created with WithTracer().
func mustSnapshot(e Executor) traceRecord {
impl := e.(*innerExecutorImpl)
if impl.tracer == nil {
return nil
}
return impl.tracer.snapshot()
}
func containsStr(slice []string, s string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}
func containsSubstr(s, sub string) bool {
return strings.Contains(s, sub)
}