Skip to content

Commit

Permalink
MAILBOX-263 improve async test stability
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1724018 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Jan 11, 2016
1 parent 4671b33 commit 697853b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 68 deletions.
Expand Up @@ -19,12 +19,13 @@


package org.apache.james.mailbox.store.event; 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.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;


import java.util.concurrent.TimeUnit;

import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.MailboxListener;
import org.apache.james.mailbox.mock.MockMailboxSession; import org.apache.james.mailbox.mock.MockMailboxSession;
import org.junit.After; import org.junit.After;
Expand All @@ -33,6 +34,7 @@


public class AsynchronousEventDeliveryTest { public class AsynchronousEventDeliveryTest {


private static final int ONE_MINUTE = (int) TimeUnit.MINUTES.toMillis(1);
private MailboxListener mailboxListener; private MailboxListener mailboxListener;
private AsynchronousEventDelivery asynchronousEventDelivery; private AsynchronousEventDelivery asynchronousEventDelivery;


Expand All @@ -51,29 +53,25 @@ public void tearDown() {
public void deliverShouldWork() throws Exception { public void deliverShouldWork() throws Exception {
MailboxListener.Event event = new MailboxListener.Event(null, null) {}; MailboxListener.Event event = new MailboxListener.Event(null, null) {};
asynchronousEventDelivery.deliver(mailboxListener, event); asynchronousEventDelivery.deliver(mailboxListener, event);
Thread.sleep(100); verify(mailboxListener, timeout(ONE_MINUTE)).event(event);
verify(mailboxListener).event(event);
} }


@Test @Test
public void deliverShouldNotPropagateException() throws Exception { public void deliverShouldNotPropagateException() throws Exception {
MailboxListener.Event event = new MailboxListener.Event(new MockMailboxSession("test"), null) {}; MailboxListener.Event event = new MailboxListener.Event(new MockMailboxSession("test"), null) {};
doThrow(new RuntimeException()).when(mailboxListener).event(event); doThrow(new RuntimeException()).when(mailboxListener).event(event);
asynchronousEventDelivery.deliver(mailboxListener, event); asynchronousEventDelivery.deliver(mailboxListener, event);
Thread.sleep(100); verify(mailboxListener, timeout(ONE_MINUTE)).event(event);
verify(mailboxListener).event(event);
} }


@Test @Test
public void deliverShouldWorkWhenThePoolIsFull() throws Exception { public void deliverShouldWorkWhenThePoolIsFull() throws Exception {
MailboxListener.Event event = new MailboxListener.Event(new MockMailboxSession("test"), null) {}; MailboxListener.Event event = new MailboxListener.Event(new MockMailboxSession("test"), null) {};
WaitMailboxListener waitMailboxListener = new WaitMailboxListener(); int operationCount = 10;
long operationCount = 10;
for (int i = 0; i < operationCount; i++) { for (int i = 0; i < operationCount; i++) {
asynchronousEventDelivery.deliver(waitMailboxListener, event); asynchronousEventDelivery.deliver(mailboxListener, event);
} }
Thread.sleep(2000); verify(mailboxListener, timeout(ONE_MINUTE).times(operationCount)).event(event);
assertThat(waitMailboxListener.getInvocationCount().get()).isEqualTo(operationCount);
} }


} }
Expand Up @@ -19,21 +19,33 @@


package org.apache.james.mailbox.store.event; package org.apache.james.mailbox.store.event;


import static org.assertj.core.api.Assertions.assertThat;

import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.MailboxListener;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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 { 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 MixedEventDelivery mixedEventDelivery;
private MailboxListener listener;


@Before @Before
public void setUp() { public void setUp() {
listener = mock(MailboxListener.class);
mixedEventDelivery = new MixedEventDelivery(new AsynchronousEventDelivery(2), new SynchronousEventDelivery()); mixedEventDelivery = new MixedEventDelivery(new AsynchronousEventDelivery(2), new SynchronousEventDelivery());
} }


Expand All @@ -44,20 +56,34 @@ public void tearDown() {


@Test @Test
public void deliverShouldWorkOnSynchronousListeners() throws Exception { 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) {}; MailboxListener.Event event = new MailboxListener.Event(null, null) {};
mixedEventDelivery.deliver(listener, event); mixedEventDelivery.deliver(listener, event);
assertThat(listener.getInvocationCount().get()).isEqualTo(1); verify(listener).event(event);
} }


@Test @Test
public void deliverShouldWorkOnAsynchronousListeners() throws Exception { public void deliverShouldEventuallyDeliverOnAsynchronousListeners() throws Exception {
WaitMailboxListener listener = new WaitMailboxListener(MailboxListener.ExecutionMode.ASYNCHRONOUS); 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) {}; MailboxListener.Event event = new MailboxListener.Event(null, null) {};
when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.ASYNCHRONOUS);
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
latch.await();
return null;
}
}).when(listener).event(event);
mixedEventDelivery.deliver(listener, event); mixedEventDelivery.deliver(listener, event);
assertThat(listener.getInvocationCount().get()).isEqualTo(0); latch.countDown();
Thread.sleep(200);
assertThat(listener.getInvocationCount().get()).isEqualTo(1);
} }


} }

This file was deleted.

0 comments on commit 697853b

Please sign in to comment.