Beispiel #1
0
        public void TestPOFCloseUsages()
        {
            MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
            ObjectPool <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, 5);
            pool.Close();

            // Test exception handling close should swallow Assert.Failures
            try
            {
                pool = MakeEmptyPool(factory);
            }
            catch (NotSupportedException)
            {
                return; // test not supported
            }
            Reset(pool, factory, expectedMethods);
            factory.DestroyObjectFail = true;
            PoolUtils.PreFill(pool, 5);
            pool.Close();
        }
Beispiel #2
0
        public void TestPOFAddObjectUsage()
        {
            MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
            ObjectPool <Object>             pool;

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

            Assert.AreEqual(0, pool.ActiveCount);
            Assert.AreEqual(0, pool.IdleCount);
            // AddObject should make a new object, pasivate it and put it in the pool
            pool.AddObject();
            Assert.AreEqual(0, pool.ActiveCount);
            Assert.AreEqual(1, pool.IdleCount);
            expectedMethods.Add(new MethodCall("CreateObject").SetReturned(ZERO));
            expectedMethods.Add(new MethodCall("SuspendObject", 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.MakeObjectFail = true;
            try
            {
                pool.AddObject();
                Assert.Fail("Expected AddObject to propagate makeObject exception.");
            }
            catch (MethodAccessException)
            {
                // expected
            }
            expectedMethods.Add(new MethodCall("CreateObject"));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            Clear(factory, expectedMethods);

            // passivateObject Exceptions should be propagated to client code from AddObject
            factory.MakeObjectFail    = false;
            factory.SuspendObjectFail = true;
            try
            {
                pool.AddObject();
                Assert.Fail("Expected AddObject to propagate passivateObject exception.");
            }
            catch (MethodAccessException)
            {
                // expected
            }
            expectedMethods.Add(new MethodCall("CreateObject").SetReturned(ONE));
            expectedMethods.Add(new MethodCall("SuspendObject", ONE));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
        }
Beispiel #3
0
        public void TestPOFReturnObjectUsages()
        {
            MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
            ObjectPool <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();
            Clear(factory, expectedMethods);

            // returned object should be passivated
            pool.ReturnObject(obj);
            // StackObjectPool, SoftReferenceObjectPool also validate on return
            expectedMethods.Add(new MethodCall("SuspendObject", obj));
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

            // Test exception handling of RturnObject
            Reset(pool, factory, expectedMethods);
            pool.AddObject();
            pool.AddObject();
            pool.AddObject();
            Assert.AreEqual(3, pool.IdleCount);
            // passivateObject should swallow exceptions and not Add the object to the pool
            obj = pool.BorrowObject();
            pool.BorrowObject();
            Assert.AreEqual(1, pool.IdleCount);
            Assert.AreEqual(2, pool.ActiveCount);
            Clear(factory, expectedMethods);
            factory.SuspendObjectFail = true;
            pool.ReturnObject(obj);
            expectedMethods.Add(new MethodCall("SuspendObject", obj));
            RemoveDestroyObjectCall(factory.MethodCalls); // The exact timing of destroyObject is flexible here.
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
            Assert.AreEqual(1, pool.IdleCount);           // Not returned
            Assert.AreEqual(1, pool.ActiveCount);         // But not in active count

            // destroyObject should swallow exceptions too
            Reset(pool, factory, expectedMethods);
            obj = pool.BorrowObject();
            Clear(factory, expectedMethods);
            factory.SuspendObjectFail = true;
            factory.DestroyObjectFail = true;
            pool.ReturnObject(obj);
        }
Beispiel #4
0
        public void TestPOFInvalidateObjectUsages()
        {
            MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
            ObjectPool <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();
            Clear(factory, expectedMethods);

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

            // Test exception handling of invalidateObject
            Reset(pool, factory, expectedMethods);
            obj = pool.BorrowObject();
            Clear(factory, expectedMethods);
            factory.DestroyObjectFail = true;
            try
            {
                pool.InvalidateObject(obj);
                Assert.Fail("Expecting destroy exception to propagate");
            }
            catch (MethodAccessException)
            {
                // Expected
            }
            Thread.Sleep(250); // could be defered
            RemoveDestroyObjectCall(factory.MethodCalls);
            Assert.AreEqual(expectedMethods, factory.MethodCalls);
        }
Beispiel #5
0
 private static void Clear(MethodCallPoolableObjectFactory factory, List <MethodCall> expectedMethods)
 {
     factory.MethodCalls.Clear();
     expectedMethods.Clear();
 }
Beispiel #6
0
 private static void Reset(ObjectPool <Object> pool, MethodCallPoolableObjectFactory factory, List <MethodCall> expectedMethods)
 {
     pool.Clear();
     Clear(factory, expectedMethods);
     factory.Reset();
 }
Beispiel #7
0
        public void TestPOFBorrowObjectUsages()
        {
            MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
            ObjectPool <Object>             pool;

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

            // Test correct behavior code paths

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

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

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

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

            factory.ActivateObjectFail = true;
            expectedMethods.Add(new MethodCall("ActivateObject", obj));
            try
            {
                obj = pool.BorrowObject();
                Assert.Fail("Expecting NoSuchElementException");
            }
            catch (NoSuchElementException)
            {
                // Expected - newly created object will also Assert.Fail to activate
            }
            // Idle object Assert.Fails activation, new one created, also Assert.Fails
            expectedMethods.Add(new MethodCall("CreateObject").SetReturned(ONE));
            expectedMethods.Add(new MethodCall("ActivateObject", ONE));
            RemoveDestroyObjectCall(factory.MethodCalls); // The exact timing of destroyObject is flexible here.
            Assert.AreEqual(expectedMethods, factory.MethodCalls);

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

            factory.ValidateObjectFail = true;
            expectedMethods.Add(new MethodCall("ActivateObject", ZERO));
            expectedMethods.Add(new MethodCall("ValidateObject", ZERO));
            try
            {
                obj = pool.BorrowObject();
            }
            catch (NoSuchElementException)
            {
                // Expected - newly created object will also Assert.Fail to validate
            }
            // Idle object is activated, but Assert.Fails validation.
            // New instance is created, activated and then Assert.Fails validation
            expectedMethods.Add(new MethodCall("CreateObject").SetReturned(ONE));
            expectedMethods.Add(new MethodCall("ActivateObject", ONE));
            expectedMethods.Add(new MethodCall("ValidateObject", ONE));
            RemoveDestroyObjectCall(factory.MethodCalls); // The exact timing of destroyObject is flexible here.
            // Second activate and validate are missing from expectedMethods
            Assert.IsTrue(factory.MethodCalls.ContainsAll(expectedMethods));
        }