NetworkBrokerDetachTest refactoring

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@934871 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2010-04-16 13:52:21 +00:00
parent fab6ba187d
commit db9533b631
1 changed files with 36 additions and 34 deletions

View File

@ -48,6 +48,8 @@ import org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter;
import org.apache.activemq.util.Wait; import org.apache.activemq.util.Wait;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class NetworkBrokerDetachTest { public class NetworkBrokerDetachTest {
@ -62,6 +64,9 @@ public class NetworkBrokerDetachTest {
protected final int networkTTL = 2; protected final int networkTTL = 2;
protected final boolean dynamicOnly = false; protected final boolean dynamicOnly = false;
protected BrokerService broker;
protected BrokerService networkedBroker;
protected BrokerService createBroker() throws Exception { protected BrokerService createBroker() throws Exception {
BrokerService broker = new BrokerService(); BrokerService broker = new BrokerService();
broker.setBrokerName(BROKER_NAME); broker.setBrokerName(BROKER_NAME);
@ -96,20 +101,34 @@ public class NetworkBrokerDetachTest {
//broker.setPersistenceAdapter(persistenceAdapter); //broker.setPersistenceAdapter(persistenceAdapter);
KahaDBPersistenceAdapter persistenceAdapter = new KahaDBPersistenceAdapter(); KahaDBPersistenceAdapter persistenceAdapter = new KahaDBPersistenceAdapter();
persistenceAdapter.setDirectory(new File("target/activemq-data/kahadb/NetworBrokerDetatchTest")); persistenceAdapter.setDirectory(new File("target/activemq-data/kahadb/" + broker.getBrokerName() + "NetworBrokerDetatchTest"));
broker.setPersistenceAdapter(persistenceAdapter); broker.setPersistenceAdapter(persistenceAdapter);
// default AMQ // default AMQ
} }
@Test @Before
public void testNetworkedBrokerDetach() throws Exception { public void init() throws Exception {
BrokerService broker = createBroker(); broker = createBroker();
broker.setDeleteAllMessagesOnStartup(true);
broker.start(); broker.start();
BrokerService networkedBroker = createNetworkedBroker(); networkedBroker = createNetworkedBroker();
networkedBroker.setDeleteAllMessagesOnStartup(true);
networkedBroker.start(); networkedBroker.start();
}
@After
public void cleanup() throws Exception {
networkedBroker.stop();
networkedBroker.waitUntilStopped();
broker.stop();
broker.waitUntilStopped();
}
@Test
public void testNetworkedBrokerDetach() throws Exception {
LOG.info("Creating Consumer on the networked broker ..."); LOG.info("Creating Consumer on the networked broker ...");
// Create a consumer on the networked broker // Create a consumer on the networked broker
ConnectionFactory consFactory = createConnectionFactory(networkedBroker); ConnectionFactory consFactory = createConnectionFactory(networkedBroker);
@ -130,23 +149,11 @@ public class NetworkBrokerDetachTest {
// We should have 0 consumer for the queue on the local broker // We should have 0 consumer for the queue on the local broker
assertTrue("got expected 0 count from mbean within time limit", verifyConsumerCount(0, destination, BROKER_NAME)); assertTrue("got expected 0 count from mbean within time limit", verifyConsumerCount(0, destination, BROKER_NAME));
networkedBroker.stop();
networkedBroker.waitUntilStopped();
broker.stop();
broker.waitUntilStopped();
} }
@Test @Test
public void testNetworkedBrokerDurableSubAfterRestart() throws Exception { public void testNetworkedBrokerDurableSubAfterRestart() throws Exception {
BrokerService brokerOne = createBroker();
brokerOne.setDeleteAllMessagesOnStartup(true);
brokerOne.start();
BrokerService brokerTwo = createNetworkedBroker();
brokerTwo.setDeleteAllMessagesOnStartup(true);
brokerTwo.start();
final AtomicInteger count = new AtomicInteger(0); final AtomicInteger count = new AtomicInteger(0);
MessageListener counter = new MessageListener() { MessageListener counter = new MessageListener() {
@ -156,8 +163,8 @@ public class NetworkBrokerDetachTest {
}; };
LOG.info("Creating durable consumer on each broker ..."); LOG.info("Creating durable consumer on each broker ...");
ActiveMQTopic destination = registerDurableConsumer(brokerTwo, counter); ActiveMQTopic destination = registerDurableConsumer(networkedBroker, counter);
registerDurableConsumer(brokerOne, counter); registerDurableConsumer(broker, counter);
assertTrue("got expected consumer count from local broker mbean within time limit", assertTrue("got expected consumer count from local broker mbean within time limit",
verifyConsumerCount(2, destination, BROKER_NAME)); verifyConsumerCount(2, destination, BROKER_NAME));
@ -165,25 +172,25 @@ public class NetworkBrokerDetachTest {
assertTrue("got expected consumer count from network broker mbean within time limit", assertTrue("got expected consumer count from network broker mbean within time limit",
verifyConsumerCount(2, destination, REM_BROKER_NAME)); verifyConsumerCount(2, destination, REM_BROKER_NAME));
sendMessageTo(destination, brokerOne); sendMessageTo(destination, broker);
assertTrue("Got one message on each", verifyMessageCount(2, count)); assertTrue("Got one message on each", verifyMessageCount(2, count));
LOG.info("Stopping brokerTwo..."); LOG.info("Stopping brokerTwo...");
brokerTwo.stop(); networkedBroker.stop();
brokerTwo.waitUntilStopped(); networkedBroker.waitUntilStopped();
LOG.info("restarting broker Two..."); LOG.info("restarting broker Two...");
brokerTwo = createNetworkedBroker(); networkedBroker = createNetworkedBroker();
brokerTwo.start(); networkedBroker.start();
LOG.info("Recreating durable Consumer on the broker after restart..."); LOG.info("Recreating durable Consumer on the broker after restart...");
registerDurableConsumer(brokerTwo, counter); registerDurableConsumer(networkedBroker, counter);
// give advisories a chance to percolate // give advisories a chance to percolate
TimeUnit.SECONDS.sleep(5); TimeUnit.SECONDS.sleep(5);
sendMessageTo(destination, brokerOne); sendMessageTo(destination, broker);
// expect similar after restart // expect similar after restart
assertTrue("got expected consumer count from local broker mbean within time limit", assertTrue("got expected consumer count from local broker mbean within time limit",
@ -199,11 +206,6 @@ public class NetworkBrokerDetachTest {
assertTrue("Got two more messages after restart", verifyMessageCount(4, count)); assertTrue("Got two more messages after restart", verifyMessageCount(4, count));
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
assertTrue("still Got just two more messages", verifyMessageCount(4, count)); assertTrue("still Got just two more messages", verifyMessageCount(4, count));
brokerTwo.stop();
brokerTwo.waitUntilStopped();
brokerOne.stop();
brokerOne.waitUntilStopped();
} }
private boolean verifyMessageCount(final int i, final AtomicInteger count) throws Exception { private boolean verifyMessageCount(final int i, final AtomicInteger count) throws Exception {