public void TestBaseInvalidateObject()
        {
            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.InvalidateObject(keya, obj0);
            Assert.AreEqual(1, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            pool.InvalidateObject(keya, obj1);
            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
        }
        public void TestBaseNumActiveNumIdle2()
        {
            try
            {
                pool = MakeEmptyPool(6);
            }
            catch (NotSupportedException)
            {
                return; // skip this test if unsupported
            }
            Object keya = MakeKey(0);
            Object keyb = MakeKey(1);

            Assert.AreEqual(0, pool.ActiveCount);
            Assert.AreEqual(0, pool.IdleCount);
            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            Assert.AreEqual(0, pool.KeyedActiveCount(keyb));
            Assert.AreEqual(0, pool.KeyedIdleCount(keyb));

            Object objA0 = pool.BorrowObject(keya);
            Object objB0 = pool.BorrowObject(keyb);

            Assert.AreEqual(2, pool.ActiveCount);
            Assert.AreEqual(0, pool.IdleCount);
            Assert.AreEqual(1, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            Assert.AreEqual(1, pool.KeyedActiveCount(keyb));
            Assert.AreEqual(0, pool.KeyedIdleCount(keyb));

            Object objA1 = pool.BorrowObject(keya);
            Object objB1 = pool.BorrowObject(keyb);

            Assert.AreEqual(4, pool.ActiveCount);
            Assert.AreEqual(0, pool.IdleCount);
            Assert.AreEqual(2, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            Assert.AreEqual(2, pool.KeyedActiveCount(keyb));
            Assert.AreEqual(0, pool.KeyedIdleCount(keyb));

            pool.ReturnObject(keya, objA0);
            pool.ReturnObject(keyb, objB0);

            Assert.AreEqual(2, pool.ActiveCount);
            Assert.AreEqual(2, pool.IdleCount);
            Assert.AreEqual(1, pool.KeyedActiveCount(keya));
            Assert.AreEqual(1, pool.KeyedIdleCount(keya));
            Assert.AreEqual(1, pool.KeyedActiveCount(keyb));
            Assert.AreEqual(1, pool.KeyedIdleCount(keyb));

            pool.ReturnObject(keya, objA1);
            pool.ReturnObject(keyb, objB1);

            Assert.AreEqual(0, pool.ActiveCount);
            Assert.AreEqual(4, pool.IdleCount);
            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(2, pool.KeyedIdleCount(keya));
            Assert.AreEqual(0, pool.KeyedActiveCount(keyb));
            Assert.AreEqual(2, pool.KeyedIdleCount(keyb));
        }
        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);
        }
        public void TestKPOFCloseUsages()
        {
            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.Close();


            // Test exception handling Close should swallow failures
            pool = MakeEmptyPool(factory);
            Reset(pool, factory, expectedMethods);
            factory.DestroyObjectFail = true;
            PoolUtils.PreFill(pool, KEY, 5);
            pool.Close();
        }
        public void testKPOFAddObjectUsage()
        {
            FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
            KeyedObjectPool <Object, Object>  pool;

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

            // AddObject should make a new object, pasivate it and put it in the pool
            pool.AddObject(KEY);
            expectedMethods.Add(new MethodCall("makeObject", KEY).SetReturned(ZERO));
            expectedMethods.Add(new MethodCall("passivateObject", KEY, ZERO));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            // Test exception handling of AddObject
            Reset(pool, factory, expectedMethods);

            // makeObject Exceptions should be propagated to client code from AddObject
            factory.CreateObjectFail = true;
            try
            {
                pool.AddObject(KEY);
                Assert.Fail("Expected AddObject to propagate makeObject exception.");
            }
            catch (MethodAccessException)
            {
                // expected
            }
            expectedMethods.Add(new MethodCall("makeObject", KEY));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            Clear(factory, expectedMethods);

            // passivateObject Exceptions should be propagated to client code from AddObject
            factory.CreateObjectFail  = false;
            factory.SuspendObjectFail = true;
            try
            {
                pool.AddObject(KEY);
                Assert.Fail("Expected AddObject to propagate passivateObject exception.");
            }
            catch (MethodAccessException)
            {
                // expected
            }
            expectedMethods.Add(new MethodCall("makeObject", KEY).SetReturned(ONE));
            expectedMethods.Add(new MethodCall("passivateObject", KEY, ONE));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
        }
        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 TestBaseBorrowReturn()
        {
            try
            {
                pool = MakeEmptyPool(3);
            }
            catch (NotSupportedException)
            {
                return; // skip this test if unsupported
            }
            Object keya = MakeKey(0);
            Object obj0 = pool.BorrowObject(keya);

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

            Assert.AreEqual(GetNthObject(keya, 1), obj1);
            Object obj2 = pool.BorrowObject(keya);

            Assert.AreEqual(GetNthObject(keya, 2), obj2);
            pool.ReturnObject(keya, obj2);
            obj2 = pool.BorrowObject(keya);
            Assert.AreEqual(GetNthObject(keya, 2), obj2);
            pool.ReturnObject(keya, obj1);
            obj1 = pool.BorrowObject(keya);
            Assert.AreEqual(GetNthObject(keya, 1), obj1);
            pool.ReturnObject(keya, obj0);
            pool.ReturnObject(keya, obj2);
            obj2 = pool.BorrowObject(keya);

            if (IsLifo())
            {
                Assert.AreEqual(GetNthObject(keya, 2), obj2);
            }
            if (IsFifo())
            {
                Assert.AreEqual(GetNthObject(keya, 0), obj2);
            }
            obj0 = pool.BorrowObject(keya);
            if (IsLifo())
            {
                Assert.AreEqual(GetNthObject(keya, 0), obj0);
            }
            if (IsFifo())
            {
                Assert.AreEqual(GetNthObject(keya, 2), obj0);
            }
        }
Exemple #8
0
        public void TestCreatePool()
        {
            KeyedObjectPoolFactory <Object, Object> factory;

            try
            {
                factory = MakeFactory();
            }
            catch (NotSupportedException)
            {
                return;
            }
            KeyedObjectPool <Object, Object> pool = factory.CreatePool();

            pool.Close();
        }
Exemple #9
0
        public static void PreFill <K, V>(KeyedObjectPool <K, V> keyedPool, K key, int count) where V : class
        {
            if (keyedPool == null)
            {
                throw new ArgumentException("keyedPool must not be null.");
            }

            if (key == null)
            {
                throw new ArgumentException("key must not be null.");
            }

            for (int i = 0; i < count; i++)
            {
                keyedPool.AddObject(key);
            }
        }
        public void TestKPOFInvalidateObjectUsages()
        {
            FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
            KeyedObjectPool <Object, Object>  pool;

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

            // Test correct behavior code paths

            obj = pool.BorrowObject(KEY);
            Clear(factory, expectedMethods);

            // invalidated object should be destroyed
            pool.InvalidateObject(KEY, obj);
            expectedMethods.Add(new MethodCall("destroyObject", KEY, obj));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            // Test exception handling of InvalidateObject
            Reset(pool, factory, expectedMethods);
            obj = pool.BorrowObject(KEY);
            Clear(factory, expectedMethods);
            factory.DestroyObjectFail = true;
            try
            {
                pool.InvalidateObject(KEY, obj);
                Assert.Fail("Expecting destroy exception to propagate");
            }
            catch (MethodAccessException)
            {
                // Expected
            }
            Thread.Sleep(250);
            TestObjectPool.RemoveDestroyObjectCall(factory.MethodCalls);
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
        }
        public void TestBaseBorrow()
        {
            try
            {
                pool = MakeEmptyPool(3);
            }
            catch (NotSupportedException)
            {
                return; // skip this test if unsupported
            }
            Object keya = MakeKey(0);
            Object keyb = MakeKey(1);

            Assert.AreEqual(GetNthObject(keya, 0), pool.BorrowObject(keya), "1");
            Assert.AreEqual(GetNthObject(keyb, 0), pool.BorrowObject(keyb), "2");
            Assert.AreEqual(GetNthObject(keyb, 1), pool.BorrowObject(keyb), "3");
            Assert.AreEqual(GetNthObject(keya, 1), pool.BorrowObject(keya), "4");
            Assert.AreEqual(GetNthObject(keyb, 2), pool.BorrowObject(keyb), "5");
            Assert.AreEqual(GetNthObject(keya, 2), pool.BorrowObject(keya), "6");
        }
        public void TestBaseAddObject()
        {
            try
            {
                pool = MakeEmptyPool(3);
            }
            catch (NotSupportedException)
            {
                return; // skip this test if unsupported
            }
            Object key = MakeKey(0);

            try
            {
                Assert.AreEqual(0, pool.IdleCount);
                Assert.AreEqual(0, pool.ActiveCount);
                Assert.AreEqual(0, pool.KeyedIdleCount(key));
                Assert.AreEqual(0, pool.KeyedActiveCount(key));
                pool.AddObject(key);
                Assert.AreEqual(1, pool.IdleCount);
                Assert.AreEqual(0, pool.ActiveCount);
                Assert.AreEqual(1, pool.KeyedIdleCount(key));
                Assert.AreEqual(0, pool.KeyedActiveCount(key));
                Object obj = pool.BorrowObject(key);
                Assert.AreEqual(GetNthObject(key, 0), obj);
                Assert.AreEqual(0, pool.IdleCount);
                Assert.AreEqual(1, pool.ActiveCount);
                Assert.AreEqual(0, pool.KeyedIdleCount(key));
                Assert.AreEqual(1, pool.KeyedActiveCount(key));
                pool.ReturnObject(key, obj);
                Assert.AreEqual(1, pool.IdleCount);
                Assert.AreEqual(0, pool.ActiveCount);
                Assert.AreEqual(1, pool.KeyedIdleCount(key));
                Assert.AreEqual(0, pool.KeyedActiveCount(key));
            }
            catch (NotSupportedException)
            {
                return; // skip this test if one of those calls is unsupported
            }
        }
 private void Reset(KeyedObjectPool <Object, Object> pool, FailingKeyedPoolableObjectFactory factory, List <MethodCall> expectedMethods)
 {
     pool.Clear();
     Clear(factory, expectedMethods);
     factory.Reset();
 }
 public void TearDown()
 {
     pool = null;
 }
        public void TestKPOFReturnObjectUsages()
        {
            FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
            KeyedObjectPool <Object, Object>  pool;

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

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

            // returned object should be passivated
            pool.ReturnObject(KEY, obj);
            expectedMethods.Add(new MethodCall("passivateObject", KEY, obj));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            // 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);
            Assert.AreEqual(3, pool.KeyedIdleCount(KEY));
            obj = pool.BorrowObject(KEY);
            obj = pool.BorrowObject(KEY);
            Assert.AreEqual(1, pool.KeyedIdleCount(KEY));
            Assert.AreEqual(2, pool.KeyedActiveCount(KEY));
            Clear(factory, expectedMethods);
            factory.SuspendObjectFail = true;
            pool.ReturnObject(KEY, obj);
            expectedMethods.Add(new MethodCall("passivateObject", KEY, obj));
            TestObjectPool.RemoveDestroyObjectCall(factory.MethodCalls); // The exact timing of destroyObject is flexible here.
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
            Assert.AreEqual(1, pool.KeyedIdleCount(KEY));                // Not added
            Assert.AreEqual(1, pool.KeyedActiveCount(KEY));              // But not active

            Reset(pool, factory, expectedMethods);
            obj = pool.BorrowObject(KEY);
            Clear(factory, expectedMethods);
            factory.SuspendObjectFail = true;
            factory.DestroyObjectFail = true;
            try
            {
                pool.ReturnObject(KEY, obj);
                if (!(pool is GenericKeyedObjectPool <Object, Object>))
                {
                    Assert.Fail("Expecting DestroyObject exception to be propagated");
                }
            }
            catch (MethodAccessException)
            {
                // Expected
            }
        }
        public void TestKPOFBorrowObjectUsages()
        {
            FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
            KeyedObjectPool <Object, Object>  pool;

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

            if (pool is GenericKeyedObjectPool <Object, Object> )
            {
                ((GenericKeyedObjectPool <Object, Object>)pool).TestOnBorrow = true;
            }

            // Test correct behavior code paths

            // existing idle object should be activated and validated
            pool.AddObject(KEY);
            Clear(factory, expectedMethods);
            obj = pool.BorrowObject(KEY);
            expectedMethods.Add(new MethodCall("activateObject", KEY, ZERO));
            expectedMethods.Add(new MethodCall("validateObject", KEY, ZERO).SetReturned(true));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
            pool.ReturnObject(KEY, obj);

            // Test exception handling of BorrowObject
            Reset(pool, factory, expectedMethods);

            // makeObject Exceptions should be propagated to client code from BorrowObject
            factory.CreateObjectFail = true;
            try
            {
                obj = pool.BorrowObject(KEY);
                Assert.Fail("Expected BorrowObject to propagate makeObject exception.");
            }
            catch (MethodAccessException)
            {
                // expected
            }
            expectedMethods.Add(new MethodCall("makeObject", KEY));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            // when activateObject fails in BorrowObject, a new object should be borrowed/created
            Reset(pool, factory, expectedMethods);
            pool.AddObject(KEY);
            Clear(factory, expectedMethods);

            factory.ActivateObjectFail = true;
            expectedMethods.Add(new MethodCall("activateObject", KEY, obj));
            try
            {
                obj = pool.BorrowObject(KEY);
                Assert.Fail("Expecting NoSuchElementException");
            }
            catch (NoSuchElementException)
            {
                //Activate should fail
            }
            // After idle object fails validation, new on is created and activation
            // fails again for the new one.
            expectedMethods.Add(new MethodCall("makeObject", KEY).SetReturned(ONE));
            expectedMethods.Add(new MethodCall("activateObject", KEY, ONE));
            TestObjectPool.RemoveDestroyObjectCall(factory.MethodCalls); // The exact timing of destroyObject is flexible here.
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            // when validateObject fails in BorrowObject, a new object should be borrowed/created
            Reset(pool, factory, expectedMethods);
            pool.AddObject(KEY);
            Clear(factory, expectedMethods);

            factory.ValidateObjectFail = true;
            // testOnBorrow is on, so this will throw when the newly created instance
            // fails validation
            try
            {
                obj = pool.BorrowObject(KEY);
                Assert.Fail("Expecting NoSuchElementException");
            }
            catch (NoSuchElementException)
            {
                // expected
            }
            // Activate, then validate for idle instance
            expectedMethods.Add(new MethodCall("activateObject", KEY, ZERO));
            expectedMethods.Add(new MethodCall("validateObject", KEY, ZERO));
            // Make new instance, activate succeeds, validate fails
            expectedMethods.Add(new MethodCall("makeObject", KEY).SetReturned(ONE));
            expectedMethods.Add(new MethodCall("activateObject", KEY, ONE));
            expectedMethods.Add(new MethodCall("validateObject", KEY, ONE));
            TestObjectPool.RemoveDestroyObjectCall(factory.MethodCalls);
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
        }