|
37 | 37 | import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; |
38 | 38 | import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; |
39 | 39 | import org.apache.hc.client5.http.config.ConnectionConfig; |
| 40 | +import org.apache.hc.client5.http.impl.ConnectionHolder; |
40 | 41 | import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; |
41 | 42 | import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; |
42 | 43 | import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint; |
|
45 | 46 | import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel; |
46 | 47 | import org.apache.hc.client5.testing.extension.async.TestAsyncClient; |
47 | 48 | import org.apache.hc.core5.http.ContentType; |
| 49 | +import org.apache.hc.core5.http.HttpConnection; |
48 | 50 | import org.apache.hc.core5.http.HttpHeaders; |
49 | 51 | import org.apache.hc.core5.http.HttpHost; |
50 | 52 | import org.apache.hc.core5.http.HttpStatus; |
@@ -89,11 +91,9 @@ protected SimpleHttpResponse handle( |
89 | 91 | void testReleaseConnection() throws Exception { |
90 | 92 | final HttpHost target = startServer(); |
91 | 93 |
|
92 | | - final PoolingAsyncClientConnectionManager connManager = PoolingAsyncClientConnectionManagerBuilder.create() |
93 | | - .build(); |
94 | | - configureClient(builder -> builder.setConnectionManager(connManager)); |
95 | 94 | final TestAsyncClient client = startClient(); |
96 | 95 |
|
| 96 | + final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager(); |
97 | 97 | connManager.setMaxTotal(1); |
98 | 98 |
|
99 | 99 | final HttpRoute route = new HttpRoute(target, null, false); |
@@ -159,11 +159,9 @@ void testReleaseConnection() throws Exception { |
159 | 159 | void testReleaseConnectionWithTimeLimits() throws Exception { |
160 | 160 | final HttpHost target = startServer(); |
161 | 161 |
|
162 | | - final PoolingAsyncClientConnectionManager connManager = PoolingAsyncClientConnectionManagerBuilder.create() |
163 | | - .build(); |
164 | | - configureClient(builder -> builder.setConnectionManager(connManager)); |
165 | 162 | final TestAsyncClient client = startClient(); |
166 | 163 |
|
| 164 | + final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager(); |
167 | 165 | connManager.setMaxTotal(1); |
168 | 166 |
|
169 | 167 | final HttpRoute route = new HttpRoute(target, null, false); |
@@ -218,11 +216,9 @@ void testReleaseConnectionWithTimeLimits() throws Exception { |
218 | 216 | void testCloseExpiredIdleConnections() throws Exception { |
219 | 217 | final HttpHost target = startServer(); |
220 | 218 |
|
221 | | - final PoolingAsyncClientConnectionManager connManager = PoolingAsyncClientConnectionManagerBuilder.create() |
222 | | - .build(); |
223 | | - configureClient(builder -> builder.setConnectionManager(connManager)); |
224 | 219 | final TestAsyncClient client = startClient(); |
225 | 220 |
|
| 221 | + final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager(); |
226 | 222 | connManager.setMaxTotal(1); |
227 | 223 |
|
228 | 224 | final HttpRoute route = new HttpRoute(target, null, false); |
@@ -312,4 +308,52 @@ void testCloseExpiredTTLConnections() throws Exception { |
312 | 308 | connManager.close(); |
313 | 309 | } |
314 | 310 |
|
| 311 | + @Test |
| 312 | + void testConnectionTimeoutSetting() throws Exception { |
| 313 | + final HttpHost target = startServer(); |
| 314 | + |
| 315 | + final TestAsyncClient client = startClient(); |
| 316 | + |
| 317 | + final Timeout connectionSocketTimeout = Timeout.ofMinutes(5); |
| 318 | + |
| 319 | + final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager(); |
| 320 | + connManager.setMaxTotal(1); |
| 321 | + connManager.setDefaultConnectionConfig(ConnectionConfig.custom() |
| 322 | + .setSocketTimeout(connectionSocketTimeout) |
| 323 | + .build()); |
| 324 | + |
| 325 | + final HttpRoute route = new HttpRoute(target, null, false); |
| 326 | + |
| 327 | + final SimpleHttpRequest request = SimpleRequestBuilder.get() |
| 328 | + .setHttpHost(target) |
| 329 | + .setPath("/") |
| 330 | + .addHeader(HttpHeaders.HOST, target.toHostString()) |
| 331 | + .build(); |
| 332 | + final HttpClientContext context = HttpClientContext.create(); |
| 333 | + |
| 334 | + final Future<AsyncConnectionEndpoint> endpointFuture1 = connManager.lease("id1", route, null, TIMEOUT, null); |
| 335 | + final AsyncConnectionEndpoint endpoint1 = endpointFuture1.get(LEASE_TIMEOUT.getDuration(), LEASE_TIMEOUT.getTimeUnit()); |
| 336 | + |
| 337 | + final Future<AsyncConnectionEndpoint> connectFuture1 = connManager.connect(endpoint1, client.getImplementation(), TIMEOUT, null, context, null); |
| 338 | + final AsyncConnectionEndpoint openEndpoint1 = connectFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); |
| 339 | + |
| 340 | + // Modify socket timeout of the endpoint |
| 341 | + endpoint1.setSocketTimeout(Timeout.ofSeconds(30)); |
| 342 | + |
| 343 | + final Future<SimpleHttpResponse> responseFuture1 = openEndpoint1.execute("ex-1", SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), null); |
| 344 | + final SimpleHttpResponse response1 = responseFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); |
| 345 | + Assertions.assertEquals(HttpStatus.SC_OK, response1.getCode()); |
| 346 | + |
| 347 | + connManager.release(endpoint1, null, TimeValue.NEG_ONE_MILLISECOND); |
| 348 | + |
| 349 | + final Future<AsyncConnectionEndpoint> endpointFuture2 = connManager.lease("id2", route, null, TIMEOUT, null); |
| 350 | + final AsyncConnectionEndpoint endpoint2 = endpointFuture2.get(LEASE_TIMEOUT.getDuration(), LEASE_TIMEOUT.getTimeUnit()); |
| 351 | + Assertions.assertTrue(endpoint2.isConnected()); |
| 352 | + |
| 353 | + final HttpConnection connection = ((ConnectionHolder) endpoint2).get(); |
| 354 | + Assertions.assertEquals(connectionSocketTimeout, connection.getSocketTimeout()); |
| 355 | + |
| 356 | + connManager.close(); |
| 357 | + } |
| 358 | + |
315 | 359 | } |
0 commit comments