This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch POOL_2_X
in repository https://gitbox.apache.org/repos/asf/commons-pool.git


The following commit(s) were added to refs/heads/POOL_2_X by this push:
     new a5033900 Test BaseGenericObjectPool.setMaxWait(Duration) directly
a5033900 is described below

commit a50339008b9b52002736c4c2192bfc516035a1d4
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 24 10:08:44 2024 -0500

    Test BaseGenericObjectPool.setMaxWait(Duration) directly
    
    Add TestGenericObjectPool.testReturnBorrowObjectWithingMaxWaitDuration()
---
 .../commons/pool2/impl/TestGenericObjectPool.java  | 28 +++++++++++++++-------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git 
a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java 
b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
index ef80c115..648466f6 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
@@ -47,7 +47,6 @@ import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
-
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
@@ -2635,27 +2634,40 @@ public class TestGenericObjectPool extends 
TestBaseObjectPool {
         assertEquals(1, genericObjectPool.getNumIdle());
     }
 
+    @Test/* maxWaitMillis x2 + padding */
+    @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
+    public void testReturnBorrowObjectWithingMaxWaitDuration() throws 
Exception {
+        final Duration maxWaitDuration = Duration.ofMillis(500);
+        try (final GenericObjectPool<String> createSlowObjectFactoryPool = new 
GenericObjectPool<>(createSlowObjectFactory(60_000))) {
+            createSlowObjectFactoryPool.setMaxTotal(1);
+            createSlowObjectFactoryPool.setMaxWait(maxWaitDuration);
+            // thread1 tries creating a slow object to make pool full.
+            final WaitingTestThread thread1 = new 
WaitingTestThread(createSlowObjectFactoryPool, 0);
+            thread1.start();
+            // Wait for thread1's reaching to create().
+            Thread.sleep(100);
+            // another one tries borrowObject. It should return within 
maxWaitMillis.
+            assertThrows(NoSuchElementException.class, () -> 
createSlowObjectFactoryPool.borrowObject(maxWaitDuration),
+                    "borrowObject must fail due to timeout by maxWaitMillis");
+            assertTrue(thread1.isAlive());
+        }
+    }
+
     @Test/* maxWaitMillis x2 + padding */
     @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
     public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception {
         final long maxWaitMillis = 500;
-
-        try (final GenericObjectPool<String> createSlowObjectFactoryPool = new 
GenericObjectPool<>(
-                createSlowObjectFactory(60000))) {
+        try (final GenericObjectPool<String> createSlowObjectFactoryPool = new 
GenericObjectPool<>(createSlowObjectFactory(60_000))) {
             createSlowObjectFactoryPool.setMaxTotal(1);
             createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis);
-
             // thread1 tries creating a slow object to make pool full.
             final WaitingTestThread thread1 = new 
WaitingTestThread(createSlowObjectFactoryPool, 0);
             thread1.start();
-
             // Wait for thread1's reaching to create().
             Thread.sleep(100);
-
             // another one tries borrowObject. It should return within 
maxWaitMillis.
             assertThrows(NoSuchElementException.class, () -> 
createSlowObjectFactoryPool.borrowObject(maxWaitMillis),
                     "borrowObject must fail due to timeout by maxWaitMillis");
-
             assertTrue(thread1.isAlive());
         }
     }

Reply via email to