-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_spawnThen_test.go
More file actions
501 lines (415 loc) · 13.1 KB
/
executor_spawnThen_test.go
File metadata and controls
501 lines (415 loc) · 13.1 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package durex_test
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/simonovic86/durex"
"github.com/simonovic86/durex/storage"
)
// TestSpawnThen_BasicFanIn verifies that SpawnThen waits for all parallel tasks.
func TestSpawnThen_BasicFanIn(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
var mu sync.Mutex
executionOrder := []string{}
parallelComplete := atomic.Bool{}
// Parallel tasks
for _, name := range []string{"task1", "task2", "task3"} {
taskName := name
executor.HandleFunc(taskName, func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, taskName)
mu.Unlock()
time.Sleep(50 * time.Millisecond) // Simulate work
return durex.Empty(), nil
})
}
// Continuation task - should only run after all parallel tasks complete
executor.HandleFunc("continuation", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
if !parallelComplete.Load() {
parallelComplete.Store(true)
}
mu.Lock()
executionOrder = append(executionOrder, "continuation")
mu.Unlock()
return durex.Empty(), nil
})
// Coordinator spawns parallel tasks with continuation
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.SpawnThen(
[]durex.Spec{
{Name: "task1"},
{Name: "task2"},
{Name: "task3"},
},
durex.Spec{Name: "continuation"},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "coordinator"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
// Wait for completion
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
// Verify all tasks executed
if len(executionOrder) != 4 {
t.Errorf("Expected 4 tasks executed, got %d: %v", len(executionOrder), executionOrder)
}
// Verify continuation was last
if len(executionOrder) > 0 && executionOrder[len(executionOrder)-1] != "continuation" {
t.Errorf("Expected continuation to be last, got: %v", executionOrder)
}
// Verify continuation ran
if !parallelComplete.Load() {
t.Error("Continuation task did not execute")
}
}
// TestSpawnThen_DataPropagation verifies data flows to continuation.
func TestSpawnThen_DataPropagation(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
var continuationData durex.M
var mu sync.Mutex
// Parallel tasks that set data
executor.HandleFunc("setData1", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
cmd.Set("result1", "value1")
return durex.Empty(), nil
})
executor.HandleFunc("setData2", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
cmd.Set("result2", "value2")
return durex.Empty(), nil
})
// Continuation receives merged data
executor.HandleFunc("aggregate", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
continuationData = make(durex.M)
for k, v := range cmd.Data {
continuationData[k] = v
}
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.SpawnThen(
[]durex.Spec{
{Name: "setData1"},
{Name: "setData2"},
},
durex.Spec{
Name: "aggregate",
Data: durex.M{"original": "data"},
},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "coordinator"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
// Verify continuation received original data
if continuationData["original"] != "data" {
t.Errorf("Expected original='data', got %v", continuationData["original"])
}
// Verify continuation received results from parallel tasks (with prefixes)
hasResult1 := false
hasResult2 := false
for k := range continuationData {
if k == "_barrier_result_setData1_result1" {
hasResult1 = true
}
if k == "_barrier_result_setData2_result2" {
hasResult2 = true
}
}
if !hasResult1 {
t.Errorf("Expected result from setData1, got data: %v", continuationData)
}
if !hasResult2 {
t.Errorf("Expected result from setData2, got data: %v", continuationData)
}
}
// TestSpawnThen_OneChildFails verifies continuation doesn't run if a child fails.
func TestSpawnThen_OneChildFails(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
continuationRan := atomic.Bool{}
executor.HandleFunc("successTask", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
})
executor.HandleFunc("failTask", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("task failed")
})
executor.HandleFunc("shouldNotRun", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
continuationRan.Store(true)
return durex.Empty(), nil
})
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.SpawnThen(
[]durex.Spec{
{Name: "successTask"},
{Name: "failTask"},
},
durex.Spec{Name: "shouldNotRun"},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "coordinator"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(2 * time.Second)
if continuationRan.Load() {
t.Error("Continuation should not run when a child task fails")
}
}
// TestSpawnThen_TraceIDPropagation verifies TraceID flows through barrier.
func TestSpawnThen_TraceIDPropagation(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
var mu sync.Mutex
traceIDs := []string{}
executor.HandleFunc("task1", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
traceIDs = append(traceIDs, cmd.TraceID)
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("task2", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
traceIDs = append(traceIDs, cmd.TraceID)
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("continuation", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
traceIDs = append(traceIDs, cmd.TraceID)
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.SpawnThen(
[]durex.Spec{
{Name: "task1"},
{Name: "task2"},
},
durex.Spec{Name: "continuation"},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
expectedTraceID := "test-trace-123"
_, err := executor.Add(ctx, durex.Spec{
Name: "coordinator",
TraceID: expectedTraceID,
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
// All tasks should have the same TraceID
if len(traceIDs) != 3 {
t.Errorf("Expected 3 trace IDs, got %d", len(traceIDs))
}
for i, traceID := range traceIDs {
if traceID != expectedTraceID {
t.Errorf("Task %d: expected TraceID %q, got %q", i, expectedTraceID, traceID)
}
}
}
// TestSpawnThen_EmptyParallelTasks verifies continuation runs with no parallel tasks.
func TestSpawnThen_EmptyParallelTasks(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(2))
continuationRan := atomic.Bool{}
executor.HandleFunc("continuation", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
continuationRan.Store(true)
return durex.Empty(), nil
})
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
// SpawnThen with empty parallel tasks - should just run continuation
return durex.SpawnThen(
[]durex.Spec{},
durex.Spec{Name: "continuation"},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "coordinator"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(1 * time.Second)
// With empty parallel tasks, no barrier is created, so continuation won't run automatically
// This is expected behavior - SpawnThen requires at least one parallel task
if continuationRan.Load() {
t.Error("Continuation should not run automatically with empty parallel tasks")
}
}
// TestSpawnThen_ChainedContinuations verifies multiple SpawnThen in sequence.
func TestSpawnThen_ChainedContinuations(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
var mu sync.Mutex
executionOrder := []string{}
// First wave
executor.HandleFunc("wave1_task1", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "wave1_task1")
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("wave1_task2", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "wave1_task2")
mu.Unlock()
return durex.Empty(), nil
})
// First continuation spawns second wave
executor.HandleFunc("wave1_done", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "wave1_done")
mu.Unlock()
return durex.SpawnThen(
[]durex.Spec{
{Name: "wave2_task1"},
{Name: "wave2_task2"},
},
durex.Spec{Name: "wave2_done"},
), nil
})
// Second wave
executor.HandleFunc("wave2_task1", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "wave2_task1")
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("wave2_task2", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "wave2_task2")
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("wave2_done", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "wave2_done")
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.SpawnThen(
[]durex.Spec{
{Name: "wave1_task1"},
{Name: "wave1_task2"},
},
durex.Spec{Name: "wave1_done"},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "coordinator"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(3 * time.Second)
mu.Lock()
defer mu.Unlock()
// Should have 6 tasks total
if len(executionOrder) != 6 {
t.Errorf("Expected 6 tasks, got %d: %v", len(executionOrder), executionOrder)
}
// Verify wave1_done comes after both wave1 tasks
wave1DoneIdx := -1
lastWave1TaskIdx := -1
for i, name := range executionOrder {
if name == "wave1_done" {
wave1DoneIdx = i
}
if name == "wave1_task1" || name == "wave1_task2" {
if i > lastWave1TaskIdx {
lastWave1TaskIdx = i
}
}
}
if wave1DoneIdx < lastWave1TaskIdx {
t.Errorf("wave1_done should come after wave1 tasks, order: %v", executionOrder)
}
// Verify wave2_done comes after both wave2 tasks
wave2DoneIdx := -1
lastWave2TaskIdx := -1
for i, name := range executionOrder {
if name == "wave2_done" {
wave2DoneIdx = i
}
if name == "wave2_task1" || name == "wave2_task2" {
if i > lastWave2TaskIdx {
lastWave2TaskIdx = i
}
}
}
if wave2DoneIdx < lastWave2TaskIdx {
t.Errorf("wave2_done should come after wave2 tasks, order: %v", executionOrder)
}
}
// TestSpawnThen_WithRetries verifies barrier waits even when children retry.
func TestSpawnThen_WithRetries(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
continuationRan := atomic.Bool{}
retryCount := atomic.Int32{}
executor.HandleFunc("retryTask", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
count := retryCount.Add(1)
if count < 2 {
return durex.Empty(), fmt.Errorf("attempt %d failed", count)
}
return durex.Empty(), nil
}, durex.Retries(2))
executor.HandleFunc("continuation", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
continuationRan.Store(true)
return durex.Empty(), nil
})
executor.HandleFunc("coordinator", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.SpawnThen(
[]durex.Spec{
{Name: "retryTask", Retries: 2},
},
durex.Spec{Name: "continuation"},
), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "coordinator"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(3 * time.Second)
// Continuation should run after retries succeed
if !continuationRan.Load() {
t.Error("Continuation should run after child task succeeds with retries")
}
if retryCount.Load() < 2 {
t.Errorf("Expected at least 2 attempts, got %d", retryCount.Load())
}
}