|
47 | 47 | import java.util.concurrent.TimeUnit; |
48 | 48 | import java.util.concurrent.atomic.AtomicBoolean; |
49 | 49 | import java.util.concurrent.atomic.AtomicInteger; |
50 | | - |
51 | 50 | import javax.management.MBeanServer; |
52 | 51 | import javax.management.ObjectName; |
53 | 52 |
|
@@ -2635,27 +2634,40 @@ public void testPreparePool() throws Exception { |
2635 | 2634 | assertEquals(1, genericObjectPool.getNumIdle()); |
2636 | 2635 | } |
2637 | 2636 |
|
| 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 | + |
2638 | 2656 | @Test/* maxWaitMillis x2 + padding */ |
2639 | 2657 | @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS) |
2640 | 2658 | public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception { |
2641 | 2659 | 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))) { |
2645 | 2661 | createSlowObjectFactoryPool.setMaxTotal(1); |
2646 | 2662 | createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis); |
2647 | | - |
2648 | 2663 | // thread1 tries creating a slow object to make pool full. |
2649 | 2664 | final WaitingTestThread thread1 = new WaitingTestThread(createSlowObjectFactoryPool, 0); |
2650 | 2665 | thread1.start(); |
2651 | | - |
2652 | 2666 | // Wait for thread1's reaching to create(). |
2653 | 2667 | Thread.sleep(100); |
2654 | | - |
2655 | 2668 | // another one tries borrowObject. It should return within maxWaitMillis. |
2656 | 2669 | assertThrows(NoSuchElementException.class, () -> createSlowObjectFactoryPool.borrowObject(maxWaitMillis), |
2657 | 2670 | "borrowObject must fail due to timeout by maxWaitMillis"); |
2658 | | - |
2659 | 2671 | assertTrue(thread1.isAlive()); |
2660 | 2672 | } |
2661 | 2673 | } |
|
0 commit comments