diff --git a/src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java b/src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java index b2b3413014..351a5ca013 100644 --- a/src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java @@ -307,7 +307,8 @@ public T borrowObject(final Duration maxWaitDuration) throws E { if (PooledObject.isNull(p)) { try { remainingWaitDuration = maxWaitDuration.minus(durationSince(startInstant)); - p = negativeDuration ? idleObjects.takeFirst() : idleObjects.pollFirst(maxWaitDuration); + p = remainingWaitDuration.isNegative() ? null + : (negativeDuration ? idleObjects.takeFirst() : idleObjects.pollFirst(maxWaitDuration)); } catch (final InterruptedException e) { // Don't surface exception type of internal locking mechanism. throw cast(e); diff --git a/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java index caa3afffdc..59fc0b7268 100644 --- a/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java @@ -1098,7 +1098,7 @@ public void testBorrowObjectOverrideMaxWaitSmall() throws Exception { try (final GenericObjectPool pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) { pool.setMaxTotal(1); pool.setMaxWait(Duration.ofMillis(1)); // small - pool.setBlockWhenExhausted(false); + pool.setBlockWhenExhausted(true); // need to merge the calculation of wait time // thread1 tries creating a slow object to make pool full. final WaitingTestThread thread1 = new WaitingTestThread<>(pool, 0); thread1.start();