Example usage for org.apache.commons.pool KeyedObjectPool getNumIdle

List of usage examples for org.apache.commons.pool KeyedObjectPool getNumIdle

Introduction

In this page you can find the example usage for org.apache.commons.pool KeyedObjectPool getNumIdle.

Prototype

int getNumIdle(Object key) throws UnsupportedOperationException;

Source Link

Document

Returns the number of instances corresponding to the given key currently idle in this pool (optional operation).

Usage

From source file:edu.illinois.enforcemop.examples.apache.pool.TestKeyedObjectPool.java

public void testKPOFReturnObjectUsages() throws Exception {
    final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
    final KeyedObjectPool pool;
    try {//  w ww.j  a  v  a  2 s  .c  o  m
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths
    obj = pool.borrowObject(KEY);
    clear(factory, expectedMethods);

    // returned object should be passivated
    pool.returnObject(KEY, obj);
    if (pool instanceof StackKeyedObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", KEY, obj).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", KEY, obj));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of returnObject
    reset(pool, factory, expectedMethods);

    // passivateObject should swallow exceptions and not add the object to the pool
    pool.addObject(KEY);
    pool.addObject(KEY);
    pool.addObject(KEY);
    assertEquals(3, pool.getNumIdle(KEY));
    obj = pool.borrowObject(KEY);
    obj = pool.borrowObject(KEY);
    assertEquals(1, pool.getNumIdle(KEY));
    assertEquals(2, pool.getNumActive(KEY));
    clear(factory, expectedMethods);
    factory.setPassivateObjectFail(true);
    pool.returnObject(KEY, obj);
    if (pool instanceof StackKeyedObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", KEY, obj).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", KEY, obj));
    TestObjectPool.removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here.
    assertEquals(expectedMethods, factory.getMethodCalls());
    assertEquals(1, pool.getNumIdle(KEY)); // Not added
    assertEquals(1, pool.getNumActive(KEY)); // But not active

    reset(pool, factory, expectedMethods);
    obj = pool.borrowObject(KEY);
    clear(factory, expectedMethods);
    factory.setPassivateObjectFail(true);
    factory.setDestroyObjectFail(true);
    try {
        pool.returnObject(KEY, obj);
        if (!(pool instanceof GenericKeyedObjectPool)) { // ugh, 1.3-compat
            fail("Expecting destroyObject exception to be propagated");
        }
    } catch (PrivateException ex) {
        // Expected
    }
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestKeyedObjectPool.java

public void testClosedPoolBehavior() throws Exception {
    final KeyedObjectPool pool;
    try {/*w w w .jav a  2  s  .c  o m*/
        pool = makeEmptyPool(new BaseKeyedPoolableObjectFactory() {
            public Object makeObject(final Object key) throws Exception {
                return new Object();
            }
        });
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }

    Object o1 = pool.borrowObject(KEY);
    Object o2 = pool.borrowObject(KEY);

    pool.close();

    try {
        pool.addObject(KEY);
        fail("A closed pool must throw an IllegalStateException when addObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    try {
        pool.borrowObject(KEY);
        fail("A closed pool must throw an IllegalStateException when borrowObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    // The following should not throw exceptions just because the pool is closed.
    assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle(KEY));
    assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle());
    pool.getNumActive();
    pool.getNumActive(KEY);
    pool.returnObject(KEY, o1);
    assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
            pool.getNumIdle(KEY));
    assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
            pool.getNumIdle());
    pool.invalidateObject(KEY, o2);
    pool.clear(KEY);
    pool.clear();
    pool.close();
}