|
51 | 51 | import javax.management.MBeanServer; |
52 | 52 | import javax.management.ObjectName; |
53 | 53 |
|
| 54 | +import org.apache.commons.lang3.time.DurationUtils; |
54 | 55 | import org.apache.commons.pool3.BasePooledObjectFactory; |
55 | 56 | import org.apache.commons.pool3.ObjectPool; |
56 | 57 | import org.apache.commons.pool3.PoolUtils; |
@@ -1070,6 +1071,52 @@ public void testBorrowObjectFairness() throws Exception { |
1070 | 1071 | } |
1071 | 1072 | } |
1072 | 1073 |
|
| 1074 | + @Test/* maxWaitMillis x2 + padding */ |
| 1075 | + @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS) |
| 1076 | + public void testBorrowObjectOverrideMaxWaitLarge() throws Exception { |
| 1077 | + try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) { |
| 1078 | + pool.setMaxTotal(1); |
| 1079 | + pool.setMaxWait(Duration.ofMillis(1_000)); // large |
| 1080 | + pool.setBlockWhenExhausted(false); |
| 1081 | + // thread1 tries creating a slow object to make pool full. |
| 1082 | + final WaitingTestThread<InterruptedException> thread1 = new WaitingTestThread<>(pool, 0); |
| 1083 | + thread1.start(); |
| 1084 | + // Wait for thread1's reaching to create(). |
| 1085 | + Thread.sleep(100); |
| 1086 | + // The test needs to make sure a wait happens in create(). |
| 1087 | + final Duration d = DurationUtils.of(() -> assertThrows(NoSuchElementException.class, () -> pool.borrowObject(Duration.ofMillis(1)), |
| 1088 | + "borrowObject must fail quickly due to timeout parameter")); |
| 1089 | + final long millis = d.toMillis(); |
| 1090 | + final long nanos = d.toNanos(); |
| 1091 | + assertTrue(nanos > 0, () -> "borrowObject(Duration) argument not respected: " + nanos); |
| 1092 | + assertTrue(millis >= 0, () -> "borrowObject(Duration) argument not respected: " + millis); // not > 0 to account for spurious waits |
| 1093 | + assertTrue(millis < 50, () -> "borrowObject(Duration) argument not respected: " + millis); |
| 1094 | + } |
| 1095 | + } |
| 1096 | + |
| 1097 | + @Test/* maxWaitMillis x2 + padding */ |
| 1098 | + @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS) |
| 1099 | + public void testBorrowObjectOverrideMaxWaitSmall() throws Exception { |
| 1100 | + try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) { |
| 1101 | + pool.setMaxTotal(1); |
| 1102 | + pool.setMaxWait(Duration.ofMillis(1)); // small |
| 1103 | + pool.setBlockWhenExhausted(false); |
| 1104 | + // thread1 tries creating a slow object to make pool full. |
| 1105 | + final WaitingTestThread<InterruptedException> thread1 = new WaitingTestThread<>(pool, 0); |
| 1106 | + thread1.start(); |
| 1107 | + // Wait for thread1's reaching to create(). |
| 1108 | + Thread.sleep(100); |
| 1109 | + // The test needs to make sure a wait happens in create(). |
| 1110 | + final Duration d = DurationUtils.of(() -> assertThrows(NoSuchElementException.class, () -> pool.borrowObject(Duration.ofMillis(500)), |
| 1111 | + "borrowObject must fail slowly due to timeout parameter")); |
| 1112 | + final long millis = d.toMillis(); |
| 1113 | + final long nanos = d.toNanos(); |
| 1114 | + assertTrue(nanos > 0, () -> "borrowObject(Duration) argument not respected: " + nanos); |
| 1115 | + assertTrue(millis >= 0, () -> "borrowObject(Duration) argument not respected: " + millis); // not > 0 to account for spurious waits |
| 1116 | + assertTrue(millis < 600, () -> "borrowObject(Duration) argument not respected: " + millis); |
| 1117 | + assertTrue(millis > 490, () -> "borrowObject(Duration) argument not respected: " + millis); |
| 1118 | + } |
| 1119 | + } |
1073 | 1120 | @Test |
1074 | 1121 | public void testBorrowTimings() throws Exception { |
1075 | 1122 | // Borrow |
@@ -2642,28 +2689,40 @@ public void testPreparePool() throws Exception { |
2642 | 2689 | assertEquals(1, genericObjectPool.getNumIdle()); |
2643 | 2690 | } |
2644 | 2691 |
|
| 2692 | + @Test/* maxWaitMillis x2 + padding */ |
| 2693 | + @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS) |
| 2694 | + public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception { |
| 2695 | + final Duration maxWaitDuration = Duration.ofMillis(500); |
| 2696 | + try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) { |
| 2697 | + createSlowObjectFactoryPool.setMaxTotal(1); |
| 2698 | + createSlowObjectFactoryPool.setMaxWait(maxWaitDuration); |
| 2699 | + // thread1 tries creating a slow object to make pool full. |
| 2700 | + final WaitingTestThread<InterruptedException> thread1 = new WaitingTestThread<>(createSlowObjectFactoryPool, 0); |
| 2701 | + thread1.start(); |
| 2702 | + // Wait for thread1's reaching to create(). |
| 2703 | + Thread.sleep(100); |
| 2704 | + // another one tries borrowObject. It should return within maxWaitMillis. |
| 2705 | + assertThrows(NoSuchElementException.class, () -> createSlowObjectFactoryPool.borrowObject(maxWaitDuration), |
| 2706 | + "borrowObject must fail due to timeout by maxWaitMillis"); |
| 2707 | + assertTrue(thread1.isAlive()); |
| 2708 | + } |
| 2709 | + } |
| 2710 | + |
2645 | 2711 | @Test /* maxWaitMillis x2 + padding */ |
2646 | 2712 | @Timeout(value = 1200, unit = TimeUnit.MILLISECONDS) |
2647 | 2713 | public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception { |
2648 | 2714 | final long maxWaitMillis = 500; |
2649 | | - |
2650 | | - try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>( |
2651 | | - createSlowObjectFactory(60000))) { |
| 2715 | + try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60000))) { |
2652 | 2716 | createSlowObjectFactoryPool.setMaxTotal(1); |
2653 | 2717 | createSlowObjectFactoryPool.setMaxWait(Duration.ofMillis(maxWaitMillis)); |
2654 | | - |
2655 | 2718 | // thread1 tries creating a slow object to make pool full. |
2656 | | - final WaitingTestThread<InterruptedException> thread1 = new WaitingTestThread<>(createSlowObjectFactoryPool, |
2657 | | - 0); |
| 2719 | + final WaitingTestThread<InterruptedException> thread1 = new WaitingTestThread<>(createSlowObjectFactoryPool, 0); |
2658 | 2720 | thread1.start(); |
2659 | | - |
2660 | 2721 | // Wait for thread1's reaching to create(). |
2661 | 2722 | Thread.sleep(100); |
2662 | | - |
2663 | 2723 | // another one tries borrowObject. It should return within maxWaitMillis. |
2664 | 2724 | assertThrows(NoSuchElementException.class, () -> createSlowObjectFactoryPool.borrowObject(maxWaitMillis), |
2665 | 2725 | "borrowObject must fail due to timeout by maxWaitMillis"); |
2666 | | - |
2667 | 2726 | assertTrue(thread1.isAlive()); |
2668 | 2727 | } |
2669 | 2728 | } |
|
0 commit comments