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 2019/04/02 12:36:28 UTC
[commons-pool] 04/04: More test cleanups.
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 a73fa240cdaffa1d5c5923ed5048b936265bcd16
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Apr 2 08:35:55 2019 -0400
More test cleanups.
---
.../pool2/impl/TestAbandonedObjectPool.java | 10 +--
.../pool2/impl/TestDefaultPooledObjectInfo.java | 2 +-
.../pool2/impl/TestGenericKeyedObjectPool.java | 14 ++--
.../commons/pool2/impl/TestGenericObjectPool.java | 92 +++++++++++-----------
.../pool2/performance/SleepingObjectFactory.java | 1 +
.../proxy/BaseTestProxiedKeyedObjectPool.java | 5 +-
.../pool2/proxy/BaseTestProxiedObjectPool.java | 3 +-
7 files changed, 68 insertions(+), 59 deletions(-)
diff --git a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java
index becfad1..df06677 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java
@@ -54,7 +54,7 @@ public class TestAbandonedObjectPool {
abandonedConfig.setRemoveAbandonedTimeout(1);
pool = new GenericObjectPool<>(
new SimpleFactory(),
- new GenericObjectPoolConfig(),
+ new GenericObjectPoolConfig<PooledTestObject>(),
abandonedConfig);
}
@@ -148,7 +148,7 @@ public class TestAbandonedObjectPool {
pool.close(); // Unregister pool created by setup
pool = new GenericObjectPool<>(
new SimpleFactory(200, 0),
- new GenericObjectPoolConfig(), abandonedConfig);
+ new GenericObjectPoolConfig<PooledTestObject>(), abandonedConfig);
final int n = 10;
pool.setMaxTotal(n);
pool.setBlockWhenExhausted(false);
@@ -186,7 +186,7 @@ public class TestAbandonedObjectPool {
pool = new GenericObjectPool<>(
// destroys take 200 ms
new SimpleFactory(200, 0),
- new GenericObjectPoolConfig(), abandonedConfig);
+ new GenericObjectPoolConfig<PooledTestObject>(), abandonedConfig);
final int n = 10;
pool.setMaxTotal(n);
pool.setBlockWhenExhausted(false);
@@ -216,8 +216,8 @@ public class TestAbandonedObjectPool {
pool.close(); // Unregister pool created by setup
pool = new GenericObjectPool<>(
// validate takes 1 second
- new SimpleFactory(0, 1000),
- new GenericObjectPoolConfig(), abandonedConfig);
+ new SimpleFactory(0, 1000),
+ new GenericObjectPoolConfig<PooledTestObject>(), abandonedConfig);
final int n = 10;
pool.setMaxTotal(n);
pool.setBlockWhenExhausted(false);
diff --git a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java
index 9960115..44a31fc 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java
@@ -113,7 +113,7 @@ public class TestDefaultPooledObjectInfo {
abandonedConfig.setRemoveAbandonedTimeout(1);
abandonedConfig.setLogAbandoned(true);
try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory(),
- new GenericObjectPoolConfig(), abandonedConfig)) {
+ new GenericObjectPoolConfig<String>(), abandonedConfig)) {
pool.borrowObject();
// pool.returnObject(s1); // Object not returned, causes abandoned object created exception
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 61c082e..ae18c90 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
@@ -422,7 +422,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
*/
@Test
public void testContructorEvictionConfig() throws Exception {
- final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
+ final GenericKeyedObjectPoolConfig<String> config = new GenericKeyedObjectPoolConfig<>();
config.setTimeBetweenEvictionRunsMillis(500);
config.setMinEvictableIdleTimeMillis(50);
config.setNumTestsPerEvictionRun(5);
@@ -433,11 +433,13 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
+ // ignore
}
assertEquals(5, p.getNumIdle("one"));
try {
Thread.sleep(500);
} catch (final InterruptedException e) {
+ // ignore
}
assertEquals(0, p.getNumIdle("one"));
}
@@ -1156,7 +1158,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_LIFO), Boolean.valueOf(objPool.getLifo()));
}
- final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
+ final GenericKeyedObjectPoolConfig<Object> config = new GenericKeyedObjectPoolConfig<>();
config.setLifo(lifo);
config.setMaxTotalPerKey(maxTotalPerKey);
config.setMaxIdlePerKey(maxIdle);
@@ -1565,7 +1567,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
// Give makeObject a little latency
factory.setMakeLatency(200);
try (final GenericKeyedObjectPool<String, String> pool = new GenericKeyedObjectPool<>(factory,
- new GenericKeyedObjectPoolConfig())) {
+ new GenericKeyedObjectPoolConfig<String>())) {
final String s = pool.borrowObject("one");
// First borrow waits on create, so wait time should be at least 200 ms
// Allow 100ms error in clock times
@@ -1974,7 +1976,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
public void testInvalidateWaiting()
throws Exception {
- final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
+ final GenericKeyedObjectPoolConfig<Object> config = new GenericKeyedObjectPoolConfig<>();
config.setMaxTotal(2);
config.setBlockWhenExhausted(true);
config.setMinIdlePerKey(0);
@@ -2178,7 +2180,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
public void testEqualsIndiscernible() throws Exception {
final HashSetFactory factory = new HashSetFactory();
try (final GenericKeyedObjectPool<String, HashSet<String>> pool = new GenericKeyedObjectPool<>(factory,
- new GenericKeyedObjectPoolConfig())) {
+ new GenericKeyedObjectPoolConfig<HashSet<String>>())) {
final HashSet<String> s1 = pool.borrowObject("a");
final HashSet<String> s2 = pool.borrowObject("a");
pool.returnObject("a", s1);
@@ -2196,7 +2198,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
public void testMutable() throws Exception {
final HashSetFactory factory = new HashSetFactory();
try (final GenericKeyedObjectPool<String, HashSet<String>> pool = new GenericKeyedObjectPool<>(factory,
- new GenericKeyedObjectPoolConfig())) {
+ new GenericKeyedObjectPoolConfig<HashSet<String>>())) {
final HashSet<String> s1 = pool.borrowObject("a");
final HashSet<String> s2 = pool.borrowObject("a");
s1.add("One");
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 6b97fb8..d9be87f 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
@@ -685,7 +685,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
private SimpleFactory simpleFactory = null;
- private void assertConfiguration(final GenericObjectPoolConfig expected, final GenericObjectPool<?> actual) throws Exception {
+ private void assertConfiguration(final GenericObjectPoolConfig<?> expected, final GenericObjectPool<?> actual) throws Exception {
assertEquals("testOnCreate",Boolean.valueOf(expected.getTestOnCreate()),
Boolean.valueOf(actual.getTestOnCreate()));
assertEquals("testOnBorrow",Boolean.valueOf(expected.getTestOnBorrow()),
@@ -1152,7 +1152,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
final SimpleFactory factory = new SimpleFactory();
// Give makeObject a little latency
factory.setMakeLatency(200);
- try (final GenericObjectPool<String> pool = new GenericObjectPool<>(factory, new GenericObjectPoolConfig())) {
+ try (final GenericObjectPool<String> pool = new GenericObjectPool<>(factory, new GenericObjectPoolConfig<String>())) {
final String s = pool.borrowObject();
// First borrow waits on create, so wait time should be at least 200 ms
// Allow 100ms error in clock times
@@ -1169,27 +1169,27 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
@Test(timeout = 60000)
public void testCloseMultiplePools1() throws Exception {
- final GenericObjectPool<String> genericObjectPool2 = new GenericObjectPool<>(simpleFactory);
- genericObjectPool.setTimeBetweenEvictionRunsMillis(1);
- genericObjectPool2.setTimeBetweenEvictionRunsMillis(1);
- genericObjectPool2.close();
+ try (final GenericObjectPool<String> genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) {
+ genericObjectPool.setTimeBetweenEvictionRunsMillis(1);
+ genericObjectPool2.setTimeBetweenEvictionRunsMillis(1);
+ }
genericObjectPool.close();
}
@Test(timeout = 60000)
public void testCloseMultiplePools2() throws Exception {
- final GenericObjectPool<String> genericObjectPool2 = new GenericObjectPool<>(simpleFactory);
- // Ensure eviction takes a long time, during which time EvictionTimer.executor's queue is empty
- simpleFactory.setDestroyLatency(1000L);
- // Ensure there is an object to evict, so that above latency takes effect
- genericObjectPool.setTimeBetweenEvictionRunsMillis(1);
- genericObjectPool2.setTimeBetweenEvictionRunsMillis(1);
- genericObjectPool.setMinEvictableIdleTimeMillis(1);
- genericObjectPool2.setMinEvictableIdleTimeMillis(1);
- genericObjectPool.addObject();
- genericObjectPool2.addObject();
- // Close both pools
- genericObjectPool2.close();
+ try (final GenericObjectPool<String> genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) {
+ // Ensure eviction takes a long time, during which time EvictionTimer.executor's queue is empty
+ simpleFactory.setDestroyLatency(1000L);
+ // Ensure there is an object to evict, so that above latency takes effect
+ genericObjectPool.setTimeBetweenEvictionRunsMillis(1);
+ genericObjectPool2.setTimeBetweenEvictionRunsMillis(1);
+ genericObjectPool.setMinEvictableIdleTimeMillis(1);
+ genericObjectPool2.setMinEvictableIdleTimeMillis(1);
+ genericObjectPool.addObject();
+ genericObjectPool2.addObject();
+ // Close both pools
+ }
genericObjectPool.close();
}
@@ -1276,9 +1276,11 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
@Test(expected=IllegalArgumentException.class)
public void testConstructorNullFactory() {
// add dummy assert (won't be invoked because of IAE) to avoid "unused" warning
- assertNotNull(new GenericObjectPool<String>(null));
- // TODO this currently causes tearDown to report an error
- // Looks like GOP needs to call close() or jmxUnregister() before throwing IAE
+ try (GenericObjectPool<String> object = new GenericObjectPool<>(null)) {
+ assertNotNull(object);
+ // TODO this currently causes tearDown to report an error
+ // Looks like GOP needs to call close() or jmxUnregister() before throwing IAE
+ }
}
@@ -1321,7 +1323,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_LIFO), Boolean.valueOf(dummyPool.getLifo()));
}
- final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
+ final GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();
config.setLifo(lifo);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
@@ -1352,7 +1354,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
@Test(timeout = 60000)
public void testDefaultConfiguration() throws Exception {
- assertConfiguration(new GenericObjectPoolConfig(),genericObjectPool);
+ assertConfiguration(new GenericObjectPoolConfig<>(),genericObjectPool);
}
/**
@@ -1365,7 +1367,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
public void testEqualsIndiscernible() throws Exception {
final HashSetFactory factory = new HashSetFactory();
try (final GenericObjectPool<HashSet<String>> pool = new GenericObjectPool<>(factory,
- new GenericObjectPoolConfig())) {
+ new GenericObjectPoolConfig<HashSet<String>>())) {
final HashSet<String> s1 = pool.borrowObject();
final HashSet<String> s2 = pool.borrowObject();
pool.returnObject(s1);
@@ -1948,7 +1950,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
Assert.assertEquals(1, result.size());
genericObjectPool.jmxUnregister();
- final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
+ final GenericObjectPoolConfig<String> config = new GenericObjectPoolConfig<>();
config.setJmxEnabled(false);
try (final GenericObjectPool<String> poolWithoutJmx = new GenericObjectPool<>(simpleFactory, config)) {
assertNull(poolWithoutJmx.getJmxName());
@@ -2416,7 +2418,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
// POOL-248
@Test(expected=IllegalStateException.class)
public void testMultipleReturnOfSameObject() throws Exception {
- try (final GenericObjectPool<String> pool = new GenericObjectPool<>(simpleFactory, new GenericObjectPoolConfig())) {
+ try (final GenericObjectPool<String> pool = new GenericObjectPool<>(simpleFactory, new GenericObjectPoolConfig<String>())) {
Assert.assertEquals(0, pool.getNumActive());
Assert.assertEquals(0, pool.getNumIdle());
@@ -2448,7 +2450,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
public void testMutable() throws Exception {
final HashSetFactory factory = new HashSetFactory();
try (final GenericObjectPool<HashSet<String>> pool = new GenericObjectPool<>(factory,
- new GenericObjectPoolConfig())) {
+ new GenericObjectPoolConfig<HashSet<String>>())) {
final HashSet<String> s1 = pool.borrowObject();
final HashSet<String> s2 = pool.borrowObject();
s1.add("One");
@@ -2506,26 +2508,28 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception {
final long maxWaitMillis = 500;
- final GenericObjectPool<String> createSlowObjectFactoryPool
- = new GenericObjectPool<>(createSlowObjectFactory(60000));
- createSlowObjectFactoryPool.setMaxTotal(1);
- createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis);
+ try (final GenericObjectPool<String> createSlowObjectFactoryPool = new GenericObjectPool<>(
+ createSlowObjectFactory(60000))) {
+ createSlowObjectFactoryPool.setMaxTotal(1);
+ createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis);
- // thread1 tries creating a slow object to make pool full.
- final WaitingTestThread thread1 = new WaitingTestThread(createSlowObjectFactoryPool, 0);
- thread1.start();
+ // thread1 tries creating a slow object to make pool full.
+ final WaitingTestThread thread1 = new WaitingTestThread(createSlowObjectFactoryPool, 0);
+ thread1.start();
- // Wait for thread1's reaching to create().
- Thread.sleep(100);
+ // Wait for thread1's reaching to create().
+ Thread.sleep(100);
- // another one tries borrowObject. It should return within maxWaitMillis.
- try {
- createSlowObjectFactoryPool.borrowObject(maxWaitMillis);
- fail("borrowObject must fail due to timeout by maxWaitMillis");
- } catch (final NoSuchElementException e) {
- }
+ // another one tries borrowObject. It should return within maxWaitMillis.
+ try {
+ createSlowObjectFactoryPool.borrowObject(maxWaitMillis);
+ fail("borrowObject must fail due to timeout by maxWaitMillis");
+ } catch (final NoSuchElementException e) {
+ // ignore
+ }
- Assert.assertTrue(thread1.isAlive());
+ Assert.assertTrue(thread1.isAlive());
+ }
}
/**
@@ -2568,7 +2572,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool {
@Test(timeout = 60000)
public void testSetConfig() throws Exception {
- final GenericObjectPoolConfig expected = new GenericObjectPoolConfig();
+ final GenericObjectPoolConfig<String> expected = new GenericObjectPoolConfig<>();
assertConfiguration(expected,genericObjectPool);
expected.setMaxTotal(2);
expected.setMaxIdle(3);
diff --git a/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java b/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java
index 65af241..1c3b544 100644
--- a/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java
+++ b/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java
@@ -76,6 +76,7 @@ public class SleepingObjectFactory implements PooledObjectFactory<Integer> {
Thread.sleep(millis);
}
catch (final InterruptedException e) {
+ // ignore
}
}
diff --git a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java
index bd89722..db29e84 100644
--- a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java
@@ -39,13 +39,14 @@ public abstract class BaseTestProxiedKeyedObjectPool {
@Before
public void setUp() {
- final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
+ final GenericKeyedObjectPoolConfig<TestObject> config = new GenericKeyedObjectPoolConfig<>();
config.setMaxTotal(3);
final KeyedPooledObjectFactory<String, TestObject> factory =
new TestKeyedObjectFactory();
- final KeyedObjectPool<String,TestObject> innerPool =
+ @SuppressWarnings("resource")
+ final KeyedObjectPool<String, TestObject> innerPool =
new GenericKeyedObjectPool<>(
factory, config);
diff --git a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java
index 7e5222a..9eca80a 100644
--- a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java
@@ -55,11 +55,12 @@ public abstract class BaseTestProxiedObjectPool {
abandonedConfig.setRemoveAbandonedTimeout(ABANDONED_TIMEOUT_SECS);
abandonedConfig.setLogWriter(pw);
- final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
+ final GenericObjectPoolConfig<TestObject> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(3);
final PooledObjectFactory<TestObject> factory = new TestObjectFactory();
+ @SuppressWarnings("resource")
final ObjectPool<TestObject> innerPool =
new GenericObjectPool<>(factory, config, abandonedConfig);