diff --git a/event_bus.go b/event_bus.go index dedc7fd..bec4eb9 100644 --- a/event_bus.go +++ b/event_bus.go @@ -211,5 +211,7 @@ func (bus *EventBus) setUpPublish(callback *eventHandler, args ...interface{}) [ // WaitAsync waits for all async callbacks to complete func (bus *EventBus) WaitAsync() { + bus.lock.Lock() + defer bus.lock.Unlock() bus.wg.Wait() } diff --git a/event_bus_test.go b/event_bus_test.go index 0cdb579..55f4050 100644 --- a/event_bus_test.go +++ b/event_bus_test.go @@ -188,3 +188,18 @@ func TestSubscribeAsync(t *testing.T) { // t.Fail() //} } + +func TestSubscribeAsyncWithMultipleGoroutine(t *testing.T) { + for i := 0; i < 100; i++ { + bus := New() + bus.SubscribeAsync("topic", func() { + time.Sleep(time.Millisecond) + }, false) + bus.Publish("topic") + go func() { + time.Sleep(time.Millisecond) + bus.Publish("topic") + }() + bus.WaitAsync() + } +}