public void TestKPOFClearUsages()
        {
            FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
            KeyedObjectPool <Object, Object>  pool;

            try
            {
                pool = MakeEmptyPool(factory);
            }
            catch (NotSupportedException)
            {
                return; // test not supported
            }
            List <MethodCall> expectedMethods = new ArrayList <MethodCall>();

            // Test correct behavior code paths
            PoolUtils.PreFill(pool, KEY, 5);
            pool.Clear();

            // Test exception handling Clear should swallow destory object failures
            Reset(pool, factory, expectedMethods);
            factory.DestroyObjectFail = true;
            PoolUtils.PreFill(pool, KEY, 5);
            pool.Clear();
        }
        public void TestClosedPoolBehavior()
        {
            KeyedObjectPool <Object, Object> pool;

            try
            {
                pool = MakeEmptyPool(new InternalKeyedPoolableObjectFactory());
            }
            catch (NotSupportedException)
            {
                return; // test not supported
            }

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

            pool.Close();

            try
            {
                pool.AddObject(KEY);
                Assert.Fail("A Closed pool must throw an IllegalStateException when AddObject is called.");
            }
            catch (IllegalStateException)
            {
                // expected
            }

            try
            {
                pool.BorrowObject(KEY);
                Assert.Fail("A Closed pool must throw an IllegalStateException when BorrowObject is called.");
            }
            catch (IllegalStateException)
            {
                // expected
            }

            // The following should not throw exceptions just because the pool is Closed.
            Assert.AreEqual(0, pool.KeyedIdleCount(KEY), "A Closed pool shouldn't have any idle objects.");
            Assert.AreEqual(0, pool.IdleCount, "A Closed pool shouldn't have any idle objects.");
            int count = pool.ActiveCount;

            count = pool.KeyedActiveCount(KEY);
            count++;
            pool.ReturnObject(KEY, o1);
            Assert.AreEqual(0, pool.KeyedIdleCount(KEY), "ReturnObject should not add items back into the idle object pool for a Closed pool.");
            Assert.AreEqual(0, pool.IdleCount, "ReturnObject should not add items back into the idle object pool for a Closed pool.");
            pool.InvalidateObject(KEY, o2);
            pool.Clear(KEY);
            pool.Clear();
            pool.Close();
        }
        public void TestBaseClear()
        {
            try
            {
                pool = MakeEmptyPool(3);
            }
            catch (NotSupportedException)
            {
                return; // skip this test if unsupported
            }
            Object keya = MakeKey(0);

            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            Object obj0 = pool.BorrowObject(keya);
            Object obj1 = pool.BorrowObject(keya);

            Assert.AreEqual(2, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            pool.ReturnObject(keya, obj1);
            pool.ReturnObject(keya, obj0);
            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(2, pool.KeyedIdleCount(keya));
            pool.Clear(keya);
            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            Object obj2 = pool.BorrowObject(keya);

            Assert.AreEqual(GetNthObject(keya, 2), obj2);
        }
 private void Reset(KeyedObjectPool <Object, Object> pool, FailingKeyedPoolableObjectFactory factory, List <MethodCall> expectedMethods)
 {
     pool.Clear();
     Clear(factory, expectedMethods);
     factory.Reset();
 }