Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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
            }
        }