fix intermittent failure - wait rather than sleep which also speeds up the test run

This commit is contained in:
gtully 2015-06-19 12:35:44 +01:00
parent b22184ebf6
commit 1a3ade0414
2 changed files with 23 additions and 27 deletions

View File

@ -36,6 +36,7 @@ import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.network.NetworkConnector;
import org.apache.activemq.util.Wait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -49,26 +50,31 @@ public class FailoverClusterTestSupport extends TestCase {
private final Map<String, BrokerService> brokers = new HashMap<String, BrokerService>();
private final List<ActiveMQConnection> connections = new ArrayList<ActiveMQConnection>();
protected void assertClientsConnectedToTwoBrokers() {
Set<String> set = new HashSet<String>();
for (ActiveMQConnection c : connections) {
if (c.getTransportChannel().getRemoteAddress() != null) {
set.add(c.getTransportChannel().getRemoteAddress());
}
}
assertTrue("Only 2 connections should be found: " + set,
set.size() == 2);
protected void assertClientsConnectedToTwoBrokers() throws Exception {
assertClientsConnectedToXBrokers(2);
}
protected void assertClientsConnectedToThreeBrokers() {
Set<String> set = new HashSet<String>();
for (ActiveMQConnection c : connections) {
if (c.getTransportChannel().getRemoteAddress() != null) {
set.add(c.getTransportChannel().getRemoteAddress());
protected void assertClientsConnectedToThreeBrokers() throws Exception {
assertClientsConnectedToXBrokers(3);
}
protected void assertClientsConnectedToXBrokers(final int x) throws Exception {
final Set<String> set = new HashSet<String>();
Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
set.clear();
for (ActiveMQConnection c : connections) {
if (c.getTransportChannel().getRemoteAddress() != null) {
set.add(c.getTransportChannel().getRemoteAddress());
}
}
return set.size() == x;
}
}
assertTrue("Only 3 connections should be found: " + set,
set.size() == 3);
});
assertTrue("Only " + x + " connections should be found: " + set,
set.size() == x);
}
protected void assertClientsConnectionsEvenlyDistributed(double minimumPercentage) {

View File

@ -147,7 +147,6 @@ public class FailoverComplexClusterTest extends FailoverClusterTestSupport {
setClientUrl("failover://(" + BROKER_A_CLIENT_TC_ADDRESS + "," + BROKER_B_CLIENT_TC_ADDRESS + ")");
createClients();
Thread.sleep(2000);
assertClientsConnectedToThreeBrokers();
@ -155,13 +154,10 @@ public class FailoverComplexClusterTest extends FailoverClusterTestSupport {
getBroker(BROKER_A_NAME).waitUntilStopped();
removeBroker(BROKER_A_NAME);
Thread.sleep(5000);
assertClientsConnectedToTwoBrokers();
createBrokerA(false, null, null, null);
getBroker(BROKER_A_NAME).waitUntilStarted();
Thread.sleep(5000);
assertClientsConnectedToThreeBrokers();
}
@ -264,14 +260,11 @@ public class FailoverComplexClusterTest extends FailoverClusterTestSupport {
getBroker(BROKER_C_NAME).waitUntilStopped();
removeBroker(BROKER_C_NAME);
Thread.sleep(5000);
assertClientsConnectedToTwoBrokers();
LOG.info("Recreating BrokerC after stop");
createBrokerC(multi, tcParams, clusterFilter, destinationFilter);
getBroker(BROKER_C_NAME).waitUntilStarted();
Thread.sleep(5000);
assertClientsConnectedToThreeBrokers();
}
@ -292,14 +285,11 @@ public class FailoverComplexClusterTest extends FailoverClusterTestSupport {
getBroker(BROKER_C_NAME).waitUntilStopped();
removeBroker(BROKER_C_NAME);
Thread.sleep(5000);
assertClientsConnectedToTwoBrokers();
assertClientsConnectionsEvenlyDistributed(.35);
createBrokerC(multi, tcParams, clusterFilter, destinationFilter);
getBroker(BROKER_C_NAME).waitUntilStarted();
Thread.sleep(5000);
assertClientsConnectedToThreeBrokers();
assertClientsConnectionsEvenlyDistributed(.20);