From b57da6c175e631592665402b9b6bccf21e7390ed Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 1 Dec 2024 15:55:22 -0500 Subject: [PATCH] Refactor test to use Durations instead of magic numbers --- .../pool3/impl/TestGenericObjectPool.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java index 94ee7a572d..3c78fca6f1 100644 --- a/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java @@ -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; @@ -819,7 +820,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception { } private BasePooledObjectFactory createDefaultPooledObjectFactory() { - return new BasePooledObjectFactory() { + return new BasePooledObjectFactory<>() { @Override public String create() { // fake @@ -835,7 +836,7 @@ public PooledObject wrap(final String obj) { } private BasePooledObjectFactory createNullPooledObjectFactory() { - return new BasePooledObjectFactory() { + return new BasePooledObjectFactory<>() { @Override public String create() { // fake @@ -850,12 +851,11 @@ public PooledObject wrap(final String obj) { }; } - private BasePooledObjectFactory createSlowObjectFactory( - final long elapsedTimeMillis) { - return new BasePooledObjectFactory() { + private BasePooledObjectFactory createSlowObjectFactory(final Duration sleepDuration) { + return new BasePooledObjectFactory<>() { @Override public String create() throws InterruptedException { - Thread.sleep(elapsedTimeMillis); + ThreadUtils.sleep(sleepDuration); return "created"; } @@ -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 pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) { + try (final GenericObjectPool pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) { pool.setMaxTotal(1); pool.setMaxWait(Duration.ofMillis(1_000)); // large pool.setBlockWhenExhausted(false); @@ -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 pool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) { + try (final GenericObjectPool pool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) { pool.setMaxTotal(1); pool.setMaxWait(Duration.ofMillis(1)); // small pool.setBlockWhenExhausted(false); @@ -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 @@ -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 @@ -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 createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60_000))) { + try (final GenericObjectPool createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(Duration.ofSeconds(60)))) { createSlowObjectFactoryPool.setMaxTotal(1); createSlowObjectFactoryPool.setMaxWait(maxWaitDuration); // thread1 tries creating a slow object to make pool full. @@ -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 createSlowObjectFactoryPool = new GenericObjectPool<>(createSlowObjectFactory(60000))) { + try (final GenericObjectPool 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.