-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler_test.go
108 lines (80 loc) · 2.42 KB
/
eventhandler_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
package rabbitmq_test
import (
"context"
"sync"
"testing"
"time"
rabbitmq "github.com/Clarilab/eh-rabbitmq/v2"
eh "github.com/Clarilab/eventhorizon"
)
func Test_Integration_SetupEventHandlers(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() })
wg1 := new(sync.WaitGroup)
wg2 := new(sync.WaitGroup)
handler1 := &handler1{wg1}
handler2 := &handler2{wg2}
ctx := context.Background()
if err = bus.SetupEventHandlers(ctx, handler1, handler2); err != nil {
t.Fatal("there should be no error:", err)
}
wg1.Add(1)
wg2.Add(1)
RegisterEvents()
defer UnregisterEvents()
if err := bus.StartHandling(); 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")
}
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 Test_SetupEventHandlersWithMiddleware(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() })
wg := new(sync.WaitGroup)
handler := &handler1{wg}
middlewares := []eh.EventHandlerMiddleware{newTestEventHandlerMiddleware(wg)}
ctx := context.Background()
if err = bus.SetupEventHandlersWithMiddlewares(ctx, middlewares, handler); err != nil {
t.Fatal("there should be no error:", err)
}
if err := bus.StartHandling(); err != nil {
t.Fatal("there should be no error:", err)
}
RegisterEvents()
t.Cleanup(UnregisterEvents)
wg.Add(2) // 'wg.Done' expected to be called by the middleware and the actual handler
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)
}
wg.Wait()
}