Skip to content

Commit b57da6c

Browse files
committed
Refactor test to use Durations instead of magic numbers
1 parent fdc87a3 commit b57da6c

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import javax.management.MBeanServer;
5252
import javax.management.ObjectName;
5353

54+
import org.apache.commons.lang3.ThreadUtils;
5455
import org.apache.commons.lang3.time.DurationUtils;
5556
import org.apache.commons.pool3.BasePooledObjectFactory;
5657
import org.apache.commons.pool3.ObjectPool;
@@ -819,7 +820,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception {
819820
}
820821

821822
private BasePooledObjectFactory<String, RuntimeException> createDefaultPooledObjectFactory() {
822-
return new BasePooledObjectFactory<String, RuntimeException>() {
823+
return new BasePooledObjectFactory<>() {
823824
@Override
824825
public String create() {
825826
// fake
@@ -835,7 +836,7 @@ public PooledObject<String> wrap(final String obj) {
835836
}
836837

837838
private BasePooledObjectFactory<String, RuntimeException> createNullPooledObjectFactory() {
838-
return new BasePooledObjectFactory<String, RuntimeException>() {
839+
return new BasePooledObjectFactory<>() {
839840
@Override
840841
public String create() {
841842
// fake
@@ -850,12 +851,11 @@ public PooledObject<String> wrap(final String obj) {
850851
};
851852
}
852853

853-
private BasePooledObjectFactory<String, InterruptedException> createSlowObjectFactory(
854-
final long elapsedTimeMillis) {
855-
return new BasePooledObjectFactory<String, InterruptedException>() {
854+
private BasePooledObjectFactory<String, InterruptedException> createSlowObjectFactory(final Duration sleepDuration) {
855+
return new BasePooledObjectFactory<>() {
856856
@Override
857857
public String create() throws InterruptedException {
858-
Thread.sleep(elapsedTimeMillis);
858+
ThreadUtils.sleep(sleepDuration);
859859
return "created";
860860
}
861861

@@ -1074,7 +1074,7 @@ public void testBorrowObjectFairness() throws Exception {
10741074
@Test/* maxWaitMillis x2 + padding */
10751075
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
10761076
public void testBorrowObjectOverrideMaxWaitLarge() throws Exception {
1077-
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
1077+
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
10781078
pool.setMaxTotal(1);
10791079
pool.setMaxWait(Duration.ofMillis(1_000)); // large
10801080
pool.setBlockWhenExhausted(false);
@@ -1097,7 +1097,7 @@ public void testBorrowObjectOverrideMaxWaitLarge() throws Exception {
10971097
@Test/* maxWaitMillis x2 + padding */
10981098
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
10991099
public void testBorrowObjectOverrideMaxWaitSmall() throws Exception {
1100-
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
1100+
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
11011101
pool.setMaxTotal(1);
11021102
pool.setMaxWait(Duration.ofMillis(1)); // small
11031103
pool.setBlockWhenExhausted(false);
@@ -1178,7 +1178,7 @@ public void testBorrowTimings() throws Exception {
11781178
/**
11791179
* On first borrow, first object fails validation, second object is OK.
11801180
* Subsequent borrows are OK. This was POOL-152.
1181-
*
1181+
*
11821182
* @throws Exception
11831183
*/
11841184
@Test
@@ -2652,7 +2652,7 @@ public void testNoInvalidateNPE() throws Exception {
26522652

26532653
/**
26542654
* Verify that when a factory returns a null object, pool methods throw NPE.
2655-
*
2655+
*
26562656
* @throws InterruptedException
26572657
*/
26582658
@Test
@@ -2693,7 +2693,7 @@ public void testPreparePool() throws Exception {
26932693
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
26942694
public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
26952695
final Duration maxWaitDuration = Duration.ofMillis(500);
2696-
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
2696+
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
26972697
createSlowObjectFactoryPool.setMaxTotal(1);
26982698
createSlowObjectFactoryPool.setMaxWait(maxWaitDuration);
26992699
// thread1 tries creating a slow object to make pool full.
@@ -2707,12 +2707,12 @@ public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
27072707
assertTrue(thread1.isAlive());
27082708
}
27092709
}
2710-
2710+
27112711
@Test /* maxWaitMillis x2 + padding */
27122712
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
27132713
public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception {
27142714
final long maxWaitMillis = 500;
2715-
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60000))) {
2715+
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
27162716
createSlowObjectFactoryPool.setMaxTotal(1);
27172717
createSlowObjectFactoryPool.setMaxWait(Duration.ofMillis(maxWaitMillis));
27182718
// thread1 tries creating a slow object to make pool full.

0 commit comments

Comments
 (0)