mirror of https://github.com/apache/activemq.git
Fix for AMQ-5113. Fixed race condition in onMessage for testSimulatenousCron which caused intermittent failures
This commit is contained in:
parent
4ba4aa21d3
commit
cbb46ea7d3
|
@ -17,12 +17,14 @@
|
|||
package org.apache.activemq.broker.scheduler;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageListener;
|
||||
|
@ -34,13 +36,25 @@ import org.apache.activemq.EmbeddedBrokerTestSupport;
|
|||
import org.apache.activemq.ScheduledMessage;
|
||||
import org.apache.activemq.broker.BrokerService;
|
||||
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.LoggerFactory;
|
||||
|
||||
@RunWith(BlockJUnit4ClassRunner.class)
|
||||
public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
|
||||
|
||||
@Rule
|
||||
public TestName testName = new TestName();
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JmsCronSchedulerTest.class);
|
||||
|
||||
@Test
|
||||
public void testSimulatenousCron() throws Exception {
|
||||
|
||||
final int COUNT = 10;
|
||||
|
@ -55,9 +69,16 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
|
|||
consumer.setMessageListener(new MessageListener() {
|
||||
@Override
|
||||
public void onMessage(Message message) {
|
||||
latch.countDown();
|
||||
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());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -67,6 +88,7 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
|
|||
TextMessage message = session.createTextMessage("test msg "+ i);
|
||||
message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "* * * * *");
|
||||
producer.send(message);
|
||||
LOG.info("Message {} sent at {}", i, new Date().toString());
|
||||
producer.close();
|
||||
//wait a couple sec so cron start time is different for next message
|
||||
Thread.sleep(2000);
|
||||
|
@ -80,6 +102,7 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
|
|||
assertEquals(COUNT, count.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCronScheduleWithTtlSet() throws Exception {
|
||||
|
||||
Connection connection = createConnection();
|
||||
|
@ -101,12 +124,18 @@ public class JmsCronSchedulerTest extends EmbeddedBrokerTestSupport {
|
|||
assertNull(consumer.receiveNoWait());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
LOG.info("Starting test {}", testName.getMethodName());
|
||||
bindAddress = "vm://localhost";
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BrokerService createBroker() throws Exception {
|
||||
return createBroker(true);
|
||||
|
|
Loading…
Reference in New Issue