From 0dce5b811d64ac17b9580d6a2d8eca1df70990a1 Mon Sep 17 00:00:00 2001 From: Daniel Fuchs Date: Wed, 14 Dec 2022 16:39:32 +0000 Subject: [PATCH] 8296610: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java failed with "BindException: Address already in use: connect" Reviewed-by: michaelm --- .../HTTPSetAuthenticatorTest.java | 60 +++++++++---------- .../SetAuthenticator/HTTPTestClient.java | 30 ++++++++++ 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java index 243c79d9644..6145f48523d 100644 --- a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java +++ b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java @@ -191,25 +191,25 @@ public class HTTPSetAuthenticatorTest extends HTTPTest { // Now tries with explicitly setting the default authenticator: it should // be invoked again. // Uncomment the code below when 8169068 is available. -// System.out.println("\nClient: Explicitly setting the default authenticator: " -// + toString(Authenticator.getDefault())); -// HTTPTestClient.connect(protocol, server, mode, Authenticator.getDefault()); -// count = authOne.count.get(); -// if (count != expectedIncrement) { -// throw new AssertionError("Authenticator #1 called " + count(count) -// + " expected it to be called " + expected(expectedIncrement)); -// } -// count = authTwo.count.get(); -// if (count != expectedIncrement) { -// throw new AssertionError("Authenticator #2 called " + count(count) -// + " expected it to be called " + expected(expectedIncrement)); -// } -// count = AUTHENTICATOR.count.get(); -// if (count != defaultCount + 2 * expectedIncrement) { -// throw new AssertionError("Default Authenticator called " + count(count) -// + " expected it to be called " -// + expected(defaultCount + 2 * expectedIncrement)); -// } + System.out.println("\nClient: Explicitly setting the default authenticator: " + + toString(Authenticator.getDefault())); + HTTPTestClient.connect(protocol, server, mode, Authenticator.getDefault()); + count = authOne.count.get(); + if (count != expectedIncrement) { + throw new AssertionError("Authenticator #1 called " + count(count) + + " expected it to be called " + expected(expectedIncrement)); + } + count = authTwo.count.get(); + if (count != expectedIncrement) { + throw new AssertionError("Authenticator #2 called " + count(count) + + " expected it to be called " + expected(expectedIncrement)); + } + count = AUTHENTICATOR.count.get(); + if (count != defaultCount + 2 * expectedIncrement) { + throw new AssertionError("Default Authenticator called " + count(count) + + " expected it to be called " + + expected(defaultCount + 2 * expectedIncrement)); + } // Now tries to set an authenticator on a connected connection. URL url = url(protocol, server.getAddress(), "/"); @@ -238,16 +238,16 @@ public class HTTPSetAuthenticatorTest extends HTTPTest { + ise); } // Uncomment the code below when 8169068 is available. -// try { -// conn.setAuthenticator(Authenticator.getDefault()); -// throw new RuntimeException("Expected IllegalStateException" -// + " trying to set an authenticator after connect" -// + " not raised."); -// } catch (IllegalStateException ise) { -// System.out.println("Client: caught expected ISE" -// + " trying to set an authenticator after connect: " -// + ise); -// } + try { + conn.setAuthenticator(Authenticator.getDefault()); + throw new RuntimeException("Expected IllegalStateException" + + " trying to set an authenticator after connect" + + " not raised."); + } catch (IllegalStateException ise) { + System.out.println("Client: caught expected ISE" + + " trying to set an authenticator after connect: " + + ise); + } try { conn.setAuthenticator(null); throw new RuntimeException("Expected" @@ -279,7 +279,7 @@ public class HTTPSetAuthenticatorTest extends HTTPTest { // All good! // return the number of times the default authenticator is supposed // to have been called. - return scheme == HttpSchemeType.NONE ? 0 : 1 * EXPECTED_AUTH_CALLS_PER_TEST; + return scheme == HttpSchemeType.NONE ? 0 : 2 * EXPECTED_AUTH_CALLS_PER_TEST; } static String toString(Authenticator a) { diff --git a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestClient.java b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestClient.java index 4d1f5c52a78..3f436ef6d02 100644 --- a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestClient.java +++ b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestClient.java @@ -23,10 +23,12 @@ import java.io.IOException; import java.net.Authenticator; +import java.net.BindException; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; +import java.time.Duration; import javax.net.ssl.HttpsURLConnection; /** @@ -35,11 +37,39 @@ import javax.net.ssl.HttpsURLConnection; */ public class HTTPTestClient extends HTTPTest { + public static final long DELAY_BEFORE_RETRY = 2500; // milliseconds + public static void connect(HttpProtocolType protocol, HTTPTestServer server, HttpAuthType authType, Authenticator auth) throws IOException { + try { + doConnect(protocol, server, authType, auth); + } catch (BindException ex) { + // sleep a bit then try again once + System.out.println("WARNING: Unexpected BindException: " + ex); + System.out.println("\tSleeping a bit and try again..."); + long start = System.nanoTime(); + System.gc(); + try { + Thread.sleep(DELAY_BEFORE_RETRY); + } catch (InterruptedException iex) { + // ignore + } + System.gc(); + System.out.println("\tRetrying after " + + Duration.ofNanos(System.nanoTime() - start).toMillis() + + " milliseconds"); + doConnect(protocol, server, authType, auth); + } + } + + public static void doConnect(HttpProtocolType protocol, + HTTPTestServer server, + HttpAuthType authType, + Authenticator auth) + throws IOException { InetSocketAddress address = server.getAddress(); final URL url = url(protocol, address, "/");