Skip to content

Commit cee63f7

Browse files
committed
Bug-fix: corrects message exchange cancellation logic in InternalHttpAsyncExecRuntime
* Fixes the problem with message exchanges over an existing persistent connection being non-cancellable * Hard-cancellation request parameter should have no bearing on H2 connections * Response timeout not be applied to H2 endpoints * Test coverage
1 parent 23041ea commit cee63f7

1 file changed

Lines changed: 8 additions & 65 deletions

File tree

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestAsyncExecRuntime.java renamed to httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestInternalHttpAsyncExecRuntime.java

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@
6262
import org.junit.jupiter.api.Test;
6363
import org.junit.jupiter.api.extension.RegisterExtension;
6464

65-
public class TestAsyncExecRuntime {
65+
public class TestInternalHttpAsyncExecRuntime {
6666

6767
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
6868

6969
@RegisterExtension
7070
private final TestAsyncResources testResources;
7171

72-
public TestAsyncExecRuntime() {
72+
public TestInternalHttpAsyncExecRuntime() {
7373
this.testResources = new TestAsyncResources(URIScheme.HTTP, ClientProtocolLevel.STANDARD, ServerProtocolLevel.STANDARD, TIMEOUT);
7474
}
7575

@@ -89,6 +89,8 @@ public TestAsyncClient startClient() throws Exception {
8989
return client;
9090
}
9191

92+
static final int REQ_NUM = 5;
93+
9294
HttpRequest createRequest(final HttpHost target) {
9395
return BasicRequestBuilder.get()
9496
.setHttpHost(target)
@@ -105,7 +107,7 @@ void testExecutionCancellation_http11HardCancellation_connectionMarkedNonReusabl
105107
final TestAsyncClient client = startClient();
106108
final ConnectionInitiator connectionInitiator = client.getImplementation();
107109
final PoolingAsyncClientConnectionManager connectionManager = client.getConnectionManager();
108-
for (int i = 0; i < 5; i++) {
110+
for (int i = 0; i < REQ_NUM; i++) {
109111
final HttpClientContext context = HttpClientContext.create();
110112

111113
final InternalTestHttpAsyncExecRuntime testRuntime = new InternalTestHttpAsyncExecRuntime(
@@ -119,7 +121,7 @@ void testExecutionCancellation_http11HardCancellation_connectionMarkedNonReusabl
119121

120122
final BasicFuture<Message<HttpResponse, byte[]>> resultFuture = new BasicFuture<>(null);
121123
final Cancellable cancellable = testRuntime.execute(
122-
"test",
124+
"test-" + i,
123125
AsyncClientPipeline.assemble()
124126
.request(createRequest(target)).noContent()
125127
.response().asByteArray()
@@ -155,7 +157,7 @@ void testExecutionCancellation_http11NoHardCancellation_connectionAlive() throws
155157
final TestAsyncClient client = startClient();
156158
final ConnectionInitiator connectionInitiator = client.getImplementation();
157159
final PoolingAsyncClientConnectionManager connectionManager = client.getConnectionManager();
158-
for (int i = 0; i < 5; i++) {
160+
for (int i = 0; i < REQ_NUM; i++) {
159161
final HttpClientContext context = HttpClientContext.create();
160162
context.setRequestConfig(RequestConfig.custom()
161163
.setHardCancellationEnabled(false)
@@ -172,7 +174,7 @@ void testExecutionCancellation_http11NoHardCancellation_connectionAlive() throws
172174

173175
final BasicFuture<Message<HttpResponse, byte[]>> resultFuture = new BasicFuture<>(null);
174176
final Cancellable cancellable = testRuntime.execute(
175-
"test",
177+
"test-" + i,
176178
AsyncClientPipeline.assemble()
177179
.request(createRequest(target)).noContent()
178180
.response().asByteArray()
@@ -204,63 +206,4 @@ public void completed(final Message<HttpResponse, byte[]> result) {
204206
}
205207
}
206208

207-
@Test
208-
void testExecutionCancellation_http2_connectionAlive() throws Exception {
209-
configureServer(bootstrap -> {
210-
bootstrap.setServerProtocolLevel(ServerProtocolLevel.H2_ONLY);
211-
bootstrap.register("/random/*", AsyncRandomHandler::new);
212-
});
213-
final HttpHost target = startServer();
214-
215-
final TestAsyncClient client = startClient();
216-
final ConnectionInitiator connectionInitiator = client.getImplementation();
217-
final PoolingAsyncClientConnectionManager connectionManager = client.getConnectionManager();
218-
219-
for (int i = 0; i < 5; i++) {
220-
final HttpClientContext context = HttpClientContext.create();
221-
final InternalTestHttpAsyncExecRuntime testRuntime = new InternalTestHttpAsyncExecRuntime(
222-
connectionManager,
223-
connectionInitiator,
224-
TlsConfig.custom()
225-
.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
226-
.build());
227-
final Future<Boolean> connectFuture = testRuntime.leaseAndConnect(target, context);
228-
Assertions.assertTrue(connectFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
229-
230-
final BasicFuture<Message<HttpResponse, byte[]>> resultFuture = new BasicFuture<>(null);
231-
final Cancellable cancellable = testRuntime.execute(
232-
"test",
233-
AsyncClientPipeline.assemble()
234-
.request(createRequest(target)).noContent()
235-
.response().asByteArray()
236-
.result(new FutureContribution<Message<HttpResponse, byte[]>>(resultFuture) {
237-
238-
@Override
239-
public void completed(final Message<HttpResponse, byte[]> result) {
240-
resultFuture.completed(result);
241-
}
242-
243-
})
244-
.create(),
245-
context);
246-
// sleep a bit
247-
Thread.sleep(i % 10);
248-
cancellable.cancel();
249-
250-
// The message exchange is expected to get aborted
251-
try {
252-
resultFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
253-
} catch (final CancellationException expected) {
254-
}
255-
Assertions.assertTrue(testRuntime.isAborted());
256-
// The underlying connection is expected to stay valid
257-
Assertions.assertTrue(testRuntime.isEndpointConnected());
258-
testRuntime.markConnectionReusable(null, TimeValue.ofMinutes(1));
259-
testRuntime.releaseEndpoint();
260-
261-
final PoolStats totalStats = connectionManager.getTotalStats();
262-
Assertions.assertTrue(totalStats.getAvailable() > 0);
263-
}
264-
}
265-
266209
}

0 commit comments

Comments
 (0)