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 TestToString() { FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory(); try { MakeEmptyPool(factory).ToString(); } catch (NotSupportedException) { return; // test not supported } }
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); }
private void Clear(FailingKeyedPoolableObjectFactory factory, List <MethodCall> expectedMethods) { factory.MethodCalls.Clear(); expectedMethods.Clear(); }
private void Reset(KeyedObjectPool <Object, Object> pool, FailingKeyedPoolableObjectFactory factory, List <MethodCall> expectedMethods) { pool.Clear(); Clear(factory, expectedMethods); factory.Reset(); }
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); }