|
29 | 29 |
|
30 | 30 | import java.io.IOException; |
31 | 31 | import java.net.InetSocketAddress; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | +import java.util.concurrent.TimeUnit; |
32 | 37 | import java.util.concurrent.TimeoutException; |
| 38 | +import java.util.concurrent.atomic.AtomicLong; |
33 | 39 | import java.util.function.Consumer; |
34 | 40 |
|
35 | 41 | import org.apache.hc.client5.http.HttpRoute; |
|
63 | 69 | import org.apache.hc.core5.io.CloseMode; |
64 | 70 | import org.apache.hc.core5.pool.PoolConcurrencyPolicy; |
65 | 71 | import org.apache.hc.core5.pool.PoolReusePolicy; |
| 72 | +import org.apache.hc.core5.pool.PoolStats; |
66 | 73 | import org.apache.hc.core5.util.TimeValue; |
67 | 74 | import org.apache.hc.core5.util.Timeout; |
68 | 75 | import org.junit.jupiter.api.AfterEach; |
@@ -391,4 +398,98 @@ void testConnectionTimeoutSetting() throws Exception { |
391 | 398 | connManager.close(); |
392 | 399 | } |
393 | 400 |
|
| 401 | + @Test |
| 402 | + void testConnectionRequestTimeout() throws Exception { |
| 403 | + configureServer(bootstrap -> bootstrap |
| 404 | + .register("/random/*", new RandomHandler())); |
| 405 | + final HttpHost target = startServer(); |
| 406 | + |
| 407 | + connManager.setMaxTotal(1); |
| 408 | + |
| 409 | + final HttpRoute route = new HttpRoute(target, null, false); |
| 410 | + final Timeout connRequestTimeout = Timeout.ofMicroseconds(1); |
| 411 | + |
| 412 | + final int concurrentThreads = 10; |
| 413 | + final CountDownLatch countDownLatch = new CountDownLatch(concurrentThreads); |
| 414 | + final AtomicLong n = new AtomicLong(concurrentThreads * 100); |
| 415 | + |
| 416 | + final ExecutorService executorService = Executors.newFixedThreadPool(concurrentThreads); |
| 417 | + for (int i = 0; i < concurrentThreads; i++) { |
| 418 | + executorService.execute(() -> { |
| 419 | + try { |
| 420 | + while (n.decrementAndGet() > 0) { |
| 421 | + try { |
| 422 | + final LeaseRequest request = connManager.lease("id1", route, connRequestTimeout, null); |
| 423 | + final ConnectionEndpoint connectionEndpoint = request.get(connRequestTimeout); |
| 424 | + connManager.release(connectionEndpoint, null, null); |
| 425 | + } catch (final InterruptedException ex) { |
| 426 | + Thread.currentThread().interrupt(); |
| 427 | + Assertions.fail("Unexpected exception", ex); |
| 428 | + } catch (final TimeoutException | ExecutionException ignored) { |
| 429 | + } |
| 430 | + } |
| 431 | + } finally { |
| 432 | + countDownLatch.countDown(); |
| 433 | + } |
| 434 | + }); |
| 435 | + } |
| 436 | + |
| 437 | + Assertions.assertTrue(countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())); |
| 438 | + Assertions.assertTrue(n.get() <= 0); |
| 439 | + |
| 440 | + final PoolStats stats = connManager.getStats(route); |
| 441 | + Assertions.assertEquals(0, stats.getLeased()); |
| 442 | + |
| 443 | + connManager.close(); |
| 444 | + } |
| 445 | + |
| 446 | + @Test |
| 447 | + void testConnectionRequestCancelLateLeaseReleased() throws Exception { |
| 448 | + configureServer(bootstrap -> bootstrap |
| 449 | + .register("/random/*", new RandomHandler())); |
| 450 | + final HttpHost target = startServer(); |
| 451 | + |
| 452 | + connManager.setMaxTotal(1); |
| 453 | + |
| 454 | + final HttpRoute route = new HttpRoute(target, null, false); |
| 455 | + final Timeout t = Timeout.ofSeconds(5); |
| 456 | + |
| 457 | + final LeaseRequest holdRequest = connManager.lease("hold", route, t, null); |
| 458 | + final ConnectionEndpoint heldEndpoint = holdRequest.get(t); |
| 459 | + |
| 460 | + final LeaseRequest pendingRequest = connManager.lease("pending", route, t, null); |
| 461 | + |
| 462 | + connManager.release(heldEndpoint, null, null); |
| 463 | + |
| 464 | + PoolStats stats; |
| 465 | + final long deadline1 = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); |
| 466 | + for (;;) { |
| 467 | + stats = connManager.getStats(route); |
| 468 | + if (stats.getLeased() == 1) { |
| 469 | + break; |
| 470 | + } |
| 471 | + if (System.nanoTime() > deadline1) { |
| 472 | + break; |
| 473 | + } |
| 474 | + Thread.yield(); |
| 475 | + } |
| 476 | + Assertions.assertEquals(1, stats.getLeased(), "Expected pending lease to complete and become leased"); |
| 477 | + Assertions.assertFalse(pendingRequest.cancel(), "Expected cancel() to lose the race once lease is completed"); |
| 478 | + |
| 479 | + final long deadline2 = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); |
| 480 | + for (;;) { |
| 481 | + stats = connManager.getStats(route); |
| 482 | + if (stats.getLeased() == 0) { |
| 483 | + break; |
| 484 | + } |
| 485 | + if (System.nanoTime() > deadline2) { |
| 486 | + break; |
| 487 | + } |
| 488 | + Thread.yield(); |
| 489 | + } |
| 490 | + Assertions.assertEquals(0, stats.getLeased(), "Late-completed lease must not remain stranded after cancel()"); |
| 491 | + |
| 492 | + connManager.close(); |
| 493 | + } |
| 494 | + |
394 | 495 | } |
0 commit comments