Author: matthieu
Date: Mon Jan 11 13:13:50 2016
New Revision: 1724018

URL: http://svn.apache.org/viewvc?rev=1724018&view=rev
Log:
MAILBOX-263 improve async test stability

Removed:
    
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/WaitMailboxListener.java
Modified:
    
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java
    
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java

Modified: 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java?rev=1724018&r1=1724017&r2=1724018&view=diff
==============================================================================
--- 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java
 (original)
+++ 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/AsynchronousEventDeliveryTest.java
 Mon Jan 11 13:13:50 2016
@@ -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 @@ import org.junit.Test;
 
 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 class AsynchronousEventDeliveryTe
     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 class AsynchronousEventDeliveryTe
         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);
     }
 
 }

Modified: 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java?rev=1724018&r1=1724017&r2=1724018&view=diff
==============================================================================
--- 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java
 (original)
+++ 
james/project/trunk/mailbox/store/src/test/java/org/apache/james/mailbox/store/event/MixedEventDeliveryTest.java
 Mon Jan 11 13:13:50 2016
@@ -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 class MixedEventDeliveryTest {
 
     @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<Void>() {
+            @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();
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to