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 2023/07/14 19:27:23 UTC

[commons-pool] 02/02: Use try-with-resources

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 86a1256a9fc786d74d405d6669073615e5bb01a8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jul 14 15:27:16 2023 -0400

    Use try-with-resources
---
 .../commons/pool2/impl/TestGenericObjectPool.java  | 46 +++++++++++-----------
 1 file changed, 23 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 e0cc113d..5d35e20d 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
@@ -2055,30 +2055,30 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
             DisconnectingWaiterFactory.DEFAULT_DISCONNECTED_LIFECYCLE_ACTION,
             (obj) -> false // all instances fail validation
         );
-        final GenericObjectPool<Waiter, IllegalStateException> pool = new GenericObjectPool<>(factory);
-        pool.setMaxWait(Duration.ofMillis(100));
-        pool.setTestOnReturn(true);
-        pool.setMaxTotal(1);
-        final Waiter w = pool.borrowObject();
         final AtomicBoolean failed = new AtomicBoolean(false);
-        final Thread t = new Thread(() -> {
-            try {
-                pool.borrowObject();
-            } catch (final NoSuchElementException e) {
-                failed.set(true);
-            }
-        });
-        Thread.sleep(10);
-        t.start();
-        // t is blocked waiting on the deque
-        Thread.sleep(10);
-        factory.disconnect();
-        pool.returnObject(w);  // validation fails, so no return
-        Thread.sleep(10);
-        factory.connect();
-        // Borrower should be able to be served now
-        t.join();
-        pool.close();
+        try (GenericObjectPool<Waiter, IllegalStateException> pool = new GenericObjectPool<>(factory)) {
+            pool.setMaxWait(Duration.ofMillis(100));
+            pool.setTestOnReturn(true);
+            pool.setMaxTotal(1);
+            final Waiter w = pool.borrowObject();
+            final Thread t = new Thread(() -> {
+                try {
+                    pool.borrowObject();
+                } catch (final NoSuchElementException e) {
+                    failed.set(true);
+                }
+            });
+            Thread.sleep(10);
+            t.start();
+            // t is blocked waiting on the deque
+            Thread.sleep(10);
+            factory.disconnect();
+            pool.returnObject(w); // validation fails, so no return
+            Thread.sleep(10);
+            factory.connect();
+            // Borrower should be able to be served now
+            t.join();
+        }
         if (failed.get()) {
             fail("Borrower timed out waiting for an instance");
         }