-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_normal_test.go
266 lines (223 loc) · 7.43 KB
/
pipeline_normal_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
package chain
import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSimplePipeline(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
collatz := NewCollatz("SingleCollatz")
ctx := context.Background()
t.Run("Odd input collatz", func(t *testing.T) {
output, err := collatz.Run(ctx, 5)
assert.NoError(t, err)
assert.Equal(t, 16, output)
})
t.Run("Even input collatz", func(t *testing.T) {
output, err := collatz.Run(ctx, 16)
assert.NoError(t, err)
assert.Equal(t, 8, output)
})
t.Run("RunAt onOdd with even input", func(t *testing.T) {
output, err := collatz.RunAt(collatz.OnOdd, ctx, 2)
assert.NoError(t, err)
assert.Equal(t, 7, output)
})
t.Run("RunAt onEven with odd input", func(t *testing.T) {
_, err := collatz.RunAt(collatz.OnEven, ctx, 5)
assert.Error(t, err)
})
}
func TestMultiPipeline(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
multiCollatz := NewPipeline(
"FiveCollatzTrial",
NewCollatz("collatz1"),
NewPipeline(
"InnerCollatz",
NewCollatz("collatz2"),
NewCollatz("collatz3"),
),
NewCollatz("collatz4"),
NewCollatz("collatz5"),
)
ctx := context.Background()
t.Run("Start with 5", func(t *testing.T) {
// 5 -> 16 -> 8 -> 4 -> 2 -> 1
output, err := multiCollatz.Run(ctx, 5)
assert.NoError(t, err)
assert.Equal(t, 1, output)
})
t.Run("Start with 10", func(t *testing.T) {
// 10 -> 5 -> 16 -> 8 -> 4 -> 2
output, err := multiCollatz.Run(ctx, 10)
assert.NoError(t, err)
assert.Equal(t, 2, output)
})
t.Run("Start with 1", func(t *testing.T) {
// 1 -> 1 -> 1 -> 1 -> 1 -> 1
output, err := multiCollatz.Run(ctx, 1)
assert.NoError(t, err)
assert.Equal(t, 1, output)
})
t.Run("Start with 0", func(t *testing.T) {
// 0 -> Finish (terminated by error)
_, err := multiCollatz.Run(ctx, 0)
assert.Error(t, err)
})
}
func TestErrorPropagation(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
t.Run("simple pipeline with single error", func(t *testing.T) {
action1 := NewCollatz("action1")
action2 := &ErrorMaker{message: "error2"}
action3 := NewCollatz("action3")
pipeline := NewPipeline("Pipeline", action1, action2, action3)
pipeline.SetRunPlan(action1, SuccessOnlyPlan(action2))
pipeline.SetRunPlan(action2, DefaultPlan(action3, action3))
pipeline.SetRunPlan(action3, TerminationPlan[int]())
// 5 -> 16 -> 16(error2) -> 8
output, err := pipeline.Run(context.Background(), 5)
assert.Error(t, err)
assert.Equal(t, err.Error(), "error2")
assert.Equal(t, 8, output)
})
t.Run("simple pipeline with multiple errors", func(t *testing.T) {
action1 := NewCollatz("action1")
action2 := &ErrorMaker{message: "error2"}
action3 := &ErrorMaker{message: "error3"}
pipeline := NewPipeline("Pipeline", action1, action2, action3)
pipeline.SetRunPlan(action1, SuccessOnlyPlan(action2))
pipeline.SetRunPlan(action2, DefaultPlan(action3, action3))
pipeline.SetRunPlan(action3, TerminationPlan[int]())
// 5 -> 16 -> 16(error2) -> 16(error3)
output, err := pipeline.Run(context.Background(), 5)
assert.Error(t, err)
assert.Equal(t, err.Error(), "error3")
assert.Equal(t, 16, output)
})
t.Run("simple pipeline with panic", func(t *testing.T) {
action1 := NewCollatz("action1")
action2 := &PanicMaker{message: "panic2"}
action3 := NewCollatz("action3")
pipeline := NewPipeline("Pipeline", action1, action2, action3)
pipeline.SetRunPlan(action1, SuccessOnlyPlan(action2))
pipeline.SetRunPlan(action2, DefaultPlan(action3, action3))
pipeline.SetRunPlan(action3, TerminationPlan[int]())
// 5 -> 16 -> 16(panic2) -> abort
output, err := pipeline.Run(context.Background(), 5)
assert.Error(t, err)
assert.Equal(t, err.Error(), "panic2")
assert.Equal(t, 16, output)
})
t.Run("simple pipeline with panic and abort planning", func(t *testing.T) {
action1 := NewCollatz("action1")
action2 := &PanicMaker{message: "panic2"}
action3 := NewCollatz("action3")
pipeline := NewPipeline("Pipeline", action1, action2, action3)
pipeline.SetRunPlan(action1, SuccessOnlyPlan(action2))
pipeline.SetRunPlan(action2, DefaultPlanWithAbort(action3, action3, action3))
pipeline.SetRunPlan(action3, TerminationPlan[int]())
// 5 -> 16 -> 16(panic2) -(abort)-> 8
output, err := pipeline.Run(context.Background(), 5)
assert.Error(t, err)
assert.Equal(t, err.Error(), "panic2")
assert.Equal(t, 8, output)
})
t.Run("multi pipeline with internal error", func(t *testing.T) {
action1 := NewCollatz("action1")
action2 := &ErrorMaker{message: "error2"}
action3 := NewCollatz("action3")
subPipeline := NewPipeline("SubPipeline", action1, action2, action3)
subPipeline.SetRunPlan(action1, SuccessOnlyPlan(action2))
subPipeline.SetRunPlan(action2, DefaultPlan(action3, action3))
subPipeline.SetRunPlan(action3, TerminationPlan[int]())
action0 := NewCollatz("action0")
action4 := NewCollatz("action4")
pipeline := NewPipeline("Pipeline", action0, subPipeline, action4)
pipeline.SetRunPlan(action0, SuccessOnlyPlan(subPipeline))
pipeline.SetRunPlan(subPipeline, DefaultPlan(action4, action4))
pipeline.SetRunPlan(action4, TerminationPlan[int]())
// 5 -> [16 -> 8 -> 8(error2) -> 4] -> 2
output, err := pipeline.Run(context.Background(), 5)
assert.Error(t, err)
assert.Equal(t, err.Error(), "error2")
assert.Equal(t, 2, output)
})
}
type Collatz struct {
*Pipeline[int]
CheckNext Action[int]
OnEven Action[int]
OnOdd Action[int]
}
func NewCollatz(name string) *Collatz {
checkNext, onEven, onOdd := &CheckNext{}, &OnEven{}, &OnOdd{}
pipeline := NewPipeline(
name,
checkNext,
onEven,
onOdd,
)
terminate := Terminate[int]()
noActionPlan := TerminationPlan[int]()
pipeline.SetRunPlan(checkNext, ActionPlan[int]{
"even": onEven,
"odd": onOdd,
})
pipeline.SetRunPlan(onEven, ActionPlan[int]{
Success: terminate,
// Skip Error, automatically configured as terminate
})
pipeline.SetRunPlan(onOdd, noActionPlan)
return &Collatz{
Pipeline: pipeline,
CheckNext: checkNext,
OnEven: onEven,
OnOdd: onOdd,
}
}
type CheckNext struct{}
func (CheckNext) Name() string { return "CheckNext" }
func (CheckNext) Directions() []string { return []string{"even", "odd"} }
func (CheckNext) Run(_ context.Context, input int) (output int, err error) {
return input, nil
}
func (CheckNext) NextDirection(_ context.Context, output int) (direction string, err error) {
if output == 1 {
return Abort, nil
} else if output == 0 {
return Error, fmt.Errorf("cannot try collatz with 0")
} else if output%2 == 0 {
return "even", nil
} else {
return "odd", nil
}
}
type OnEven struct{}
func (OnEven) Name() string { return "OnEven" }
func (OnEven) Run(_ context.Context, input int) (output int, err error) {
if input%2 != 0 {
// direction is intended bug. on running pipeline, it should be changed as Error
return input, fmt.Errorf("input is not even")
}
return input / 2, nil
}
type OnOdd struct{}
func (OnOdd) Name() string { return "OnOdd" }
func (OnOdd) Run(_ context.Context, input int) (output int, err error) {
return 3*input + 1, nil
}
type ErrorMaker struct{ message string }
func (e ErrorMaker) Name() string { return e.message }
func (e ErrorMaker) Run(_ context.Context, input int) (output int, err error) {
return input, errors.New(e.message)
}
type PanicMaker struct{ message string }
func (p PanicMaker) Name() string { return p.message }
func (p PanicMaker) Run(_ context.Context, _ int) (output int, err error) {
panic(errors.New(p.message))
}