Update the test case so that its not dependent on port 61616

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1086378 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2011-03-28 20:10:41 +00:00
parent 442232514c
commit b0a38ff6c1
1 changed files with 94 additions and 92 deletions

View File

@ -60,6 +60,7 @@ public class ExpiredMessagesTest extends CombinationTestSupport {
public ActiveMQDestination dlqDestination = new ActiveMQQueue("ActiveMQ.DLQ"); public ActiveMQDestination dlqDestination = new ActiveMQQueue("ActiveMQ.DLQ");
public boolean useTextMessage = true; public boolean useTextMessage = true;
public boolean useVMCursor = true; public boolean useVMCursor = true;
protected String brokerUri;
public static Test suite() { public static Test suite() {
return suite(ExpiredMessagesTest.class); return suite(ExpiredMessagesTest.class);
@ -69,59 +70,60 @@ public class ExpiredMessagesTest extends CombinationTestSupport {
junit.textui.TestRunner.run(suite()); junit.textui.TestRunner.run(suite());
} }
protected void setUp() throws Exception { protected void setUp() throws Exception {
final boolean deleteAllMessages = true; final boolean deleteAllMessages = true;
broker = createBroker(deleteAllMessages, 100); broker = createBroker(deleteAllMessages, 100);
brokerUri = broker.getTransportConnectors().get(0).getPublishableConnectString();
} }
public void testExpiredMessages() throws Exception { public void testExpiredMessages() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUri);
connection = factory.createConnection(); connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination); producer = session.createProducer(destination);
producer.setTimeToLive(100); producer.setTimeToLive(100);
consumer = session.createConsumer(destination); consumer = session.createConsumer(destination);
connection.start(); connection.start();
final AtomicLong received = new AtomicLong(); final AtomicLong received = new AtomicLong();
Thread consumerThread = new Thread("Consumer Thread") { Thread consumerThread = new Thread("Consumer Thread") {
public void run() {
long start = System.currentTimeMillis();
try {
long end = System.currentTimeMillis();
while (end - start < 3000) {
if (consumer.receive(1000) != null) {
received.incrementAndGet();
}
Thread.sleep(100);
end = System.currentTimeMillis();
}
consumer.close();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
};
consumerThread.start();
final int numMessagesToSend = 10000;
Thread producingThread = new Thread("Producing Thread") {
public void run() { public void run() {
long start = System.currentTimeMillis();
try { try {
int i = 0; long end = System.currentTimeMillis();
while (i++ < numMessagesToSend) { while (end - start < 3000) {
producer.send(session.createTextMessage("test")); if (consumer.receive(1000) != null) {
} received.incrementAndGet();
producer.close(); }
Thread.sleep(100);
end = System.currentTimeMillis();
}
consumer.close();
} catch (Throwable ex) { } catch (Throwable ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
}; };
producingThread.start(); consumerThread.start();
final int numMessagesToSend = 10000;
Thread producingThread = new Thread("Producing Thread") {
public void run() {
try {
int i = 0;
while (i++ < numMessagesToSend) {
producer.send(session.createTextMessage("test"));
}
producer.close();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
};
producingThread.start();
consumerThread.join(); consumerThread.join();
producingThread.join(); producingThread.join();
@ -195,7 +197,7 @@ public class ExpiredMessagesTest extends CombinationTestSupport {
}, 60 * 1000); }, 60 * 1000);
assertEquals("dlq returned all expired", dlqListener.count, totalExpiredCount); assertEquals("dlq returned all expired", dlqListener.count, totalExpiredCount);
} }
class DLQListener implements MessageListener { class DLQListener implements MessageListener {
@ -207,18 +209,18 @@ public class ExpiredMessagesTest extends CombinationTestSupport {
}; };
private MessageConsumer createDlqConsumer(Connection connection) throws Exception { private MessageConsumer createDlqConsumer(Connection connection) throws Exception {
return connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(dlqDestination); return connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(dlqDestination);
} }
public void initCombosForTestRecoverExpiredMessages() { public void initCombosForTestRecoverExpiredMessages() {
addCombinationValues("useVMCursor", new Object[] {Boolean.TRUE, Boolean.FALSE}); addCombinationValues("useVMCursor", new Object[] {Boolean.TRUE, Boolean.FALSE});
} }
public void testRecoverExpiredMessages() throws Exception { public void testRecoverExpiredMessages() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory( ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
"failover://tcp://localhost:61616"); "failover://"+brokerUri);
connection = factory.createConnection(); connection = factory.createConnection();
connection.start(); connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@ -283,8 +285,8 @@ public class ExpiredMessagesTest extends CombinationTestSupport {
assertEquals("all dequeues were expired", view.getDequeues().getCount(), view.getExpired().getCount()); assertEquals("all dequeues were expired", view.getDequeues().getCount(), view.getExpired().getCount());
} }
private BrokerService createBroker(boolean deleteAllMessages, long expireMessagesPeriod) throws Exception { private BrokerService createBroker(boolean deleteAllMessages, long expireMessagesPeriod) throws Exception {
BrokerService broker = new BrokerService(); BrokerService broker = new BrokerService();
broker.setBrokerName("localhost"); broker.setBrokerName("localhost");
broker.setDestinations(new ActiveMQDestination[]{destination}); broker.setDestinations(new ActiveMQDestination[]{destination});
AMQPersistenceAdapter adaptor = new AMQPersistenceAdapter(); AMQPersistenceAdapter adaptor = new AMQPersistenceAdapter();
@ -302,17 +304,17 @@ public class ExpiredMessagesTest extends CombinationTestSupport {
policyMap.setDefaultEntry(defaultPolicy); policyMap.setDefaultEntry(defaultPolicy);
broker.setDestinationPolicy(policyMap); broker.setDestinationPolicy(policyMap);
broker.setDeleteAllMessagesOnStartup(deleteAllMessages); broker.setDeleteAllMessagesOnStartup(deleteAllMessages);
broker.addConnector("tcp://localhost:61616"); broker.addConnector("tcp://localhost:0");
broker.start(); broker.start();
broker.waitUntilStarted(); broker.waitUntilStarted();
return broker; return broker;
} }
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
connection.stop(); connection.stop();
broker.stop(); broker.stop();
broker.waitUntilStopped(); broker.waitUntilStopped();
} }
} }