This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-pool.git
commit 814da2b6408bf2dce57f475c3d85706fab1f62bb Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Mon Aug 23 08:35:57 2021 -0400 Sort members. --- .../commons/pool2/impl/BaseGenericObjectPool.java | 28 ++--- .../commons/pool2/impl/BaseObjectPoolConfig.java | 68 +++++------ .../commons/pool2/impl/GenericKeyedObjectPool.java | 62 +++++----- .../commons/pool2/impl/GenericObjectPool.java | 24 ++-- .../apache/commons/pool2/impl/PoolImplUtils.java | 22 ++-- src/test/java/org/apache/commons/pool2/Waiter.java | 18 +-- .../pool2/impl/TestDefaultPooledObject.java | 132 ++++++++++----------- .../commons/pool2/impl/TestGenericObjectPool.java | 118 +++++++++--------- .../TestGenericObjectPoolFactoryCreateFailure.java | 4 +- 9 files changed, 238 insertions(+), 238 deletions(-) diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java index 39dca27..554e5dd 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -560,6 +560,20 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { } /** + * Gets the duration to sleep between runs of the idle + * object evictor thread. When non-positive, no idle object evictor thread + * will be run. + * + * @return number of milliseconds to sleep between evictor runs + * + * @see #setTimeBetweenEvictionRuns + * @since 2.11.0 + */ + public final Duration getDurationBetweenEvictionRuns() { + return durationBetweenEvictionRuns; + } + + /** * Gets the {@link EvictionPolicy} defined for this pool. * * @return the eviction policy @@ -1112,20 +1126,6 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return number of milliseconds to sleep between evictor runs * * @see #setTimeBetweenEvictionRuns - * @since 2.11.0 - */ - public final Duration getDurationBetweenEvictionRuns() { - return durationBetweenEvictionRuns; - } - - /** - * Gets the duration to sleep between runs of the idle - * object evictor thread. When non-positive, no idle object evictor thread - * will be run. - * - * @return number of milliseconds to sleep between evictor runs - * - * @see #setTimeBetweenEvictionRuns * @since 2.10.0 * @deprecated {@link #getDurationBetweenEvictionRuns()}. */ diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java b/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java index d06f380..0811054 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java @@ -297,6 +297,21 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** + * Gets the value for the {@code timeBetweenEvictionRuns} configuration + * attribute for pools created with this configuration instance. + * + * @return The current setting of {@code timeBetweenEvictionRuns} for + * this configuration instance + * + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() + * @since 2.11.0 + */ + public Duration getDurationBetweenEvictionRuns() { + return durationBetweenEvictionRuns; + } + + /** * Gets the value for the {@code evictionPolicyClass} configuration * attribute for pools created with this configuration instance. * @@ -648,21 +663,6 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * * @see GenericObjectPool#getDurationBetweenEvictionRuns() * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() - * @since 2.11.0 - */ - public Duration getDurationBetweenEvictionRuns() { - return durationBetweenEvictionRuns; - } - - /** - * Gets the value for the {@code timeBetweenEvictionRuns} configuration - * attribute for pools created with this configuration instance. - * - * @return The current setting of {@code timeBetweenEvictionRuns} for - * this configuration instance - * - * @see GenericObjectPool#getDurationBetweenEvictionRuns() - * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() * @since 2.10.0 * @deprecated Use {@link #getDurationBetweenEvictionRuns()}. */ @@ -734,34 +734,34 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * Sets the value for the {@code evictorShutdownTimeout} configuration * attribute for pools created with this configuration instance. * - * @param evictorShutdownTimeout The new setting of + * @param evictorShutdownTimeoutDuration The new setting of * {@code evictorShutdownTimeout} for this configuration * instance * * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() - * @since 2.10.0 - * @deprecated Use {@link #setEvictorShutdownTimeout(Duration)}. + * @since 2.11.0 */ - @Deprecated - public void setEvictorShutdownTimeoutMillis(final Duration evictorShutdownTimeout) { - setEvictorShutdownTimeout(evictorShutdownTimeout); + public void setEvictorShutdownTimeout(final Duration evictorShutdownTimeoutDuration) { + this.evictorShutdownTimeoutDuration = PoolImplUtils.nonNull(evictorShutdownTimeoutDuration, DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT); } /** * Sets the value for the {@code evictorShutdownTimeout} configuration * attribute for pools created with this configuration instance. * - * @param evictorShutdownTimeoutDuration The new setting of + * @param evictorShutdownTimeout The new setting of * {@code evictorShutdownTimeout} for this configuration * instance * * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() - * @since 2.11.0 + * @since 2.10.0 + * @deprecated Use {@link #setEvictorShutdownTimeout(Duration)}. */ - public void setEvictorShutdownTimeout(final Duration evictorShutdownTimeoutDuration) { - this.evictorShutdownTimeoutDuration = PoolImplUtils.nonNull(evictorShutdownTimeoutDuration, DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT); + @Deprecated + public void setEvictorShutdownTimeoutMillis(final Duration evictorShutdownTimeout) { + setEvictorShutdownTimeout(evictorShutdownTimeout); } /** @@ -849,31 +849,31 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * Sets the value for the {@code maxWait} configuration attribute for pools * created with this configuration instance. * - * @param maxWaitMillis The new setting of {@code maxWaitMillis} + * @param maxWaitDuration The new setting of {@code maxWaitDuration} * for this configuration instance * * @see GenericObjectPool#getMaxWaitDuration() * @see GenericKeyedObjectPool#getMaxWaitDuration() - * @deprecated Use {@link #setMaxWait(Duration)}. + * @since 2.11.0 */ - @Deprecated - public void setMaxWaitMillis(final long maxWaitMillis) { - setMaxWait(Duration.ofMillis(maxWaitMillis)); + public void setMaxWait(final Duration maxWaitDuration) { + this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, DEFAULT_MAX_WAIT); } /** * Sets the value for the {@code maxWait} configuration attribute for pools * created with this configuration instance. * - * @param maxWaitDuration The new setting of {@code maxWaitDuration} + * @param maxWaitMillis The new setting of {@code maxWaitMillis} * for this configuration instance * * @see GenericObjectPool#getMaxWaitDuration() * @see GenericKeyedObjectPool#getMaxWaitDuration() - * @since 2.11.0 + * @deprecated Use {@link #setMaxWait(Duration)}. */ - public void setMaxWait(final Duration maxWaitDuration) { - this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, DEFAULT_MAX_WAIT); + @Deprecated + public void setMaxWaitMillis(final long maxWaitMillis) { + setMaxWait(Duration.ofMillis(maxWaitMillis)); } /** diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java index d9ca745..842f70a 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java @@ -90,8 +90,6 @@ import org.apache.commons.pool2.UsageTracking; public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> implements KeyedObjectPool<K, T>, GenericKeyedObjectPoolMXBean<K>, UsageTracking<T> { - private static final Integer ZERO = Integer.valueOf(0); - /** * Maintains information on the per key queue for a given key. * @@ -188,6 +186,8 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } + private static final Integer ZERO = Integer.valueOf(0); + // JMX specific attributes private static final String ONAME_BASE = "org.apache.commons.pool2:type=GenericKeyedObjectPool,name="; @@ -512,14 +512,6 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> return p.getObject(); } - @Override - String getStatsString() { - // Simply listed in AB order. - return super.getStatsString() + - String.format(", fairness=%s, maxIdlePerKey%,d, maxTotalPerKey=%,d, minIdlePerKey=%,d, numTotal=%,d", - fairness, maxIdlePerKey, maxTotalPerKey, minIdlePerKey, numTotal.get()); - } - /** * Calculate the number of objects that need to be created to attempt to * maintain the minimum number of idle objects while not exceeded the limits @@ -557,7 +549,6 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> return objectDefecit; } - /** * Clears any objects sitting idle in the pool by removing them from the * idle instance sub-pools and then invoking the configured @@ -701,6 +692,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } } + /** * Creates a new pooled object. * @@ -838,7 +830,6 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } } - /** * Destroy the wrapped, pooled object. * @@ -886,6 +877,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } } + @Override void ensureMinIdle() throws Exception { final int minIdlePerKeySave = getMinIdlePerKey(); @@ -931,7 +923,6 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } } - /** * {@inheritDoc} * <p> @@ -1080,6 +1071,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } } + /** * Gets a reference to the factory used to create, destroy and validate * the objects used by this pool. @@ -1185,15 +1177,15 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> return poolMap.values().stream().mapToInt(e -> e.getIdleObjects().size()).sum(); } - - //--- JMX support ---------------------------------------------------------- - @Override public int getNumIdle(final K key) { final ObjectDeque<T> objectDeque = poolMap.get(key); return objectDeque != null ? objectDeque.getIdleObjects().size() : 0; } + + //--- JMX support ---------------------------------------------------------- + /** * Calculate the number of objects to test in a run of the idle object * evictor. @@ -1248,6 +1240,14 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> return result; } + @Override + String getStatsString() { + // Simply listed in AB order. + return super.getStatsString() + + String.format(", fairness=%s, maxIdlePerKey%,d, maxTotalPerKey=%,d, minIdlePerKey=%,d, numTotal=%,d", + fairness, maxIdlePerKey, maxTotalPerKey, minIdlePerKey, numTotal.get()); + } + /** * Checks to see if there are any threads currently waiting to borrow * objects but are blocked waiting for more objects to become available. @@ -1676,21 +1676,6 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } /** - * Whether there is at least one thread waiting on this deque, add an pool object. - * @param key pool key. - * @param idleObjects list of idle pool objects. - */ - private void whenWaitersAddObject(final K key, final LinkedBlockingDeque<PooledObject<T>> idleObjects) { - if (idleObjects.hasTakeWaiters()) { - try { - addObject(key); - } catch (final Exception e) { - swallowException(e); - } - } - } - - /** * @since 2.10.0 */ @Override @@ -1705,4 +1690,19 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } } + /** + * Whether there is at least one thread waiting on this deque, add an pool object. + * @param key pool key. + * @param idleObjects list of idle pool objects. + */ + private void whenWaitersAddObject(final K key, final LinkedBlockingDeque<PooledObject<T>> idleObjects) { + if (idleObjects.hasTakeWaiters()) { + try { + addObject(key); + } catch (final Exception e) { + swallowException(e); + } + } + } + } diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java index d732fe1..0ca56bf 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java @@ -369,18 +369,6 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> return p.getObject(); } - PooledObject<T> getPooledObject(final T obj) { - return allObjects.get(new IdentityWrapper<>(obj)); - } - - @Override - String getStatsString() { - // Simply listed in AB order. - return super.getStatsString() + - String.format(", createdCount=%,d, makeObjectCount=%,d, maxIdle=%,d, minIdle=%,d", - createdCount.get(), makeObjectCount, maxIdle, minIdle); - } - /** * Borrows an object from the pool using the specific waiting time which only * applies if {@link #getBlockWhenExhausted()} is true. @@ -898,6 +886,18 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> return 0; } + PooledObject<T> getPooledObject(final T obj) { + return allObjects.get(new IdentityWrapper<>(obj)); + } + + @Override + String getStatsString() { + // Simply listed in AB order. + return super.getStatsString() + + String.format(", createdCount=%,d, makeObjectCount=%,d, maxIdle=%,d, minIdle=%,d", + createdCount.get(), makeObjectCount, maxIdle, minIdle); + } + /** * {@inheritDoc} * <p> diff --git a/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java b/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java index 7765f6c..412c8f2 100644 --- a/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java +++ b/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java @@ -181,6 +181,17 @@ class PoolImplUtils { } /** + * Returns a non-null duration, value if non-null, otherwise defaultValue. + * + * @param value May be null. + * @param defaultValue May not be null/ + * @return value if non-null, otherwise defaultValue. + */ + static Duration nonNull(final Duration value, final Duration defaultValue) { + return value != null ? value : Objects.requireNonNull(defaultValue, "defaultValue"); + } + + /** * Converts a {@link TimeUnit} to a {@link ChronoUnit}. * * @param timeUnit A TimeUnit. @@ -209,17 +220,6 @@ class PoolImplUtils { } /** - * Returns a non-null duration, value if non-null, otherwise defaultValue. - * - * @param value May be null. - * @param defaultValue May not be null/ - * @return value if non-null, otherwise defaultValue. - */ - static Duration nonNull(final Duration value, final Duration defaultValue) { - return value != null ? value : Objects.requireNonNull(defaultValue, "defaultValue"); - } - - /** * Converts am amount and TimeUnit into a Duration. * * @param amount the amount of the duration, measured in terms of the unit, positive or negative diff --git a/src/test/java/org/apache/commons/pool2/Waiter.java b/src/test/java/org/apache/commons/pool2/Waiter.java index 78924ae..0a3166a 100644 --- a/src/test/java/org/apache/commons/pool2/Waiter.java +++ b/src/test/java/org/apache/commons/pool2/Waiter.java @@ -28,6 +28,14 @@ import java.util.concurrent.atomic.AtomicInteger; */ public class Waiter { private static final AtomicInteger instanceCount = new AtomicInteger(); + /** TODO Reuse Apache Commons Lang ThreadUtils */ + public static void sleepQuietly(final long millis) { + try { + Thread.sleep(millis); + } catch (final InterruptedException e) { + // be quiet + } + } private boolean active; private boolean valid; private long latency; @@ -35,6 +43,7 @@ public class Waiter { private long lastIdleTimeMillis; private long passivationCount; private long validationCount; + private final int id = instanceCount.getAndIncrement(); public Waiter(final boolean active, final boolean valid, final long latency) { @@ -171,13 +180,4 @@ public class Waiter { buff.append("latency = " + latency + '\n'); return buff.toString(); } - - /** TODO Reuse Apache Commons Lang ThreadUtils */ - public static void sleepQuietly(final long millis) { - try { - Thread.sleep(millis); - } catch (final InterruptedException e) { - // be quiet - } - } } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObject.java b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObject.java index abf8a0b..1057516 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObject.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObject.java @@ -39,6 +39,56 @@ import org.junit.jupiter.api.Test; */ public class TestDefaultPooledObject { + /** + * JIRA: POOL-279 + * + * @throws Exception May occur in some failure modes + */ + @Test + public void testGetIdleTimeMillis() throws Exception { + final DefaultPooledObject<Object> dpo = new DefaultPooledObject<>(new Object()); + final AtomicBoolean negativeIdleTimeReturned = new AtomicBoolean(false); + final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 3); + final Runnable allocateAndDeallocateTask = () -> { + for (int i1 = 0; i1 < 10000; i1++) { + if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { + negativeIdleTimeReturned.set(true); + break; + } + if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { + negativeIdleTimeReturned.set(true); + break; + } + } + dpo.allocate(); + for (int i2 = 0; i2 < 10000; i2++) { + if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { + negativeIdleTimeReturned.set(true); + break; + } + } + dpo.deallocate(); + }; + final Runnable getIdleTimeTask = () -> { + for (int i = 0; i < 10000; i++) { + if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { + negativeIdleTimeReturned.set(true); + break; + } + } + }; + final double probabilityOfAllocationTask = 0.7; + final List<Future<?>> futures = new ArrayList<>(); + for (int i = 1; i <= 10000; i++) { + final Runnable randomTask = Math.random() < probabilityOfAllocationTask ? allocateAndDeallocateTask : getIdleTimeTask; + futures.add(executor.submit(randomTask)); + } + for (final Future<?> future : futures) { + future.get(); + } + assertFalse(negativeIdleTimeReturned.get(), "DefaultPooledObject.getIdleTimeMillis() returned a negative value"); + } + @Test public void testInitialStateActiveDuration() throws InterruptedException { final PooledObject<Object> dpo = new DefaultPooledObject<>(new Object()); @@ -62,25 +112,6 @@ public class TestDefaultPooledObject { } @Test - public void testInitialStateIdleDuration() throws InterruptedException { - final PooledObject<Object> dpo = new DefaultPooledObject<>(new Object()); - // Sleep MUST be "long enough" to test that we are not returning a negative time. - Thread.sleep(200); - // In the initial state, all instants are the creation instant: last borrow, last use, last return. - // In the initial state, the active duration is the time between "now" and the creation time. - // In the initial state, the idle duration is the time between "now" and the last return, which is the creation time. - assertFalse(dpo.getIdleDuration().isNegative()); - assertFalse(dpo.getIdleDuration().isZero()); - // We use greaterThanOrEqualTo instead of equal because "now" many be different when each argument is evaluated. - assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getActiveDuration())); - // Deprecated - // assertThat(dpo.getIdleDuration().toMillis(), lessThanOrEqualTo(dpo.getIdleTimeMillis())); - // assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getIdleTime())); - assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getActiveTime())); - assertThat(dpo.getIdleDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis())); - } - - @Test public void testInitialStateCreateInstant() { final PooledObject<Object> dpo = new DefaultPooledObject<>(new Object()); @@ -113,53 +144,22 @@ public class TestDefaultPooledObject { assertThat(duration1, lessThan(duration2)); } - /** - * JIRA: POOL-279 - * - * @throws Exception May occur in some failure modes - */ @Test - public void testGetIdleTimeMillis() throws Exception { - final DefaultPooledObject<Object> dpo = new DefaultPooledObject<>(new Object()); - final AtomicBoolean negativeIdleTimeReturned = new AtomicBoolean(false); - final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 3); - final Runnable allocateAndDeallocateTask = () -> { - for (int i1 = 0; i1 < 10000; i1++) { - if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { - negativeIdleTimeReturned.set(true); - break; - } - if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { - negativeIdleTimeReturned.set(true); - break; - } - } - dpo.allocate(); - for (int i2 = 0; i2 < 10000; i2++) { - if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { - negativeIdleTimeReturned.set(true); - break; - } - } - dpo.deallocate(); - }; - final Runnable getIdleTimeTask = () -> { - for (int i = 0; i < 10000; i++) { - if (dpo.getIdleDuration().isNegative() || dpo.getIdleTime().isNegative()) { - negativeIdleTimeReturned.set(true); - break; - } - } - }; - final double probabilityOfAllocationTask = 0.7; - final List<Future<?>> futures = new ArrayList<>(); - for (int i = 1; i <= 10000; i++) { - final Runnable randomTask = Math.random() < probabilityOfAllocationTask ? allocateAndDeallocateTask : getIdleTimeTask; - futures.add(executor.submit(randomTask)); - } - for (final Future<?> future : futures) { - future.get(); - } - assertFalse(negativeIdleTimeReturned.get(), "DefaultPooledObject.getIdleTimeMillis() returned a negative value"); + public void testInitialStateIdleDuration() throws InterruptedException { + final PooledObject<Object> dpo = new DefaultPooledObject<>(new Object()); + // Sleep MUST be "long enough" to test that we are not returning a negative time. + Thread.sleep(200); + // In the initial state, all instants are the creation instant: last borrow, last use, last return. + // In the initial state, the active duration is the time between "now" and the creation time. + // In the initial state, the idle duration is the time between "now" and the last return, which is the creation time. + assertFalse(dpo.getIdleDuration().isNegative()); + assertFalse(dpo.getIdleDuration().isZero()); + // We use greaterThanOrEqualTo instead of equal because "now" many be different when each argument is evaluated. + assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getActiveDuration())); + // Deprecated + // assertThat(dpo.getIdleDuration().toMillis(), lessThanOrEqualTo(dpo.getIdleTimeMillis())); + // assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getIdleTime())); + assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getActiveTime())); + assertThat(dpo.getIdleDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis())); } } \ No newline at end of file diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java index 8e38303..47d628d 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -1002,65 +1002,6 @@ public class TestGenericObjectPool extends TestBaseObjectPool { } } - @Test - public void testBorrowTimings() throws Exception { - // Borrow - final String object = genericObjectPool.borrowObject(); - final PooledObject<String> po = genericObjectPool.getPooledObject(object); - // In the initial state, all instants are the creation instant: last borrow, last use, last return. - // In the initial state, the active duration is the time between "now" and the creation time. - // In the initial state, the idle duration is the time between "now" and the last return, which is the creation time. - // But... this PO might have already been used in other tests in this class. - - final Instant lastBorrowInstant1 = po.getLastBorrowInstant(); - final Instant lastReturnInstant1 = po.getLastReturnInstant(); - final Instant lastUsedInstant1 = po.getLastUsedInstant(); - - assertThat(po.getCreateInstant(), lessThanOrEqualTo(lastBorrowInstant1)); - assertThat(po.getCreateInstant(), lessThanOrEqualTo(lastReturnInstant1)); - assertThat(po.getCreateInstant(), lessThanOrEqualTo(lastUsedInstant1)); - assertThat(po.getCreateTime(), lessThanOrEqualTo(lastBorrowInstant1.toEpochMilli())); - assertThat(po.getCreateTime(), lessThanOrEqualTo(lastReturnInstant1.toEpochMilli())); - assertThat(po.getCreateTime(), lessThanOrEqualTo(lastUsedInstant1.toEpochMilli())); - - // Sleep MUST be "long enough" to detect that more than 0 milliseconds have elapsed. - // Need an API in Java 8 to get the clock granularity. - Thread.sleep(200); - - assertFalse(po.getActiveDuration().isNegative()); - assertFalse(po.getActiveDuration().isZero()); - // We use greaterThanOrEqualTo instead of equal because "now" many be different when each argument is evaluated. - assertThat(1L, lessThanOrEqualTo(2L)); // sanity check - assertThat(Duration.ZERO, lessThanOrEqualTo(Duration.ZERO.plusNanos(1))); // sanity check - assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getIdleDuration())); - // Deprecated - assertThat(po.getActiveDuration().toMillis(), lessThanOrEqualTo(po.getActiveTimeMillis())); - assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getActiveTime())); - // - // TODO How to compare ID with AD since other tests may have touched the PO? - assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getIdleTime())); - assertThat(po.getActiveDuration().toMillis(), lessThanOrEqualTo(po.getIdleTimeMillis())); - // - assertThat(po.getCreateInstant(), lessThanOrEqualTo(po.getLastBorrowInstant())); - assertThat(po.getCreateInstant(), lessThanOrEqualTo(po.getLastReturnInstant())); - assertThat(po.getCreateInstant(), lessThanOrEqualTo(po.getLastUsedInstant())); - - assertThat(lastBorrowInstant1, lessThanOrEqualTo(po.getLastBorrowInstant())); - assertThat(lastReturnInstant1, lessThanOrEqualTo(po.getLastReturnInstant())); - assertThat(lastUsedInstant1, lessThanOrEqualTo(po.getLastUsedInstant())); - - genericObjectPool.returnObject(object); - - assertFalse(po.getActiveDuration().isNegative()); - assertFalse(po.getActiveDuration().isZero()); - assertThat(po.getActiveDuration().toMillis(), lessThanOrEqualTo(po.getActiveTimeMillis())); - assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getActiveTime())); - - assertThat(lastBorrowInstant1, lessThanOrEqualTo(po.getLastBorrowInstant())); - assertThat(lastReturnInstant1, lessThanOrEqualTo(po.getLastReturnInstant())); - assertThat(lastUsedInstant1, lessThanOrEqualTo(po.getLastUsedInstant())); - } - /* * Note: This test relies on timing for correct execution. There *should* be * enough margin for this to work correctly on most (all?) systems but be @@ -1120,6 +1061,65 @@ public class TestGenericObjectPool extends TestBaseObjectPool { } } + @Test + public void testBorrowTimings() throws Exception { + // Borrow + final String object = genericObjectPool.borrowObject(); + final PooledObject<String> po = genericObjectPool.getPooledObject(object); + // In the initial state, all instants are the creation instant: last borrow, last use, last return. + // In the initial state, the active duration is the time between "now" and the creation time. + // In the initial state, the idle duration is the time between "now" and the last return, which is the creation time. + // But... this PO might have already been used in other tests in this class. + + final Instant lastBorrowInstant1 = po.getLastBorrowInstant(); + final Instant lastReturnInstant1 = po.getLastReturnInstant(); + final Instant lastUsedInstant1 = po.getLastUsedInstant(); + + assertThat(po.getCreateInstant(), lessThanOrEqualTo(lastBorrowInstant1)); + assertThat(po.getCreateInstant(), lessThanOrEqualTo(lastReturnInstant1)); + assertThat(po.getCreateInstant(), lessThanOrEqualTo(lastUsedInstant1)); + assertThat(po.getCreateTime(), lessThanOrEqualTo(lastBorrowInstant1.toEpochMilli())); + assertThat(po.getCreateTime(), lessThanOrEqualTo(lastReturnInstant1.toEpochMilli())); + assertThat(po.getCreateTime(), lessThanOrEqualTo(lastUsedInstant1.toEpochMilli())); + + // Sleep MUST be "long enough" to detect that more than 0 milliseconds have elapsed. + // Need an API in Java 8 to get the clock granularity. + Thread.sleep(200); + + assertFalse(po.getActiveDuration().isNegative()); + assertFalse(po.getActiveDuration().isZero()); + // We use greaterThanOrEqualTo instead of equal because "now" many be different when each argument is evaluated. + assertThat(1L, lessThanOrEqualTo(2L)); // sanity check + assertThat(Duration.ZERO, lessThanOrEqualTo(Duration.ZERO.plusNanos(1))); // sanity check + assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getIdleDuration())); + // Deprecated + assertThat(po.getActiveDuration().toMillis(), lessThanOrEqualTo(po.getActiveTimeMillis())); + assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getActiveTime())); + // + // TODO How to compare ID with AD since other tests may have touched the PO? + assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getIdleTime())); + assertThat(po.getActiveDuration().toMillis(), lessThanOrEqualTo(po.getIdleTimeMillis())); + // + assertThat(po.getCreateInstant(), lessThanOrEqualTo(po.getLastBorrowInstant())); + assertThat(po.getCreateInstant(), lessThanOrEqualTo(po.getLastReturnInstant())); + assertThat(po.getCreateInstant(), lessThanOrEqualTo(po.getLastUsedInstant())); + + assertThat(lastBorrowInstant1, lessThanOrEqualTo(po.getLastBorrowInstant())); + assertThat(lastReturnInstant1, lessThanOrEqualTo(po.getLastReturnInstant())); + assertThat(lastUsedInstant1, lessThanOrEqualTo(po.getLastUsedInstant())); + + genericObjectPool.returnObject(object); + + assertFalse(po.getActiveDuration().isNegative()); + assertFalse(po.getActiveDuration().isZero()); + assertThat(po.getActiveDuration().toMillis(), lessThanOrEqualTo(po.getActiveTimeMillis())); + assertThat(po.getActiveDuration(), lessThanOrEqualTo(po.getActiveTime())); + + assertThat(lastBorrowInstant1, lessThanOrEqualTo(po.getLastBorrowInstant())); + assertThat(lastReturnInstant1, lessThanOrEqualTo(po.getLastReturnInstant())); + assertThat(lastUsedInstant1, lessThanOrEqualTo(po.getLastUsedInstant())); + } + /** * On first borrow, first object fails validation, second object is OK. * Subsequent borrows are OK. This was POOL-152. diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java index 0415d39..a625787 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java @@ -35,8 +35,6 @@ import org.junit.jupiter.api.Timeout; */ public class TestGenericObjectPoolFactoryCreateFailure { - private static final Duration NEG_ONE_DURATION = Duration.ofMillis(-1); - private static class SingleObjectFactory extends BasePooledObjectFactory<Object> { private final AtomicBoolean created = new AtomicBoolean(); @@ -92,6 +90,8 @@ public class TestGenericObjectPoolFactoryCreateFailure { } } + private static final Duration NEG_ONE_DURATION = Duration.ofMillis(-1); + private static void println(final String msg) { // System.out.println(msg); }