@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 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()); } }
@Test default void failingRegisteredListenersShouldNotAbortRegisteredDelivery() { EventBusTestFixture.MailboxListenerCountingSuccessfulExecution listener = new EventBusTestFixture.EventMatcherThrowingListener(ImmutableSet.of(EVENT)); eventBus().register(listener, KEY_1); eventBus().dispatch(EVENT, KEY_1).block(); eventBus().dispatch(EVENT_2, KEY_1).block(); WAIT_CONDITION .until(() -> assertThat(listener.numberOfEventCalls()).isEqualTo(1)); }
@Test default void dispatchShouldNotNotifyUnregisteredListener() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1).unregister(); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void callingAllUnregisterMethodShouldUnregisterTheListener() throws Exception { MailboxListener listener = newListener(); Registration registration = eventBus().register(listener, KEY_1); eventBus().register(listener, KEY_1).unregister(); registration.unregister(); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@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 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 registerShouldAllowDuplicatedRegistration() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, 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()); }
@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 registeredListenersShouldNotReceiveNoopEvents() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); MailboxListener.Added noopEvent = new MailboxListener.Added(MailboxSession.SessionId.of(18), User.fromUsername("bob"), MailboxPath.forUser("bob", "mailbox"), TestId.of(58), ImmutableSortedMap.of(), Event.EventId.random()); eventBus().dispatch(noopEvent, KEY_1).block(); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void dispatchShouldCallListenerOnceWhenSeveralKeysMatching() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().register(listener, KEY_2); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1, KEY_2)).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 unregisterShouldHaveNotNotifyWhenCalledOnDifferentKeys() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().register(listener, KEY_2).unregister(); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void unregisterShouldRemoveDoubleRegisteredListener() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, KEY_1); eventBus().register(listener, KEY_1).unregister(); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void unregisterShouldBeIdempotentForKeyRegistrations() { MailboxListener listener = newListener(); Registration registration = eventBus().register(listener, KEY_1); registration.unregister(); assertThatCode(registration::unregister) .doesNotThrowAnyException(); }