public void TestBadConnection() { int MAX_CONNECTIONS = 3; ObjectPoolConfiguration config = new ObjectPoolConfiguration(); config.MaxObjects = (MAX_CONNECTIONS); config.MaxIdle = (MAX_CONNECTIONS); config.MinIdle = (MAX_CONNECTIONS); config.MinEvictableIdleTimeMillis = (60 * 1000); config.MaxWait = (60 * 1000); MyTestConnectionFactory fact = new MyTestConnectionFactory(); ObjectPool <MyTestConnection> pool = new ObjectPool <MyTestConnection>(fact, config); //borrow first connection and return MyTestConnection conn = pool.BorrowObject(); Assert.AreEqual(1, fact.TotalCreatedConnections); pool.ReturnObject(conn); Assert.AreEqual(1, fact.TotalCreatedConnections); //re-borrow same connection and return conn = pool.BorrowObject(); Assert.AreEqual(1, fact.TotalCreatedConnections); pool.ReturnObject(conn); Assert.AreEqual(1, fact.TotalCreatedConnections); //dispose and make sure we get a new connection conn.Dispose(); conn = pool.BorrowObject(); Assert.AreEqual(2, fact.TotalCreatedConnections); pool.ReturnObject(conn); Assert.AreEqual(2, fact.TotalCreatedConnections); }
public void TestCreateBadConnection() { MyTestConnectionFactory fact = new MyTestConnectionFactory(); fact.CreateBadConnection = (true); ObjectPool <MyTestConnection> pool = new ObjectPool <MyTestConnection>(fact, new ObjectPoolConfiguration()); try { pool.BorrowObject(); Assert.Fail("expected exception"); } catch (ConnectorException e) { Assert.AreEqual("Connection is bad", e.Message); } }
public void TestWithManyThreads() { int NUM_ITERATIONS = 10; int NUM_THREADS = 10; int MAX_CONNECTIONS = NUM_THREADS - 3; //make sure we get some waiting ObjectPoolConfiguration config = new ObjectPoolConfiguration(); config.MaxObjects = (MAX_CONNECTIONS); config.MaxIdle = (MAX_CONNECTIONS); config.MinIdle = (MAX_CONNECTIONS); config.MinEvictableIdleTimeMillis = (60 * 1000); config.MaxWait = (60 * 1000); MyTestConnectionFactory fact = new MyTestConnectionFactory(); ObjectPool <MyTestConnection> pool = new ObjectPool <MyTestConnection>(fact, config); MyTestThread[] threads = new MyTestThread[NUM_THREADS]; for (int i = 0; i < threads.Length; i++) { threads[i] = new MyTestThread(pool, NUM_ITERATIONS); threads[i].Start(); } foreach (MyTestThread thread in threads) { thread.Shutdown(); } //these should be the same since we never //should have disposed anything Assert.AreEqual(MAX_CONNECTIONS, fact.TotalCreatedConnections); ObjectPool <MyTestConnection> .Statistics stats = pool.GetStatistics(); Assert.AreEqual(0, stats.NumActive); Assert.AreEqual(MAX_CONNECTIONS, stats.NumIdle); pool.Shutdown(); stats = pool.GetStatistics(); Assert.AreEqual(0, stats.NumActive); Assert.AreEqual(0, stats.NumIdle); }
public void TestIdleCleanup() { ObjectPoolConfiguration config = new ObjectPoolConfiguration(); config.MaxObjects = (3); config.MaxIdle = (2); config.MinIdle = (1); config.MinEvictableIdleTimeMillis = (3000); config.MaxWait = (60 * 1000); MyTestConnectionFactory fact = new MyTestConnectionFactory(); ObjectPool <MyTestConnection> pool = new ObjectPool <MyTestConnection>(fact, config); MyTestConnection conn1 = (MyTestConnection)pool.BorrowObject(); MyTestConnection conn2 = (MyTestConnection)pool.BorrowObject(); MyTestConnection conn3 = (MyTestConnection)pool.BorrowObject(); Assert.AreEqual(3, fact.TotalCreatedConnections); pool.ReturnObject(conn1); Assert.AreEqual(1, pool.GetStatistics().NumIdle); pool.ReturnObject(conn2); Assert.AreEqual(2, pool.GetStatistics().NumIdle); pool.ReturnObject(conn3); Assert.AreEqual(2, pool.GetStatistics().NumIdle); Assert.AreEqual(false, conn1.IsGood); Assert.AreEqual(true, conn2.IsGood); Assert.AreEqual(true, conn3.IsGood); Thread.Sleep(((int)(config.MinEvictableIdleTimeMillis + 1000))); MyTestConnection conn4 = (MyTestConnection)pool.BorrowObject(); Assert.AreSame(conn3, conn4); Assert.AreEqual(false, conn1.IsGood); Assert.AreEqual(false, conn2.IsGood); Assert.AreEqual(true, conn3.IsGood); Assert.AreEqual(true, conn4.IsGood); }