See AMQ-4886. Updated tearDown so it can't hang, reduced timeouts, updated to JUnit4

This commit is contained in:
Kevin Earls 2013-11-14 15:55:04 +01:00
parent e57aeb3786
commit c60af64165
1 changed files with 64 additions and 15 deletions

View File

@ -23,27 +23,43 @@ import java.util.HashSet;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.Vector; import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.jms.*; import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.AutoFailTestSupport; import org.apache.activemq.AutoFailTestSupport;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.region.Destination; import org.apache.activemq.broker.region.Destination;
import org.apache.activemq.broker.region.DestinationStatistics; import org.apache.activemq.broker.region.DestinationStatistics;
import org.apache.activemq.broker.region.RegionBroker; import org.apache.activemq.broker.region.RegionBroker;
import org.apache.activemq.broker.util.LoggingBrokerPlugin; import org.apache.activemq.broker.util.LoggingBrokerPlugin;
import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQDestination;
import org.junit.rules.TestName;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.junit.Assert.*;
interface Configurer { interface Configurer {
public void configure(BrokerService broker) throws Exception; public void configure(BrokerService broker) throws Exception;
} }
public class AMQ2149Test extends AutoFailTestSupport { public class AMQ2149Test {
private static final Logger LOG = LoggerFactory.getLogger(AMQ2149Test.class); private static final Logger LOG = LoggerFactory.getLogger(AMQ2149Test.class);
@Rule
public TestName testName = new TestName();
private static final String BROKER_CONNECTOR = "tcp://localhost:61617"; private static final String BROKER_CONNECTOR = "tcp://localhost:61617";
private static final String DEFAULT_BROKER_URL = "failover:("+ BROKER_CONNECTOR private static final String DEFAULT_BROKER_URL = "failover:("+ BROKER_CONNECTOR
@ -80,7 +96,7 @@ public class AMQ2149Test extends AutoFailTestSupport {
broker.getSystemUsage().getMemoryUsage().setLimit(MESSAGE_LENGTH_BYTES * 200 * NUM_SENDERS_AND_RECEIVERS); broker.getSystemUsage().getMemoryUsage().setLimit(MESSAGE_LENGTH_BYTES * 200 * NUM_SENDERS_AND_RECEIVERS);
broker.addConnector(BROKER_CONNECTOR); broker.addConnector(BROKER_CONNECTOR);
broker.setBrokerName(getName()); broker.setBrokerName(testName.getMethodName());
broker.setDataDirectoryFile(dataDirFile); broker.setDataDirectoryFile(dataDirFile);
if (configurer != null) { if (configurer != null) {
configurer.configure(broker); configurer.configure(broker);
@ -91,23 +107,31 @@ public class AMQ2149Test extends AutoFailTestSupport {
protected void configurePersistenceAdapter(BrokerService brokerService) throws Exception { protected void configurePersistenceAdapter(BrokerService brokerService) throws Exception {
} }
@Before
public void setUp() throws Exception { public void setUp() throws Exception {
setMaxTestTime(30*60*1000); LOG.debug("Starting test {}", testName.getMethodName());
setAutoFail(true); dataDirFile = new File("target/"+ testName.getMethodName());
dataDirFile = new File("target/"+ getName());
numtoSend = DEFAULT_NUM_TO_SEND; numtoSend = DEFAULT_NUM_TO_SEND;
brokerStopPeriod = DEFAULT_BROKER_STOP_PERIOD; brokerStopPeriod = DEFAULT_BROKER_STOP_PERIOD;
sleepBetweenSend = SLEEP_BETWEEN_SEND_MS; sleepBetweenSend = SLEEP_BETWEEN_SEND_MS;
brokerURL = DEFAULT_BROKER_URL; brokerURL = DEFAULT_BROKER_URL;
} }
@After
public void tearDown() throws Exception { public void tearDown() throws Exception {
synchronized(brokerLock) { ExecutorService executor = Executors.newSingleThreadExecutor();
if (broker!= null) { Future<Boolean> future = executor.submit(new TeardownTask(brokerLock, broker));
broker.stop(); try {
broker.waitUntilStopped(); LOG.debug("Teardown started.");
} long start = System.currentTimeMillis();
Boolean result = future.get(30, TimeUnit.SECONDS);
long finish = System.currentTimeMillis();
LOG.debug("Result of teardown: {} after {} ms ", result, (finish - start));
} catch (TimeoutException e) {
fail("Teardown timed out");
AutoFailTestSupport.dumpAllThreads(testName.getMethodName());
} }
executor.shutdownNow();
exceptions.clear(); exceptions.clear();
} }
@ -339,6 +363,7 @@ public class AMQ2149Test extends AutoFailTestSupport {
verifyStats(false); verifyStats(false);
} }
@Test(timeout = 5 * 60 * 1000)
public void testOrderWithRestart() throws Exception { public void testOrderWithRestart() throws Exception {
createBroker(new Configurer() { createBroker(new Configurer() {
public void configure(BrokerService broker) throws Exception { public void configure(BrokerService broker) throws Exception {
@ -360,7 +385,8 @@ public class AMQ2149Test extends AutoFailTestSupport {
verifyStats(true); verifyStats(true);
} }
@Test(timeout = 5 * 60 * 1000)
public void testTopicOrderWithRestart() throws Exception { public void testTopicOrderWithRestart() throws Exception {
createBroker(new Configurer() { createBroker(new Configurer() {
public void configure(BrokerService broker) throws Exception { public void configure(BrokerService broker) throws Exception {
@ -380,10 +406,12 @@ public class AMQ2149Test extends AutoFailTestSupport {
verifyStats(true); verifyStats(true);
} }
@Test(timeout = 5 * 60 * 1000)
public void testQueueTransactionalOrderWithRestart() throws Exception { public void testQueueTransactionalOrderWithRestart() throws Exception {
doTestTransactionalOrderWithRestart(ActiveMQDestination.QUEUE_TYPE); doTestTransactionalOrderWithRestart(ActiveMQDestination.QUEUE_TYPE);
} }
@Test(timeout = 5 * 60 * 1000)
public void testTopicTransactionalOrderWithRestart() throws Exception { public void testTopicTransactionalOrderWithRestart() throws Exception {
doTestTransactionalOrderWithRestart(ActiveMQDestination.TOPIC_TYPE); doTestTransactionalOrderWithRestart(ActiveMQDestination.TOPIC_TYPE);
} }
@ -477,7 +505,7 @@ public class AMQ2149Test extends AutoFailTestSupport {
} }
private void verifyOrderedMessageReceipt(byte destinationType, int concurrentPairs, boolean transactional) throws Exception { private void verifyOrderedMessageReceipt(byte destinationType, int concurrentPairs, boolean transactional) throws Exception {
Vector<Thread> threads = new Vector<Thread>(); Vector<Thread> threads = new Vector<Thread>();
Vector<Receiver> receivers = new Vector<Receiver>(); Vector<Receiver> receivers = new Vector<Receiver>();
@ -529,3 +557,24 @@ public class AMQ2149Test extends AutoFailTestSupport {
} }
} }
class TeardownTask implements Callable<Boolean> {
private Object brokerLock;
private BrokerService broker;
public TeardownTask(Object brokerLock, BrokerService broker) {
this.brokerLock = brokerLock;
this.broker = broker;
}
@Override
public Boolean call() throws Exception {
synchronized(brokerLock) {
if (broker!= null) {
broker.stop();
broker.waitUntilStopped();
}
}
return Boolean.TRUE;
}
}