Skip to content

Commit

Permalink
Refactor test to use Durations instead of magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 1, 2024
1 parent fdc87a3 commit b57da6c
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.commons.lang3.ThreadUtils;
import org.apache.commons.lang3.time.DurationUtils;
import org.apache.commons.pool3.BasePooledObjectFactory;
import org.apache.commons.pool3.ObjectPool;
Expand Down Expand Up @@ -819,7 +820,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception {
}

private BasePooledObjectFactory<String, RuntimeException> createDefaultPooledObjectFactory() {
return new BasePooledObjectFactory<String, RuntimeException>() {
return new BasePooledObjectFactory<>() {
@Override
public String create() {
// fake
Expand All @@ -835,7 +836,7 @@ public PooledObject<String> wrap(final String obj) {
}

private BasePooledObjectFactory<String, RuntimeException> createNullPooledObjectFactory() {
return new BasePooledObjectFactory<String, RuntimeException>() {
return new BasePooledObjectFactory<>() {
@Override
public String create() {
// fake
Expand All @@ -850,12 +851,11 @@ public PooledObject<String> wrap(final String obj) {
};
}

private BasePooledObjectFactory<String, InterruptedException> createSlowObjectFactory(
final long elapsedTimeMillis) {
return new BasePooledObjectFactory<String, InterruptedException>() {
private BasePooledObjectFactory<String, InterruptedException> createSlowObjectFactory(final Duration sleepDuration) {
return new BasePooledObjectFactory<>() {
@Override
public String create() throws InterruptedException {
Thread.sleep(elapsedTimeMillis);
ThreadUtils.sleep(sleepDuration);
return "created";
}

Expand Down Expand Up @@ -1074,7 +1074,7 @@ public void testBorrowObjectFairness() throws Exception {
@Test/* maxWaitMillis x2 + padding */
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
public void testBorrowObjectOverrideMaxWaitLarge() throws Exception {
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
pool.setMaxTotal(1);
pool.setMaxWait(Duration.ofMillis(1_000)); // large
pool.setBlockWhenExhausted(false);
Expand All @@ -1097,7 +1097,7 @@ public void testBorrowObjectOverrideMaxWaitLarge() throws Exception {
@Test/* maxWaitMillis x2 + padding */
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
public void testBorrowObjectOverrideMaxWaitSmall() throws Exception {
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
try (final GenericObjectPool<String, InterruptedException> pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
pool.setMaxTotal(1);
pool.setMaxWait(Duration.ofMillis(1)); // small
pool.setBlockWhenExhausted(false);
Expand Down Expand Up @@ -1178,7 +1178,7 @@ public void testBorrowTimings() throws Exception {
/**
* On first borrow, first object fails validation, second object is OK.
* Subsequent borrows are OK. This was POOL-152.
*
*
* @throws Exception
*/
@Test
Expand Down Expand Up @@ -2652,7 +2652,7 @@ public void testNoInvalidateNPE() throws Exception {

/**
* Verify that when a factory returns a null object, pool methods throw NPE.
*
*
* @throws InterruptedException
*/
@Test
Expand Down Expand Up @@ -2693,7 +2693,7 @@ public void testPreparePool() throws Exception {
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
final Duration maxWaitDuration = Duration.ofMillis(500);
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) {
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
createSlowObjectFactoryPool.setMaxTotal(1);
createSlowObjectFactoryPool.setMaxWait(maxWaitDuration);
// thread1 tries creating a slow object to make pool full.
Expand All @@ -2707,12 +2707,12 @@ public void testReturnBorrowObjectWithingMaxWaitDuration() throws Exception {
assertTrue(thread1.isAlive());
}
}

@Test /* maxWaitMillis x2 + padding */
@Timeout(value = 1200, unit = TimeUnit.MILLISECONDS)
public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception {
final long maxWaitMillis = 500;
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60000))) {
try (final GenericObjectPool<String, InterruptedException> createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) {
createSlowObjectFactoryPool.setMaxTotal(1);
createSlowObjectFactoryPool.setMaxWait(Duration.ofMillis(maxWaitMillis));
// thread1 tries creating a slow object to make pool full.
Expand Down

0 comments on commit b57da6c

Please sign in to comment.