Skip to content

Commit 5150b5d

Browse files
committed
Refactor test to use Durations instead of magic numbers
1 parent 96d52c1 commit 5150b5d

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

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

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

53+
import org.apache.commons.lang3.ThreadUtils;
5354
import org.apache.commons.lang3.time.DurationUtils;
5455
import org.apache.commons.pool2.BasePooledObjectFactory;
5556
import org.apache.commons.pool2.ObjectPool;
@@ -856,11 +857,11 @@ public PooledObject<String> wrap(final String obj) {
856857
};
857858
}
858859

859-
private BasePooledObjectFactory<String> createSlowObjectFactory(final long elapsedTimeMillis) {
860+
private BasePooledObjectFactory<String> createSlowObjectFactory(final Duration sleepDuration) {
860861
return new BasePooledObjectFactory<String>() {
861862
@Override
862863
public String create() throws InterruptedException {
863-
Thread.sleep(elapsedTimeMillis);
864+
ThreadUtils.sleep(sleepDuration);
864865
return "created";
865866
}
866867

@@ -1078,7 +1079,7 @@ public void testBorrowObjectFairness() throws Exception {
10781079
@Test
10791080
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
10801081
public void testBorrowObjectOverrideMaxWaitLarge() throws Exception {
1081-
try (final GenericObjectPool<String> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
1082+
try (final GenericObjectPool<String> pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
10821083
pool.setMaxTotal(1);
10831084
pool.setMaxWait(Duration.ofMillis(1_000)); // large
10841085
pool.setBlockWhenExhausted(false);
@@ -1101,7 +1102,7 @@ public void testBorrowObjectOverrideMaxWaitLarge() throws Exception {
11011102
@Test
11021103
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
11031104
public void testBorrowObjectOverrideMaxWaitSmall() throws Exception {
1104-
try (final GenericObjectPool<String> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
1105+
try (final GenericObjectPool<String> pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
11051106
pool.setMaxTotal(1);
11061107
pool.setMaxWait(Duration.ofMillis(1)); // small
11071108
pool.setBlockWhenExhausted(false);
@@ -2686,7 +2687,7 @@ public void testPreparePool() throws Exception {
26862687
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
26872688
public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
26882689
final Duration maxWaitDuration = Duration.ofMillis(500);
2689-
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
2690+
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
26902691
createSlowObjectFactoryPool.setMaxTotal(1);
26912692
createSlowObjectFactoryPool.setMaxWait(maxWaitDuration);
26922693
// thread1 tries creating a slow object to make pool full.
@@ -2705,7 +2706,7 @@ public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
27052706
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
27062707
public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception {
27072708
final long maxWaitMillis = 500;
2708-
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
2709+
try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
27092710
createSlowObjectFactoryPool.setMaxTotal(1);
27102711
createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis);
27112712
// thread1 tries creating a slow object to make pool full.

0 commit comments

Comments
 (0)