@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 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 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 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()); }
@Test default void crossEventBusRegistrationShouldBeAllowed() throws Exception { MailboxListener mailboxListener = newListener(); eventBus().register(mailboxListener, KEY_1); eventBus2().dispatch(EVENT, KEY_1).block(); verify(mailboxListener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void allGroupListenersShouldBeExecutedWhenGenericGroups() throws Exception { MailboxListener listener1 = newListener(); MailboxListener listener2 = newListener(); eventBus().register(listener1, new GenericGroup("a")); eventBus().register(listener2, new GenericGroup("b")); eventBus().dispatch(EVENT, NO_KEYS).block(); verify(listener1, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); verify(listener2, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); } }
@Test default void unregisteredDistantListenersShouldNotBeNotified() throws Exception { MailboxListener mailboxListener = newListener(); eventBus().register(mailboxListener, KEY_1).unregister(); eventBus2().dispatch(EVENT, ImmutableSet.of(KEY_1)).block(); verify(mailboxListener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void registerShouldNotDispatchPastEventsForGroups() throws Exception { MailboxListener listener = newListener(); eventBus().dispatch(EVENT, NO_KEYS).block(); eventBus().register(listener, GROUP_A); verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never()) .event(any()); }
@Test default void dispatchShouldCallSynchronousListener() 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()); }
@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 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 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 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 registerShouldAcceptAlreadyUnregisteredGroups() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, GROUP_A).unregister(); eventBus().register(listener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any()); }
@Test default void groupListenersShouldNotReceiveNoopEvents() throws Exception { MailboxListener listener = newListener(); eventBus().register(listener, GROUP_A); 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, NO_KEYS).block(); verify(listener, 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()); }