List of usage examples for org.apache.commons.pool.impl GenericKeyedObjectPool setWhenExhaustedAction
public synchronized void setWhenExhaustedAction(byte whenExhaustedAction)
From source file:Pool146.java
public void testBlockedKeyDoesNotBlockPoolImproved() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory); pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK); pool.setMaxActive(1);//from w w w. j ava 2s .co m pool.setMaxTotal(-1); // Borrow with one key Object obj = pool.borrowObject("one"); // Borrow again with same key, should be blocked Runnable simple = new SimpleTestThread(pool, "one"); Thread borrowThread = new Thread(simple); borrowThread.start(); pool.borrowObject("two"); assert (pool.getNumActive("one") == 1); assert (pool.getNumActive("two") == 1); // Unblock and join the thread pool.returnObject("one", obj); borrowThread.join(); assert (pool.getNumActive("one") == 0); assert (pool.getNumActive("two") == 1); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericKeyedObjectPool.java
public void testBlockedKeyDoesNotBlockPool() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory); pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); pool.setMaxWait(5000);//from w w w . ja v a 2 s . c o m pool.setMaxActive(1); pool.setMaxTotal(-1); pool.borrowObject("one"); long start = System.currentTimeMillis(); // Needs to be in a separate thread as this will block Runnable simple = new SimpleTestThread(pool, "one"); Thread borrowThread = new Thread(simple, "borrowThread"); borrowThread.start(); // This should be almost instant. If it isn't it means this thread got // stuck behind the thread created above which is bad. // Give other thread a chance to start //Thread.sleep(1000); pool.borrowObject("two"); long end = System.currentTimeMillis(); // If it fails it will be more than 4000ms (5000 less the 1000 sleep) // If it passes it should be almost instant // Use 3000ms as the threshold - should avoid timing issues on most // (all? platforms) //assertTrue ((end-start) < 4000); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericKeyedObjectPool.java
@Test // @Schedules({/*from w w w.j a v a 2 s . c o m*/ // @Schedule(name = "BorrowOneBeforeTwo", value = "[beforeBorrow:afterBorrow]@borrowThread -> borrowTwo@main")}) public void testBlockedKeyDoesNotBlockPoolImproved() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory); pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); pool.setMaxActive(1); pool.setMaxTotal(-1); // Borrow with one key Object obj = pool.borrowObject("one"); // Borrow again with same key, should be blocked Runnable simple = new SimpleTestThread(pool, "one"); Thread borrowThread = new Thread(simple, "borrowThread"); borrowThread.start(); // Wait for sometime and borrow with another key // Should not block Thread.sleep(1000); pool.borrowObject("two"); assertEquals(1, pool.getNumActive("one")); assertEquals(1, pool.getNumActive("two")); // Unblock and join the thread pool.returnObject("one", obj); borrowThread.join(); assertEquals(0, pool.getNumActive("one")); assertEquals(1, pool.getNumActive("two")); }
From source file:edu.illinois.imunit.examples.apache.pool.TestGenericKeyedObjectPool.java
@Test @Schedules({//ww w . j a v a2 s. c om @Schedule(name = "BorrowOneBeforeTwo", value = "[beforeBorrow:afterBorrow]@borrowThread -> borrowTwo@main") }) public void testBlockedKeyDoesNotBlockPoolImproved() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory); pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); pool.setMaxActive(1); pool.setMaxTotal(-1); // Borrow with one key Object obj = pool.borrowObject("one"); // Borrow again with same key, should be blocked Runnable simple = new SimpleTestThread(pool, "one"); Thread borrowThread = new Thread(simple, "borrowThread"); borrowThread.start(); // Wait for sometime and borrow with another key // Should not block Thread.sleep(1000); fireEvent("borrowTwo"); pool.borrowObject("two"); assertEquals(1, pool.getNumActive("one")); assertEquals(1, pool.getNumActive("two")); // Unblock and join the thread pool.returnObject("one", obj); borrowThread.join(); assertEquals(0, pool.getNumActive("one")); assertEquals(1, pool.getNumActive("two")); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericKeyedObjectPool.java
public void testSettersAndGetters() throws Exception { GenericKeyedObjectPool pool = new GenericKeyedObjectPool(); {/*from ww w. j a va 2 s. c o m*/ pool.setFactory(new SimpleFactory()); } { pool.setMaxActive(123); assertEquals(123, pool.getMaxActive()); } { pool.setMaxIdle(12); assertEquals(12, pool.getMaxIdle()); } { pool.setMaxWait(1234L); assertEquals(1234L, pool.getMaxWait()); } { pool.setMinEvictableIdleTimeMillis(12345L); assertEquals(12345L, pool.getMinEvictableIdleTimeMillis()); } { pool.setNumTestsPerEvictionRun(11); assertEquals(11, pool.getNumTestsPerEvictionRun()); } { pool.setTestOnBorrow(true); assertTrue(pool.getTestOnBorrow()); pool.setTestOnBorrow(false); assertTrue(!pool.getTestOnBorrow()); } { pool.setTestOnReturn(true); assertTrue(pool.getTestOnReturn()); pool.setTestOnReturn(false); assertTrue(!pool.getTestOnReturn()); } { pool.setTestWhileIdle(true); assertTrue(pool.getTestWhileIdle()); pool.setTestWhileIdle(false); assertTrue(!pool.getTestWhileIdle()); } { pool.setTimeBetweenEvictionRunsMillis(11235L); assertEquals(11235L, pool.getTimeBetweenEvictionRunsMillis()); } { pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK); assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction()); pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL); assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool.getWhenExhaustedAction()); pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW); assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction()); } }