From 224961138fc54a027773cf0ca79c2ff2a91b24ce Mon Sep 17 00:00:00 2001 From: Max Ekman Date: Fri, 16 Oct 2020 13:55:06 +0200 Subject: [PATCH] Fix handler type of async event handler middleware --- middleware/eventhandler/async/eventhandler.go | 26 ++++++++++++------- .../eventhandler/async/eventhandler_test.go | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/middleware/eventhandler/async/eventhandler.go b/middleware/eventhandler/async/eventhandler.go index 89fcea32..80aa5652 100644 --- a/middleware/eventhandler/async/eventhandler.go +++ b/middleware/eventhandler/async/eventhandler.go @@ -26,18 +26,26 @@ import ( func NewMiddleware() (eh.EventHandlerMiddleware, chan Error) { errCh := make(chan Error, 20) return eh.EventHandlerMiddleware(func(h eh.EventHandler) eh.EventHandler { - return eh.EventHandlerFunc(func(ctx context.Context, event eh.Event) error { - go func() { - if err := h.HandleEvent(ctx, event); err != nil { - // Always try to deliver errors. - errCh <- Error{err, ctx, event} - } - }() - return nil - }) + return &eventHandler{h, errCh} }), errCh } +type eventHandler struct { + eh.EventHandler + errCh chan Error +} + +// HandleEvent implements the HandleEvent method of the EventHandler. +func (h *eventHandler) HandleEvent(ctx context.Context, event eh.Event) error { + go func() { + if err := h.EventHandler.HandleEvent(ctx, event); err != nil { + // Always try to deliver errors. + h.errCh <- Error{err, ctx, event} + } + }() + return nil +} + // Error is an async error containing the error and the event. type Error struct { Err error diff --git a/middleware/eventhandler/async/eventhandler_test.go b/middleware/eventhandler/async/eventhandler_test.go index fce51585..dee9b7b2 100644 --- a/middleware/eventhandler/async/eventhandler_test.go +++ b/middleware/eventhandler/async/eventhandler_test.go @@ -46,7 +46,7 @@ func TestEventHandler(t *testing.T) { } inner.RLock() if !reflect.DeepEqual(inner.Events, []eh.Event{event}) { - t.Error("the event shoud have been handeled:", inner.Events) + t.Error("the event should have been handeled:", inner.Events) } inner.RUnlock()