-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow_run_test.go
More file actions
159 lines (141 loc) · 3.85 KB
/
Copy pathworkflow_run_test.go
File metadata and controls
159 lines (141 loc) · 3.85 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
package chain
import (
"context"
"errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestRecoverOnRun(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
divide := func(name string) Action[int] {
return NewSimpleAction(
name,
func(_ context.Context, input int) (int, error) { return 1 / input, nil },
)
}
setTen := func(name string) Action[int] {
return NewSimpleAction(
name,
func(_ context.Context, _ int) (int, error) { return 10, nil },
)
}
ctx := context.Background()
type testCase struct {
closure func(t *testing.T) func()
}
testCases := map[string]testCase{
"recover from action": {
func(t *testing.T) func() {
return func() {
workflow := NewWorkflow("Calculation", divide("1"))
_, err := workflow.Run(ctx, 0)
assert.Error(t, err)
assert.Contains(t, err.Error(), "divide by zero")
}
}},
"skip actions after recover": {
func(t *testing.T) func() {
return func() {
workflow := NewWorkflow("Calculation", divide("1"), setTen("2"))
output, err := workflow.Run(ctx, 0)
assert.Error(t, err)
assert.Contains(t, err.Error(), "divide by zero")
assert.NotEqual(t, 10, output)
}
}},
"internal workflow panic recovers": {
func(t *testing.T) func() {
return func() {
subWorkFlow := NewWorkflow("SubWorkflow", divide(".1"), setTen(".2"))
superWorkflow := NewWorkflow(
"SuperWorkflow",
setTen("1"),
divide("2"),
subWorkFlow,
setTen("4"),
)
output, err := superWorkflow.Run(ctx, 0)
assert.Error(t, err)
assert.Contains(t, err.Error(), "divide by zero")
assert.NotEqual(t, 10, output)
}
}},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
code := tc.closure(t)
assert.NotPanics(t, code)
})
}
}
func TestPanicPropagation(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
newIncrementor := func() Action[int] {
return NewSimpleAction(
"increment",
func(_ context.Context, input int) (int, error) { return input + 1, nil },
)
}
panicker := NewSimpleAction(
"panicker",
func(_ context.Context, input int) (int, error) { panic("test") },
)
level1 := NewWorkflow(
"level1",
newIncrementor(), panicker, newIncrementor())
level2 := NewWorkflow(
"level2",
newIncrementor(), level1, newIncrementor())
level3 := NewWorkflow(
"level3",
newIncrementor(), level2, newIncrementor())
type testCase struct {
actionToRun Action[int]
expected int
}
testCases := map[string]testCase{
"internal workflow aborts": {
actionToRun: level1,
expected: 1,
},
"level2 workflow aborts by level1": {
actionToRun: level2,
expected: 2,
},
"triple depth workflow aborts by level1": {
actionToRun: level3,
expected: 3,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert.NotPanics(t, func() {
result, err := tc.actionToRun.Run(context.Background(), 0)
assert.Error(t, err)
assert.Equal(t, tc.expected, result)
})
})
}
}
func TestWorkflow_RunAtErrorAggregation(t *testing.T) {
firstErr := errors.New("first action failed")
secondErr := errors.New("second action failed")
first := NewSimpleAction(
"first",
func(_ context.Context, input int) (int, error) { return input + 1, firstErr },
)
second := NewSimpleAction(
"second",
func(_ context.Context, input int) (int, error) { return input + 1, secondErr },
)
workflow := NewWorkflow("workflow", first, second)
workflow.SetRunPlan(first, RunPlan[int]{Failure: second})
output, err := workflow.Run(context.Background(), 0)
assert.Error(t, err)
assert.Equal(t, 2, output)
assert.Contains(t, err.Error(), firstErr.Error())
assert.Contains(t, err.Error(), secondErr.Error())
assert.Less(t, strings.Index(err.Error(), firstErr.Error()), strings.Index(err.Error(), secondErr.Error()))
}