prefill(keyedPool, iter.next(), count);
@Test public void testPrefillObjectPool() throws Exception { try { PoolUtils.prefill(null, 1); fail("PoolUtils.prefill(ObjectPool,int) must not allow null pool."); } catch (final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final ObjectPool<Object> pool = createProxy(ObjectPool.class, calledMethods)) { PoolUtils.prefill(pool, 0); final List<String> expectedMethods = new ArrayList<>(); assertEquals(expectedMethods, calledMethods); calledMethods.clear(); PoolUtils.prefill(pool, 3); for (int i = 0; i < 3; i++) { expectedMethods.add("addObject"); } assertEquals(expectedMethods, calledMethods); } }
@Test public void testPrefillKeyedObjectPool() throws Exception { try { PoolUtils.prefill(null, new Object(), 1); fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null pool."); } catch (final IllegalArgumentException iae) { // expected } try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object, Object> pool = createProxy(KeyedObjectPool.class, (List<String>) null)) { PoolUtils.prefill(pool, (Object) null, 1); fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null key."); } catch (final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object, Object> pool = createProxy(KeyedObjectPool.class, calledMethods)) { PoolUtils.prefill(pool, new Object(), 0); final List<String> expectedMethods = new ArrayList<>(); assertEquals(expectedMethods, calledMethods); calledMethods.clear(); PoolUtils.prefill(pool, new Object(), 3); for (int i = 0; i < 3; i++) { expectedMethods.add("addObject"); } assertEquals(expectedMethods, calledMethods); } }
@Test public void testPrefillKeyedObjectPoolCollection() throws Exception { try (@SuppressWarnings("unchecked") final KeyedObjectPool<String, String> pool = createProxy(KeyedObjectPool.class, (List<String>) null)) { PoolUtils.prefill(pool, (Collection<String>) null, 1); fail("PoolUtils.prefill(KeyedObjectPool,Collection,int) must not accept null keys."); } catch (final IllegalArgumentException iae) { // expected } final List<String> calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final KeyedObjectPool<String, Object> pool = createProxy(KeyedObjectPool.class, calledMethods)) { final Set<String> keys = new HashSet<>(); PoolUtils.prefill(pool, keys, 0); final List<String> expectedMethods = new ArrayList<>(); assertEquals(expectedMethods, calledMethods); calledMethods.clear(); keys.add("one"); keys.add("two"); keys.add("three"); PoolUtils.prefill(pool, keys, 3); for (int i = 0; i < keys.size() * 3; i++) { expectedMethods.add("addObject"); } assertEquals(expectedMethods, calledMethods); } }
@Test public void testKPOFCloseUsages() throws Exception { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); KeyedObjectPool<Object, Object> pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { return; // test not supported } final List<MethodCall> expectedMethods = new ArrayList<>(); /// Test correct behavior code paths PoolUtils.prefill(pool, KEY, 5); pool.close(); //// Test exception handling close should swallow failures try (final KeyedObjectPool<Object, Object> pool2 = makeEmptyPool(factory)) { reset(pool2, factory, expectedMethods); factory.setDestroyObjectFail(true); PoolUtils.prefill(pool2, KEY, 5); } }
@Test public void testPOFClearUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); final ObjectPool<Object> pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { return; // test not supported } final List<MethodCall> expectedMethods = new ArrayList<>(); /// Test correct behavior code paths PoolUtils.prefill(pool, 5); pool.clear(); //// Test exception handling clear should swallow destory object failures reset(pool, factory, expectedMethods); factory.setDestroyObjectFail(true); PoolUtils.prefill(pool, 5); pool.clear(); pool.close(); }
@Test public void testKPOFClearUsages() throws Exception { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); final KeyedObjectPool<Object,Object> pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { return; // test not supported } final List<MethodCall> expectedMethods = new ArrayList<>(); /// Test correct behavior code paths PoolUtils.prefill(pool, KEY, 5); pool.clear(); //// Test exception handling clear should swallow destroy object failures reset(pool, factory, expectedMethods); factory.setDestroyObjectFail(true); PoolUtils.prefill(pool, KEY, 5); pool.clear(); pool.close(); }
@Test public void testPOFCloseUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); ObjectPool<Object> pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { return; // test not supported } final List<MethodCall> expectedMethods = new ArrayList<>(); /// Test correct behavior code paths PoolUtils.prefill(pool, 5); pool.close(); //// Test exception handling close should swallow failures try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { return; // test not supported } reset(pool, factory, expectedMethods); factory.setDestroyObjectFail(true); PoolUtils.prefill(pool, 5); pool.close(); }
private void checkEvict(final boolean lifo) throws Exception { // yea this is hairy but it tests all the code paths in GOP.evict() genericObjectPool.setSoftMinEvictableIdleTimeMillis(10); genericObjectPool.setMinIdle(2); genericObjectPool.setTestWhileIdle(true); genericObjectPool.setLifo(lifo); PoolUtils.prefill(genericObjectPool, 5); genericObjectPool.evict(); simpleFactory.setEvenValid(false); simpleFactory.setOddValid(false); simpleFactory.setThrowExceptionOnActivate(true); genericObjectPool.evict(); PoolUtils.prefill(genericObjectPool, 5); simpleFactory.setThrowExceptionOnActivate(false); simpleFactory.setThrowExceptionOnPassivate(true); genericObjectPool.evict(); simpleFactory.setThrowExceptionOnPassivate(false); simpleFactory.setEvenValid(true); simpleFactory.setOddValid(true); Thread.sleep(125); genericObjectPool.evict(); assertEquals(2, genericObjectPool.getNumIdle()); }
prefill(keyedPool, iter.next(), count);