/** * Verifies maxSleeping contract: When returnObject triggers maxSleeping exceeded, the bottom * (oldest) instance in the pool is destroyed to make room for the newly returning instance, which * is pushed onto the idle object stack. */ public void testRemoveOldest() throws Exception { pool._maxSleeping = 2; Object obj0 = pool.borrowObject(""); Object obj1 = pool.borrowObject(""); Object obj2 = pool.borrowObject(""); pool.returnObject("", obj0); // Push 0 onto bottom of stack pool.returnObject("", obj1); // Push 1 pool.returnObject("", obj2); // maxSleeping exceeded -> 0 destroyed, 2 pushed assertEquals("2", pool.borrowObject("")); // 2 was pushed on top assertEquals("1", pool.borrowObject("")); // 1 still there assertEquals("3", pool.borrowObject("")); // New instance created (0 is gone) }
public void testToString() throws Exception { StackKeyedObjectPool pool = new StackKeyedObjectPool(new SimpleFactory()); assertNotNull(pool.toString()); Object obj = pool.borrowObject("key"); assertNotNull(pool.toString()); pool.returnObject("key", obj); assertNotNull(pool.toString()); }
public void testIdleCap() throws Exception { Object[] active = new Object[100]; for (int i = 0; i < 100; i++) { active[i] = pool.borrowObject(""); } assertEquals(100, pool.getNumActive("")); assertEquals(0, pool.getNumIdle("")); for (int i = 0; i < 100; i++) { pool.returnObject("", active[i]); assertEquals(99 - i, pool.getNumActive("")); assertEquals((i < 8 ? i + 1 : 8), pool.getNumIdle("")); } }
public void testCloseBug() throws Exception { { Object obj0 = pool.borrowObject(""); Object obj1 = pool.borrowObject(""); assertEquals(2, pool.getNumActive("")); assertEquals(0, pool.getNumIdle("")); pool.returnObject("", obj1); pool.returnObject("", obj0); assertEquals(0, pool.getNumActive("")); assertEquals(2, pool.getNumIdle("")); } { Object obj0 = pool.borrowObject("2"); Object obj1 = pool.borrowObject("2"); assertEquals(2, pool.getNumActive("2")); assertEquals(0, pool.getNumIdle("2")); pool.returnObject("2", obj1); pool.returnObject("2", obj0); assertEquals(0, pool.getNumActive("2")); assertEquals(2, pool.getNumIdle("2")); } pool.close(); }