< prev index next >

test/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java

Print this page

        

@@ -53,10 +53,11 @@
 import java.util.Arrays;
 import java.util.Base64;
 import java.util.List;
 import java.util.Objects;
 import java.util.Random;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.stream.Collectors;
 import javax.net.ssl.SSLContext;
 import sun.net.www.HeaderParser;
 
 /**

@@ -143,14 +144,52 @@
             default:
                 throw new InternalError("Unknown server type: " + authType);
         }
     }
 
-    static HttpServer createHttpServer(HttpProtocolType protocol) throws IOException {
+    /**
+     * The HttpServerFactory ensure that the local port used by an HttpServer
+     * previously created by the current test/VM will not get reused by
+     * a subsequent test in the same VM.
+     */
+    private static final class HttpServerFactory {
+        private static final int MAX = 10;
+        private static final CopyOnWriteArrayList<String> addresses =
+            new CopyOnWriteArrayList<>();
+        private static HttpServer newHttpServer(HttpProtocolType protocol)
+                throws IOException {
         switch (protocol) {
             case HTTP:  return HttpServer.create();
-            case HTTPS: return configure(HttpsServer.create());
+               case HTTPS: return HttpsServer.create();
+               default: throw new InternalError("Unsupported protocol " + protocol);
+            }
+        }
+        static <T extends HttpServer> T create(HttpProtocolType protocol)
+                throws IOException {
+            final int max = addresses.size() + MAX;
+            for (int i = 1; i <= max; i++) {
+                HttpServer server = newHttpServer(protocol);
+                server.bind(new InetSocketAddress("127.0.0.1", 0), 0);
+                InetSocketAddress address = server.getAddress();
+                String key = address.toString();
+                if (addresses.addIfAbsent(key)) {
+                   System.out.println("Server bound to: " + key
+                                      + " after " + i + " attempt(s)");
+                   return (T) server;
+                }
+                System.out.println("warning: address " + key
+                                   + " already used. Retrying bind.");
+            }
+            throw new IOException("Couldn't bind servers after " + max + " attempts: "
+                                  + "addresses used before: " + addresses);
+        }
+    }
+
+    static HttpServer createHttpServer(HttpProtocolType protocol) throws IOException {
+        switch (protocol) {
+            case HTTP:  return HttpServerFactory.create(protocol);
+            case HTTPS: return configure(HttpServerFactory.create(protocol));
             default: throw new InternalError("Unsupported protocol " + protocol);
         }
     }
 
     static HttpsServer configure(HttpsServer server) throws IOException {

@@ -191,11 +230,10 @@
         HttpServer impl = createHttpServer(protocol);
         final HTTPTestServer server = new HTTPTestServer(impl, null, delegate);
         final HttpHandler hh = server.createHandler(schemeType, auth, authType);
         HttpContext ctxt = impl.createContext(path, hh);
         server.configureAuthentication(ctxt, schemeType, auth, authType);
-        impl.bind(new InetSocketAddress("127.0.0.1", 0), 0);
         impl.start();
         return server;
     }
 
     public static HTTPTestServer createProxy(HttpProtocolType protocol,

@@ -213,12 +251,10 @@
                 ? new HttpsProxyTunnel(impl, null, delegate)
                 : new HTTPTestServer(impl, null, delegate);
         final HttpHandler hh = server.createHandler(schemeType, auth, authType);
         HttpContext ctxt = impl.createContext(path, hh);
         server.configureAuthentication(ctxt, schemeType, auth, authType);
-
-        impl.bind(new InetSocketAddress("127.0.0.1", 0), 0);
         impl.start();
 
         return server;
     }
 

@@ -251,11 +287,10 @@
         InetSocketAddress redirectAddr = redirectTarget.getAddress();
         URL locationURL = url(targetProtocol, redirectAddr, "/");
         final HttpHandler hh = redirectingServer.create300Handler(locationURL,
                                              HttpAuthType.SERVER, code300);
         impl.createContext("/", hh);
-        impl.bind(new InetSocketAddress("127.0.0.1", 0), 0);
         impl.start();
         return redirectingServer;
     }
 
     public InetSocketAddress getAddress() {
< prev index next >