diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java index 15eb844e5..2a6751745 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java @@ -596,6 +596,7 @@ public void enumAvailable(final Callback> callback) { } finally { this.lock.unlock(); } + fireCallbacks(); } /** @@ -615,6 +616,7 @@ public void enumLeased(final Callback> callback) { } finally { this.lock.unlock(); } + fireCallbacks(); } private void purgePoolMap() { diff --git a/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java new file mode 100644 index 000000000..383e439e3 --- /dev/null +++ b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolPendingRequestExpiry.java @@ -0,0 +1,102 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.core5.pool; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.hc.core5.io.CloseMode; +import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +final class TestPoolPendingRequestExpiry { + + private static ManagedConnPool createPool(final PoolConcurrencyPolicy policy) { + switch (policy) { + case STRICT: + return new StrictConnPool<>(1, 1); + case LAX: + return new LaxConnPool<>(1); + default: + return new RouteSegmentedConnPool<>( + 1, 1, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, (c, mode) -> c.close(mode)); + } + } + + @ParameterizedTest + @EnumSource(PoolConcurrencyPolicy.class) + void closeExpiredFailsExpiredPendingRequest(final PoolConcurrencyPolicy policy) throws Exception { + final ManagedConnPool pool = createPool(policy); + try { + final PoolEntry leased = + pool.lease("r1", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + assertNotNull(leased); + final Future> pending = + pool.lease("r1", null, Timeout.ofMilliseconds(50), null); + + Thread.sleep(150); + pool.closeExpired(); + + final ExecutionException ex = assertThrows(ExecutionException.class, + () -> pending.get(5, TimeUnit.SECONDS)); + assertInstanceOf(TimeoutException.class, ex.getCause()); + } finally { + pool.close(CloseMode.IMMEDIATE); + } + } + + @ParameterizedTest + @EnumSource(PoolConcurrencyPolicy.class) + void closeIdleFailsExpiredPendingRequest(final PoolConcurrencyPolicy policy) throws Exception { + final ManagedConnPool pool = createPool(policy); + try { + final PoolEntry leased = + pool.lease("r1", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + assertNotNull(leased); + final Future> pending = + pool.lease("r1", null, Timeout.ofMilliseconds(50), null); + + Thread.sleep(150); + pool.closeIdle(TimeValue.ofMilliseconds(1)); + + final ExecutionException ex = assertThrows(ExecutionException.class, + () -> pending.get(5, TimeUnit.SECONDS)); + assertInstanceOf(TimeoutException.class, ex.getCause()); + } finally { + pool.close(CloseMode.IMMEDIATE); + } + } + +}