diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java index 191ad791cb2..0b138c497ff 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java @@ -19,12 +19,13 @@ package org.apache.james.mailbox.store.event; -import static org.assertj.core.api.Assertions.assertThat; - import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; +import java.util.concurrent.TimeUnit; + import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.mock.MockMailboxSession; import org.junit.After; @@ -33,6 +34,7 @@ public class AsynchronousEventDeliveryTest { + private static final int ONE_MINUTE = (int) TimeUnit.MINUTES.toMillis(1); private MailboxListener mailboxListener; private AsynchronousEventDelivery asynchronousEventDelivery; @@ -51,8 +53,7 @@ public void tearDown() { public void deliverShouldWork() throws Exception { MailboxListener.Event event = new MailboxListener.Event(null, null) {}; asynchronousEventDelivery.deliver(mailboxListener, event); - Thread.sleep(100); - verify(mailboxListener).event(event); + verify(mailboxListener, timeout(ONE_MINUTE)).event(event); } @Test @@ -60,20 +61,17 @@ public void deliverShouldNotPropagateException() throws Exception { MailboxListener.Event event = new MailboxListener.Event(new MockMailboxSession("test"), null) {}; doThrow(new RuntimeException()).when(mailboxListener).event(event); asynchronousEventDelivery.deliver(mailboxListener, event); - Thread.sleep(100); - verify(mailboxListener).event(event); + verify(mailboxListener, timeout(ONE_MINUTE)).event(event); } @Test public void deliverShouldWorkWhenThePoolIsFull() throws Exception { MailboxListener.Event event = new MailboxListener.Event(new MockMailboxSession("test"), null) {}; - WaitMailboxListener waitMailboxListener = new WaitMailboxListener(); - long operationCount = 10; + int operationCount = 10; for (int i = 0; i < operationCount; i++) { - asynchronousEventDelivery.deliver(waitMailboxListener, event); + asynchronousEventDelivery.deliver(mailboxListener, event); } - Thread.sleep(2000); - assertThat(waitMailboxListener.getInvocationCount().get()).isEqualTo(operationCount); + verify(mailboxListener, timeout(ONE_MINUTE).times(operationCount)).event(event); } } diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java index e19e514e69e..76301385fbb 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java @@ -19,21 +19,33 @@ package org.apache.james.mailbox.store.event; -import static org.assertj.core.api.Assertions.assertThat; - import org.apache.james.mailbox.MailboxListener; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class MixedEventDeliveryTest { + private static final int DELIVERY_DELAY = (int) TimeUnit.MILLISECONDS.toMillis(100); + private static final long ONE_MINUTE = 60000; private MixedEventDelivery mixedEventDelivery; + private MailboxListener listener; @Before public void setUp() { + listener = mock(MailboxListener.class); mixedEventDelivery = new MixedEventDelivery(new AsynchronousEventDelivery(2), new SynchronousEventDelivery()); } @@ -44,20 +56,34 @@ public void tearDown() { @Test public void deliverShouldWorkOnSynchronousListeners() throws Exception { - WaitMailboxListener listener = new WaitMailboxListener(MailboxListener.ExecutionMode.SYNCHRONOUS); + when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.SYNCHRONOUS); MailboxListener.Event event = new MailboxListener.Event(null, null) {}; mixedEventDelivery.deliver(listener, event); - assertThat(listener.getInvocationCount().get()).isEqualTo(1); + verify(listener).event(event); } @Test - public void deliverShouldWorkOnAsynchronousListeners() throws Exception { - WaitMailboxListener listener = new WaitMailboxListener(MailboxListener.ExecutionMode.ASYNCHRONOUS); + public void deliverShouldEventuallyDeliverOnAsynchronousListeners() throws Exception { + MailboxListener.Event event = new MailboxListener.Event(null, null) {}; + when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.ASYNCHRONOUS); + mixedEventDelivery.deliver(listener, event); + verify(listener, timeout(DELIVERY_DELAY * 10)).event(event); + } + + @Test(timeout = ONE_MINUTE) + public void deliverShouldNotBlockOnAsynchronousListeners() throws Exception { MailboxListener.Event event = new MailboxListener.Event(null, null) {}; + when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.ASYNCHRONOUS); + final CountDownLatch latch = new CountDownLatch(1); + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + latch.await(); + return null; + } + }).when(listener).event(event); mixedEventDelivery.deliver(listener, event); - assertThat(listener.getInvocationCount().get()).isEqualTo(0); - Thread.sleep(200); - assertThat(listener.getInvocationCount().get()).isEqualTo(1); + latch.countDown(); } } diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/WaitMailboxListener.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/WaitMailboxListener.java deleted file mode 100644 index 370720dc7be..00000000000 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/WaitMailboxListener.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.james.mailbox.store.event; - -import org.apache.james.mailbox.MailboxListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.atomic.AtomicLong; - -public class WaitMailboxListener implements MailboxListener { - - private static final Logger LOGGER = LoggerFactory.getLogger(WaitMailboxListener.class); - - private final AtomicLong invocationCount; - private final ExecutionMode executionMode; - - public WaitMailboxListener(ExecutionMode executionMode) { - this.invocationCount = new AtomicLong(0); - this.executionMode = executionMode; - } - - public WaitMailboxListener() { - this(ExecutionMode.ASYNCHRONOUS); - } - - public AtomicLong getInvocationCount() { - return invocationCount; - } - - @Override - public ListenerType getType() { - return ListenerType.MAILBOX; - } - - @Override - public ExecutionMode getExecutionMode() { - return executionMode; - } - - @Override - public void event(Event event) { - try { - Thread.sleep(100); - invocationCount.incrementAndGet(); - } catch (InterruptedException e) { - LOGGER.info("interrupted", e); - } - } -}