-
Notifications
You must be signed in to change notification settings - Fork 5
/
group_test.go
490 lines (375 loc) · 12.2 KB
/
group_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
package commander
// import (
// "context"
// "testing"
// "time"
// "github.com/jeroenrinzema/commander/dialects/mock"
// "github.com/jeroenrinzema/commander/internal/metadata"
// "github.com/jeroenrinzema/commander/internal/types"
// )
// type handler struct {
// messages chan interface{}
// }
// func (handle *handler) Handle(message *Message, writer Writer) {
// handle.messages <- message
// }
// // NewMockClient initializes a new in-memory mocking dialect, group and commander client used for testing
// func NewMockClient() (*Group, *Client) {
// dialect := mock.NewDialect()
// group := NewGroup(
// NewTopic("events", dialect, EventMessage, DefaultMode),
// NewTopic("commands", dialect, CommandMessage, DefaultMode),
// )
// client, _ := NewClient(group)
// return group, client
// }
// // TestProduceCommand tests if able to produce a command
// func TestProduceCommand(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// message := types.NewMessage("testing", 1, nil, nil)
// err := group.ProduceCommand(message)
// if err != nil {
// t.Error(err)
// }
// }
// // TestProduceEvent tests if able to produce a event
// func TestProduceEvent(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// parent := types.NewMessage("parent", 1, nil, nil)
// child := parent.NewMessage("tested", 1, nil, nil)
// err := group.ProduceEvent(child)
// if err != nil {
// t.Error(err)
// }
// }
// // TestProduceEventNoTopics tests if a error is returned when no topics for production are found
// func TestProduceEventNoTopics(t *testing.T) {
// group := NewGroup()
// client, _ := NewClient(group)
// defer client.Close()
// parent := types.NewMessage("parent", 1, nil, nil)
// event := parent.NewMessage("tested", 1, nil, nil)
// err := group.ProduceEvent(event)
// if err != ErrNoTopic {
// t.Fatal("no topic error is not thrown when expected")
// }
// }
// // TestAsyncCommand tests if plausible to create a async command
// func TestAsyncCommand(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// message := types.NewMessage("tested", 1, nil, nil)
// err := group.AsyncCommand(message)
// if err != nil {
// t.Error(err)
// }
// }
// // TestSyncCommand tests if able to send a sync command
// func TestSyncCommand(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// action := "testing"
// message := types.NewMessage(action, 1, nil, nil)
// message.EOS = true
// group.HandleFunc(CommandMessage, action, func(message *Message, writer Writer) {
// _, err := writer.Event(action, 1, nil, nil)
// if err != nil {
// t.Error(err)
// }
// })
// event, err := group.SyncCommand(message)
// event.Ack()
// if err != nil {
// t.Error(err)
// }
// parent, has := metadata.ParentIDFromContext(event.Ctx())
// if !has || parent != metadata.ParentID(message.ID) {
// t.Error("command id and parent do not match")
// }
// }
// func BenchmarkSyncCommand(b *testing.B) {
// group, client := NewMockClient()
// defer client.Close()
// action := "command"
// message := types.NewMessage(action, 1, nil, nil)
// message.EOS = true
// group.HandleFunc(CommandMessage, action, func(message *Message, writer Writer) {
// _, err := writer.Event(action, 1, nil, nil)
// if err != nil {
// b.Error(err)
// }
// })
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// event, err := group.SyncCommand(message)
// if err != nil {
// b.Error(err)
// }
// parent, has := metadata.ParentIDFromContext(event.Ctx())
// if !has || parent != metadata.ParentID(message.ID) {
// b.Error("command id and parent do not match")
// }
// event.Ack()
// }
// }
// // TestAwaitEvent tests if plausible to await a event
// func TestAwaitEvent(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// timeout := 100 * time.Millisecond
// parent := types.NewMessage("parent", 1, nil, nil)
// go func() {
// event := parent.NewMessage("tested", 1, nil, nil)
// err := group.ProduceEvent(event)
// if err != nil {
// t.Error(err)
// }
// }()
// messages, closer, err := group.NewConsumerWithDeadline(timeout, EventMessage)
// if err != nil {
// t.Fatal(err)
// return
// }
// defer closer()
// message, err := group.AwaitMessage(messages, metadata.ParentID(parent.ID))
// message.Ack()
// if err != nil {
// t.Fatal(err)
// return
// }
// }
// // TestAwaitEventAction tests if plausible to await a event action
// func TestAwaitEventAction(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// action := "process"
// timeout := 100 * time.Millisecond
// parent := types.NewMessage(action, 1, nil, nil)
// go func() {
// event := parent.NewMessage(action, 1, nil, nil)
// err := group.ProduceEvent(event)
// if err != nil {
// t.Error(err)
// }
// }()
// messages, closer, err := group.NewConsumerWithDeadline(timeout, EventMessage)
// if err != nil {
// t.Fatal(err)
// return
// }
// defer closer()
// message, err := group.AwaitEventWithAction(messages, metadata.ParentID(parent.ID), action)
// if err != nil {
// t.Error(err)
// }
// message.Ack()
// }
// // TestAwaitEventIgnoreParent tests if plausible to await a event with action
// func TestAwaitEventIgnoreParent(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// timeout := 100 * time.Millisecond
// parent := types.NewMessage("parent", 1, nil, nil)
// messages, closer, err := group.NewConsumerWithDeadline(timeout, EventMessage)
// if err != nil {
// t.Fatal(err)
// return
// }
// go func() {
// event := parent.NewMessage("ignore", 1, nil, nil)
// err := group.ProduceEvent(event)
// if err != nil {
// t.Error(err)
// }
// }()
// defer closer()
// message, err := group.AwaitEventWithAction(messages, metadata.ParentID(parent.ID), "process")
// if err != ErrTimeout {
// t.Error(err)
// }
// message.Ack()
// }
// // TestEventConsumer tests if events get consumed
// func TestEventConsumer(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// events, close, err := group.NewConsumer(EventMessage)
// if err != nil {
// panic(err)
// }
// defer close()
// parent := types.NewMessage("parent", 1, nil, nil)
// event := parent.NewMessage("tested", 1, nil, nil)
// group.ProduceEvent(event)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case event := <-events:
// event.Ack()
// case <-ctx.Done():
// t.Error("no message was consumed within the deadline")
// }
// }
// // TestCommandConsumer tests if commands get consumed
// func TestCommandConsumer(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// messages, close, err := group.NewConsumer(CommandMessage)
// if err != nil {
// panic(err)
// }
// defer close()
// command := types.NewMessage("testing", 1, nil, nil)
// group.ProduceCommand(command)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case message := <-messages:
// message.Ack()
// case <-ctx.Done():
// t.Error("no message was consumed within the deadline")
// }
// }
// // TestEventHandleFunc tests if a event handle func get's called
// func TestEventHandleFunc(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// action := "testing"
// delivered := make(chan *Message, 1)
// _, err := group.HandleFunc(EventMessage, action, func(message *Message, writer Writer) {
// delivered <- message
// })
// if err != nil {
// t.Error(err)
// }
// event := types.NewMessage(action, 1, nil, nil)
// group.ProduceEvent(event)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case <-delivered:
// case <-ctx.Done():
// t.Error("the events handle was not called within the deadline")
// }
// }
// // TestEventHandle testis if a event handle get's called
// func TestEventHandle(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// action := "testing"
// delivered := make(chan interface{}, 1)
// handle := &handler{delivered}
// group.Handle(EventMessage, action, handle)
// event := types.NewMessage(action, 1, nil, nil)
// group.ProduceEvent(event)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case <-delivered:
// case <-ctx.Done():
// t.Error("the events handle was not called within the deadline")
// }
// }
// // TestActionEventHandle tests if event actions are isolated
// func TestActionEventHandle(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// delivered := make(chan interface{}, 2)
// failure := make(chan interface{}, 2)
// group.Handle(EventMessage, "create", &handler{delivered})
// group.Handle(EventMessage, "delete", &handler{failure})
// event := types.NewMessage("create", 1, nil, nil)
// group.ProduceEvent(event)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case <-delivered:
// case <-failure:
// t.Error("the event action was not isolated")
// case <-ctx.Done():
// t.Error("the events handle was not called within the deadline")
// }
// }
// // TestActionCommandHandle tests if command actions are isolated
// func TestActionCommandHandle(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// delivered := make(chan interface{}, 1)
// failure := make(chan interface{}, 1)
// group.Handle(CommandMessage, "delete", &handler{failure})
// group.Handle(CommandMessage, "create", &handler{delivered})
// command := types.NewMessage("create", 1, nil, nil)
// group.ProduceCommand(command)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case <-delivered:
// case <-failure:
// t.Error("the event action was not isolated")
// case <-ctx.Done():
// t.Error("the events handle was not called within the deadline")
// }
// }
// // TestCommandTimestampPassed tests if the command timestamp is passed to the produced event
// func TestCommandTimestampPassed(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// var timestamp time.Time
// command := types.NewMessage("command", 1, nil, nil)
// delivered := make(chan interface{}, 1)
// group.HandleFunc(CommandMessage, "command", func(message *Message, writer Writer) {
// timestamp = message.Timestamp
// writer.Event("event", 1, nil, nil)
// })
// group.HandleFunc(EventMessage, "event", func(message *Message, writer Writer) {
// parent, has := metadata.ParentTimestampFromContext(message.Ctx())
// if !has {
// t.Error("timestamp is not set")
// }
// if time.Time(parent).Unix() != timestamp.Unix() {
// t.Error("the event timestamp does not match the command timestamp")
// }
// delivered <- message
// })
// group.ProduceCommand(command)
// deadline := time.Now().Add(500 * time.Millisecond)
// ctx, cancel := context.WithDeadline(context.Background(), deadline)
// defer cancel()
// select {
// case <-delivered:
// case <-ctx.Done():
// t.Error("the events handle was not called within the deadline")
// }
// }
// // TestMessageMarked tests if the command timestamp is passed to the produced event
// func TestMessageMarked(t *testing.T) {
// message := types.NewMessage("testing", 1, nil, nil)
// message.Reset()
// go func() {
// message.Ack()
// }()
// message.Finally()
// }
// // TestNewConsumer tests if able to create a new consumer
// func TestNewConsumer(t *testing.T) {
// group, client := NewMockClient()
// defer client.Close()
// var err error
// _, _, err = group.NewConsumer(EventMessage)
// if err != nil {
// t.Error(err)
// }
// unkown := types.MessageType(0)
// _, _, err = group.NewConsumer(unkown)
// if err != ErrNoTopic {
// t.Error("unexpected error", err)
// }
// }