-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoordinator_test.go
1219 lines (1042 loc) · 31.4 KB
/
coordinator_test.go
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package choreograph_test
import (
"context"
"fmt"
"sort"
"sync/atomic"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/worldline-go/choreograph"
)
type contextKey string
type track struct {
name string
callbackName string
}
type timedTrack struct {
track
timestamp int64
}
type executionTracker struct {
expectedTrack []track
gotTrack []timedTrack
}
func (e *executionTracker) getExecutionFn(name, callbackName string) func(ctx context.Context) error {
return func(ctx context.Context) error {
e.registerExecution(name, callbackName)
return nil
}
}
func (e *executionTracker) registerExecution(name, callbackName string) {
e.gotTrack = append(e.gotTrack, timedTrack{
track: track{
name: name,
callbackName: callbackName,
},
timestamp: time.Now().UnixNano(),
})
}
func (e *executionTracker) assertExpectation(t *testing.T) {
t.Helper()
sort.SliceStable(e.gotTrack, func(i, j int) bool {
return e.gotTrack[i].timestamp < e.gotTrack[j].timestamp
})
require.Equalf(t, len(e.expectedTrack), len(e.gotTrack), "incorrect run count in execution tracker")
// check the order
for i := range e.expectedTrack {
assert.Equalf(t,
e.expectedTrack[i].name,
e.gotTrack[i].name,
"expected different execution name at index %d", i,
)
assert.Equalf(t,
e.expectedTrack[i].callbackName,
e.gotTrack[i].callbackName,
"expected different execution callback name at index %d", i,
)
}
}
func TestCoordinator_RunConcurrent(t *testing.T) {
tests := []struct {
name string
testFunc func(*testing.T)
}{
{
name: "test concurrent running",
testFunc: testConcurrentRunning,
},
{
name: "test acceptable input data",
testFunc: testAcceptableInputData,
},
{
name: "test if failing on first input block second input",
testFunc: testReusingContextForMultiInputs,
},
}
for _, tt := range tests {
t.Run(tt.name, tt.testFunc)
}
}
// job for first input fail which leads to context cancellation,
// this should not lead to block processing of another inputs.
func testReusingContextForMultiInputs(t *testing.T) {
dummyErr := errors.New("dummy error")
// setting single worker
ctx := context.Background()
et := &executionTracker{
expectedTrack: []track{
{name: "1", callbackName: "preCheck"},
{name: "2", callbackName: "preCheck"},
{name: "2", callbackName: "job"},
{name: "shouldRunOnlyFor2", callbackName: "preCheck"},
{name: "shouldRunOnlyFor2", callbackName: "job"},
},
}
c, err := prepareCoordinatorWithSteps(
[]*choreograph.Step{
{
Name: "shouldRunForBoth",
Job: func(_ context.Context, input interface{}) error {
inputToStr := input.(string)
if inputToStr == "1" {
return dummyErr
}
et.registerExecution(inputToStr, "job")
return nil
},
PreCheck: func(_ context.Context, input interface{}) error {
inputToStr := input.(string)
et.registerExecution(inputToStr, "preCheck")
return nil
},
},
{
Name: "shouldRunOnlyFor2",
Job: et.getExecutionFn("shouldRunOnlyFor2", "job"),
PreCheck: et.getExecutionFn("shouldRunOnlyFor2", "preCheck"),
},
},
choreograph.WithWorkerCount(1),
)
require.NoError(t, err)
results, err := c.RunConcurrent(ctx, []interface{}{"1", "2"})
require.NoError(t, err)
out := <-results
require.ErrorIs(t, out.ExecutionErrors[0], dummyErr)
require.Error(t, out.RuntimeError)
out = <-results
require.Lenf(t, out.ExecutionErrors, 0, "no execution errors expected")
require.NoError(t, out.RuntimeError)
et.assertExpectation(t)
}
func testAcceptableInputData(t *testing.T) {
coordinator := choreograph.NewCoordinator()
// check int as input
_, err := coordinator.RunConcurrent(context.Background(), 34)
assert.ErrorIs(t, err, choreograph.ErrInputMustBeSlice)
// check string as input
_, err = coordinator.RunConcurrent(context.Background(), "34")
assert.ErrorIs(t, err, choreograph.ErrInputMustBeSlice)
// check []string as input
_, err = coordinator.RunConcurrent(context.Background(), []string{"a", "b"})
assert.NoError(t, err)
// check []int as input
_, err = coordinator.RunConcurrent(context.Background(), []int{1, 2})
assert.NoError(t, err)
}
func testConcurrentRunning(t *testing.T) {
const (
inputsCount int64 = 1000
runCounts int = 2
)
var (
inputs = make([]interface{}, inputsCount)
preCheckCounter int64
jobCounter int64
resultsCounter int64
idx int64
)
for ; idx < inputsCount; idx++ {
inputs[idx] = idx
}
steps := []*choreograph.Step{
{
Name: "first",
PreCheck: func(ctx context.Context) error {
atomic.AddInt64(&preCheckCounter, 1)
return nil
},
Job: func(ctx context.Context) error {
atomic.AddInt64(&jobCounter, 1)
return nil
},
},
{
Name: "second",
PreCheck: func(ctx context.Context) error {
atomic.AddInt64(&preCheckCounter, 1)
return nil
},
Job: func(ctx context.Context) error {
atomic.AddInt64(&jobCounter, 1)
return nil
},
},
}
c, err := prepareCoordinatorWithSteps(steps, choreograph.WithWorkerCount(4))
require.NoError(t, err)
// to check if we can run concurrent multiple times we do it in loop
for run := 0; run < runCounts; run++ {
preCheckCounter = 0
jobCounter = 0
resultsCounter = 0
resultsChan, err := c.RunConcurrent(context.Background(), inputs)
require.NoError(t, err)
for r := range resultsChan {
resultsCounter++
require.Lenf(t, r.ExecutionErrors, 0, "no execution errors expected")
require.NoError(t, r.RuntimeError)
}
expectedCallbackCounter := inputsCount * int64(len(steps))
require.Equalf(t, inputsCount, resultsCounter, "expected %d on results counter, got %d", inputsCount, resultsCounter)
require.Equalf(t, expectedCallbackCounter, preCheckCounter, "expected %d on pre-check counter, got %d", expectedCallbackCounter, preCheckCounter)
require.Equalf(t, expectedCallbackCounter, jobCounter, "expected %d on job counter, got %d", expectedCallbackCounter, jobCounter)
}
}
func TestCoordinator_Run(t *testing.T) {
tests := []struct {
name string
testFunc func(*testing.T)
}{
{
name: "steps run without error",
testFunc: testStepsRunNoErr,
},
{
name: "sharing data with context",
testFunc: testSharingDataWithContext,
},
{
name: "pre-check can modify context data",
testFunc: testAccessToDataFromPreviousExecutions,
},
{
name: "execution order is correct",
testFunc: testExecutionCorrectness,
},
{
name: "execution is continued on preCheck error and continued on job error",
testFunc: testExecutionContinueCancel,
},
{
name: "retrieving preCheck and job returned errors",
testFunc: testRetrieveErrors,
},
{
name: "test passing input data",
testFunc: testPassingInputData,
},
}
for _, tt := range tests {
t.Run(tt.name, tt.testFunc)
}
}
func testStepsRunNoErr(t *testing.T) {
et := executionTracker{
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "first", callbackName: "job"},
{name: "second", callbackName: "preCheck"},
{name: "second", callbackName: "job"},
},
}
steps := []*choreograph.Step{
{
Name: "first",
Job: et.getExecutionFn("first", "job"),
PreCheck: et.getExecutionFn("first", "preCheck"),
},
{
Name: "second",
Job: et.getExecutionFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
}
c, err := prepareCoordinatorWithSteps(steps)
require.NoError(t, err)
execErrs, runtimeErr := c.Run(context.Background(), nil)
require.Lenf(t, execErrs, 0, "no execution errors expected")
require.NoError(t, runtimeErr)
et.assertExpectation(t)
}
func testSharingDataWithContext(t *testing.T) {
type testData struct {
text string
value int
}
var (
td = testData{text: "some text", value: 10}
contextTestingTKey contextKey = "testingT"
contextDataKey contextKey = "data"
)
jobExecuted := false
ctx := context.WithValue(context.Background(), contextDataKey, td)
ctx = context.WithValue(ctx, contextTestingTKey, t)
c, err := prepareCoordinatorWithSteps(
[]*choreograph.Step{{
Name: "first",
Job: func(ctx context.Context) error {
localTd, ok := ctx.Value(contextDataKey).(testData)
if !ok {
return fmt.Errorf("expected 'testData' struct object")
}
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
if !ok {
return fmt.Errorf("expected 'testingT' struct object")
}
assert.Equal(testingT, td, localTd)
jobExecuted = true
return nil
},
PreCheck: func(ctx context.Context) error { return nil },
}},
)
require.NoError(t, err)
execErrs, runtimeErr := c.Run(ctx, nil)
require.Lenf(t, execErrs, 0, "no execution errors expected")
require.NoError(t, runtimeErr)
assert.Truef(t, jobExecuted, "expected that job will be executed")
}
func testAccessToDataFromPreviousExecutions(t *testing.T) {
et := executionTracker{
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "first", callbackName: "job"},
{name: "second", callbackName: "preCheck"},
{name: "second", callbackName: "job"},
{name: "third", callbackName: "preCheck"},
{name: "forth", callbackName: "preCheck"},
{name: "forth", callbackName: "job"},
},
}
var (
contextTestingTKey contextKey = "testingT"
firstPreCheckReturnVal = 11
firstPreCheckReturnErr error = nil
firstJobReturnVal = 12
firstJobReturnErr error = nil
secondPreCheckReturnVal = 21
secondPreCheckReturnErr error = nil
thirdPreCheckReturnVal = 21
)
c, err := prepareCoordinatorWithSteps(
[]*choreograph.Step{
{
Name: "first",
PreCheck: func(ctx context.Context) (int, error) {
et.registerExecution("first", "preCheck")
return firstPreCheckReturnVal, firstPreCheckReturnErr
},
Job: func(ctx context.Context) (int, error) {
et.registerExecution("first", "job")
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
dataBag, ok := ctx.Value(choreograph.DataBagContextKey).(*choreograph.DataBag)
require.Truef(testingT, ok, "expected that data bag is there")
val, ok := dataBag.GetPreCheckData("first")
require.Truef(testingT, ok, "expected that data from pre-check is there")
require.Equal(testingT, firstPreCheckReturnVal, val)
return firstJobReturnVal, firstJobReturnErr
},
},
{
Name: "second",
PreCheck: func(ctx context.Context) (int, error) {
et.registerExecution("second", "preCheck")
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
dataBag, ok := ctx.Value(choreograph.DataBagContextKey).(*choreograph.DataBag)
require.Truef(testingT, ok, "expected that data bag is there")
val, ok := dataBag.GetJobData("first")
require.Truef(testingT, ok, "expected that data from pre-check is there")
require.Equal(testingT, firstJobReturnVal, val)
return secondPreCheckReturnVal, secondPreCheckReturnErr
},
Job: func(ctx context.Context) error {
et.registerExecution("second", "job")
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
dataBag, ok := ctx.Value(choreograph.DataBagContextKey).(*choreograph.DataBag)
require.Truef(testingT, ok, "expected that data bag is there")
pcFirst, ok := dataBag.GetPreCheckData("first")
require.Truef(testingT, ok, "expected that data from pre-check is there")
require.Equal(testingT, firstPreCheckReturnVal, pcFirst)
jobFirst, ok := dataBag.GetJobData("first")
require.Truef(testingT, ok, "expected that data from pre-check is there")
require.Equal(testingT, firstJobReturnVal, jobFirst)
pcSecond, ok := dataBag.GetPreCheckData("second")
require.Truef(testingT, ok, "expected that data from pre-check is there")
require.Equal(testingT, secondPreCheckReturnVal, pcSecond)
return nil
},
},
{
Name: "third",
PreCheck: func(ctx context.Context) (int, error) {
et.registerExecution("third", "preCheck")
return thirdPreCheckReturnVal, errors.New("dummy error")
},
Job: func(ctx context.Context) error {
// we don't expect this to run
et.registerExecution("third", "job")
return nil
},
},
{
Name: "forth",
PreCheck: func(ctx context.Context) error {
et.registerExecution("forth", "preCheck")
return nil
},
Job: func(ctx context.Context) error {
// we don't expect this to run
et.registerExecution("forth", "job")
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
dataBag, err := choreograph.GetDataBagFromCtx(ctx)
require.NoError(testingT, err)
data, ok := dataBag.GetPreCheckData("third")
require.Truef(testingT, ok, "expected that data from pre-check is there")
require.Equal(testingT, thirdPreCheckReturnVal, data)
return nil
},
},
},
)
require.NoError(t, err)
ctx := context.WithValue(context.Background(), contextTestingTKey, t)
execErrs, runtimeErr := c.Run(ctx, nil)
// 1 because third pre-check is returning it
require.Lenf(t, execErrs, 1, "1 execution error expected")
require.NoError(t, runtimeErr)
et.assertExpectation(t)
}
func testExecutionCorrectness(t *testing.T) {
et := executionTracker{
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "first", callbackName: "job"},
{name: "second", callbackName: "preCheck"},
{name: "second", callbackName: "job"},
{name: "third", callbackName: "preCheck"},
{name: "third", callbackName: "job"},
{name: "fourth", callbackName: "preCheck"},
{name: "fourth", callbackName: "job"},
{name: "fifth", callbackName: "preCheck"},
{name: "fifth", callbackName: "job"},
{name: "sixth", callbackName: "preCheck"},
{name: "sixth", callbackName: "job"},
{name: "seventh", callbackName: "preCheck"},
{name: "seventh", callbackName: "job"},
{name: "eight", callbackName: "preCheck"},
{name: "eight", callbackName: "job"},
{name: "ninth", callbackName: "preCheck"},
{name: "ninth", callbackName: "job"},
{name: "tenth", callbackName: "preCheck"},
{name: "tenth", callbackName: "job"},
},
}
steps := []*choreograph.Step{
{
Name: "first",
Job: et.getExecutionFn("first", "job"),
PreCheck: et.getExecutionFn("first", "preCheck"),
},
{
Name: "second",
Job: et.getExecutionFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
{
Name: "third",
Job: et.getExecutionFn("third", "job"),
PreCheck: et.getExecutionFn("third", "preCheck"),
},
{
Name: "fourth",
Job: et.getExecutionFn("fourth", "job"),
PreCheck: et.getExecutionFn("fourth", "preCheck"),
},
{
Name: "fifth",
Job: et.getExecutionFn("fifth", "job"),
PreCheck: et.getExecutionFn("fifth", "preCheck"),
},
{
Name: "sixth",
Job: et.getExecutionFn("sixth", "job"),
PreCheck: et.getExecutionFn("sixth", "preCheck"),
},
{
Name: "seventh",
Job: et.getExecutionFn("seventh", "job"),
PreCheck: et.getExecutionFn("seventh", "preCheck"),
},
{
Name: "eight",
Job: et.getExecutionFn("eight", "job"),
PreCheck: et.getExecutionFn("eight", "preCheck"),
},
{
Name: "ninth",
Job: et.getExecutionFn("ninth", "job"),
PreCheck: et.getExecutionFn("ninth", "preCheck"),
},
{
Name: "tenth",
Job: et.getExecutionFn("tenth", "job"),
PreCheck: et.getExecutionFn("tenth", "preCheck"),
},
}
ctx := context.Background()
c, err := prepareCoordinatorWithSteps(steps)
require.NoError(t, err)
execErrs, runtimeErr := c.Run(ctx, nil)
if assert.Lenf(t, execErrs, 0, "no execution errors expected") && assert.NoError(t, runtimeErr) {
et.assertExpectation(t)
}
}
func testExecutionContinueCancel(t *testing.T) {
et := &executionTracker{}
errReturningFn := func(name, callbackName string) func(_ context.Context) error {
return func(_ context.Context) error {
et.registerExecution(name, callbackName)
return errors.New("dummy error")
}
}
cancelledCtx, cancel := context.WithCancel(context.Background())
cancel()
testCases := []struct {
name string
steps []*choreograph.Step
ctx context.Context
expectedTrack []track
expectedExecutionErrorCount int
expectedRuntimeError error
}{
{
name: "first preCheck error",
steps: []*choreograph.Step{
{
Name: "first",
Job: et.getExecutionFn("first", "job"),
PreCheck: errReturningFn("first", "preCheck"),
},
{
Name: "second",
Job: et.getExecutionFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
},
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "second", callbackName: "preCheck"},
{name: "second", callbackName: "job"},
},
expectedExecutionErrorCount: 1,
},
{
name: "first job error",
steps: []*choreograph.Step{
{
Name: "first",
PreCheck: et.getExecutionFn("first", "preCheck"),
Job: errReturningFn("first", "job"),
},
{
Name: "second",
Job: et.getExecutionFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
},
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "first", callbackName: "job"},
},
expectedRuntimeError: choreograph.ErrJobFailed,
expectedExecutionErrorCount: 1,
},
{
name: "second preCheck error",
steps: []*choreograph.Step{
{
Name: "first",
PreCheck: et.getExecutionFn("first", "preCheck"),
Job: et.getExecutionFn("first", "job"),
},
{
Name: "second",
Job: et.getExecutionFn("second", "job"),
PreCheck: errReturningFn("second", "preCheck"),
},
},
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "first", callbackName: "job"},
{name: "second", callbackName: "preCheck"},
},
expectedRuntimeError: nil,
expectedExecutionErrorCount: 1,
},
{
name: "second job error",
steps: []*choreograph.Step{
{
Name: "first",
PreCheck: et.getExecutionFn("first", "preCheck"),
Job: et.getExecutionFn("first", "job"),
},
{
Name: "second",
Job: errReturningFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
},
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
{name: "first", callbackName: "job"},
{name: "second", callbackName: "preCheck"},
{name: "second", callbackName: "job"},
},
expectedRuntimeError: choreograph.ErrJobFailed,
expectedExecutionErrorCount: 1,
},
{
name: "context cancelled",
steps: []*choreograph.Step{
{
Name: "first",
PreCheck: et.getExecutionFn("first", "preCheck"),
Job: et.getExecutionFn("first", "job"),
},
{
Name: "second",
Job: errReturningFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
},
ctx: cancelledCtx,
expectedTrack: []track{},
expectedRuntimeError: context.Canceled,
expectedExecutionErrorCount: 0,
},
{
name: "cancel after first pre-check",
steps: []*choreograph.Step{
{
Name: "first",
PreCheck: func(_ context.Context) error {
et.registerExecution("first", "preCheck")
return choreograph.ErrExecutionCanceled
},
Job: et.getExecutionFn("first", "job"),
},
{
Name: "second",
Job: et.getExecutionFn("second", "job"),
PreCheck: et.getExecutionFn("second", "preCheck"),
},
},
expectedTrack: []track{
{name: "first", callbackName: "preCheck"},
},
expectedRuntimeError: choreograph.ErrExecutionCanceled,
expectedExecutionErrorCount: 1,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
et.expectedTrack = tt.expectedTrack
et.gotTrack = []timedTrack{}
ctx := tt.ctx
if ctx == nil {
ctx = context.Background()
}
c, err := prepareCoordinatorWithSteps(tt.steps)
require.NoError(t, err)
execErrs, runtimeErr := c.Run(ctx, nil)
require.Lenf(t, execErrs, tt.expectedExecutionErrorCount, "no execution errors expected")
if tt.expectedRuntimeError != nil {
assert.ErrorIs(t, runtimeErr, tt.expectedRuntimeError)
} else {
assert.NoError(t, runtimeErr)
}
et.assertExpectation(t)
})
}
}
func testRetrieveErrors(t *testing.T) {
dummyErr := errors.New("dummy error")
dummyErr2 := errors.New("other dummy error")
testCases := []struct {
name string
steps []*choreograph.Step
expectedErrors []error
expectedRunError bool
}{
{
name: "receiving job error",
steps: []*choreograph.Step{
{
Job: func(_ context.Context) error { return dummyErr },
PreCheck: func(_ context.Context) error { return nil },
},
},
expectedErrors: []error{
dummyErr,
},
expectedRunError: true,
},
{
name: "second job error",
steps: []*choreograph.Step{
{
Job: func(_ context.Context) error { return nil },
PreCheck: func(_ context.Context) error { return nil },
},
{
Job: func(_ context.Context) error { return dummyErr },
PreCheck: func(_ context.Context) error { return nil },
},
},
expectedErrors: []error{
dummyErr,
},
expectedRunError: true,
},
{
name: "two preCheck errors",
steps: []*choreograph.Step{
{
Job: func(_ context.Context) error { return nil },
PreCheck: func(_ context.Context) error { return dummyErr },
},
{
Job: func(_ context.Context) error { return nil },
PreCheck: func(_ context.Context) error { return dummyErr2 },
},
},
expectedErrors: []error{
dummyErr,
dummyErr2,
},
expectedRunError: false,
},
{
name: "first preCheck error second job error",
steps: []*choreograph.Step{
{
Job: func(_ context.Context) error { return nil },
PreCheck: func(_ context.Context) error { return dummyErr },
},
{
Job: func(_ context.Context) error { return dummyErr2 },
PreCheck: func(_ context.Context) error { return nil },
},
},
expectedErrors: []error{
dummyErr,
dummyErr2,
},
expectedRunError: true,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
c, err := prepareCoordinatorWithSteps(tt.steps)
require.NoError(t, err)
execErrs, runtimeErr := c.Run(ctx, nil)
if tt.expectedRunError {
require.Error(t, runtimeErr)
} else {
require.NoError(t, runtimeErr)
}
// this assertion should be removed after next removing GetExecutionErrors method
require.Equal(t, execErrs, c.GetExecutionErrors())
if assert.Len(t, execErrs, len(tt.expectedErrors)) {
for errIdx := range tt.expectedErrors {
errors.Is(execErrs[errIdx], tt.expectedErrors[errIdx])
}
}
})
}
}
func testPassingInputData(t *testing.T) {
const contextTestingTKey contextKey = "testingT"
type testStruct struct {
val int
}
testCases := []struct {
name string
steps []*choreograph.Step
input interface{}
expectedErr error
}{
{
name: "provide int into interface",
steps: []*choreograph.Step{
{
Job: func(ctx context.Context, input interface{}) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
assert.Equal(testingT, 666, input)
return nil
},
PreCheck: func(ctx context.Context, input interface{}) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
assert.Equal(testingT, 666, input)
return nil
},
},
},
input: 666,
},
{
name: "provide int into int",
steps: []*choreograph.Step{
{
Job: func(ctx context.Context, input int) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
assert.Equal(testingT, 666, input)
return nil
},
PreCheck: func(ctx context.Context, input int) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
assert.Equal(testingT, 666, input)
return nil
},
},
},
input: 666,
},
{
name: "provide string into int",
steps: []*choreograph.Step{
{
Job: func(ctx context.Context, input int) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
testingT.Errorf("this should not be runned")
return nil
},
PreCheck: func(ctx context.Context, input int) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)
require.Truef(testingT, ok, "expected 'testingT' struct object")
testingT.Errorf("this should not be runned")
return nil
},
},
},
input: "text",
expectedErr: choreograph.ErrUnassignableParameter,
},
{
name: "provide struct into struct",
steps: []*choreograph.Step{
{
Job: func(ctx context.Context, input testStruct) error {
testingT, ok := ctx.Value(contextTestingTKey).(*testing.T)