diff --git a/activemq-optional/src/test/java/org/apache/activemq/transport/http/HttpTransportBrokerTest.java b/activemq-optional/src/test/java/org/apache/activemq/transport/http/HttpTransportBrokerTest.java index ed3b568a79..daefd97062 100755 --- a/activemq-optional/src/test/java/org/apache/activemq/transport/http/HttpTransportBrokerTest.java +++ b/activemq-optional/src/test/java/org/apache/activemq/transport/http/HttpTransportBrokerTest.java @@ -16,7 +16,11 @@ */ package org.apache.activemq.transport.http; +import java.net.Socket; import java.net.URI; +import java.net.URL; + +import javax.net.SocketFactory; import junit.framework.Test; import junit.textui.TestRunner; @@ -24,9 +28,13 @@ import junit.textui.TestRunner; import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.transport.TransportBrokerTestSupport; +import org.apache.activemq.util.Wait; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; public class HttpTransportBrokerTest extends TransportBrokerTestSupport { + private static final Log LOG = LogFactory.getLog(HttpTransportBrokerTest.class); protected String getBindLocation() { return "http://localhost:8081"; } @@ -34,11 +42,28 @@ public class HttpTransportBrokerTest extends TransportBrokerTestSupport { protected void setUp() throws Exception { maxWait = 2000; super.setUp(); - Thread.sleep(1000); - Thread.yield(); + waitForJettySocketToAccept(getBindLocation()); } - protected BrokerService createBroker() throws Exception { + private void waitForJettySocketToAccept(String bindLocation) throws Exception { + final URL url = new URL(bindLocation); + assertTrue("Jetty endpoint is available", Wait.waitFor(new Wait.Condition() { + + public boolean isSatisified() throws Exception { + boolean canConnect = false; + try { + Socket socket = SocketFactory.getDefault().createSocket(url.getHost(), url.getPort()); + socket.close(); + canConnect = true; + } catch (Exception e) { + LOG.warn("verify jettty available, failed to connect to " + url + e); + } + return canConnect; + }}, 60 * 1000)); + + } + + protected BrokerService createBroker() throws Exception { BrokerService broker = BrokerFactory.createBroker(new URI("broker:()/localhost?persistent=false&useJmx=false")); connector = broker.addConnector(getBindLocation()); return broker; @@ -47,7 +72,7 @@ public class HttpTransportBrokerTest extends TransportBrokerTestSupport { protected void tearDown() throws Exception { super.tearDown(); // Give the jetty server enough time to shutdown before starting another one - Thread.sleep(500); + Thread.sleep(100); } public static Test suite() { diff --git a/activemq-optional/src/test/java/org/apache/activemq/transport/https/HttpsTransportBrokerTest.java b/activemq-optional/src/test/java/org/apache/activemq/transport/https/HttpsTransportBrokerTest.java index 5485a686cc..289d537a40 100644 --- a/activemq-optional/src/test/java/org/apache/activemq/transport/https/HttpsTransportBrokerTest.java +++ b/activemq-optional/src/test/java/org/apache/activemq/transport/https/HttpsTransportBrokerTest.java @@ -35,15 +35,6 @@ public class HttpsTransportBrokerTest extends HttpTransportBrokerTest { System.setProperty("javax.net.ssl.keyStoreType", "jks"); //System.setProperty("javax.net.debug", "ssl,handshake,data,trustmanager"); super.setUp(); - - Thread.sleep(2000); - Thread.yield(); - } - - protected void tearDown() throws Exception { - super.tearDown(); - // Give the jetty server more time to shutdown before starting another one - Thread.sleep(2000); } public static Test suite() {