-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8208693: HttpClient: Extend the request timeout's scope to cover the response body #27469
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
Draft
vy
wants to merge
14
commits into
openjdk:master
Choose a base branch
from
vy:respBodyTime
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+846
−4
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e525599
Extend response timeout to cover body
vy 582ade7
Remove `MultiExchange::cancelTimer`
vy dcb1252
Start tests with an established connection
vy ebf6236
Fix `H3_REQUEST_REJECTED` handling in `Http3ExchangeImpl`
vy 42d2106
Extend tests to cover client's retry mechanism
vy 974ac81
Use `HTTP_3_URI_ONLY` for client's HTTP/3 discovery mechanism
vy 07f2377
Remove cross-links between tests, they are not maintainable
vy bb04f87
Document test methods
vy 5d185cd
Merge remote-tracking branch 'upstream/master' into respBodyTime
vy defe15d
Suppress warnings in `sendResponseBodySlowly()`
vy 71a5272
Fix timer leak (detected by `RedirectTimeoutTest`)
vy 2dce1b9
Improve `assertInstanceOf` statements
vy 5355782
Reset timer on retried/forwarded requests
vy 14e9b87
Check the entire causal chain to verify `HttpTimeoutException`
vy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
test/jdk/java/net/httpclient/TimeoutResponseBodyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* | ||
* 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. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import jdk.internal.net.http.common.Logger; | ||
import jdk.internal.net.http.common.Utils; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.InputStream; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/* | ||
* @test id=retriesDisabled | ||
* @bug 8208693 | ||
* @summary Verifies `HttpRequest::timeout` is effective for *response body* | ||
* timeouts when all retry mechanisms are disabled. | ||
* | ||
* @library /test/lib | ||
* /test/jdk/java/net/httpclient/lib | ||
* @build TimeoutResponseTestSupport | ||
* | ||
* @run junit/othervm | ||
* -Djdk.httpclient.auth.retrylimit=0 | ||
* -Djdk.httpclient.disableRetryConnect | ||
* -Djdk.httpclient.redirects.retrylimit=0 | ||
* -Dtest.requestTimeoutMillis=1000 | ||
* TimeoutResponseBodyTest | ||
*/ | ||
|
||
/* | ||
* @test id=retriesEnabledForResponseFailure | ||
* @bug 8208693 | ||
* @summary Verifies `HttpRequest::timeout` is effective for *response body* | ||
* timeouts, where some initial responses are intentionally configured | ||
* to fail to trigger retries. | ||
* | ||
* @library /test/lib | ||
* /test/jdk/java/net/httpclient/lib | ||
* @build TimeoutResponseTestSupport | ||
* | ||
* @run junit/othervm | ||
* -Djdk.httpclient.auth.retrylimit=0 | ||
* -Djdk.httpclient.disableRetryConnect | ||
* -Djdk.httpclient.redirects.retrylimit=3 | ||
* -Dtest.requestTimeoutMillis=1000 | ||
* -Dtest.responseFailureWaitDurationMillis=600 | ||
* TimeoutResponseBodyTest | ||
*/ | ||
|
||
/** | ||
* Verifies {@link HttpRequest#timeout() HttpRequest.timeout()} is effective | ||
* for <b>response body</b> timeouts. | ||
* | ||
* @implNote | ||
* | ||
* Using a response body subscriber (i.e., {@link InputStream}) of type that | ||
* allows gradual consumption of the response body after successfully building | ||
* an {@link HttpResponse} instance to ensure timeouts are propagated even | ||
* after the {@code HttpResponse} construction. | ||
* <p> | ||
* Each test is provided a pristine ephemeral client to avoid any unexpected | ||
* effects due to pooling. | ||
*/ | ||
class TimeoutResponseBodyTest extends TimeoutResponseTestSupport { | ||
|
||
private static final Logger LOGGER = Utils.getDebugLogger( | ||
TimeoutResponseBodyTest.class.getSimpleName()::toString, Utils.DEBUG); | ||
|
||
/** | ||
* Tests timeouts using | ||
* {@link HttpClient#send(HttpRequest, HttpResponse.BodyHandler) HttpClient::send} | ||
* against a server blocking without delivering the response body. | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("serverRequestPairs") | ||
void testSendOnMissingBody(ServerRequestPair pair) throws Exception { | ||
|
||
ServerRequestPair.SERVER_HANDLER_BEHAVIOUR = | ||
ServerRequestPair.ServerHandlerBehaviour.BLOCK_BEFORE_BODY_DELIVERY; | ||
|
||
try (HttpClient client = pair.createClientWithEstablishedConnection()) { | ||
assertTimeoutPreemptively(REQUEST_TIMEOUT.multipliedBy(2), () -> { | ||
LOGGER.log("Sending the request"); | ||
HttpResponse<InputStream> response = client.send( | ||
pair.request(), HttpResponse.BodyHandlers.ofInputStream()); | ||
LOGGER.log("Consuming the obtained response"); | ||
verifyResponseBodyDoesNotArrive(response); | ||
}); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Tests timeouts using | ||
* {@link HttpClient#sendAsync(HttpRequest, HttpResponse.BodyHandler) HttpClient::sendAsync} | ||
* against a server blocking without delivering the response body. | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("serverRequestPairs") | ||
void testSendAsyncOnMissingBody(ServerRequestPair pair) throws Exception { | ||
|
||
ServerRequestPair.SERVER_HANDLER_BEHAVIOUR = | ||
ServerRequestPair.ServerHandlerBehaviour.BLOCK_BEFORE_BODY_DELIVERY; | ||
|
||
try (HttpClient client = pair.createClientWithEstablishedConnection()) { | ||
assertTimeoutPreemptively(REQUEST_TIMEOUT.multipliedBy(2), () -> { | ||
LOGGER.log("Sending the request asynchronously"); | ||
CompletableFuture<HttpResponse<InputStream>> responseFuture = client.sendAsync( | ||
pair.request(), HttpResponse.BodyHandlers.ofInputStream()); | ||
LOGGER.log("Obtaining the response"); | ||
HttpResponse<InputStream> response = responseFuture.get(); | ||
LOGGER.log("Consuming the obtained response"); | ||
verifyResponseBodyDoesNotArrive(response); | ||
}); | ||
} | ||
|
||
} | ||
|
||
private static void verifyResponseBodyDoesNotArrive(HttpResponse<InputStream> response) { | ||
assertEquals(200, response.statusCode()); | ||
assertThrowsHttpTimeoutException(() -> { | ||
try (InputStream responseBodyStream = response.body()) { | ||
int readByte = responseBodyStream.read(); | ||
fail("Unexpected read byte: " + readByte); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Tests timeouts using | ||
* {@link HttpClient#send(HttpRequest, HttpResponse.BodyHandler) HttpClient::send} | ||
* against a server delivering the response body very slowly. | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("serverRequestPairs") | ||
void testSendOnSlowBody(ServerRequestPair pair) throws Exception { | ||
|
||
ServerRequestPair.SERVER_HANDLER_BEHAVIOUR = | ||
ServerRequestPair.ServerHandlerBehaviour.DELIVER_BODY_SLOWLY; | ||
|
||
try (HttpClient client = pair.createClientWithEstablishedConnection()) { | ||
assertTimeoutPreemptively(REQUEST_TIMEOUT.multipliedBy(2), () -> { | ||
LOGGER.log("Sending the request"); | ||
HttpResponse<InputStream> response = client.send( | ||
pair.request(), HttpResponse.BodyHandlers.ofInputStream()); | ||
LOGGER.log("Consuming the obtained response"); | ||
verifyResponseBodyArrivesSlow(response); | ||
}); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Tests timeouts using | ||
* {@link HttpClient#sendAsync(HttpRequest, HttpResponse.BodyHandler) HttpClient::sendAsync} | ||
* against a server delivering the response body very slowly. | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource("serverRequestPairs") | ||
void testSendAsyncOnSlowBody(ServerRequestPair pair) throws Exception { | ||
|
||
ServerRequestPair.SERVER_HANDLER_BEHAVIOUR = | ||
ServerRequestPair.ServerHandlerBehaviour.DELIVER_BODY_SLOWLY; | ||
|
||
try (HttpClient client = pair.createClientWithEstablishedConnection()) { | ||
assertTimeoutPreemptively(REQUEST_TIMEOUT.multipliedBy(2), () -> { | ||
LOGGER.log("Sending the request asynchronously"); | ||
CompletableFuture<HttpResponse<InputStream>> responseFuture = client.sendAsync( | ||
pair.request(), HttpResponse.BodyHandlers.ofInputStream()); | ||
LOGGER.log("Obtaining the response"); | ||
HttpResponse<InputStream> response = responseFuture.get(); | ||
LOGGER.log("Consuming the obtained response"); | ||
verifyResponseBodyArrivesSlow(response); | ||
}); | ||
} | ||
|
||
} | ||
|
||
private static void verifyResponseBodyArrivesSlow(HttpResponse<InputStream> response) { | ||
assertEquals(200, response.statusCode()); | ||
assertThrowsHttpTimeoutException(() -> { | ||
try (InputStream responseBodyStream = response.body()) { | ||
int i = 0; | ||
int l = ServerRequestPair.CONTENT_LENGTH; | ||
for (; i < l; i++) { | ||
LOGGER.log("Reading byte %s/%s", i, l); | ||
int readByte = responseBodyStream.read(); | ||
if (readByte < 0) { | ||
break; | ||
} | ||
assertEquals(i, readByte); | ||
} | ||
fail("Should not have reached here! (i=%s)".formatted(i)); | ||
} | ||
}); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Without this, server handler's
exchange.resetStream(Http3Error.H3_REQUEST_REJECTED.code())
results in client to fail the request, instead of retrying it. I am not sure ifH3_REQUEST_REJECTED
is the only error code we should guard against.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.
Figured the problem is more convoluted than it appears. @jaikiran will soon land a PR adressing this issue. I will hold this PR until then.