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 2025/06/10 17:54:45 UTC
(commons-pool) 01/05: Use try with resources
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch POOL_2_X
in repository https://gitbox.apache.org/repos/asf/commons-pool.git
commit 9bf0502021c76dc9ac54e313c05ef4b84d74c449
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 10 13:10:28 2025 -0400
Use try with resources
---
.../pool2/impl/TestGenericKeyedObjectPool.java | 39 ++++++++++------------
1 file changed, 18 insertions(+), 21 deletions(-)
diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
index da15c3e4..f7dbd0d1 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
@@ -2109,28 +2109,25 @@ public class TestGenericKeyedObjectPool extends AbstractTestKeyedObjectPool {
factory.makeLatency = 500;
factory.setValidationEnabled(true); // turn on factory-level validatiom
factory.setValid(false); // make validation fail uniformly
- final GenericKeyedObjectPool<String, String> pool = new GenericKeyedObjectPool<>(factory);
-
- pool.setBlockWhenExhausted(true);
- pool.setMaxWait(maxWaitDuration);
- pool.setMaxTotalPerKey(1);
- pool.setMaxTotal(1);
- pool.setTestOnCreate(true);
- final Instant startTime = Instant.now();
-
- // Try to borrow an object. Validation will fail.
- // Then we will wait on the pool.
- try {
- pool.borrowObject("a");
- } catch (NoSuchElementException ex) {
- // expected
+ try (GenericKeyedObjectPool<String, String> pool = new GenericKeyedObjectPool<>(factory)) {
+ pool.setBlockWhenExhausted(true);
+ pool.setMaxWait(maxWaitDuration);
+ pool.setMaxTotalPerKey(1);
+ pool.setMaxTotal(1);
+ pool.setTestOnCreate(true);
+ final Instant startTime = Instant.now();
+ // Try to borrow an object. Validation will fail.
+ // Then we will wait on the pool.
+ try {
+ pool.borrowObject("a");
+ } catch (NoSuchElementException ex) {
+ // expected
+ }
+ // Should have timed out after 1000 ms from the start time
+ final Duration duration = Duration.between(startTime, Instant.now());
+ assertTrue(duration.toMillis() < maxWaitDuration.toMillis() + 10, // allow for some timing delay
+ "Thread A should have timed out after " + maxWaitDuration.toMillis() + " ms, but took " + duration.toMillis() + " ms");
}
-
- // Should have timed out after 1000 ms from the start time
- final Duration duration = Duration.between(startTime, Instant.now());
- assertTrue(duration.toMillis() < maxWaitDuration.toMillis() + 10, // allow for some timing delay
- "Thread A should have timed out after " + maxWaitDuration.toMillis() + " ms, but took " + duration.toMillis() + " ms");
- pool.close();
}
@Test