-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.go
309 lines (241 loc) · 6.04 KB
/
e2e_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
package rcgo
import (
"context"
"encoding/json"
"fmt"
"sync"
"testing"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/suite"
)
// This test suite contains all tests that require a running server.
type E2ETestSuite struct {
suite.Suite
ctx context.Context
url string
lApp string
l *Listener
pApp string
p *Publisher
}
func (s *E2ETestSuite) SetupSuite() {
s.url = "amqp://user:password@localhost"
s.lApp = "testingListenerApp"
s.pApp = "testingPublisherApp"
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
s.ctx = ctx
_, err := amqp.Dial(s.url)
if err != nil {
s.FailNow("Failed to connect to RabbitMQ can not run e2e test")
return
}
pconfigs := NewPublisherDefaultConfigs(s.url)
pconfigs.ReplyTimeout = time.Second * 45
s.p = NewPublisher(pconfigs, s.pApp)
}
func TestE2ETestSuite(t *testing.T) {
if testing.Short() {
t.Skip("skipping e2e test in short mode")
}
suite.Run(t, new(E2ETestSuite))
}
func (s *E2ETestSuite) TearDownSuite() {
err := s.p.Stop()
if err != nil {
s.T().Logf("error: %s", err.Error())
}
}
func (s *E2ETestSuite) TestE2E_Cmds() {
cmdTyp := fmt.Sprintf("%s.%s", s.lApp, "cmd")
data := map[string]interface{}{
"data": "data",
}
calls := 0
lconfigs := NewListenerDefaultConfigs(s.url)
lconfigs.LogLevel = "disabled"
l := NewListener(lconfigs, s.lApp)
wg := sync.WaitGroup{}
wg.Add(2)
l.AddCommandHandler(
cmdTyp,
func(ctx context.Context, c *Cmd) error {
s.Len(c.Id, 36)
s.Equal(s.pApp, c.Source)
s.Equal(s.lApp, c.Target)
s.Equal(cmdTyp, c.Type)
s.WithinDuration(time.Now(), c.GenerationTime, time.Second*1)
s.Exactly(data, c.Data)
calls++
wg.Done()
return nil
})
err := l.Listen(s.ctx)
s.Nil(err)
// Provide sufficient time for the listener to start.
time.Sleep(time.Microsecond * 150)
err = s.p.SendCmd(s.ctx, s.lApp, cmdTyp, data)
s.Nil(err)
err = s.p.SendCmd(s.ctx, s.lApp, cmdTyp, data)
s.Nil(err)
wg.Wait()
s.Equal(2, calls)
err = l.Stop()
s.Nil(err)
}
func (s *E2ETestSuite) TestE2E_Events() {
eventTyp := "orderPlaced"
data := map[string]interface{}{
"data": "data",
}
calls := 0
// These ids are used to ensure that both
// handlers receive the same id.
var id1 string
var id2 string
lconfigs := NewListenerDefaultConfigs(s.url)
lconfigs.LogLevel = "disabled"
l := NewListener(lconfigs, s.lApp)
wg := sync.WaitGroup{}
wg.Add(2)
l.AddEventHandler(
eventTyp,
func(ctx context.Context, e *Event) error {
s.Len(e.Id, 36)
id1 = e.Id
s.Equal(s.pApp, e.Source)
s.Equal(eventTyp, e.Type)
s.WithinDuration(time.Now(), e.GenerationTime, time.Second*1)
s.Exactly(data, e.Data)
calls++
wg.Done()
return nil
})
err := l.Listen(s.ctx)
s.Nil(err)
// In this test, we generate an additional listener
// to verify the broadcast functionality of events
// to all apps that register for any event.
configsOtherListener := NewListenerDefaultConfigs(s.url)
configsOtherListener.LogLevel = "disabled"
otherListener := NewListener(configsOtherListener, "otherListener")
otherListener.AddEventHandler(
eventTyp,
func(ctx context.Context, e *Event) error {
s.Len(e.Id, 36)
id2 = e.Id
s.Equal(s.pApp, e.Source)
s.Equal(eventTyp, e.Type)
s.WithinDuration(time.Now(), e.GenerationTime, time.Second*1)
s.Exactly(data, e.Data)
calls++
wg.Done()
return nil
})
err = otherListener.Listen(s.ctx)
s.Nil(err)
// Provide sufficient time for the listener to start.
time.Sleep(time.Millisecond * 100)
err = s.p.PublishEvent(s.ctx, eventTyp, data)
s.Nil(err)
wg.Wait()
s.Exactly(id1, id2)
s.Equal(2, calls)
err = l.Stop()
s.Nil(err)
err = otherListener.Stop()
s.Nil(err)
}
func (s *E2ETestSuite) TestE2E_Queries() {
queryTyp := fmt.Sprintf("%s.%s", s.lApp, "query")
data := map[string]interface{}{
"data": "data",
}
dataRes := map[string]interface{}{
"data": "data",
}
calls := 0
lconfigs := NewListenerDefaultConfigs(s.url)
lconfigs.LogLevel = "disabled"
l := NewListener(lconfigs, s.lApp)
wg := sync.WaitGroup{}
wg.Add(2)
l.AddQueryHandler(
queryTyp,
func(ctx context.Context, q *Query) (interface{}, error) {
s.Equal(queryTyp, q.Target)
s.Equal(queryTyp, q.Type)
s.WithinDuration(time.Now(), q.GenerationTime, time.Second*1)
s.Exactly(data, q.Data)
calls++
wg.Done()
return dataRes, nil
})
err := l.Listen(s.ctx)
s.Nil(err)
// Provide sufficient time for the listener to start.
time.Sleep(time.Millisecond * 100)
res1, err := s.p.RequestReply(s.ctx, s.lApp, queryTyp, data)
s.Nil(err)
if err != nil {
// unblock if err
wg.Done()
}
res2, err := s.p.RequestReply(s.ctx, s.lApp, queryTyp, data)
s.Nil(err)
if err != nil {
// unblock if err
wg.Done()
}
wg.Wait()
dataResJson, _ := json.Marshal(dataRes)
s.Exactly(dataResJson, res1)
dataResJson, _ = json.Marshal(dataRes)
s.Exactly(dataResJson, res2)
s.Equal(2, calls)
err = l.Stop()
s.Nil(err)
}
func (s *E2ETestSuite) TestE2E_Close() {
// create a new listener to this test
lconfigs := NewListenerDefaultConfigs(s.url)
lconfigs.LogLevel = "disabled"
s.l = NewListener(lconfigs, s.lApp)
go s.l.Listen(s.ctx)
// Provide sufficient time for the listener to start.
time.Sleep(time.Millisecond * 100)
err := s.l.Stop()
s.Nil(err)
}
func (s *E2ETestSuite) TestPublisher_StartAfterStop() {
s.p.Start(context.Background())
// Stop the publisher
err := s.p.Stop()
s.Nil(err)
// Try to start the publisher again
err = s.p.Start(context.Background())
s.Nil(err)
// Stop the publisher
err = s.p.Stop()
s.Nil(err)
// Try to start the publisher again
err = s.p.Start(context.Background())
s.Nil(err)
}
func (s *E2ETestSuite) TestListener_StartAfterStop() {
lconfigs := NewListenerDefaultConfigs(s.url)
lconfigs.LogLevel = "disabled"
l := NewListener(lconfigs, s.lApp)
// Stop the listener
err := l.Stop()
s.Nil(err)
// Try to start the listener again
err = l.Listen(context.Background())
s.Nil(err)
// Stop the listener
err = l.Stop()
s.Nil(err)
// Try to start the listener again
err = l.Listen(context.Background())
s.Nil(err)
}