private void doDeliverToListener(MailboxListener mailboxListener, Event event) { TimeMetric timer = metricFactory.timer(timerName(mailboxListener)); try (Closeable mdc = MDCBuilder.create() .addContext(EventBus.StructuredLoggingFields.EVENT_ID, event.getEventId()) .addContext(EventBus.StructuredLoggingFields.EVENT_CLASS, event.getClass()) .addContext(EventBus.StructuredLoggingFields.USER, event.getUser()) .addContext(EventBus.StructuredLoggingFields.LISTENER_CLASS, mailboxListener.getClass()) .build()) { mailboxListener.event(event); } catch (Exception e) { throw new RuntimeException(e); } finally { timer.stopAndPublish(); } }
@Test default void allRegisteredListenersShouldBeExecutedWhenARegisteredListenerFails() throws Exception { MailboxListener listener = newListener(); MailboxListener failingListener = mock(MailboxListener.class); when(failingListener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.SYNCHRONOUS); doThrow(new RuntimeException()).when(failingListener).event(any()); eventBus().register(failingListener, KEY_1); eventBus().register(listener, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); } }
@Override public ExecutionStages deliver(MailboxListener listener, Event event, DeliveryOption option) { Mono<Void> executionResult = deliverByOption(listener, event, option); return toExecutionStages(listener.getExecutionMode(), executionResult); }
static MailboxListener newListener() { MailboxListener listener = mock(MailboxListener.class); when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.SYNCHRONOUS); return listener; } }
@Test default void dispatchShouldNotifyLocalRegisteredListenerWithoutDelay() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, times(1)).event(any()); }
@Test default void dispatchShouldNotBlockAsynchronousListener() throws Exception { MailboxListener listener = newListener(); when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.ASYNCHRONOUS); CountDownLatch latch = new CountDownLatch(1); doAnswer(invocation -> { latch.await(); return null; }).when(listener).event(EVENT); assertTimeout(Duration.ofSeconds(2), () -> { eventBus().dispatch(EVENT, NO_KEYS).block(); latch.countDown(); }); }
@Test default void localDispatchedListenersShouldBeDispatchedWithoutDelay() throws Exception { MailboxListener mailboxListener1 = newListener(); MailboxListener mailboxListener2 = newListener(); eventBus().register(mailboxListener1, KEY_1); eventBus2().register(mailboxListener2, KEY_1); eventBus2().dispatch(EVENT, KEY_1).block(); verify(mailboxListener2, times(1)).event(any()); verify(mailboxListener1, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void allGroupListenersShouldBeExecutedWhenAGroupListenerFails() throws Exception { MailboxListener listener = newListener(); MailboxListener failingListener = mock(MailboxListener.class); when(failingListener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.SYNCHRONOUS); doThrow(new RuntimeException()).when(failingListener).event(any()); eventBus().register(failingListener, GROUP_A); eventBus().register(listener, new GroupB()); eventBus().dispatch(EVENT, NO_KEYS).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void dispatchShouldNotifyAllListenersRegisteredOnAKey() throws Exception { MailboxListener listener = newListener(); MailboxListener listener2 = newListener(); eventBus().register(listener, KEY_1); eventBus().register(listener2, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); verify(listener2, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void dispatchShouldNotifyOnlyRegisteredListener() throws Exception { MailboxListener listener = newListener(); MailboxListener listener2 = newListener(); eventBus().register(listener, KEY_1); eventBus().register(listener2, KEY_2); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); verify(listener2, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void dispatchShouldNotifyRegisteredListeners() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void allRegisteredListenersShouldBeDispatched() throws Exception { MailboxListener mailboxListener1 = newListener(); MailboxListener mailboxListener2 = newListener(); eventBus().register(mailboxListener1, KEY_1); eventBus2().register(mailboxListener2, KEY_1); eventBus2().dispatch(EVENT, KEY_1).block(); verify(mailboxListener1, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); verify(mailboxListener2, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void registerShouldNotDispatchPastEventsInDistributedContext() throws Exception { MailboxListener listener = newListener(); eventBus2().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); eventBus().register(listener, KEY_1); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void dispatchShouldNotThrowWhenAGroupListenerFails() throws Exception { MailboxListener listener = newListener(); doThrow(new RuntimeException()).when(listener).event(any()); eventBus().register(listener, GROUP_A); assertThatCode(() -> eventBus().dispatch(EVENT, NO_KEYS).block()) .doesNotThrowAnyException(); }
@Test default void registerShouldNotDispatchPastEvents() throws Exception { MailboxListener listener = newListener(); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); eventBus().register(listener, KEY_1); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void dispatchShouldNotThrowWhenARegisteredListenerFails() throws Exception { MailboxListener listener = newListener(); doThrow(new RuntimeException()).when(listener).event(any()); eventBus().register(listener, KEY_1); assertThatCode(() -> eventBus().dispatch(EVENT, NO_KEYS).block()) .doesNotThrowAnyException(); }
@Test default void dispatchShouldNotNotifyListenerRegisteredOnOtherKeys() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_2)).block(); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void dispatchShouldAcceptSeveralKeys() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1, KEY_2)).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void eachListenerGroupShouldReceiveEvents() throws Exception { MailboxListener listener = newListener(); MailboxListener listener2 = newListener(); eventBus().register(listener, GROUP_A); eventBus().register(listener2, new GroupB()); eventBus().dispatch(EVENT, NO_KEYS).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); verify(listener2, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void listenerGroupShouldReceiveEvents() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }