-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventbus_test.go
525 lines (400 loc) · 12.9 KB
/
eventbus_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
// Copyright (c) 2014 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rabbitmq_test
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/Clarilab/clarimq/v2"
rabbitmq "github.com/Clarilab/eh-rabbitmq/v2"
eh "github.com/Clarilab/eventhorizon"
"github.com/Clarilab/eventhorizon/eventbus"
"github.com/Clarilab/eventhorizon/mocks"
"github.com/Clarilab/eventhorizon/namespace"
"github.com/Clarilab/eventhorizon/uuid"
"github.com/orlangure/gnomock"
gnormq "github.com/orlangure/gnomock/preset/rabbitmq"
)
const (
rmqVersion = "3.13.3-management"
rmqUser = "guest"
rmqPasswd = "guest"
)
var (
rmqPort int //nolint:gochecknoglobals // test code
rmqManagementPort int //nolint:gochecknoglobals // test code
)
func TestMain(m *testing.M) {
preset := gnormq.Preset(gnormq.WithUser(rmqUser, rmqPasswd), gnormq.WithVersion(rmqVersion))
container, err := gnomock.Start(preset, gnomock.WithUseLocalImagesFirst())
if err != nil {
panic(err)
}
rmqPort = container.DefaultPort()
rmqManagementPort = container.Port(gnormq.ManagementPort)
m.Run()
}
func Test_Integration_AddHandler(t *testing.T) { //nolint:paralleltest // must not run in parallel
bus, _, err := newTestEventBus("")
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
eventbus.TestAddHandler(t, bus)
}
func Test_Integration_RemoveHandler(t *testing.T) { //nolint:paralleltest // must not run in parallel
bus, _, err := newTestEventBus("app-id")
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
t.Run("happy path", func(t *testing.T) { //nolint:paralleltest // must not run in parallel
handler := mocks.NewEventHandler("handler-1")
queueName := fmt.Sprintf("%s_%s", "app-id", handler.HandlerType())
if err := bus.AddHandler(context.Background(), eh.MatchAll{}, handler); err != nil {
t.Fatal("there should be no error:", err)
}
if len(bus.RegisteredHandlers()) != 1 {
t.Fatal("there should be 1 registered handler")
}
if expectQueueConsumerCount(t, queueName, 1); err != nil {
t.Fatal("there should be 1 consumer on the queue")
}
if err := bus.RemoveHandler(eh.EventHandlerType("handler-1")); err != nil {
t.Fatal("there should be no error:", err)
}
if len(bus.RegisteredHandlers()) != 0 {
t.Fatal("there should be 0 registered handler")
}
if expectQueueConsumerCount(t, queueName, 0); err != nil {
t.Fatal("there should be 0 consumer on the queue")
}
})
t.Run("handler not registered", func(t *testing.T) { //nolint:paralleltest // must not run in parallel
err := bus.RemoveHandler(eh.EventHandlerType("handler-1"))
if !errors.Is(err, rabbitmq.ErrHandlerNotRegistered) {
t.Fatal("error should be: 'handler not registered'", err)
}
})
}
func Test_Integration_EventBus(t *testing.T) { //nolint:paralleltest // must not run in parallel
bus1, appID, err := newTestEventBus("", rabbitmq.WithHandlerConsumeAfterAdd(true))
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus1.Close() })
bus2, _, err := newTestEventBus(appID, rabbitmq.WithHandlerConsumeAfterAdd(true))
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus2.Close() })
t.Logf("using stream: %s_events", appID)
eventbus.AcceptanceTest(t, bus1, bus2, time.Second)
}
func Test_Integration_EventBusLoadTest(t *testing.T) { //nolint:paralleltest // must not run in parallel
bus, appID, err := newTestEventBus("", rabbitmq.WithHandlerConsumeAfterAdd(true))
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
t.Logf("using stream: %s_events", appID)
eventbus.LoadTest(t, bus)
}
func Test_Integration_ExternalConnections(t *testing.T) { //nolint:paralleltest // must not run in parallel
amqpURI := fmt.Sprintf("amqp://%s:%s@localhost:%d/", rmqUser, rmqPasswd, rmqPort)
waitTime := 5 * time.Second
t.Run("without external connections", func(t *testing.T) { //nolint:paralleltest // must not run in parallel
for range 3 {
bus, err := rabbitmq.NewEventBus(
amqpURI,
"test-app",
uuid.New().String(),
"eh-rabbitmq-test",
"rabbit",
)
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
}
time.Sleep(waitTime) // wait for connections to be fully established
if err := expectConnectionCount(t, 6); err != nil {
t.Fatal("there should be 6 connections")
}
})
t.Run("with external connections", func(t *testing.T) { //nolint:paralleltest // must not run in parallel
publishConn, err := clarimq.NewConnection(amqpURI)
if err != nil {
t.Fatal("there should be no error:", err)
}
consumeConn, err := clarimq.NewConnection(amqpURI)
if err != nil {
t.Fatal("there should be no error:", err)
}
for range 3 {
bus, err := rabbitmq.NewEventBus(
"it does not matter what the uri is",
"test-app",
uuid.New().String(),
"eh-rabbitmq-test",
"rabbit",
rabbitmq.WithClariMQConnections(publishConn, consumeConn),
)
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
}
time.Sleep(waitTime) // wait for connections to be fully established
if err := expectConnectionCount(t, 2); err != nil {
t.Fatal("there should be 2 connections")
}
})
}
func Benchmark_EventBus(b *testing.B) {
bus, appID, err := newTestEventBus("")
if err != nil {
b.Fatal("there should be no error:", err)
}
b.Cleanup(func() { bus.Close() })
b.Logf("using stream: %s_events", appID)
eventbus.Benchmark(b, bus)
}
func Test_Integration_MaxRetriesExceededHandler(t *testing.T) { //nolint:paralleltest // must not run in parallel
wg := new(sync.WaitGroup)
wg.Add(1)
// the max-retries-exceeded-handler
handler := rabbitmq.MaxRetriesExceededHandler(func(ctx context.Context, event eh.Event, errorMessage string) error {
ns := namespace.FromContext(ctx)
if !strings.Contains(errorMessage, errTestError.Error()) {
t.Fatal("error message should contain 'error-from-mock-event-handler'")
}
// assert namespace
if ns != "test-namespace" {
t.Fatal("namespace should be 'test-namespace'")
}
data, ok := event.Data().(*mocks.EventData)
if !ok {
t.Fatal("data should be of type mocks.EventData")
}
// assert event data
if data.Content != "event-content" {
t.Fatal("content should be 'event-content'")
}
// call done to finish test
wg.Done()
return nil
})
uri := fmt.Sprintf("amqp://%s:%s@localhost:%d/", rmqUser, rmqPasswd, rmqPort)
// create event bus with retry options
bus, err := rabbitmq.NewEventBus(uri, createAppID(), uuid.New().String(), "eh-rabbitmq-test", "rabbit",
rabbitmq.WithRetry(2, []time.Duration{time.Second}, handler),
)
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
eventHandler := new(mockErrorEventHandler)
// add mock event handler that always returns an error
if err := bus.AddHandler(context.Background(), eh.MatchAll{}, eventHandler); err != nil {
t.Fatal("there should be no error:", err)
}
if bus.StartHandling(); err != nil {
t.Fatal("there should be no error:", err)
}
// publish test event
if err := bus.PublishEvent(
namespace.NewContext(context.Background(), "test-namespace"),
eh.NewEvent(
mocks.EventType,
mocks.EventData{Content: "event-content"},
time.Now(),
)); err != nil {
t.Fatal("there should be no error:", err)
}
// wait for max-retries-exceeded-handler to be called
wg.Wait()
}
func Test_Integration_AddHandlerAfterHandlingAlreadyStarted(t *testing.T) { //nolint:paralleltest // must not run in parallel
bus, _, err := newTestEventBus("")
if err != nil {
t.Fatal("there should be no error:", err)
}
t.Cleanup(func() { bus.Close() })
// setup first handler
wg1 := new(sync.WaitGroup)
handler1 := &handler1{wg1}
wg1.Add(1)
ctx := context.Background()
if err = bus.SetupEventHandlers(ctx, handler1); err != nil {
t.Fatal("there should be no error:", err)
}
// start handling events
if err := bus.StartHandling(); err != nil {
t.Fatal("there should be no error:", err)
}
// setup second handler AFTER handling already started
// the handler should automatically be started
wg2 := new(sync.WaitGroup)
handler2 := &handler2{wg2}
wg2.Add(1)
if err = bus.SetupEventHandlers(ctx, handler2); err != nil {
t.Fatal("there should be no error:", err)
}
handlers := bus.RegisteredHandlers()
if len(handlers) != 2 {
t.Fatal("there should be 2 registered handlers")
}
RegisterEvents()
t.Cleanup(UnregisterEvents)
if err := bus.PublishEventWithOptions(
ctx,
eh.NewEvent(Handler1Event, new(HandlerEventData), time.Now()),
rabbitmq.WithPublishingTopic(handler1Topic),
); err != nil {
t.Fatal("there should be no error:", err)
}
if err := bus.PublishEventWithOptions(
ctx,
eh.NewEvent(Handler2Event, new(HandlerEventData), time.Now()),
rabbitmq.WithPublishingTopic(handler2Topic),
); err != nil {
t.Fatal("there should be no error:", err)
}
// waiting for handlers to handle events.
wg1.Wait()
wg2.Wait()
}
func newTestEventBus(appID string, options ...rabbitmq.Option) (*rabbitmq.EventBus, string, error) {
// Get a random app ID.
if appID == "" {
appID = createAppID()
}
uri := fmt.Sprintf("amqp://%s:%s@localhost:%d/", rmqUser, rmqPasswd, rmqPort)
bus, err := rabbitmq.NewEventBus(uri, appID, uuid.New().String(), "eh-rabbitmq-test", "rabbit", options...)
if err != nil {
return nil, "", fmt.Errorf("could not create event bus: %w", err)
}
return bus, appID, nil
}
func newTestEventHandlerMiddleware(wg *sync.WaitGroup) eh.EventHandlerMiddleware {
return func(h eh.EventHandler) eh.EventHandler {
return &testEventMiddlewareHandler{
inner: h,
wg: wg,
}
}
}
type testEventMiddlewareHandler struct {
inner eh.EventHandler
wg *sync.WaitGroup
}
func (m *testEventMiddlewareHandler) HandlerType() eh.EventHandlerType {
return m.inner.HandlerType()
}
func (m *testEventMiddlewareHandler) HandleEvent(ctx context.Context, event eh.Event) error {
if err := m.inner.HandleEvent(ctx, event); err != nil {
return err
}
m.wg.Done()
return nil
}
func createAppID() string {
bts := make([]byte, 8)
_, _ = rand.Read(bts)
return "app-" + hex.EncodeToString(bts)
}
func expectConnectionCount(t *testing.T, expected int) error {
t.Helper()
type rqmAPIResponse []struct{}
url := fmt.Sprintf("http://localhost:%d/api/connections", rmqManagementPort)
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatal("there should be no error:", err)
}
request.SetBasicAuth(rmqUser, rmqPasswd)
var apiResp rqmAPIResponse
do := func() bool {
return len(apiResp) == expected
}
return compare(t, request, &apiResp, do)
}
func expectQueueConsumerCount(t *testing.T, queueName string, expected int) error {
t.Helper()
type queue struct {
Name string `json:"name"`
Consumers int `json:"consumers"`
}
type rqmAPIResponse []queue
url := fmt.Sprintf("http://localhost:%d/api/queues", rmqManagementPort)
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatal("there should be no error:", err)
}
request.SetBasicAuth(rmqUser, rmqPasswd)
var apiResp rqmAPIResponse
do := func() bool {
for i := range apiResp {
return apiResp[i].Name == queueName && apiResp[i].Consumers == expected
}
return false
}
return compare(t, request, &apiResp, do)
}
func compare(t *testing.T, request *http.Request, result any, compareFn func() bool) error {
t.Helper()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
retries := 0
restClient := new(http.Client)
for range ticker.C {
retries++
pollRMQ(t, restClient, request, &result)
if compareFn() {
return nil
}
if retries >= 10 {
return errors.New("failed to compare after retry limit exceeded") //nolint:goerr113 // test code
}
}
return nil
}
func pollRMQ(t *testing.T, client *http.Client, request *http.Request, result any) {
t.Helper()
resp, err := client.Do(request)
if err != nil {
t.Fatal("there should be no error:", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatal("RabbitMQ management API should return status code 200")
}
buff := new(bytes.Buffer)
_, err = buff.ReadFrom(resp.Body)
if err != nil {
t.Fatal("there should be no error:", err)
}
err = json.Unmarshal(buff.Bytes(), &result)
if err != nil {
t.Fatal("there should be no error:", err)
}
}