Fix for AMQ-5113. Fixed race condition in onMessage for testSimulatenousCron which caused intermittent failures

This commit is contained in:
Kevin Earls 2014-03-20 13:33:03 +01:00
parent 4ba4aa21d3
commit cbb46ea7d3
1 changed files with 34 additions and 5 deletions

View File

@ -17,12 +17,14 @@
package org.apache.activemq.broker.scheduler; package org.apache.activemq.broker.scheduler;
import java.io.File; import java.io.File;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageConsumer; import javax.jms.MessageConsumer;
import javax.jms.MessageListener; import javax.jms.MessageListener;
@ -34,13 +36,25 @@ import org.apache.activemq.EmbeddedBrokerTestSupport;
import org.apache.activemq.ScheduledMessage; import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.util.IOHelper; import org.apache.activemq.util.IOHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@RunWith(BlockJUnit4ClassRunner.class)
public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport { public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
@Rule
public TestName testName = new TestName();
private static final Logger LOG = LoggerFactory.getLogger(JmsCronSchedulerTest.class); private static final Logger LOG = LoggerFactory.getLogger(JmsCronSchedulerTest.class);
@Test
public void testSimulatenousCron() throws Exception { public void testSimulatenousCron() throws Exception {
final int COUNT = 10; final int COUNT = 10;
@ -55,18 +69,26 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
consumer.setMessageListener(new MessageListener() { consumer.setMessageListener(new MessageListener() {
@Override @Override
public void onMessage(Message message) { public void onMessage(Message message) {
latch.countDown();
count.incrementAndGet(); count.incrementAndGet();
LOG.debug("Received one Message, count is at: " + count.get()); latch.countDown();
assertTrue(message instanceof TextMessage);
TextMessage tm = (TextMessage) message;
try {
LOG.info("Received [{}] count: {} ", tm.getText(), count.get());
} catch (JMSException e) {
LOG.error("Unexpected exception in onMessage", e);
fail("Unexpected exception in onMessage: " + e.getMessage());
}
} }
}); });
connection.start(); connection.start();
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
MessageProducer producer = session.createProducer(destination); MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("test msg "+i); TextMessage message = session.createTextMessage("test msg "+ i);
message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "* * * * *"); message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "* * * * *");
producer.send(message); producer.send(message);
LOG.info("Message {} sent at {}", i, new Date().toString());
producer.close(); producer.close();
//wait a couple sec so cron start time is different for next message //wait a couple sec so cron start time is different for next message
Thread.sleep(2000); Thread.sleep(2000);
@ -80,6 +102,7 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
assertEquals(COUNT, count.get()); assertEquals(COUNT, count.get());
} }
@Test
public void testCronScheduleWithTtlSet() throws Exception { public void testCronScheduleWithTtlSet() throws Exception {
Connection connection = createConnection(); Connection connection = createConnection();
@ -101,12 +124,18 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
assertNull(consumer.receiveNoWait()); assertNull(consumer.receiveNoWait());
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
LOG.info("Starting test {}", testName.getMethodName());
bindAddress = "vm://localhost"; bindAddress = "vm://localhost";
super.setUp(); super.setUp();
} }
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Override @Override
protected BrokerService createBroker() throws Exception { protected BrokerService createBroker() throws Exception {
return createBroker(true); return createBroker(true);