Skip to content

Commit 9d5e2da

Browse files
committed
allow multiple handlers for the same event within one event group
1 parent 43308fb commit 9d5e2da

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

components/cqrs/event_processor_group.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ func NewEventGroupProcessorWithConfig(router *message.Router, config EventGroupP
133133
//
134134
// Compared to AddHandlers, AddHandlersGroup allows to have multiple handlers that share the same subscriber instance.
135135
//
136+
// It's allowed to have multiple handlers for the same event type in one group, but we recommend to not do that.
137+
// Please keep in mind that those handlers will be processed within the same message.
138+
// If first handler succeeds and the second fails, the message will be re-delivered and the first will be re-executed.
139+
//
136140
// Handlers group needs to be unique within the EventProcessor instance.
137141
//
138142
// Handler group name is used as handler's name in router.
@@ -203,6 +207,8 @@ func (p EventGroupProcessor) routerHandlerGroupFunc(handlers []GroupEventHandler
203207
return func(msg *message.Message) error {
204208
messageEventName := p.config.Marshaler.NameFromMessage(msg)
205209

210+
handledAnyEvent := false
211+
206212
for _, handler := range handlers {
207213
initEvent := handler.NewEvent()
208214
expectedEventName := p.config.Marshaler.Name(initEvent)
@@ -249,6 +255,9 @@ func (p EventGroupProcessor) routerHandlerGroupFunc(handlers []GroupEventHandler
249255
return err
250256
}
251257

258+
handledAnyEvent = true
259+
}
260+
if handledAnyEvent {
252261
return nil
253262
}
254263

components/cqrs/event_processor_group_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,27 @@ func TestEventProcessor_handler_group(t *testing.T) {
300300
},
301301
}
302302

303-
handler1Calls := 0
304-
handler2Calls := 0
303+
var handlersCalls []int
305304

306305
handlers := []cqrs.GroupEventHandler{
307306
cqrs.NewGroupEventHandler(func(ctx context.Context, event *TestEvent) error {
308307
assert.EqualValues(t, event1, event)
309308

310-
handler1Calls++
309+
handlersCalls = append(handlersCalls, 1)
311310

312311
return nil
313312
}),
314313
cqrs.NewGroupEventHandler(func(ctx context.Context, event *AnotherTestEvent) error {
315314
assert.EqualValues(t, event2, event)
316315

317-
handler2Calls++
316+
handlersCalls = append(handlersCalls, 2)
317+
318+
return nil
319+
}),
320+
cqrs.NewGroupEventHandler(func(ctx context.Context, event *AnotherTestEvent) error {
321+
assert.EqualValues(t, event2, event)
322+
323+
handlersCalls = append(handlersCalls, 3)
318324

319325
return nil
320326
}),
@@ -382,8 +388,7 @@ func TestEventProcessor_handler_group(t *testing.T) {
382388
t.Fatal("message 2 not acked")
383389
}
384390

385-
assert.Equal(t, 1, handler1Calls)
386-
assert.Equal(t, 1, handler2Calls)
391+
assert.Equal(t, []int{1, 2, 3}, handlersCalls)
387392
}
388393

389394
func TestEventGroupProcessor_original_msg_set_to_ctx(t *testing.T) {

0 commit comments

Comments
 (0)