-
Notifications
You must be signed in to change notification settings - Fork 13
/
process_test.go
317 lines (297 loc) · 8.48 KB
/
process_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
package pipeline
import (
"context"
"reflect"
"testing"
"time"
)
func TestProcess(t *testing.T) {
t.Parallel()
const maxTestDuration = time.Second
type args struct {
ctxTimeout time.Duration
processDuration time.Duration
processReturnsErrors bool
cancelDuration time.Duration
in []int
}
type want struct {
open bool
out []int
canceled []int
canceledErrs []string
}
tests := []struct {
name string
args args
want want
}{
{
name: "out closes if in closes but the context isn't canceled",
args: args{
ctxTimeout: 2 * maxTestDuration,
processDuration: 0,
in: []int{1, 2, 3},
},
want: want{
open: false,
out: []int{1, 2, 3},
canceled: nil,
},
}, {
name: "cancel is called on elements after the context is canceled",
args: args{
ctxTimeout: maxTestDuration / 2,
processDuration: maxTestDuration / 11,
in: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
want: want{
open: false,
out: []int{1, 2, 3, 4, 5},
canceled: []int{6, 7, 8, 9, 10},
canceledErrs: []string{
"context deadline exceeded",
"context deadline exceeded",
"context deadline exceeded",
"context deadline exceeded",
"context deadline exceeded",
},
},
}, {
name: "out stays open as long as in is open",
args: args{
ctxTimeout: maxTestDuration / 2,
processDuration: (maxTestDuration / 2) - (100 * time.Millisecond),
cancelDuration: (maxTestDuration / 2) - (100 * time.Millisecond),
in: []int{1, 2, 3},
},
want: want{
open: true,
out: []int{1},
canceled: []int{2},
canceledErrs: []string{
"context deadline exceeded",
},
},
}, {
name: "when an error is returned during process, it is passed to cancel",
args: args{
ctxTimeout: maxTestDuration - 100*time.Millisecond,
processDuration: (maxTestDuration - 200*time.Millisecond) / 2,
processReturnsErrors: true,
cancelDuration: 0,
in: []int{1, 2, 3},
},
want: want{
open: false,
out: nil,
canceled: []int{1, 2, 3},
canceledErrs: []string{
"process error: 1",
"process error: 2",
"context deadline exceeded",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// Create the in channel
in := make(chan int)
go func() {
defer close(in)
for _, i := range test.args.in {
in <- i
}
}()
// Setup the Processor
ctx, cancel := context.WithTimeout(context.Background(), test.args.ctxTimeout)
defer cancel()
processor := &mockProcessor[int]{
processDuration: test.args.processDuration,
processReturnsErrs: test.args.processReturnsErrors,
cancelDuration: test.args.cancelDuration,
}
out := Process[int, int](ctx, processor, in)
// Collect the outputs
timeout := time.After(maxTestDuration)
var outs []int
var isOpen bool
loop:
for {
select {
case o, open := <-out:
if !open {
isOpen = false
break loop
}
isOpen = true
outs = append(outs, o)
case <-timeout:
break loop
}
}
// Expecting the out channel to be open or closed
if test.want.open != isOpen {
t.Errorf("%t != %t", test.want.open, isOpen)
}
// Expecting processed outputs
if !reflect.DeepEqual(test.want.out, outs) {
t.Errorf("%+v != %+v", test.want.out, outs)
}
// Expecting canceled inputs
if !reflect.DeepEqual(test.want.canceled, processor.canceled) {
t.Errorf("%+v != %+v", test.want.canceled, processor.canceled)
}
// Expecting canceled errors
if !reflect.DeepEqual(test.want.canceledErrs, processor.errs) {
t.Errorf("%+v != %+v", test.want.canceledErrs, processor.errs)
}
})
}
}
func TestProcessConcurrently(t *testing.T) {
t.Parallel()
const maxTestDuration = time.Second
type args struct {
ctxTimeout time.Duration
processDuration time.Duration
processReturnsErrors bool
cancelDuration time.Duration
concurrently int
in []int
}
type want struct {
open bool
out []int
canceled []int
canceledErrs []string
}
tests := []struct {
name string
args args
want want
}{
{
name: "out closes if in closes but the context isn't canceled",
args: args{
ctxTimeout: 2 * maxTestDuration, // context never times out
processDuration: maxTestDuration/3 - (100 * time.Millisecond), // 3 processed per processor
concurrently: 2, // * 2 processors = 6 processed, pipe closes
in: []int{1, 2, 3, 4, 5, 6},
},
want: want{
open: false,
out: []int{1, 2, 3, 4, 5, 6},
canceled: nil,
},
}, {
name: "cancel is called on elements after the context is canceled",
args: args{
ctxTimeout: maxTestDuration / 2, // context times out before the test ends
processDuration: (maxTestDuration / 4) - (10 * time.Millisecond), // 2 processed per processor before timeout
concurrently: 3, // * 3 processors = 6 processed, 4 canceled, pipe closes
in: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
want: want{
open: false,
out: []int{1, 2, 3, 4, 5, 6},
canceled: []int{7, 8, 9, 10},
canceledErrs: []string{
"context deadline exceeded",
"context deadline exceeded",
"context deadline exceeded",
"context deadline exceeded",
},
},
}, {
name: "out stays open as long as in is open",
args: args{
ctxTimeout: maxTestDuration / 2, // context times out half way through the test
processDuration: (maxTestDuration / 2) - (100 * time.Millisecond), // process fires onces per processor
cancelDuration: (maxTestDuration / 2) - (100 * time.Millisecond), // cancel fires once per process
concurrently: 3, // * 3 proceses = 3 canceled, 3 processed, 1 still in the pipe
in: []int{1, 2, 3, 4, 5, 6, 7},
},
want: want{
open: true,
out: []int{1, 2, 3},
canceled: []int{4, 5, 6},
canceledErrs: []string{
"context deadline exceeded",
"context deadline exceeded",
"context deadline exceeded",
},
},
}, {
name: "when an error is returned during process, it is passed to cancel",
args: args{
ctxTimeout: maxTestDuration - 100*time.Millisecond,
processDuration: (maxTestDuration - 200*time.Millisecond) / 2,
processReturnsErrors: true,
cancelDuration: 0,
concurrently: 1,
in: []int{1, 2, 3},
},
want: want{
open: false,
out: nil,
canceled: []int{1, 2, 3},
canceledErrs: []string{
"process error: 1",
"process error: 2",
"context deadline exceeded",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// Create the in channel
in := Emit(test.args.in...)
// Setup the Processor
ctx, cancel := context.WithTimeout(context.Background(), test.args.ctxTimeout)
defer cancel()
processor := &mockProcessor[int]{
processDuration: test.args.processDuration,
processReturnsErrs: test.args.processReturnsErrors,
cancelDuration: test.args.cancelDuration,
}
out := ProcessConcurrently[int, int](ctx, test.args.concurrently, processor, in)
var outs []int
var isOpen bool
timeout := time.After(maxTestDuration)
loop:
for {
select {
case i, open := <-out:
isOpen = open
if !open {
break loop
}
outs = append(outs, i)
case <-timeout:
break loop
}
}
// Expecting the out channel to be open or closed
if test.want.open != isOpen {
t.Errorf("open = %t, want %t", isOpen, test.want.open)
}
// Expecting canceled inputs
if !containsAll(test.want.out, outs) {
t.Errorf("out = %+v, want %+v", outs, test.want.out)
}
// Expecting canceled inputs
if !containsAll(test.want.canceled, processor.canceled) {
t.Errorf("canceled = %+v, want %+v", processor.canceled, test.want.canceled)
}
// Expecting canceled errors
if !containsAll(test.want.canceledErrs, processor.errs) {
t.Errorf("canceledErrs = %+v, want %+v", processor.errs, test.want.canceledErrs)
}
})
}
}