-
Notifications
You must be signed in to change notification settings - Fork 69
/
component_test.go
545 lines (472 loc) · 13.4 KB
/
component_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
package async
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
proc := mockProcessor{}
type args struct {
name string
p ProcessorFunc
cf ConsumerFactory
fs FailStrategy
retries uint
retryWait time.Duration
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "success",
args: args{name: "name", p: proc.Process, cf: &mockConsumerFactory{}, fs: NackExitStrategy},
wantErr: false,
},
{
name: "failed, missing name",
args: args{name: "", p: proc.Process, cf: &mockConsumerFactory{}, fs: NackExitStrategy},
wantErr: true,
},
{
name: "failed, missing processor func",
args: args{name: "name", p: nil, cf: &mockConsumerFactory{}, fs: NackExitStrategy},
wantErr: true,
},
{
name: "failed, missing consumer",
args: args{name: "name", p: proc.Process, cf: nil, fs: NackExitStrategy},
wantErr: true,
},
{
name: "failed, invalid fail strategy",
args: args{name: "name", p: proc.Process, cf: &mockConsumerFactory{}, fs: 3},
wantErr: true,
},
{
name: "failed, invalid retry retry timeout",
args: args{name: "name", p: proc.Process, cf: &mockConsumerFactory{}, retryWait: -2},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.name, tt.args.cf, tt.args.p).
WithFailureStrategy(tt.args.fs).
WithRetries(tt.args.retries).
WithRetryWait(tt.args.retryWait).
Create()
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
type proxyBuilder struct {
proc mockProcessor
cnr mockConsumer
cf ConsumerFactory
fs FailStrategy
retries int
retryWait time.Duration
concurrency uint
}
func run(ctx context.Context, t *testing.T, builder *proxyBuilder) error {
if builder.cf == nil {
builder.cf = &mockConsumerFactory{c: &builder.cnr}
}
cmp, err := New("test", builder.cf, builder.proc.Process).
WithFailureStrategy(builder.fs).
WithRetries(uint(builder.retries)).
WithRetryWait(builder.retryWait).
WithConcurrency(builder.concurrency).
Create()
assert.NoError(t, err)
return cmp.Run(ctx)
}
// TestCreate_ReturnsError expects an error when concurrency > 1 and component does not allow out of order processing
func TestCreate_ReturnsError(t *testing.T) {
cnr := mockConsumer{}
builder := proxyBuilder{
cnr: cnr,
cf: &mockConsumerFactory{c: &cnr},
concurrency: 2,
}
cmp, err := New("test", builder.cf, builder.proc.Process).
WithFailureStrategy(builder.fs).
WithRetries(uint(builder.retries)).
WithRetryWait(builder.retryWait).
WithConcurrency(builder.concurrency).
Create()
require.NotNil(t, cmp)
require.NoError(t, err)
got := cmp.processing(context.Background())
want := "async component creation: cannot create in-order component with concurrency > 1"
assert.EqualError(t, got, want)
}
// TestRun_ReturnsError expects a consumer consume Error
func TestRun_ReturnsError(t *testing.T) {
builder := proxyBuilder{
cnr: mockConsumer{consumeError: true},
}
err := run(context.Background(), t, &builder)
assert.True(t, errors.Is(err, errConsumer))
assert.Equal(t, 0, builder.proc.execs)
}
// TestRun_WithCancel_CloseError expects a consumer closing error
func TestRun_WithCancel_CloseError(t *testing.T) {
builder := proxyBuilder{
cnr: mockConsumer{clsError: true},
}
ctx, cnl := context.WithCancel(context.Background())
cnl()
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.Equal(t, errConsumerClose, err)
assert.Equal(t, 0, builder.proc.execs)
}
// TestRun_Process_Error_NackExitStrategy expects a PROC ERROR
// from an error producing processor
// which will cause the component to return with an error
// due to the Nack FailureStrategy
func TestRun_Process_Error_NackExitStrategy(t *testing.T) {
builder := proxyBuilder{
proc: mockProcessor{errReturn: true},
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
}
ctx := context.Background()
builder.cnr.chMsg <- &mockMessage{ctx: ctx}
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.True(t, errors.Is(err, errProcess))
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_Process_Error_NackStrategy expects a PROC ERROR
// from an error producing processor
// but due to the Nack FailureStrategy, it will continue processing other messages
func TestRun_Process_Error_NackStrategy(t *testing.T) {
builder := proxyBuilder{
proc: mockProcessor{errReturn: true},
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
fs: NackStrategy,
}
ctx, cnl := context.WithCancel(context.Background())
builder.cnr.chMsg <- &mockMessage{ctx: ctx}
ch := make(chan bool)
go func() {
assert.NoError(t, run(ctx, t, &builder))
ch <- true
}()
time.Sleep(10 * time.Millisecond)
cnl()
select {
case _, ok := <-builder.cnr.chErr:
if ok {
assert.Fail(t, "we don't expect an error , given our nack failure strategy setup")
}
default:
assert.True(t, <-ch)
}
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_ProcessError_WithNackError expects a PROC ERROR
// from an error producing processor
// but also a Nack Error from the message
// This will cause the component to stop execution, as it cannot execute the Nack failure strategy
func TestRun_ProcessError_WithNackError(t *testing.T) {
builder := proxyBuilder{
proc: mockProcessor{errReturn: true},
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
fs: NackStrategy,
}
ctx := context.Background()
builder.cnr.chMsg <- &mockMessage{ctx: ctx, nackError: true}
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.True(t, errors.Is(err, errNack))
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_ParallelProcessError_WithNackError expects a PROC ERROR
// same as TestRun_ProcessError_WithNackError, just with concurrency
func TestRun_ParallelProcessError_WithNackError(t *testing.T) {
builder := proxyBuilder{
proc: mockProcessor{errReturn: true},
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
outOfOrder: true,
},
fs: NackStrategy,
concurrency: 10,
}
ctx := context.Background()
builder.cnr.chMsg <- &mockMessage{ctx: ctx, nackError: true}
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.True(t, errors.Is(err, errNack))
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_Process_Error_AckStrategy expects a PROC ERROR
// from an error producing processor
// but due to the Ack FailureStrategy, it will continue processing other messages
func TestRun_Process_Error_AckStrategy(t *testing.T) {
builder := proxyBuilder{
proc: mockProcessor{errReturn: true},
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
fs: NackStrategy,
}
ctx, cnl := context.WithCancel(context.Background())
builder.cnr.chMsg <- &mockMessage{ctx: ctx}
ch := make(chan bool)
go func() {
assert.NoError(t, run(ctx, t, &builder))
ch <- true
}()
time.Sleep(10 * time.Millisecond)
cnl()
select {
case _, ok := <-builder.cnr.chErr:
if ok {
assert.Fail(t, "we don't expect an error , given our ack failure strategy setup")
}
default:
assert.True(t, <-ch)
}
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_ProcessError_WithAckError expects a PROC ERROR
// from an error producing processor
// but also an Ack Error from the message
// This will cause the component to stop execution, as it cannot execute the Nack failure strategy
func TestRun_ProcessError_WithAckError(t *testing.T) {
builder := proxyBuilder{
proc: mockProcessor{errReturn: true},
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
fs: AckStrategy,
}
ctx := context.Background()
builder.cnr.chMsg <- &mockMessage{ctx: ctx, ackError: true}
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.True(t, errors.Is(err, errAck))
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_MessageAckError expects an ack error from the message acknowledgement
// it will break the execution of the component due to the default NackExit failure strategy
func TestRun_MessageAckError(t *testing.T) {
builder := proxyBuilder{
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
}
ctx := context.Background()
builder.cnr.chMsg <- &mockMessage{ctx: ctx, ackError: true}
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.Equal(t, errAck, err)
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_ConsumeError will break the component execution,
// when an error is injected into the consumers error channel
// while using the default NackExit Failure Strategy
func TestRun_ConsumeError(t *testing.T) {
builder := proxyBuilder{
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
}
ctx := context.Background()
builder.cnr.chErr <- errConsumer
err := run(ctx, t, &builder)
assert.True(t, errors.Is(err, errConsumer))
assert.Equal(t, 0, builder.proc.execs)
}
// TestRun_ConsumeError_WithRetry will retry the specified amount of times
// before exiting the execution
func TestRun_ConsumeError_WithRetry(t *testing.T) {
retries := 3
cf := &mockConsumerFactory{errRet: true}
builder := proxyBuilder{
cf: cf,
retries: retries,
retryWait: 2 * time.Millisecond,
}
err := run(context.Background(), t, &builder)
assert.Error(t, err)
assert.True(t, retries <= cf.execs)
assert.Equal(t, 0, builder.proc.execs)
}
// TestRun_ConsumeError_WithRetry_AndContextCancel will retry after a consumer error
// only a small amount fo times, due to the context being canceled as well
func TestRun_ConsumeError_WithRetry_AndContextCancel(t *testing.T) {
retries := 33
cf := &mockConsumerFactory{errRet: true}
builder := proxyBuilder{
cf: cf,
retries: retries,
retryWait: 2 * time.Millisecond,
}
ctx, cnl := context.WithCancel(context.Background())
cnl()
err := run(ctx, t, &builder)
assert.Error(t, err)
assert.Equal(t, ctx.Err(), context.Canceled)
assert.True(t, retries > cf.execs)
assert.Equal(t, 0, builder.proc.execs)
}
// TestRun_Process_Shutdown verifies the process shuts down on a context cancellation action
func TestRun_Process_Shutdown(t *testing.T) {
builder := proxyBuilder{
cnr: mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
},
}
builder.cnr.chMsg <- &mockMessage{ctx: context.Background()}
ch := make(chan bool)
ctx, cnl := context.WithCancel(context.Background())
go func() {
err1 := run(ctx, t, &builder)
assert.NoError(t, err1)
ch <- true
}()
time.Sleep(10 * time.Millisecond)
cnl()
assert.True(t, <-ch)
assert.Equal(t, 1, builder.proc.execs)
}
// TestRun_Process_Error_InvalidStrategy expects a invalid failure strategy error
// NOTE : we injected the failure strategy after the construction,
// in order to avoid the failure strategy check
func TestRun_Process_Error_InvalidStrategy(t *testing.T) {
cnr := mockConsumer{
chMsg: make(chan Message, 10),
chErr: make(chan error, 10),
}
proc := mockProcessor{errReturn: true}
cmp, err := New("test", &mockConsumerFactory{c: &cnr}, proc.Process).
Create()
assert.NoError(t, err)
cmp.failStrategy = 4
ctx := context.Background()
cnr.chMsg <- &mockMessage{ctx: ctx}
err = cmp.Run(ctx)
assert.Error(t, err)
assert.Equal(t, errInvalidFS, err)
assert.Equal(t, 1, proc.execs)
}
type mockMessage struct {
ctx context.Context
ackError bool
nackError bool
}
func (mm *mockMessage) Context() context.Context {
return mm.ctx
}
// Decode is not called in our tests, because the mockProcessor will ignore the message decoding
func (mm *mockMessage) Decode(interface{}) error {
return nil
}
var errAck = errors.New("MESSAGE ACK ERROR")
func (mm *mockMessage) Ack() error {
if mm.ackError {
return errAck
}
return nil
}
var errNack = errors.New("MESSAGE NACK ERROR")
func (mm *mockMessage) Nack() error {
if mm.nackError {
return errNack
}
return nil
}
func (mm *mockMessage) Source() string {
return ""
}
func (mm *mockMessage) Payload() []byte {
return []byte{}
}
type mockProcessor struct {
errReturn bool
mux sync.Mutex
execs int
}
var errProcess = errors.New("PROC ERROR")
func (mp *mockProcessor) Process(Message) error {
mp.mux.Lock()
mp.execs++
mp.mux.Unlock()
if mp.errReturn {
return errProcess
}
return nil
}
func (mp *mockProcessor) GetExecs() int {
mp.mux.Lock()
defer mp.mux.Unlock()
return mp.execs
}
type mockConsumerFactory struct {
c Consumer
errRet bool
execs int
}
var errFactory = errors.New("FACTORY ERROR")
func (mcf *mockConsumerFactory) Create() (Consumer, error) {
mcf.execs++
if mcf.errRet {
return nil, errFactory
}
return mcf.c, nil
}
type mockConsumer struct {
consumeError bool
clsError bool
chMsg chan Message
chErr chan error
outOfOrder bool
}
func (mc *mockConsumer) OutOfOrder() bool {
return mc.outOfOrder
}
var errConsumer = errors.New("CONSUMER ERROR")
func (mc *mockConsumer) Consume(context.Context) (<-chan Message, <-chan error, error) {
if mc.consumeError {
return nil, nil, errConsumer
}
return mc.chMsg, mc.chErr, nil
}
var errConsumerClose = errors.New("CONSUMER CLOSE ERROR")
func (mc *mockConsumer) Close() error {
if mc.clsError {
return errConsumerClose
}
return nil
}