You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by am...@apache.org on 2013/07/04 11:18:23 UTC

svn commit: r1499703 - /cxf/dosgi/trunk/systests2/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/AbstractDosgiTest.java

Author: amichai
Date: Thu Jul  4 09:18:22 2013
New Revision: 1499703

URL: http://svn.apache.org/r1499703
Log:
Close sockets in AbstractDosgiTest.waitWebPage() and getFreePort()

Modified:
    cxf/dosgi/trunk/systests2/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/AbstractDosgiTest.java

Modified: cxf/dosgi/trunk/systests2/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/AbstractDosgiTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/systests2/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/AbstractDosgiTest.java?rev=1499703&r1=1499702&r2=1499703&view=diff
==============================================================================
--- cxf/dosgi/trunk/systests2/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/AbstractDosgiTest.java (original)
+++ cxf/dosgi/trunk/systests2/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/AbstractDosgiTest.java Thu Jul  4 09:18:22 2013
@@ -21,6 +21,7 @@ package org.apache.cxf.dosgi.systests2.m
 import java.io.IOException;
 import java.net.ConnectException;
 import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
 import java.net.MalformedURLException;
 import java.net.ServerSocket;
 import java.net.Socket;
@@ -103,9 +104,9 @@ public class AbstractDosgiTest {
         return (GreeterService)factory.create();
     }
 
-    protected Bundle getBundleByName(BundleContext bc, String sn) {
+    protected Bundle getBundleByName(BundleContext bc, String name) {
         for (Bundle bundle : bc.getBundles()) {
-            if (bundle.getSymbolicName().equals(sn)) {
+            if (bundle.getSymbolicName().equals(name)) {
                 return bundle;
             }
         }
@@ -113,8 +114,10 @@ public class AbstractDosgiTest {
     }
 
     protected int getFreePort() throws IOException {
-        ServerSocket socket = new ServerSocket(0);
+        ServerSocket socket = new ServerSocket();
         try {
+            socket.setReuseAddress(true); // enables quickly reopening socket on same port
+            socket.bind(new InetSocketAddress(0)); // zero finds a free port
             return socket.getLocalPort();
         } finally {
             socket.close();
@@ -123,11 +126,12 @@ public class AbstractDosgiTest {
 
     protected void waitWebPage(String urlSt) throws InterruptedException, TimeoutException {
         System.out.println("Waiting for url " + urlSt);
+        HttpURLConnection con = null;
         long startTime = System.currentTimeMillis();
         while (true) {
             try {
                 URL url = new URL(urlSt);
-                HttpURLConnection con = (HttpURLConnection)url.openConnection();
+                con = (HttpURLConnection)url.openConnection();
                 int status = con.getResponseCode();
                 if (status == 200) {
                     return;
@@ -138,6 +142,10 @@ public class AbstractDosgiTest {
                 throw new RuntimeException(e.getMessage(), e);
             } catch (IOException e) {
                 throw new RuntimeException(e.getMessage(), e);
+            } finally {
+                if (con != null) {
+                    con.disconnect();
+                }
             }
             sleepOrTimeout(startTime, TIMEOUT, "Timeout waiting for web page " + urlSt);
         }