-
Notifications
You must be signed in to change notification settings - Fork 18
/
event_bus_test.go
160 lines (127 loc) · 3.77 KB
/
event_bus_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
package channelx_test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/Ksloveyuan/channelx"
channelx_mock "github.com/Ksloveyuan/channelx/mocks"
"github.com/golang/mock/gomock"
)
func TestEventBus(t *testing.T) {
const (
fakeEventID channelx.EventID = 1
)
ctl := gomock.NewController(t)
defer ctl.Finish()
logger := channelx.NewConsoleLogger()
t.Run("happy flow", func(t *testing.T) {
eventBus := channelx.NewEventBus(logger, 4,4,2, time.Second, 5*time.Second)
eventBus.Start()
fakeEvent := channelx_mock.NewMockEvent(ctl)
fakeEvent.EXPECT().ID().Return(fakeEventID).AnyTimes()
waitGroup := sync.WaitGroup{}
fakeHandler := channelx_mock.NewMockEventHandler(ctl)
fakeHandler.EXPECT().OnEvent(gomock.Any(), fakeEvent).Do(func(_,_ interface{}) {
waitGroup.Done()
})
eventBus.Subscribe(fakeEventID, fakeHandler)
waitGroup.Add(1)
eventBus.Publish(fakeEvent)
waitGroup.Wait()
eventBus.Stop()
})
t.Run("timout", func(t *testing.T) {
eventBus := channelx.NewEventBus(logger, 4,4,0, time.Second, time.Second)
eventBus.Start()
fakeError := fmt.Errorf("timeout")
fakeEvent := channelx_mock.NewMockEvent(ctl)
fakeEvent.EXPECT().ID().Return(fakeEventID).AnyTimes()
waitGroup := sync.WaitGroup{}
fakeHandler := channelx_mock.NewMockEventHandler(ctl)
fakeHandler.EXPECT().CanAutoRetry(fakeError).Return(true)
fakeHandler.EXPECT().OnEvent(gomock.Any(), fakeEvent).DoAndReturn(func(ctx context.Context, event channelx.Event) error {
select {
case <-ctx.Done():
waitGroup.Done()
logger.Infof("timeout")
return fakeError
case <-time.After(2 * time.Second):
return nil
}
})
eventBus.Subscribe(fakeEventID, fakeHandler)
waitGroup.Add(1)
eventBus.Publish(fakeEvent)
waitGroup.Wait()
eventBus.Stop()
})
t.Run("auto retry", func(t *testing.T) {
eventBus := channelx.NewEventBus(logger, 4,4,2, time.Second, 5 * time.Second)
eventBus.Start()
fakeError := fmt.Errorf("fake error")
fakeEvent := channelx_mock.NewMockEvent(ctl)
fakeEvent.EXPECT().ID().Return(fakeEventID).AnyTimes()
waitGroup := sync.WaitGroup{}
fakeHandler := channelx_mock.NewMockEventHandler(ctl)
fakeHandler.EXPECT().CanAutoRetry(fakeError).Return(true).Times(3)
fakeHandler.EXPECT().OnEvent(gomock.Any(), fakeEvent).DoAndReturn(func(_,_ interface{}) error {
waitGroup.Done()
return fakeError
}).Times(3)
eventBus.Subscribe(fakeEventID, fakeHandler)
waitGroup.Add(3)
resultChan := eventBus.Publish(fakeEvent)
waitGroup.Wait()
result := <-resultChan
if result.Err != fakeError {
t.Fail()
}
eventBus.Stop()
})
}
func TestEventBus_Example(t *testing.T) {
logger := channelx.NewConsoleLogger()
eventBus := channelx.NewEventBus(logger, 4,4,2, time.Second, 5 * time.Second)
eventBus.Start()
handler := NewExampleHandler(logger)
eventBus.Subscribe(ExampleEventID, handler)
eventBus.Publish(NewExampleEvent())
eventBus.Stop()
}
const ExampleEventID channelx.EventID = 1
type ExampleEvent struct {
id channelx.EventID
}
func NewExampleEvent() ExampleEvent {
return ExampleEvent{id:ExampleEventID}
}
func (evt ExampleEvent) ID() channelx.EventID {
return evt.id
}
type ExampleHandler struct {
logger channelx.Logger
}
func NewExampleHandler(logger channelx.Logger) *ExampleHandler {
return &ExampleHandler{
logger: logger,
}
}
func (h ExampleHandler) Logger() channelx.Logger{
return h.logger
}
func (h ExampleHandler) CanAutoRetry(err error) bool {
return false
}
func (h ExampleHandler) OnEvent(ctx context.Context, event channelx.Event) error {
if event.ID() != ExampleEventID {
return fmt.Errorf("subscribe wrong event(%d)", event.ID())
}
_, ok := event.(ExampleEvent)
if !ok {
return fmt.Errorf("failed to convert received event to ExampleEvent")
}
h.Logger().Infof("event handled")
return nil
}