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"

Reviewed-by: michaelm
  • Loading branch information
dfuch committed Dec 14, 2022
1 parent ed8a212 commit 0dce5b8
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

3 comments on commit 0dce5b8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 0dce5b8 Mar 28, 2024

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 0dce5b8 Mar 28, 2024

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-0dce5b81 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 0dce5b81 from the openjdk/jdk repository.

The commit being backported was authored by Daniel Fuchs on 14 Dec 2022 and was reviewed by Michael McMahon.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-0dce5b81:backport-GoeLin-0dce5b81
$ git checkout backport-GoeLin-0dce5b81
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-0dce5b81

Please sign in to comment.