Skip to content

Commit a503390

Browse files
committed
Test BaseGenericObjectPool.setMaxWait(Duration) directly
Add TestGenericObjectPool.testReturnBorrowObjectWithingMaxWaitDuration()
1 parent 53a7c67 commit a503390

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.util.concurrent.TimeUnit;
4848
import java.util.concurrent.atomic.AtomicBoolean;
4949
import java.util.concurrent.atomic.AtomicInteger;
50-
5150
import javax.management.MBeanServer;
5251
import javax.management.ObjectName;
5352

@@ -2635,27 +2634,40 @@ public void testPreparePool() throws Exception {
26352634
assertEquals(1, genericObjectPool.getNumIdle());
26362635
}
26372636

2637+
@Test/* maxWaitMillis x2 + padding */
2638+
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
2639+
public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
2640+
final Duration maxWaitDuration = Duration.ofMillis(500);
2641+
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
2642+
createSlowObjectFactoryPool.setMaxTotal(1);
2643+
createSlowObjectFactoryPool.setMaxWait(maxWaitDuration);
2644+
// thread1 tries creating a slow object to make pool full.
2645+
final WaitingTestThread thread1 = new WaitingTestThread(createSlowObjectFactoryPool, 0);
2646+
thread1.start();
2647+
// Wait for thread1's reaching to create().
2648+
Thread.sleep(100);
2649+
// another one tries borrowObject. It should return within maxWaitMillis.
2650+
assertThrows(NoSuchElementException.class, () -> createSlowObjectFactoryPool.borrowObject(maxWaitDuration),
2651+
"borrowObject must fail due to timeout by maxWaitMillis");
2652+
assertTrue(thread1.isAlive());
2653+
}
2654+
}
2655+
26382656
@Test/* maxWaitMillis x2 + padding */
26392657
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
26402658
public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception {
26412659
final long maxWaitMillis = 500;
2642-
2643-
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(
2644-
createSlowObjectFactory(60000))) {
2660+
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
26452661
createSlowObjectFactoryPool.setMaxTotal(1);
26462662
createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis);
2647-
26482663
// thread1 tries creating a slow object to make pool full.
26492664
final WaitingTestThread thread1 = new WaitingTestThread(createSlowObjectFactoryPool, 0);
26502665
thread1.start();
2651-
26522666
// Wait for thread1's reaching to create().
26532667
Thread.sleep(100);
2654-
26552668
// another one tries borrowObject. It should return within maxWaitMillis.
26562669
assertThrows(NoSuchElementException.class, () -> createSlowObjectFactoryPool.borrowObject(maxWaitMillis),
26572670
"borrowObject must fail due to timeout by maxWaitMillis");
2658-
26592671
assertTrue(thread1.isAlive());
26602672
}
26612673
}

0 commit comments

Comments
 (0)