HttpTestUtils.connectToUrl: interrupt if takes too long

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/e299b110
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/e299b110
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/e299b110

Branch: refs/heads/0.4.0
Commit: e299b110fcbcbfe6fe286ac453fb3b59972df51d
Parents: b97d6dd
Author: Aled Sage <[email protected]>
Authored: Sun Oct 14 14:43:43 2012 +0100
Committer: Aled Sage <[email protected]>
Committed: Tue Oct 16 22:23:04 2012 +0100

----------------------------------------------------------------------
 .../main/java/brooklyn/test/HttpTestUtils.java  | 51 +++++++++++++++-----
 1 file changed, 40 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e299b110/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
----------------------------------------------------------------------
diff --git a/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java 
b/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
index a9167fe..0a2c6c5 100644
--- a/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
+++ b/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
@@ -10,9 +10,11 @@ import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
@@ -45,18 +47,45 @@ public class HttpTestUtils {
      * Connects to the given url and returns the connection.
      */
     public static URLConnection connectToUrl(String u) throws Exception {
-        URL url = new URL(u);
-        URLConnection connection = url.openConnection();
-        TrustingSslSocketFactory.configure(connection);
-        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
-            @Override public boolean verify(String s, SSLSession sslSession) {
-                return true;
+        final URL url = new URL(u);
+        final AtomicReference<URLConnection> result = new 
AtomicReference<URLConnection>();
+        final AtomicReference<Exception> exception = new 
AtomicReference<Exception>();
+        
+        Thread thread = new Thread("url-test-connector") {
+            public void run() {
+                try {
+                    URLConnection connection = url.openConnection();
+                    TrustingSslSocketFactory.configure(connection);
+                    HttpsURLConnection.setDefaultHostnameVerifier(new 
HostnameVerifier() {
+                        @Override public boolean verify(String s, SSLSession 
sslSession) {
+                            return true;
+                        }
+                    });
+                    connection.connect();
+    
+                    connection.getContentLength(); // Make sure the connection 
is made.
+                    result.set(connection);
+                } catch (Exception e) {
+                    exception.set(e);
+                    LOG.info("Error connecting to url "+url+" (propagating)", 
e);
+                }
             }
-        });
-        connection.connect();
-
-        connection.getContentLength(); // Make sure the connection is made.
-        return connection;
+        };
+        try {
+            thread.start();
+            thread.join(60*1000);
+            
+            if (thread.isAlive()) {
+                throw new IllegalStateException("Connect to URL not complete 
within 60 seconds, for url "+url+"; stacktrace 
"+Arrays.toString(thread.getStackTrace()));
+            } else if (exception.get() != null) {
+                throw exception.get();
+            } else {
+                return result.get();
+            }
+            
+        } finally {
+            thread.interrupt();
+        }
     }
 
     public static int getHttpStatusCode(String url) throws Exception {

Reply via email to