@Test default void exceedingMaxRetriesShouldStopConsumingFailedEvent() throws Exception { ThrowingListener throwingListener = throwingListener(); eventBus().register(throwingListener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); TimeUnit.SECONDS.sleep(5); int numberOfCallsAfterExceedMaxRetries = throwingListener.timeElapsed.size(); TimeUnit.SECONDS.sleep(5); assertThat(throwingListener.timeElapsed.size()) .isEqualTo(numberOfCallsAfterExceedMaxRetries); }
default Registration register(MailboxListener.GroupMailboxListener groupMailboxListener) { return register(groupMailboxListener, groupMailboxListener.getDefaultGroup()); } }
default Mono<Void> dispatch(Event event, RegistrationKey key) { return dispatch(event, ImmutableSet.of(key)); }
@Test default void registerShouldThrowWhenAGroupIsAlreadyUsed() { MailboxListener listener = newListener(); MailboxListener listener2 = newListener(); eventBus().register(listener, GROUP_A); assertThatThrownBy(() -> eventBus().register(listener2, GROUP_A)) .isInstanceOf(GroupAlreadyRegistered.class); }
@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 retryingIsNotAppliedForKeyRegistrations() { EventCollector eventCollector = eventCollector(); doThrow(new RuntimeException()) .when(eventCollector).event(EVENT); eventBus().register(eventCollector, KEY_1); eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); assertThat(eventCollector.getEvents()) .isEmpty(); }
MailboxPath path = MailboxPath.forUser(username, "INBOX"); MailboxId mailboxId = mailboxManager.createMailbox(path, session).get(); retrieveEventBus(mailboxManager).register( event -> { MessageUid u = ((MailboxListener.Added) event).getUids().iterator().next();
@Test default void failingGroupListenersShouldNotAbortGroupDelivery() { EventBusTestFixture.MailboxListenerCountingSuccessfulExecution listener = new EventBusTestFixture.EventMatcherThrowingListener(ImmutableSet.of(EVENT)); eventBus().register(listener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); eventBus().dispatch(EVENT_2, NO_KEYS).block(); WAIT_CONDITION .until(() -> assertThat(listener.numberOfEventCalls()).isEqualTo(1)); }
@Test default void unregisterShouldBeIdempotentForGroups() { MailboxListener listener = newListener(); Registration registration = eventBus().register(listener, GROUP_A); registration.unregister(); assertThatCode(registration::unregister) .doesNotThrowAnyException(); }
@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 unregisterShouldBeIdempotentForKeyRegistrations() { MailboxListener listener = newListener(); Registration registration = eventBus().register(listener, KEY_1); registration.unregister(); assertThatCode(registration::unregister) .doesNotThrowAnyException(); }
@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 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 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 listenersShouldBeAbleToDispatch() { AtomicBoolean successfulRetry = new AtomicBoolean(false); MailboxListener listener = event -> { if (event.getEventId().equals(EVENT_ID)) { eventBus().dispatch(EVENT_2, NO_KEYS).block(); successfulRetry.set(true); } }; eventBus().register(listener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); WAIT_CONDITION.until(successfulRetry::get); }
@Test default void groupListenersShouldBeExecutedOnceInAControlledEnvironment() throws Exception { MailboxListener mailboxListener = newListener(); eventBus().register(mailboxListener, GROUP_A); eventBus2().register(mailboxListener, GROUP_A); eventBus2().dispatch(EVENT, NO_KEYS).block(); verify(mailboxListener, 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 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()); }