NO-JIRA make MessageConsumerTest deterministic

The method testStopConnectionDuringOnMessage in this class was relying
on sleep() calls for critical timing. This test fails sometimes on
fast-tests so I modified it to use a latch and a few waitFor calls to be
more deterministic. It also runs in a third of the time now.
This commit is contained in:
Justin Bertram 2018-02-22 21:03:22 -06:00 committed by Clebert Suconic
parent bfb6500c69
commit 2df9c0ee76
2 changed files with 15 additions and 14 deletions

View File

@ -64,6 +64,11 @@
<artifactId>artemis-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-junit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -46,6 +46,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.activemq.artemis.jms.tests.util.ProxyAssertSupport;
import org.apache.activemq.artemis.junit.Wait;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.junit.Assert;
import org.junit.Test;
@ -2285,7 +2286,6 @@ public class MessageConsumerTest extends JMSTestCase {
}
}
// This is commented out until http:// jira.jboss.com/jira/browse/JBMESSAGING-983 is complete
@Test
public void testStopConnectionDuringOnMessage() throws Exception {
if (log.isTraceEnabled()) {
@ -2294,12 +2294,13 @@ public class MessageConsumerTest extends JMSTestCase {
final AtomicInteger messagesReceived = new AtomicInteger(0);
MessageListener myListener = new MessageListener() {
@Override
public void onMessage(final Message message) {
messagesReceived.incrementAndGet();
CountDownLatch messagesReceivedLatch = new CountDownLatch(1);
MessageListener myListener = message -> {
if (messagesReceived.incrementAndGet() == 10) {
messagesReceivedLatch.countDown();
try {
Thread.sleep(100L);
Thread.sleep(200L);
} catch (InterruptedException e) {
// Ignore
}
@ -2336,7 +2337,7 @@ public class MessageConsumerTest extends JMSTestCase {
queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
}
Thread.sleep(500L);
messagesReceivedLatch.await(500, TimeUnit.MILLISECONDS);
log.trace("Stopping consumer connection");
consumerConnection.stop();
@ -2349,17 +2350,12 @@ public class MessageConsumerTest extends JMSTestCase {
queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
}
log.trace("Sleeping a bit to check that no messages are received");
Thread.sleep(2000);
ProxyAssertSupport.assertEquals("Should not receive any messages after the connection has been stopped", countAfterStop, messagesReceived.get());
ProxyAssertSupport.assertFalse("Should not receive any messages after the connection has been stopped", Wait.waitFor(() -> messagesReceived.get() > countAfterStop, 2000, 1000));
log.trace("Restarting consumer connection");
consumerConnection.start();
log.trace("Sleeping to allow remaining messages to arrive");
Thread.sleep(15000);
ProxyAssertSupport.assertEquals("Should have received all messages after restarting", MESSAGE_COUNT, messagesReceived.get());
ProxyAssertSupport.assertTrue("Should have received all messages after restarting", Wait.waitFor(() -> messagesReceived.get() == MESSAGE_COUNT, 15000, 100));
} finally {
if (producerConnection != null) {
producerConnection.close();