List of usage examples for org.apache.commons.pool.impl GenericObjectPool evict
public void evict() throws Exception
Perform numTests
idle object eviction tests, evicting examined objects that meet the criteria for eviction.
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void checkEvict(boolean lifo) throws Exception { // yea this is hairy but it tests all the code paths in GOP.evict() final SimpleFactory factory = new SimpleFactory(); final GenericObjectPool pool = new GenericObjectPool(factory); pool.setSoftMinEvictableIdleTimeMillis(10); pool.setMinIdle(2);//from w w w .j a v a 2 s .c o m pool.setTestWhileIdle(true); pool.setLifo(lifo); PoolUtils.prefill(pool, 5); pool.evict(); factory.setEvenValid(false); factory.setOddValid(false); factory.setThrowExceptionOnActivate(true); pool.evict(); PoolUtils.prefill(pool, 5); factory.setThrowExceptionOnActivate(false); factory.setThrowExceptionOnPassivate(true); pool.evict(); factory.setThrowExceptionOnPassivate(false); factory.setEvenValid(true); factory.setOddValid(true); Thread.sleep(125); pool.evict(); assertEquals(2, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
private void checkEvictionOrder(boolean lifo) throws Exception { SimpleFactory factory = new SimpleFactory(); GenericObjectPool pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2);/*w ww.j a va2s. co m*/ pool.setMinEvictableIdleTimeMillis(100); pool.setLifo(lifo); for (int i = 0; i < 5; i++) { pool.addObject(); Thread.sleep(100); } // Order, oldest to youngest, is "0", "1", ...,"4" pool.evict(); // Should evict "0" and "1" Object obj = pool.borrowObject(); assertTrue("oldest not evicted", !obj.equals("0")); assertTrue("second oldest not evicted", !obj.equals("1")); // 2 should be next out for FIFO, 4 for LIFO assertEquals("Wrong instance returned", lifo ? "4" : "2", obj); // Two eviction runs in sequence factory = new SimpleFactory(); pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2); pool.setMinEvictableIdleTimeMillis(100); pool.setLifo(lifo); for (int i = 0; i < 5; i++) { pool.addObject(); Thread.sleep(100); } pool.evict(); // Should evict "0" and "1" pool.evict(); // Should evict "2" and "3" obj = pool.borrowObject(); assertEquals("Wrong instance remaining in pool", "4", obj); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
private void checkEvictorVisiting(boolean lifo) throws Exception { VisitTrackerFactory factory = new VisitTrackerFactory(); GenericObjectPool pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2);//from w w w . j av a 2s. c om pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); for (int i = 0; i < 8; i++) { pool.addObject(); } pool.evict(); // Visit oldest 2 - 0 and 1 Object obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); // borrow, return, borrow, return // FIFO will move 0 and 1 to end // LIFO, 7 out, then in, then out, then in pool.evict(); // Should visit 2 and 3 in either case for (int i = 0; i < 8; i++) { VisitTracker tracker = (VisitTracker) pool.borrowObject(); if (tracker.getId() >= 4) { assertEquals("Unexpected instance visited " + tracker.getId(), 0, tracker.getValidateCount()); } else { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1, tracker.getValidateCount()); } } factory = new VisitTrackerFactory(); pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(3); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); for (int i = 0; i < 8; i++) { pool.addObject(); } pool.evict(); // 0, 1, 2 pool.evict(); // 3, 4, 5 obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); // borrow, return, borrow, return // FIFO 3,4,5,6,7,0,1,2 // LIFO 7,6,5,4,3,2,1,0 // In either case, pointer should be at 6 pool.evict(); // Should hit 6,7,0 - 0 for second time for (int i = 0; i < 8; i++) { VisitTracker tracker = (VisitTracker) pool.borrowObject(); if (tracker.getId() != 0) { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1, tracker.getValidateCount()); } else { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 2, tracker.getValidateCount()); } } // Randomly generate a pools with random numTests // and make sure evictor cycles through elements appropriately int[] smallPrimes = { 2, 3, 5, 7 }; Random random = new Random(); random.setSeed(System.currentTimeMillis()); for (int i = 0; i < 4; i++) { pool.setNumTestsPerEvictionRun(smallPrimes[i]); for (int j = 0; j < 5; j++) { pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(3); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); pool.setMaxIdle(-1); int instanceCount = 10 + random.nextInt(20); pool.setMaxActive(instanceCount); for (int k = 0; k < instanceCount; k++) { pool.addObject(); } // Execute a random number of evictor runs int runs = 10 + random.nextInt(50); for (int k = 0; k < runs; k++) { pool.evict(); } // Number of times evictor should have cycled through the pool int cycleCount = (runs * pool.getNumTestsPerEvictionRun()) / instanceCount; // Look at elements and make sure they are visited cycleCount // or cycleCount + 1 times VisitTracker tracker = null; int visitCount = 0; for (int k = 0; k < instanceCount; k++) { tracker = (VisitTracker) pool.borrowObject(); assertTrue(pool.getNumActive() <= pool.getMaxActive()); visitCount = tracker.getValidateCount(); assertTrue(visitCount >= cycleCount && visitCount <= cycleCount + 1); } } } }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testEvictionSoftMinIdle() throws Exception { GenericObjectPool pool = null; class TimeTest extends BasePoolableObjectFactory { private final long createTime; public TimeTest() { createTime = System.currentTimeMillis(); }/*from w w w. j av a 2 s .c o m*/ public Object makeObject() throws Exception { return new TimeTest(); } public long getCreateTime() { return createTime; } } pool = new GenericObjectPool(new TimeTest()); pool.setMaxIdle(5); pool.setMaxActive(5); pool.setNumTestsPerEvictionRun(5); pool.setMinEvictableIdleTimeMillis(3000L); pool.setSoftMinEvictableIdleTimeMillis(1000L); pool.setMinIdle(2); Object[] active = new Object[5]; Long[] creationTime = new Long[5]; for (int i = 0; i < 5; i++) { active[i] = pool.borrowObject(); creationTime[i] = new Long(((TimeTest) active[i]).getCreateTime()); } for (int i = 0; i < 5; i++) { pool.returnObject(active[i]); } // Soft evict all but minIdle(2) Thread.sleep(1500L); pool.evict(); assertEquals("Idle count different than expected.", 2, pool.getNumIdle()); // Hard evict the rest. Thread.sleep(2000L); pool.evict(); assertEquals("Idle count different than expected.", 0, pool.getNumIdle()); }