-
Notifications
You must be signed in to change notification settings - Fork 1
/
flo_test.go
368 lines (325 loc) · 8.24 KB
/
flo_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
package flo_test
import (
"context"
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
"github.com/codyoss/flo"
)
func TestFloValidateFailures(t *testing.T) {
tests := []struct {
name string
b *flo.Builder
s1 flo.Step
s2 flo.Step
s3 flo.Step
}{
{"not enough steps", flo.NewBuilder(), badFunc, nil, nil},
{"bad step type 1", flo.NewBuilder(), "", "", nil},
{"bad step type 2", flo.NewBuilder(), 0, 0, nil},
{"bad step type 3", flo.NewBuilder(), false, false, nil},
{"bad step type 4", flo.NewBuilder(), badFunc, badFunc, nil},
{"type mismatch", flo.NewBuilder(), start, square, nil},
{"type mismatch with interface", flo.NewBuilder(), start, read, nil},
{"wrong first step", flo.NewBuilder(), end, start, nil},
{"wrong interior step", flo.NewBuilder(), start, start, end},
{"wrong last step", flo.NewBuilder(), start, middle, start},
{"wrong input chan type", flo.NewBuilder(flo.WithInput("")), middle, end, nil},
{"wrong input chan type", flo.NewBuilder(flo.WithInput(0)), middle, end, nil},
{"wrong input chan type", flo.NewBuilder(flo.WithInput(false)), middle, end, nil},
{"input chan type mismatch", flo.NewBuilder(flo.WithInput(make(chan int, 1)), flo.WithOutput(0)), middleStringerToStringer, endStringThing, nil},
{"wrong output chan type", flo.NewBuilder(flo.WithInput(make(chan string, 1)), flo.WithOutput(false)), middle, middle, nil},
{"wrong output chan type", flo.NewBuilder(flo.WithInput(make(chan string, 1)), flo.WithOutput("")), middle, middle, nil},
{"wrong output chan type", flo.NewBuilder(flo.WithInput(make(chan string, 1)), flo.WithOutput("")), middle, middle, nil},
{"output chan type mismatch", flo.NewBuilder(flo.WithInput(make(chan string, 1)), flo.WithOutput(make(chan io.Closer, 1))), middle, middle, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.s1 != nil {
tt.b.Add(tt.s1)
}
if tt.s2 != nil {
tt.b.Add(tt.s2)
}
if tt.s3 != nil {
tt.b.Add(tt.s3)
}
err := tt.b.Validate()
if err == nil {
t.Errorf("got nil, want error")
}
})
}
}
func TestFloBuildAndExecuteReturnsErr(t *testing.T) {
err := flo.NewBuilder().BuildAndExecute(context.Background())
if err == nil {
t.Errorf("got %v, want nil", err)
}
}
func TestFloValidateAssignable(t *testing.T) {
err := flo.NewBuilder().Add(startStringThing).Add(endStringThing).Validate()
if err != nil {
t.Errorf("got %v, want nil", err)
}
}
func TestFloValidateAssignableInChan(t *testing.T) {
err := flo.NewBuilder(flo.WithInput(make(chan stringThing, 1))).
Add(middleStringerToStringer).Add(endStringThing).
Validate()
if err != nil {
t.Errorf("got %v, want nil", err)
}
}
func TestFloValidateAssignableOutputChan(t *testing.T) {
err := flo.NewBuilder(flo.WithInput(make(chan stringThing, 1)), flo.WithOutput(make(chan fmt.Stringer, 1))).
Add(middleStringThingToStringThing).Add(middleStringThingToStringThing).
Validate()
if err != nil {
t.Errorf("got %v, want nil", err)
}
}
func TestWithErrorHandler(t *testing.T) {
inCh := make(chan string, 1)
eh := &errHandle{}
if eh.hasHandled {
t.Fatal("got true, want false")
}
inCh <- "test"
close(inCh)
err := flo.NewBuilder(flo.WithInput(inCh), flo.WithErrorHandler(eh.handleError)).
Add(erroringMiddle).
Add(end).
BuildAndExecute(context.Background())
if err != nil {
t.Fatalf("got %v, want nil", err)
}
if !eh.hasHandled {
t.Fatal("got false, want true")
}
}
func TestWithSetErrorHandler(t *testing.T) {
inCh := make(chan string, 1)
eh := &errHandle{}
if eh.hasHandled {
t.Fatal("got true, want false")
}
stepEh := &errHandle{}
if stepEh.hasHandled {
t.Fatal("got true, want false")
}
inCh <- "test"
close(inCh)
err := flo.NewBuilder(flo.WithInput(inCh), flo.WithErrorHandler(eh.handleError)).
Add(erroringMiddle, flo.WithStepErrorHandler(stepEh.handleError)).
Add(end).
BuildAndExecute(context.Background())
if err != nil {
t.Fatalf("got %v, want nil", err)
}
if eh.hasHandled {
t.Fatal("got true, want false")
}
if !stepEh.hasHandled {
t.Fatal("got false, want true")
}
}
func TestFloBuildAndExecute(t *testing.T) {
c := &completeFlo{i: 2}
if c.i != 2 {
t.Fatalf("got %d, want 2", c)
}
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Duration(1*time.Millisecond), cancel)
err := flo.NewBuilder().
Add(c.start).
Add(c.middle).
Add(c.end).
BuildAndExecute(ctx)
if err != nil {
t.Fatalf("got %v, want nil", err)
}
if c.i != 4 {
t.Fatalf("got %d, want 4", c.i)
}
}
func TestFloBuildAndExecuteWithInOutChannels(t *testing.T) {
inputChannel := make(chan string, 1)
inputChannel <- "Hello World"
close(inputChannel)
outputChannel := make(chan string, 1)
err := flo.NewBuilder(flo.WithInput(inputChannel), flo.WithOutput(outputChannel)).
Add(middle).
Add(middle).
BuildAndExecute(context.Background())
if err != nil {
t.Fatalf("got %v, want nil", err)
}
if got := <-outputChannel; got != "HELLO WORLD" {
t.Fatalf("got %s, want HELLO WORLD", got)
}
close(outputChannel)
}
// Benchmark flo vs non-flo
func BenchmarkFlo(b *testing.B) {
inCh := make(chan int, 5)
outCh := make(chan int, 5)
var wg sync.WaitGroup
wg.Add(1)
go func() {
_ = flo.NewBuilder(flo.WithInput(inCh), flo.WithOutput(outCh), flo.WithParallelism(5)).
Add(addInts).
Add(addInts).
Add(addInts).
Add(addInts).
Add(addInts).
BuildAndExecute(context.Background())
wg.Done()
}()
for n := 0; n < b.N; n++ {
inCh <- 1
<-outCh
}
close(inCh)
wg.Wait()
close(outCh)
}
func BenchmarkNonFlo(b *testing.B) {
inCh := make(chan int, 5)
inCh2 := make(chan int, 5)
inCh3 := make(chan int, 5)
inCh4 := make(chan int, 5)
inCh5 := make(chan int, 5)
outCh := make(chan int, 5)
var wg sync.WaitGroup
var wg2 sync.WaitGroup
var wg3 sync.WaitGroup
var wg4 sync.WaitGroup
var wg5 sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
wg2.Add(1)
wg3.Add(1)
wg4.Add(1)
wg5.Add(1)
go func() {
for v := range inCh {
val, _ := addInts(context.Background(), v)
inCh2 <- val
}
wg.Done()
}()
go func() {
for v := range inCh2 {
val, _ := addInts(context.Background(), v)
inCh3 <- val
}
wg2.Done()
}()
go func() {
for v := range inCh3 {
val, _ := addInts(context.Background(), v)
inCh4 <- val
}
wg3.Done()
}()
go func() {
for v := range inCh4 {
val, _ := addInts(context.Background(), v)
inCh5 <- val
}
wg4.Done()
}()
go func() {
for v := range inCh5 {
val, _ := addInts(context.Background(), v)
outCh <- val
}
wg5.Done()
}()
}
for n := 0; n < b.N; n++ {
inCh <- 1
<-outCh
}
close(inCh)
wg.Wait()
close(inCh2)
wg2.Wait()
close(inCh3)
wg3.Wait()
close(inCh4)
wg4.Wait()
close(inCh5)
wg5.Wait()
close(outCh)
}
// helpers
func badFunc() {}
func start(ctx context.Context) (string, error) {
return "hey", nil
}
func middle(ctx context.Context, s string) (string, error) {
return strings.ToUpper(s), nil
}
func end(ctx context.Context, s string) error {
return nil
}
func square(ctx context.Context, i int) (int, error) {
return i * i, nil
}
func addInts(ctx context.Context, i int) (int, error) {
return i + i, nil
}
func read(ctx context.Context, r io.Reader) error {
return nil
}
type stringThing string
func (s stringThing) String() string {
return "stringThing"
}
func startStringThing(ctx context.Context) (stringThing, error) {
return "", nil
}
func middleStringerToStringer(ctx context.Context, s fmt.Stringer) (fmt.Stringer, error) {
return s, nil
}
func middleStringThingToStringThing(ctx context.Context, s stringThing) (stringThing, error) {
return s, nil
}
func endStringThing(ctx context.Context, s fmt.Stringer) error {
return nil
}
type errHandle struct {
hasHandled bool
}
func (e *errHandle) handleError(err error) {
e.hasHandled = true
}
func erroringMiddle(ctx context.Context, s string) (string, error) {
return "", fmt.Errorf("Bad things")
}
type completeFlo struct {
i int
once sync.Once
sync.Mutex
}
func (c *completeFlo) start(ctx context.Context) (int, error) {
c.Lock()
defer c.Unlock()
return c.i, nil
}
func (c *completeFlo) middle(ctx context.Context, i int) (int, error) {
return i * i, nil
}
func (c *completeFlo) end(ctx context.Context, i int) error {
c.Lock()
defer c.Unlock()
c.once.Do(func() {
c.i = i
})
return nil
}