Skip to content

Commit

Permalink
8296610: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthentic…
Browse files Browse the repository at this point in the history
…atorTest.java failed with "BindException: Address already in use: connect"

Backport-of: 0dce5b811d64ac17b9580d6a2d8eca1df70990a1
  • Loading branch information
GoeLin committed Apr 1, 2024
1 parent e59eeb0 commit 7680369
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,25 @@ public int run(HTTPTestServer server,
// 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(), "/");
Expand Down Expand Up @@ -238,16 +238,16 @@ public int run(HTTPTestServer server,
+ 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"
Expand Down Expand Up @@ -279,7 +279,7 @@ public int run(HTTPTestServer server,
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -35,11 +37,39 @@
*/
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, "/");
Expand Down

1 comment on commit 7680369

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.