Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        public void TestBaseNumActiveNumIdle()
        {
            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);

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

            Assert.AreEqual(2, pool.KeyedActiveCount(keya));
            Assert.AreEqual(0, pool.KeyedIdleCount(keya));
            pool.ReturnObject(keya, obj1);
            Assert.AreEqual(1, pool.KeyedActiveCount(keya));
            Assert.AreEqual(1, pool.KeyedIdleCount(keya));
            pool.ReturnObject(keya, obj0);
            Assert.AreEqual(0, pool.KeyedActiveCount(keya));
            Assert.AreEqual(2, pool.KeyedIdleCount(keya));

            Assert.AreEqual(0, pool.KeyedActiveCount("xyzzy12345"));
            Assert.AreEqual(0, pool.KeyedIdleCount("xyzzy12345"));
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        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
            }
        }
Exemplo n.º 7
0
        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
            }
        }
Exemplo n.º 8
0
        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);
        }