You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/08/13 13:34:47 UTC

[commons-pool] branch master updated: Add test.

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


The following commit(s) were added to refs/heads/master by this push:
     new 66a449b  Add test.
66a449b is described below

commit 66a449bcbe695df59c4f8c4725d2e7ebc7f018dd
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 13 09:34:44 2021 -0400

    Add test.
---
 .../commons/pool2/impl/TestGenericObjectPool.java  | 65 ++++++++++++++--------
 1 file changed, 42 insertions(+), 23 deletions(-)

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 d40a4f6..ffa97a0 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
@@ -34,6 +34,7 @@ import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
 import java.nio.charset.UnsupportedCharsetException;
 import java.time.Duration;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -1023,6 +1024,46 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
         }
     }
 
+    @Test
+    public void testBorrowTimings() throws Exception {
+        // Borrow
+        final String object = genericObjectPool.borrowObject();
+        final PooledObject<String> dpo = genericObjectPool.getPooledObject(object);
+
+        final Instant lastBorrowInstant1 = dpo.getLastBorrowInstant();
+        final Instant lastReturnInstant1 = dpo.getLastReturnInstant();
+        final Instant lastUsedInstant1 = dpo.getLastUsedInstant();
+
+        assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(lastBorrowInstant1));
+        assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(lastReturnInstant1));
+        assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(lastUsedInstant1));
+
+        // 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(dpo.getActiveDuration().isNegative());
+        assertFalse(dpo.getActiveDuration().isZero());
+        assertThat(dpo.getActiveDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis()));
+        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getActiveTime()));
+        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getIdleDuration()));
+        //
+        assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(dpo.getLastBorrowInstant()));
+        assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(dpo.getLastReturnInstant()));
+        assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(dpo.getLastUsedInstant()));
+
+        assertThat(lastBorrowInstant1, lessThanOrEqualTo(dpo.getLastBorrowInstant()));
+        assertThat(lastReturnInstant1, lessThanOrEqualTo(dpo.getLastReturnInstant()));
+        assertThat(lastUsedInstant1, lessThanOrEqualTo(dpo.getLastUsedInstant()));
+
+        // Return
+        genericObjectPool.returnObject(object);
+
+        assertFalse(dpo.getActiveDuration().isNegative());
+        assertFalse(dpo.getActiveDuration().isZero());
+        assertThat(dpo.getActiveDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis()));
+        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getActiveTime()));
+    }
+
     /*
      * 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
@@ -1181,6 +1222,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
         genericObjectPool.close();
     }
 
+
     @Test
     @Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
     public void testConcurrentBorrowAndEvict() throws Exception {
@@ -1209,7 +1251,6 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
         }
     }
 
-
     /**
      * POOL-231 - verify that concurrent invalidates of the same object do not
      * corrupt pool destroyCount.
@@ -1900,28 +1941,6 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
     }
 
     @Test
-    public void testGetActiveDuration() throws Exception {
-        // Borrow
-        final String object = genericObjectPool.borrowObject();
-        final PooledObject<String> dpo = genericObjectPool.getPooledObject(object);
-
-        // Sleep MUST be "long enough" to detect that more than 0 milliseconds have elapsed.
-        Thread.sleep(200);
-        assertFalse(dpo.getActiveDuration().isNegative());
-        assertFalse(dpo.getActiveDuration().isZero());
-        assertThat(dpo.getActiveDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis()));
-        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getActiveTime()));
-
-        // Return
-        genericObjectPool.returnObject(object);
-
-        assertFalse(dpo.getActiveDuration().isNegative());
-        assertFalse(dpo.getActiveDuration().isZero());
-        assertThat(dpo.getActiveDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis()));
-        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getActiveTime()));
-    }
-
-    @Test
     public void testGetFactoryType_DefaultPooledObjectFactory() {
         try (final GenericObjectPool<String> pool = new GenericObjectPool<>(createDefaultPooledObjectFactory())) {
             assertNotNull((pool.getFactoryType()));