-
Notifications
You must be signed in to change notification settings - Fork 5.9k
8340182: Java HttpClient does not follow default retry limit of 3 retries #25490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
|
||
/* | ||
* @test | ||
* @bug 8340182 | ||
* @summary Retry limit system property | ||
* @library /test/lib /test/jdk/java/net/httpclient/lib | ||
* @build jdk.httpclient.test.lib.http2.Http2TestServer | ||
* @run junit HttpClientRetryLimitTest | ||
* @run junit/othervm -Djdk.httpclient.auth.retrylimit=1 HttpClientRetryLimitTest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dfuch, shall we test against zero and negative values too? (Both are accepted by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could. We could also throw in garbage to see what happens. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New test scenarios have been added in 18bac9f. We have default, positive, zero and negative values. |
||
* @run junit/othervm -Djdk.httpclient.auth.retrylimit=0 HttpClientRetryLimitTest | ||
* @run junit/othervm -Djdk.httpclient.auth.retrylimit=-1 HttpClientRetryLimitTest | ||
*/ | ||
|
||
import jdk.httpclient.test.lib.common.HttpServerAdapters; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.IOException; | ||
import java.net.Authenticator; | ||
import java.net.PasswordAuthentication; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
|
||
class HttpClientRetryLimitTest implements HttpServerAdapters { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable to rename - and fix the |
||
|
||
// This is the system default value for jdk.httpclient.auth.retrylimit | ||
private static final int DEFAULT_RETRY_LIMIT = 3; | ||
private static final int RETRY_LIMIT = Integer.getInteger( | ||
"jdk.httpclient.auth.retrylimit", DEFAULT_RETRY_LIMIT); | ||
|
||
static Stream<HttpClient.Version> args() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also test against with SSL and without SSL cases. See |
||
return Stream.of( | ||
HttpClient.Version.HTTP_1_1, | ||
HttpClient.Version.HTTP_2 | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("args") | ||
public void testDefaultSystemProperty(HttpClient.Version version) throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you made the class package-private in 18bac9f. You could have additionally made the method package-private too. |
||
AtomicInteger requestCount = new AtomicInteger(0); | ||
|
||
try (HttpTestServer httpTestServer = HttpTestServer.create(version)) { | ||
|
||
HttpTestHandler httpTestHandler = t -> { | ||
t.getResponseHeaders() | ||
.addHeader("WWW-Authenticate", "Basic realm=\"Test\""); | ||
t.sendResponseHeaders(401,0); | ||
}; | ||
|
||
httpTestServer.addHandler(httpTestHandler, "/"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the past, there has been occasions in CI where a test server received connections from a totally unrelated test running in parallel on the same host. To avoid such mishaps, we better salt the path a bit by, say, appending the class' simple name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the catch - the class simple name has been appended in 18bac9f There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @p-nima, the handler still accepts all calls to |
||
httpTestServer.start(); | ||
try ( | ||
HttpClient client = HttpClient.newBuilder() | ||
.authenticator(new Authenticator() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure the client will fire the request using the protocol version of our preference, could you also pass |
||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
requestCount.incrementAndGet(); | ||
return new PasswordAuthentication("username", "password".toCharArray()); | ||
} | ||
}) | ||
.build()) { | ||
|
||
HttpRequest request = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(new URI("http://" + httpTestServer.serverAuthority() + "/" + this.getClass().getSimpleName() + "/")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure the client will fire the request using the protocol version of our preference, could you also pass |
||
.build(); | ||
throw assertThrows(IOException.class, () -> client.send(request, HttpResponse.BodyHandlers.discarding())); | ||
} catch (Exception e) { | ||
if (RETRY_LIMIT > 0){ | ||
assertEquals(RETRY_LIMIT, requestCount.get(), | ||
"Expected number of request retries was " + RETRY_LIMIT + " but actual was "+requestCount); | ||
} | ||
else{ | ||
requestCount.decrementAndGet(); | ||
assertEquals(0, requestCount.get(), | ||
"Expected number of request retries was 0 but actual was "+requestCount); | ||
} | ||
e.printStackTrace(); | ||
} | ||
Comment on lines
+101
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, you should be using
|
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think adding a retrylimit=0 would be beneficial too? This way every scenario would be covered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added additional test case scenarios in 18bac9f