--- old/test/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java 2017-02-02 15:57:54.000000000 +0000 +++ new/test/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java 2017-02-02 15:57:54.000000000 +0000 @@ -55,6 +55,7 @@ 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; @@ -145,10 +146,48 @@ } } + /** + * 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 addresses = + new CopyOnWriteArrayList<>(); + private static HttpServer newHttpServer(HttpProtocolType protocol) + throws IOException { + switch (protocol) { + case HTTP: return HttpServer.create(); + case HTTPS: return HttpsServer.create(); + default: throw new InternalError("Unsupported protocol " + protocol); + } + } + static 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 HttpServer.create(); - case HTTPS: return configure(HttpsServer.create()); + case HTTP: return HttpServerFactory.create(protocol); + case HTTPS: return configure(HttpServerFactory.create(protocol)); default: throw new InternalError("Unsupported protocol " + protocol); } } @@ -193,7 +232,6 @@ 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; } @@ -215,8 +253,6 @@ 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; @@ -253,7 +289,6 @@ 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; }