fix brokerSerivce instances left running if assertions fail

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1404348 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-10-31 21:06:15 +00:00
parent 13270e4e74
commit 0ad3cfc11e
1 changed files with 59 additions and 57 deletions

View File

@ -22,19 +22,14 @@ import static org.junit.Assume.assumeNotNull;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Set; import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance; import javax.management.ObjectInstance;
import javax.management.ObjectName; import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.junit.Test;
public class DuplexNetworkMBeanTest { public class DuplexNetworkMBeanTest {
protected static final Logger LOG = LoggerFactory.getLogger(DuplexNetworkMBeanTest.class); protected static final Logger LOG = LoggerFactory.getLogger(DuplexNetworkMBeanTest.class);
@ -44,10 +39,10 @@ public class DuplexNetworkMBeanTest {
BrokerService broker = new BrokerService(); BrokerService broker = new BrokerService();
broker.setBrokerName("broker"); broker.setBrokerName("broker");
broker.addConnector("tcp://localhost:61617?transport.reuseAddress=true"); broker.addConnector("tcp://localhost:61617?transport.reuseAddress=true");
return broker; return broker;
} }
protected BrokerService createNetworkedBroker() throws Exception { protected BrokerService createNetworkedBroker() throws Exception {
BrokerService broker = new BrokerService(); BrokerService broker = new BrokerService();
broker.setBrokerName("networkedBroker"); broker.setBrokerName("networkedBroker");
@ -56,65 +51,72 @@ public class DuplexNetworkMBeanTest {
networkConnector.setDuplex(true); networkConnector.setDuplex(true);
return broker; return broker;
} }
@Test @Test
public void testMbeanPresenceOnNetworkBrokerRestart() throws Exception { public void testMbeanPresenceOnNetworkBrokerRestart() throws Exception {
BrokerService broker = createBroker(); BrokerService broker = createBroker();
broker.start(); try {
assertEquals(1, countMbeans(broker, "Connector", 30000)); broker.start();
assertEquals(0, countMbeans(broker, "Connection")); assertEquals(1, countMbeans(broker, "Connector", 30000));
BrokerService networkedBroker = null; assertEquals(0, countMbeans(broker, "Connection"));
for (int i=0; i<numRestarts; i++) { BrokerService networkedBroker = null;
networkedBroker = createNetworkedBroker(); for (int i=0; i<numRestarts; i++) {
networkedBroker.start(); networkedBroker = createNetworkedBroker();
assertEquals(1, countMbeans(networkedBroker, "NetworkBridge", 2000)); networkedBroker.start();
assertEquals(1, countMbeans(broker, "NetworkBridge", 2000)); assertEquals(1, countMbeans(networkedBroker, "NetworkBridge", 2000));
assertEquals(1, countMbeans(broker, "Connection")); assertEquals(1, countMbeans(broker, "NetworkBridge", 2000));
networkedBroker.stop(); assertEquals(1, countMbeans(broker, "Connection"));
networkedBroker.waitUntilStopped(); networkedBroker.stop();
assertEquals(0, countMbeans(networkedBroker, "stopped")); networkedBroker.waitUntilStopped();
assertEquals(0, countMbeans(broker, "NetworkBridge")); assertEquals(0, countMbeans(networkedBroker, "stopped"));
assertEquals(0, countMbeans(broker, "NetworkBridge"));
}
assertEquals(0, countMbeans(networkedBroker, "NetworkBridge"));
assertEquals(0, countMbeans(networkedBroker, "Connector"));
assertEquals(0, countMbeans(networkedBroker, "Connection"));
assertEquals(1, countMbeans(broker, "Connector"));
} finally {
broker.stop();
broker.waitUntilStopped();
} }
assertEquals(0, countMbeans(networkedBroker, "NetworkBridge"));
assertEquals(0, countMbeans(networkedBroker, "Connector"));
assertEquals(0, countMbeans(networkedBroker, "Connection"));
assertEquals(1, countMbeans(broker, "Connector"));
broker.stop();
broker.waitUntilStopped();
} }
@Test @Test
public void testMbeanPresenceOnBrokerRestart() throws Exception { public void testMbeanPresenceOnBrokerRestart() throws Exception {
BrokerService networkedBroker = createNetworkedBroker(); BrokerService networkedBroker = createNetworkedBroker();
networkedBroker.start(); try {
assertEquals(1, countMbeans(networkedBroker, "Connector", 30000)); networkedBroker.start();
assertEquals(0, countMbeans(networkedBroker, "Connection")); assertEquals(1, countMbeans(networkedBroker, "Connector", 30000));
assertEquals(0, countMbeans(networkedBroker, "Connection"));
BrokerService broker = null;
for (int i=0; i<numRestarts; i++) { BrokerService broker = null;
broker = createBroker(); for (int i=0; i<numRestarts; i++) {
broker.start(); broker = createBroker();
assertEquals(1, countMbeans(networkedBroker, "NetworkBridge", 5000)); try {
assertEquals("restart number: " + i, 1, countMbeans(broker, "Connection", 10000)); broker.start();
assertEquals(1, countMbeans(networkedBroker, "NetworkBridge", 5000));
broker.stop(); assertEquals("restart number: " + i, 1, countMbeans(broker, "Connection", 10000));
broker.waitUntilStopped(); } finally {
assertEquals(0, countMbeans(broker, "stopped")); broker.stop();
broker.waitUntilStopped();
}
assertEquals(0, countMbeans(broker, "stopped"));
}
//assertEquals(0, countMbeans(networkedBroker, "NetworkBridge"));
assertEquals(1, countMbeans(networkedBroker, "Connector"));
assertEquals(0, countMbeans(networkedBroker, "Connection"));
assertEquals(0, countMbeans(broker, "Connection"));
} finally {
networkedBroker.stop();
networkedBroker.waitUntilStopped();
} }
//assertEquals(0, countMbeans(networkedBroker, "NetworkBridge"));
assertEquals(1, countMbeans(networkedBroker, "Connector"));
assertEquals(0, countMbeans(networkedBroker, "Connection"));
assertEquals(0, countMbeans(broker, "Connection"));
networkedBroker.stop();
networkedBroker.waitUntilStopped();
} }
private int countMbeans(BrokerService broker, String type) throws Exception { private int countMbeans(BrokerService broker, String type) throws Exception {
return countMbeans(broker, type, 0); return countMbeans(broker, type, 0);
} }
private int countMbeans(BrokerService broker, String type, int timeout) throws Exception { private int countMbeans(BrokerService broker, String type, int timeout) throws Exception {
@ -135,14 +137,14 @@ public class DuplexNetworkMBeanTest {
logAllMbeans(broker); logAllMbeans(broker);
} }
} while ((mbeans == null || mbeans.isEmpty()) && expiryTime > System.currentTimeMillis()); } while ((mbeans == null || mbeans.isEmpty()) && expiryTime > System.currentTimeMillis());
// If port 1099 is in use when the Broker starts, starting the jmx // If port 1099 is in use when the Broker starts, starting the jmx
// connector will fail. So, if we have no mbsc to query, skip the // connector will fail. So, if we have no mbsc to query, skip the
// test. // test.
if (timeout > 0) { if (timeout > 0) {
assumeNotNull(mbeans); assumeNotNull(mbeans);
} }
return count; return count;
} }