-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
462 lines (410 loc) · 12.7 KB
/
router_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
package pubsub_test
import (
"context"
"errors"
"fmt"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/hmoragrega/pubsub"
"github.com/hmoragrega/pubsub/pubsubtest/stubs"
)
var (
errorDummy = errors.New("dummy error")
handlerDummy = pubsub.HandlerFunc(func(_ context.Context, _ *pubsub.Message) error {
return nil
})
)
func TestRouter_Run(t *testing.T) {
ctx := context.Background()
t.Run("zero value router can run without failure", func(t *testing.T) {
var router pubsub.Router
err := router.Run(ctx)
if err != nil {
t.Fatalf("unexpected error running an empty Router: %v", err)
}
})
t.Run("cannot subscribe twice using the same consumer name", func(t *testing.T) {
var router pubsub.Router
err := router.Register("same", &stubs.SubscriberStub{}, handlerDummy)
if err != nil {
t.Fatalf("unexpected error registering the consumer: %v", err)
}
err = router.Register("same", &stubs.SubscriberStub{}, handlerDummy)
if err == nil {
t.Fatalf("unexpected success registering the consumer again: %v", err)
}
})
t.Run("started consumers are stopped on any subscribe failure", func(t *testing.T) {
// feed the results for the mocks, we cannot know
// which will be the order due to the randomness
// of iterating the consumer map.
results := make(chan error, 3)
results <- nil
results <- nil
results <- errorDummy
var (
router pubsub.Router
stopped uint32
)
for i, s := range [3]*stubs.SubscriberStub{{}, {}, {}} {
if err := router.Register(strconv.Itoa(i), s, handlerDummy); err != nil {
t.Fatalf("cannot register handler %d: %v", i, err)
}
s.SubscribeFunc = func() (<-chan pubsub.Next, error) {
return nil, <-results
}
s.StopFunc = func(_ context.Context) error {
atomic.AddUint32(&stopped, 1)
return nil
}
}
err := router.Run(ctx)
if !errors.Is(err, errorDummy) {
t.Fatalf("unexpected error: got %v, want %v", err, errorDummy)
}
if stopped != 2 {
t.Fatalf("unexpected number of stopped subscribers: got %d, want 2", stopped)
}
})
t.Run("all consumers are stopped by default if one fails", func(t *testing.T) {
router := pubsub.Router{
OnReceive: func(_ context.Context, _ string, _ pubsub.ReceivedMessage, err error) error {
return err
},
}
var stopped uint32
for i, s := range [3]*stubs.SubscriberStub{{}, {}, {}} {
i := i
if err := router.Register(strconv.Itoa(i), s, handlerDummy); err != nil {
t.Fatalf("cannot register handler %d: %v", i, err)
}
s.SubscribeFunc = func() (<-chan pubsub.Next, error) {
next := make(chan pubsub.Next, 1)
if i == 2 {
next <- pubsub.Next{Err: errorDummy}
} else {
next <- pubsub.Next{Err: context.Canceled}
}
return next, nil
}
s.StopFunc = func(_ context.Context) error {
atomic.AddUint32(&stopped, 1)
return nil
}
}
err := router.Run(ctx)
if !errors.Is(err, errorDummy) {
t.Fatalf("unexpected error: got %v, want %v", err, errorDummy)
}
if stopped != 3 {
t.Fatalf("unexpected number of stopped subscribers: got %d, want 2", stopped)
}
})
t.Run("errors stopping the consumers are reported", func(t *testing.T) {
var (
router pubsub.Router
consumers uint32 = 3
)
results := make(chan error, consumers)
results <- nil
results <- nil
results <- errorDummy
ctx, cancel := context.WithCancel(ctx)
running := make(chan struct{}, consumers)
for i, s := range [3]*stubs.SubscriberStub{{}, {}, {}} {
if err := router.Register(strconv.Itoa(i), s, handlerDummy); err != nil {
t.Fatalf("cannot register handler %d: %v", i, err)
}
s.SubscribeFunc = func() (<-chan pubsub.Next, error) {
running <- struct{}{}
return nil, nil
}
s.StopFunc = func(_ context.Context) error {
return <-results
}
}
go func() {
// wait until all consumers are running
for i := 0; i < int(consumers); i++ {
<-running
}
cancel()
}()
err := router.Run(ctx)
if !errors.Is(err, errorDummy) {
t.Fatalf("unexpected error: got %v, want %v", err, errorDummy)
}
})
t.Run("checkpoints are called", func(t *testing.T) {
consumerName := "foo"
var checkpointsCalled int
ctx, cancel := context.WithCancel(context.Background())
msg := stubs.NewNoOpReceivedMessage()
msg.AckFunc = func(ctx context.Context) error {
cancel()
return nil
}
verifyCheckpoint := func(checkpoint string, name string, message pubsub.ReceivedMessage, err error) error {
checkpointsCalled++
if err != nil {
return err
}
if message.(*stubs.ReceivedMessageStub) != msg {
return fmt.Errorf("%s mesage is not the equal; got %+v", checkpoint, message)
}
if name != consumerName {
return fmt.Errorf("%s consumer is not correct; got %+v, want %s", checkpoint, name, consumerName)
}
return nil
}
router := pubsub.Router{
OnReceive: func(_ context.Context, name string, message pubsub.ReceivedMessage, err error) error {
return verifyCheckpoint("OnReceive", name, message, err)
},
OnUnmarshal: func(_ context.Context, name string, message pubsub.ReceivedMessage, err error) error {
return verifyCheckpoint("OnUnmarshal", name, message, err)
},
OnHandler: func(_ context.Context, name string, message pubsub.ReceivedMessage, err error) error {
return verifyCheckpoint("OnHandler", name, message, err)
},
OnAck: func(_ context.Context, name string, message pubsub.ReceivedMessage, err error) error {
return verifyCheckpoint("OnAck", name, message, err)
},
}
s := &stubs.SubscriberStub{
SubscribeFunc: func() (<-chan pubsub.Next, error) {
next := make(chan pubsub.Next, 1)
next <- pubsub.Next{Message: msg}
return next, nil
},
StopFunc: func(ctx context.Context) error {
return nil
},
}
err := router.Register(consumerName, s, handlerDummy)
if err != nil {
t.Fatal("cannot register handler", err)
}
err = router.Run(ctx)
if err != nil {
t.Fatal("unexpected error in the router", err)
}
if checkpointsCalled != 4 {
t.Fatalf("unexpected number of hooks called; got %d, want 4", checkpointsCalled)
}
})
t.Run("checkpoint exits", func(t *testing.T) {
routers := []*pubsub.Router{
{
OnReceive: func(_ context.Context, _ string, _ pubsub.ReceivedMessage, _ error) error {
return errorDummy
},
}, {
OnUnmarshal: func(_ context.Context, _ string, _ pubsub.ReceivedMessage, _ error) error {
return errorDummy
},
}, {
OnHandler: func(_ context.Context, _ string, _ pubsub.ReceivedMessage, _ error) error {
return errorDummy
},
}, {
OnAck: func(_ context.Context, _ string, _ pubsub.ReceivedMessage, _ error) error {
return errorDummy
},
},
}
for _, router := range routers {
s := &stubs.SubscriberStub{
SubscribeFunc: func() (<-chan pubsub.Next, error) {
msg := stubs.NewNoOpReceivedMessage()
msg.AckFunc = func(ctx context.Context) error {
return nil
}
next := make(chan pubsub.Next, 1)
next <- pubsub.Next{Message: msg}
return next, nil
},
StopFunc: func(ctx context.Context) error {
return nil
},
}
err := router.Register("foo", s, handlerDummy)
if err != nil {
t.Fatal("cannot register handler", err)
}
err = router.Run(ctx)
if !errors.Is(err, errorDummy) {
t.Fatalf("expected checkpoint exit error; got %v", err)
}
}
})
t.Run("continue on errors", func(t *testing.T) {
s := &stubs.SubscriberStub{
SubscribeFunc: func() (<-chan pubsub.Next, error) {
next := make(chan pubsub.Next, 5)
next <- pubsub.Next{Err: errorDummy}
next <- pubsub.Next{Message: stubs.NewNoOpReceivedMessage()}
next <- pubsub.Next{Message: stubs.NewNoOpReceivedMessage()}
next <- pubsub.Next{Message: stubs.NewNoOpReceivedMessage()}
next <- pubsub.Next{Message: stubs.NewNoOpReceivedMessage()}
return next, nil
},
StopFunc: func(ctx context.Context) error {
return nil
},
}
var (
unmarshallerCalls int
handlerCalls int
)
router := pubsub.Router{
Unmarshaller: pubsub.UnmarshallerFunc(func(topic string, msg pubsub.ReceivedMessage) (interface{}, error) {
unmarshallerCalls++
if unmarshallerCalls == 1 {
return nil, errorDummy
}
return nil, nil
}),
AckDecider: pubsub.DisableAutoAck,
OnProcess: pubsub.WrapOnProcess(nil, func(_ context.Context, _ string, _ time.Duration, _ pubsub.ReceivedMessage, _ error) {}),
MessageContext: pubsub.WrapMessageContext(nil, func(parent context.Context, _ string, _ pubsub.ReceivedMessage) context.Context {
return parent
}),
OnAck: pubsub.WrapCheckpoint(nil, func(_ context.Context, _ string, _ pubsub.ReceivedMessage, _ error) error {
return nil
}),
OnNext: pubsub.WrapNext(nil, func(ctx context.Context, consumerName string, next pubsub.Next) pubsub.Next {
return next
}),
}
ctx, cancel := context.WithCancel(context.Background())
err := router.Register("foo", s, pubsub.HandlerFunc(func(ctx context.Context, _ *pubsub.Message) error {
handlerCalls++
switch handlerCalls {
case 1:
return errorDummy
case 2:
return nil
}
cancel()
return ctx.Err()
}))
if err != nil {
t.Fatal("cannot register handler", err)
}
err = router.Run(ctx)
if err != nil {
t.Fatalf("expected clean shutdown; got %v", err)
}
})
t.Run("errors on running routers", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var router pubsub.Router
_ = router.Register("foo", &stubs.SubscriberStub{
SubscribeFunc: func() (<-chan pubsub.Next, error) {
return nil, nil
},
StopFunc: func(ctx context.Context) error {
return nil
},
}, handlerDummy)
errs := make(chan error, 2)
go func() {
errs <- router.Run(ctx)
}()
go func() {
errs <- router.Run(ctx)
}()
err := <-errs
if !errors.Is(err, pubsub.ErrRouterAlreadyRunning) {
t.Fatalf("unexpected error starting the router twice; got %v", err)
}
err = router.Register("bar", &stubs.SubscriberStub{}, handlerDummy)
if !errors.Is(err, pubsub.ErrRouterAlreadyRunning) {
t.Fatalf("unexpected error registering handlers when router is running; got %v", err)
}
// stop the router.
cancel()
<-errs
err = router.Register("bar", &stubs.SubscriberStub{}, handlerDummy)
if !errors.Is(err, pubsub.ErrRouterAlreadyStopped) {
t.Fatalf("unexpected error registering handlers when router has stopped; got %v", err)
}
err = router.Run(ctx)
if !errors.Is(err, pubsub.ErrRouterAlreadyStopped) {
t.Fatalf("unexpected error starting a stopped router; got %v", err)
}
})
t.Run("re-schedule messages with backoff", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Hour) //TODO
defer cancel()
var rescheduled int32 = 0
var want int32 = 2
router := pubsub.Router{
OnAck: func(ctx context.Context, topic string, msg pubsub.ReceivedMessage, err error) error {
// stop the router on ack errors.
if err != nil {
return err
}
// stop after all messages have been rescheduled correctly.
if want == atomic.AddInt32(&rescheduled, 1) {
cancel()
}
return nil
},
}
errHandler := pubsub.HandlerFunc(func(_ context.Context, _ *pubsub.Message) error {
return errors.New("dummy err")
})
defaultChan := make(chan pubsub.Next)
overrideChan := make(chan pubsub.Next)
// normal consumer with default exponential backoff
_ = router.Register("default", &stubs.SubscriberStub{
SubscribeFunc: func() (<-chan pubsub.Next, error) {
return defaultChan, nil
},
StopFunc: func(ctx context.Context) error {
return nil
},
}, errHandler, pubsub.WithAckDecider(pubsub.ReScheduleOnError))
// consumer with overridden linear backoff.
_ = router.Register("override", &stubs.SubscriberStub{
SubscribeFunc: func() (<-chan pubsub.Next, error) {
return overrideChan, nil
},
StopFunc: func(ctx context.Context) error {
return nil
},
}, errHandler, pubsub.WithAckDecider(pubsub.ReScheduleOnError), pubsub.WithBackoff(pubsub.LinearBackoff(time.Second)))
go func() {
m := stubs.NewNoOpReceivedMessage()
m.ReScheduleFunc = func(ctx context.Context, delay time.Duration) error {
if delay != time.Minute {
return fmt.Errorf("unexpected re-scheduling delay in the default backoff")
}
return nil
}
defaultChan <- pubsub.Next{Message: m}
}()
go func() {
m := stubs.NewNoOpReceivedMessage()
m.ReScheduleFunc = func(ctx context.Context, delay time.Duration) error {
if delay != time.Second {
return fmt.Errorf("unexpected re-scheduling delay in the overridden backoff")
}
return nil
}
overrideChan <- pubsub.Next{Message: m}
}()
err := router.Run(ctx)
if err != nil {
t.Fatalf("unexpected router error: %v", err)
}
if err := ctx.Err(); err != context.Canceled {
t.Fatalf("unexpected context error: %v", err)
}
})
}