-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_recovery_test.go
More file actions
589 lines (487 loc) · 16.5 KB
/
Copy pathexecutor_recovery_test.go
File metadata and controls
589 lines (487 loc) · 16.5 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package durex_test
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/simonovic86/durex"
"github.com/simonovic86/durex/storage"
)
// TestRecovery_OnRecoverCalled verifies OnRecover is called after retries exhausted.
func TestRecovery_OnRecoverCalled(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
recoverCalled := atomic.Bool{}
errorReceived := ""
var mu sync.Mutex
executor.HandleFunc("recoverTest", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("primary failure")
},
durex.Retries(2),
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
recoverCalled.Store(true)
mu.Lock()
errorReceived = err.Error()
mu.Unlock()
return durex.Empty(), nil
}),
)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "recoverTest"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
// Wait for execution and recovery
time.Sleep(500 * time.Millisecond)
if !recoverCalled.Load() {
t.Error("OnRecover was not called")
}
mu.Lock()
defer mu.Unlock()
if errorReceived != "primary failure" {
t.Errorf("Expected error 'primary failure', got %q", errorReceived)
}
}
// TestRecovery_OnRecoverReceivesCommandData verifies recovery gets full command data.
func TestRecovery_OnRecoverReceivesCommandData(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
var mu sync.Mutex
recoveredData := durex.M{}
recoveredName := ""
recoveredID := ""
executor.HandleFunc("dataRecoverTest", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("fail with data")
},
durex.Retries(0),
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
mu.Lock()
recoveredData = cmd.Data
recoveredName = cmd.Name
recoveredID = cmd.ID
mu.Unlock()
return durex.Empty(), nil
}),
)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
instance, err := executor.Add(ctx, durex.Spec{
Name: "dataRecoverTest",
Data: durex.M{"user_id": "123", "action": "payment"},
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
if recoveredData["user_id"] != "123" {
t.Errorf("Expected user_id '123', got %v", recoveredData["user_id"])
}
if recoveredData["action"] != "payment" {
t.Errorf("Expected action 'payment', got %v", recoveredData["action"])
}
if recoveredName != "dataRecoverTest" {
t.Errorf("Expected name 'dataRecoverTest', got %q", recoveredName)
}
if recoveredID != instance.ID {
t.Errorf("Expected ID %q, got %q", instance.ID, recoveredID)
}
}
// TestRecovery_SpawnCompensationCommands verifies saga pattern compensation.
func TestRecovery_SpawnCompensationCommands(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
compensationExecuted := &sync.Map{}
// Main command that fails
executor.HandleFunc("processPayment", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("payment processor down")
},
durex.Retries(1),
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
// Spawn compensation commands (saga pattern)
return durex.Spawn(
durex.Spec{Name: "refundPayment", Data: cmd.Data},
durex.Spec{Name: "releaseInventory", Data: cmd.Data},
durex.Spec{Name: "notifyCustomer", Data: durex.M{
"error": err.Error(),
"user_id": cmd.Data["user_id"],
}},
), nil
}),
)
// Compensation commands
executor.HandleFunc("refundPayment", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
compensationExecuted.Store("refundPayment", cmd.GetString("order_id"))
return durex.Empty(), nil
})
executor.HandleFunc("releaseInventory", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
compensationExecuted.Store("releaseInventory", cmd.GetString("order_id"))
return durex.Empty(), nil
})
executor.HandleFunc("notifyCustomer", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
compensationExecuted.Store("notifyCustomer", cmd.GetString("user_id"))
compensationExecuted.Store("error_message", cmd.GetString("error"))
return durex.Empty(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{
Name: "processPayment",
Data: durex.M{"order_id": "order-789", "user_id": "user-456"},
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
// Wait for main command retries and compensation
time.Sleep(600 * time.Millisecond)
// Verify all compensation commands executed
if v, ok := compensationExecuted.Load("refundPayment"); !ok || v != "order-789" {
t.Errorf("refundPayment not executed with correct data: %v", v)
}
if v, ok := compensationExecuted.Load("releaseInventory"); !ok || v != "order-789" {
t.Errorf("releaseInventory not executed with correct data: %v", v)
}
if v, ok := compensationExecuted.Load("notifyCustomer"); !ok || v != "user-456" {
t.Errorf("notifyCustomer not executed with correct data: %v", v)
}
if v, ok := compensationExecuted.Load("error_message"); !ok || v != "payment processor down" {
t.Errorf("Error message not passed to notification: %v", v)
}
}
// TestRecovery_NoRecoverIfSuccess verifies OnRecover not called on success.
func TestRecovery_NoRecoverIfSuccess(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
recoverCalled := atomic.Bool{}
executor.HandleFunc("successNoRecover", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil // Success
},
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
recoverCalled.Store(true)
return durex.Empty(), nil
}),
)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "successNoRecover"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
if recoverCalled.Load() {
t.Error("OnRecover should not be called on success")
}
}
// TestRecovery_NoRecoverIfRetriesRemain verifies OnRecover only after exhaustion.
func TestRecovery_NoRecoverIfRetriesRemain(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
attempts := atomic.Int32{}
recoverCalled := atomic.Bool{}
executor.HandleFunc("retryBeforeRecover", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
attempt := attempts.Add(1)
if attempt < 3 {
return durex.Empty(), errors.New("temporary failure")
}
return durex.Empty(), nil // Succeed on 3rd attempt
},
durex.Retries(5), // More retries than needed
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
recoverCalled.Store(true)
return durex.Empty(), nil
}),
)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "retryBeforeRecover"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(400 * time.Millisecond)
if got := attempts.Load(); got != 3 {
t.Errorf("Expected 3 attempts, got %d", got)
}
if recoverCalled.Load() {
t.Error("OnRecover should not be called when command eventually succeeds")
}
}
// TestRecovery_WithStructCommand verifies recovery with Command struct.
func TestRecovery_WithStructCommand(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
recoverCalled := atomic.Bool{}
// Register compensation command
executor.HandleFunc("structCompensation", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
})
// Register main command
cmd := &RecoverableCommand{
onRecover: func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
recoverCalled.Store(true)
return durex.Spawn(durex.Spec{Name: "structCompensation"}), nil
},
}
executor.Register(cmd)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "recoverableCmd"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(300 * time.Millisecond)
if !recoverCalled.Load() {
t.Error("Recover should be called for struct command")
}
}
// TestExpired_OnExpiredCalled verifies OnExpired is called when deadline passes.
func TestExpired_OnExpiredCalled(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
expiredCalled := atomic.Bool{}
executeCalled := atomic.Bool{}
executor.HandleFunc("expiredTest", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
executeCalled.Store(true)
return durex.Empty(), nil
},
durex.OnExpired(func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
expiredCalled.Store(true)
return durex.Empty(), nil
}),
)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
// Add with delay longer than deadline
_, err := executor.Add(ctx, durex.Spec{
Name: "expiredTest",
Delay: 200 * time.Millisecond,
Deadline: 50 * time.Millisecond, // Deadline before delay completes
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(400 * time.Millisecond)
if !expiredCalled.Load() {
t.Error("OnExpired should be called when deadline passes")
}
// Execute might or might not be called depending on timing
// The key test is that OnExpired IS called
}
// TestExpired_SpawnFollowUpCommands verifies expired can spawn new commands.
func TestExpired_SpawnFollowUpCommands(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(2))
followUpExecuted := atomic.Bool{}
executor.HandleFunc("willExpire", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
time.Sleep(500 * time.Millisecond) // Long-running (will be preempted by deadline check)
return durex.Empty(), nil
},
durex.OnExpired(func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Spawn(durex.Spec{
Name: "handleExpiredTask",
Data: durex.M{"original_task": cmd.ID},
}), nil
}),
)
executor.HandleFunc("handleExpiredTask", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
followUpExecuted.Store(true)
return durex.Empty(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
// Add with short deadline
_, err := executor.Add(ctx, durex.Spec{
Name: "willExpire",
Delay: 100 * time.Millisecond,
Deadline: 50 * time.Millisecond,
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(400 * time.Millisecond)
if !followUpExecuted.Load() {
t.Error("Follow-up command from OnExpired should execute")
}
}
// TestRecovery_ErrorHandler verifies global error handler is called.
func TestRecovery_ErrorHandler(t *testing.T) {
store := storage.NewMemory()
errorHandlerCalled := atomic.Bool{}
var handledError error
var handledCmd *durex.Instance
var mu sync.Mutex
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithErrorHandler(func(cmd *durex.Instance, err error) {
errorHandlerCalled.Store(true)
mu.Lock()
handledError = err
handledCmd = cmd
mu.Unlock()
}),
)
executor.HandleFunc("errorHandlerTest", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("global handler error")
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{
Name: "errorHandlerTest",
Retries: 0,
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
if !errorHandlerCalled.Load() {
t.Error("Global error handler should be called")
}
mu.Lock()
defer mu.Unlock()
if handledError == nil || handledError.Error() != "global handler error" {
t.Errorf("Expected 'global handler error', got %v", handledError)
}
if handledCmd == nil || handledCmd.Name != "errorHandlerTest" {
t.Error("Error handler should receive the failed command")
}
}
// TestRecovery_BothErrorHandlerAndOnRecover verifies both are called.
func TestRecovery_BothErrorHandlerAndOnRecover(t *testing.T) {
store := storage.NewMemory()
errorHandlerCalled := atomic.Bool{}
onRecoverCalled := atomic.Bool{}
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithErrorHandler(func(cmd *durex.Instance, err error) {
errorHandlerCalled.Store(true)
}),
)
executor.HandleFunc("bothHandlers", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("fail")
},
durex.Retries(0),
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
onRecoverCalled.Store(true)
return durex.Empty(), nil
}),
)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{Name: "bothHandlers"})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
if !errorHandlerCalled.Load() {
t.Error("Global error handler should be called")
}
if !onRecoverCalled.Load() {
t.Error("OnRecover should be called")
}
}
// TestRecovery_CompensationChain verifies multi-level compensation.
func TestRecovery_CompensationChain(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(4))
var mu sync.Mutex
executionOrder := []string{}
executor.HandleFunc("step1", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "step1")
mu.Unlock()
return cmd.ContinueSequence(nil), nil
})
executor.HandleFunc("step2", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "step2")
mu.Unlock()
return cmd.ContinueSequence(nil), nil
})
// Step 3 fails and triggers compensation
executor.HandleFunc("step3", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "step3-fail")
mu.Unlock()
return durex.Empty(), errors.New("step 3 failed")
},
durex.Retries(0),
durex.OnRecover(func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "step3-recover")
mu.Unlock()
// Compensate for steps 1 and 2
return durex.Spawn(
durex.Spec{Name: "undoStep2"},
durex.Spec{Name: "undoStep1"},
), nil
}),
)
executor.HandleFunc("undoStep2", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "undoStep2")
mu.Unlock()
return durex.Empty(), nil
})
executor.HandleFunc("undoStep1", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
mu.Lock()
executionOrder = append(executionOrder, "undoStep1")
mu.Unlock()
return durex.Empty(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
_, err := executor.Add(ctx, durex.Spec{
Name: "step1",
Sequence: []string{"step2", "step3"},
})
if err != nil {
t.Fatalf("Add failed: %v", err)
}
time.Sleep(600 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
// Verify we have the expected events
expectedEvents := map[string]bool{
"step1": true,
"step2": true,
"step3-fail": true,
"step3-recover": true,
"undoStep1": true,
"undoStep2": true,
}
for _, event := range executionOrder {
delete(expectedEvents, event)
}
if len(expectedEvents) > 0 {
t.Errorf("Missing events: %v. Got order: %v", expectedEvents, executionOrder)
}
}
// RecoverableCommand is a test command struct that implements Recoverable.
type RecoverableCommand struct {
durex.BaseCommand
onRecover func(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error)
}
func (c *RecoverableCommand) Name() string { return "recoverableCmd" }
func (c *RecoverableCommand) Execute(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("intentional failure")
}
func (c *RecoverableCommand) Recover(ctx context.Context, cmd *durex.Instance, err error) (durex.Result, error) {
if c.onRecover != nil {
return c.onRecover(ctx, cmd, err)
}
return durex.Empty(), nil
}