--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2003/08/31 19:24:56 1.1 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2005/05/09 17:15:27 1.22 @@ -1,631 +1,1575 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import java.util.concurrent.*; import junit.framework.*; -import java.util.List; +import java.util.*; -public class ThreadPoolExecutorTest extends TestCase{ +public class ThreadPoolExecutorTest extends JSR166TestCase { public static void main(String[] args) { junit.textui.TestRunner.run (suite()); } - - public static Test suite() { return new TestSuite(ThreadPoolExecutorTest.class); } - private static long SHORT_DELAY_MS = 100; - private static long MEDIUM_DELAY_MS = 1000; - private static long LONG_DELAY_MS = 10000; + static class ExtendedTPE extends ThreadPoolExecutor { + volatile boolean beforeCalled = false; + volatile boolean afterCalled = false; + volatile boolean terminatedCalled = false; + public ExtendedTPE() { + super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue()); + } + protected void beforeExecute(Thread t, Runnable r) { + beforeCalled = true; + } + protected void afterExecute(Runnable r, Throwable t) { + afterCalled = true; + } + protected void terminated() { + terminatedCalled = true; + } + } -//---- testThread class to implement ThreadFactory for use in constructors - static class testThread implements ThreadFactory{ + static class FailingThreadFactory implements ThreadFactory{ + int calls = 0; public Thread newThread(Runnable r){ + if (++calls > 1) return null; return new Thread(r); } } + -//---- testReject class to implement RejectedExecutionHandler for use in the constructors - static class testReject implements RejectedExecutionHandler{ - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} - } - - public Runnable newRunnable(){ - return new Runnable(){ - public void run(){ - try{Thread.sleep(MEDIUM_DELAY_MS); - } catch(Exception e){ - } - } - }; - } - - /** - * Test to verify that execute successfully executes a runnable + * execute successfully executes a runnable */ - public void testExecute(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testExecute() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); try { - one.execute(new Runnable(){ - public void run(){ - try{ + p1.execute(new Runnable() { + public void run() { + try { Thread.sleep(SHORT_DELAY_MS); - }catch(InterruptedException e){ - fail("unexpected exception"); + } catch(InterruptedException e){ + threadUnexpectedException(); } } }); - Thread.sleep(SHORT_DELAY_MS * 2); - }catch(InterruptedException e){ - fail("unexpected exception"); - } finally { - one.shutdown(); - } + Thread.sleep(SMALL_DELAY_MS); + } catch(InterruptedException e){ + unexpectedException(); + } + joinPool(p1); } /** - * Test to verify getActiveCount gives correct values + * getActiveCount increases but doesn't overestimate, when a + * thread becomes active */ - public void testGetActiveCount(){ - ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testGetActiveCount() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(0, p2.getActiveCount()); + p2.execute(new MediumRunnable()); try { - assertEquals(0, two.getActiveCount()); - two.execute(newRunnable()); - try{Thread.sleep(10);}catch(Exception e){} - assertEquals(1, two.getActiveCount()); - } finally { - two.shutdown(); + Thread.sleep(SHORT_DELAY_MS); + } catch(Exception e){ + unexpectedException(); } + assertEquals(1, p2.getActiveCount()); + joinPool(p2); + } + + /** + * prestartCoreThread starts a thread if under corePoolSize, else doesn't + */ + public void testPrestartCoreThread() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(0, p2.getPoolSize()); + assertTrue(p2.prestartCoreThread()); + assertEquals(1, p2.getPoolSize()); + assertTrue(p2.prestartCoreThread()); + assertEquals(2, p2.getPoolSize()); + assertFalse(p2.prestartCoreThread()); + assertEquals(2, p2.getPoolSize()); + joinPool(p2); + } + + /** + * prestartAllCoreThreads starts all corePoolSize threads + */ + public void testPrestartAllCoreThreads() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(0, p2.getPoolSize()); + p2.prestartAllCoreThreads(); + assertEquals(2, p2.getPoolSize()); + p2.prestartAllCoreThreads(); + assertEquals(2, p2.getPoolSize()); + joinPool(p2); } /** - * Test to verify getCompleteTaskCount gives correct values + * getCompletedTaskCount increases, but doesn't overestimate, + * when tasks complete */ - public void testGetCompletedTaskCount(){ - ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testGetCompletedTaskCount() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(0, p2.getCompletedTaskCount()); + p2.execute(new ShortRunnable()); try { - assertEquals(0, two.getCompletedTaskCount()); - two.execute(newRunnable()); - try{Thread.sleep(2000);}catch(Exception e){} - assertEquals(1, two.getCompletedTaskCount()); - } finally { - two.shutdown(); - } + Thread.sleep(SMALL_DELAY_MS); + } catch(Exception e){ + unexpectedException(); + } + assertEquals(1, p2.getCompletedTaskCount()); + try { p2.shutdown(); } catch(SecurityException ok) { return; } + joinPool(p2); } /** - * Test to verify getCorePoolSize gives correct values + * getCorePoolSize returns size given in constructor if not otherwise set */ - public void testGetCorePoolSize(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - try { - assertEquals(1, one.getCorePoolSize()); - } finally { - one.shutdown(); - } + public void testGetCorePoolSize() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(1, p1.getCorePoolSize()); + joinPool(p1); } /** - * Test to verify getKeepAliveTime gives correct values + * getKeepAliveTime returns value given in constructor if not otherwise set + */ + public void testGetKeepAliveTime() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); + joinPool(p2); + } + + + /** + * getThreadFactory returns factory in constructor if not set + */ + public void testGetThreadFactory() { + ThreadFactory tf = new SimpleThreadFactory(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10), tf, new NoOpREHandler()); + assertSame(tf, p.getThreadFactory()); + joinPool(p); + } + + /** + * setThreadFactory sets the thread factory returned by getThreadFactory */ - public void testGetKeepAliveTime(){ - ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testSetThreadFactory() { + ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + ThreadFactory tf = new SimpleThreadFactory(); + p.setThreadFactory(tf); + assertSame(tf, p.getThreadFactory()); + joinPool(p); + } + + + /** + * setThreadFactory(null) throws NPE + */ + public void testSetThreadFactoryNull() { + ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); try { - assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS)); + p.setThreadFactory(null); + shouldThrow(); + } catch (NullPointerException success) { } finally { - two.shutdown(); + joinPool(p); } } - - /** - * Test to verify getLargestPoolSize gives correct values + + /** + * getRejectedExecutionHandler returns handler in constructor if not set + */ + public void testGetRejectedExecutionHandler() { + RejectedExecutionHandler h = new NoOpREHandler(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10), h); + assertSame(h, p.getRejectedExecutionHandler()); + joinPool(p); + } + + /** + * setRejectedExecutionHandler sets the handler returned by + * getRejectedExecutionHandler + */ + public void testSetRejectedExecutionHandler() { + ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + RejectedExecutionHandler h = new NoOpREHandler(); + p.setRejectedExecutionHandler(h); + assertSame(h, p.getRejectedExecutionHandler()); + joinPool(p); + } + + + /** + * setRejectedExecutionHandler(null) throws NPE */ - public void testGetLargestPoolSize(){ - ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testSetRejectedExecutionHandlerNull() { + ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); try { - assertEquals(0, two.getLargestPoolSize()); - two.execute(newRunnable()); - two.execute(newRunnable()); - try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){} - assertEquals(2, two.getLargestPoolSize()); + p.setRejectedExecutionHandler(null); + shouldThrow(); + } catch (NullPointerException success) { } finally { - two.shutdown(); + joinPool(p); } } + /** - * Test to verify getMaximumPoolSize gives correct values + * getLargestPoolSize increases, but doesn't overestimate, when + * multiple threads active */ - public void testGetMaximumPoolSize(){ - ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testGetLargestPoolSize() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); try { - assertEquals(2, two.getMaximumPoolSize()); - } finally { - two.shutdown(); - } + assertEquals(0, p2.getLargestPoolSize()); + p2.execute(new MediumRunnable()); + p2.execute(new MediumRunnable()); + Thread.sleep(SHORT_DELAY_MS); + assertEquals(2, p2.getLargestPoolSize()); + } catch(Exception e){ + unexpectedException(); + } + joinPool(p2); } /** - * Test to verify getPoolSize gives correct values + * getMaximumPoolSize returns value given in constructor if not + * otherwise set */ - public void testGetPoolSize(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - try { - assertEquals(0, one.getPoolSize()); - one.execute(newRunnable()); - assertEquals(1, one.getPoolSize()); - } finally { - one.shutdown(); - } + public void testGetMaximumPoolSize() { + ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(2, p2.getMaximumPoolSize()); + joinPool(p2); } /** - * Test to verify getTaskCount gives correct values + * getPoolSize increases, but doesn't overestimate, when threads + * become active */ - public void testGetTaskCount(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testGetPoolSize() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertEquals(0, p1.getPoolSize()); + p1.execute(new MediumRunnable()); + assertEquals(1, p1.getPoolSize()); + joinPool(p1); + } + + /** + * getTaskCount increases, but doesn't overestimate, when tasks submitted + */ + public void testGetTaskCount() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); try { - assertEquals(0, one.getTaskCount()); - for(int i = 0; i < 5; i++) - one.execute(newRunnable()); - try{Thread.sleep(SHORT_DELAY_MS);}catch(Exception e){} - assertEquals(5, one.getTaskCount()); - } finally { - one.shutdown(); - } + assertEquals(0, p1.getTaskCount()); + p1.execute(new MediumRunnable()); + Thread.sleep(SHORT_DELAY_MS); + assertEquals(1, p1.getTaskCount()); + } catch(Exception e){ + unexpectedException(); + } + joinPool(p1); } /** - * Test to verify isShutDown gives correct values + * isShutDown is false before shutdown, true after */ - public void testIsShutdown(){ + public void testIsShutdown() { - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertFalse(p1.isShutdown()); + try { p1.shutdown(); } catch(SecurityException ok) { return; } + assertTrue(p1.isShutdown()); + joinPool(p1); + } + + + /** + * isTerminated is false before termination, true after + */ + public void testIsTerminated() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertFalse(p1.isTerminated()); try { - assertFalse(one.isShutdown()); + p1.execute(new MediumRunnable()); + } finally { + try { p1.shutdown(); } catch(SecurityException ok) { return; } } - finally { - one.shutdown(); + try { + assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(p1.isTerminated()); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * isTerminating is not true when running or when terminated + */ + public void testIsTerminating() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertFalse(p1.isTerminating()); + try { + p1.execute(new SmallRunnable()); + assertFalse(p1.isTerminating()); + } finally { + try { p1.shutdown(); } catch(SecurityException ok) { return; } } - assertTrue(one.isShutdown()); + try { + assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(p1.isTerminated()); + assertFalse(p1.isTerminating()); + } catch(Exception e){ + unexpectedException(); + } } - /** - * Test to verify isTerminated gives correct values - * Makes sure termination does not take an innapropriate - * amount of time + * getQueue returns the work queue, which contains queued tasks */ - public void testIsTerminated(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testGetQueue() { + BlockingQueue q = new ArrayBlockingQueue(10); + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); + FutureTask[] tasks = new FutureTask[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); + p1.execute(tasks[i]); + } try { - one.execute(newRunnable()); + Thread.sleep(SHORT_DELAY_MS); + BlockingQueue wq = p1.getQueue(); + assertSame(q, wq); + assertFalse(wq.contains(tasks[0])); + assertTrue(wq.contains(tasks[4])); + for (int i = 1; i < 5; ++i) + tasks[i].cancel(true); + p1.shutdownNow(); + } catch(Exception e) { + unexpectedException(); } finally { - one.shutdown(); + joinPool(p1); } - boolean flag = false; - try{ - flag = one.awaitTermination(10, TimeUnit.SECONDS); - }catch(Exception e){} - assertTrue(one.isTerminated()); - if(!flag) - fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); } /** - * Test to verify that purge correctly removes cancelled tasks - * from the queue + * remove(task) removes queued task, and fails to remove active task */ - public void testPurge(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testRemove() { + BlockingQueue q = new ArrayBlockingQueue(10); + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); + FutureTask[] tasks = new FutureTask[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); + p1.execute(tasks[i]); + } try { - CancellableTask[] tasks = new CancellableTask[5]; - for(int i = 0; i < 5; i++){ - tasks[i] = new CancellableTask(newRunnable()); - one.execute(tasks[i]); - } - tasks[4].cancel(true); - tasks[3].cancel(true); - one.purge(); - long count = one.getTaskCount(); - assertTrue(count >= 2 && count < 5); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(p1.remove(tasks[0])); + assertTrue(q.contains(tasks[4])); + assertTrue(q.contains(tasks[3])); + assertTrue(p1.remove(tasks[4])); + assertFalse(p1.remove(tasks[4])); + assertFalse(q.contains(tasks[4])); + assertTrue(q.contains(tasks[3])); + assertTrue(p1.remove(tasks[3])); + assertFalse(q.contains(tasks[3])); + } catch(Exception e) { + unexpectedException(); } finally { - one.shutdown(); + joinPool(p1); } } /** - * Test to verify shutDownNow returns a list - * containing the correct number of elements + * purge removes cancelled tasks from the queue + */ + public void testPurge() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + FutureTask[] tasks = new FutureTask[5]; + for(int i = 0; i < 5; i++){ + tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); + p1.execute(tasks[i]); + } + tasks[4].cancel(true); + tasks[3].cancel(true); + p1.purge(); + long count = p1.getTaskCount(); + assertTrue(count >= 2 && count < 5); + joinPool(p1); + } + + /** + * shutDownNow returns a list containing tasks that were not run */ - public void testShutDownNow(){ - ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(10)); + public void testShutDownNow() { + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); List l; try { for(int i = 0; i < 5; i++) - one.execute(newRunnable()); + p1.execute(new MediumPossiblyInterruptedRunnable()); } finally { - l = one.shutdownNow(); + try { + l = p1.shutdownNow(); + } catch (SecurityException ok) { return; } + } - assertTrue(one.isShutdown()); + assertTrue(p1.isShutdown()); assertTrue(l.size() <= 4); } - - - - - // Exception Tests - //---- Tests if corePoolSize argument is less than zero + /** + * Constructor throws if corePoolSize argument is less than zero + */ public void testConstructor1() { - try{ - new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + shouldThrow(); } - catch (IllegalArgumentException i){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero + */ public void testConstructor2() { - try{ - new ThreadPoolExecutor(1,-1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + shouldThrow(); } - catch (IllegalArgumentException i2){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero + */ public void testConstructor3() { - try{ - new ThreadPoolExecutor(1,0,100L,TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + shouldThrow(); } - catch (IllegalArgumentException i3){} + catch (IllegalArgumentException success){} } - //---- Tests if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero + */ public void testConstructor4() { - try{ + try { new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + shouldThrow(); } - catch (IllegalArgumentException i4){} + catch (IllegalArgumentException success){} } - //---- Tests if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize + */ public void testConstructor5() { - try{ - new ThreadPoolExecutor(2,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue(10)); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + shouldThrow(); } - catch (IllegalArgumentException i5){} + catch (IllegalArgumentException success){} } - //---- Tests if workQueue is set to null - public void testNullPointerException() { - try{ - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + /** + * Constructor throws if workQueue is set to null + */ + public void testConstructorNullPointerException() { + try { + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); + shouldThrow(); } - catch (NullPointerException n){} + catch (NullPointerException success){} } - //---- Tests if corePoolSize argument is less than zero + /** + * Constructor throws if corePoolSize argument is less than zero + */ public void testConstructor6() { - try{ - new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue(10),new testThread()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); - }catch (IllegalArgumentException i6){} + try { + new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + shouldThrow(); + } catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero + */ public void testConstructor7() { - try{ - new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + shouldThrow(); } - catch (IllegalArgumentException i7){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero + */ public void testConstructor8() { - try{ - new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + shouldThrow(); } - catch (IllegalArgumentException i8){} + catch (IllegalArgumentException success){} } - //---- Tests if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero + */ public void testConstructor9() { - try{ - new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + shouldThrow(); } - catch (IllegalArgumentException i9){} + catch (IllegalArgumentException success){} } - //---- Tests if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize + */ public void testConstructor10() { - try{ - new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); + shouldThrow(); } - catch (IllegalArgumentException i10){} + catch (IllegalArgumentException success){} } - //---- Tests if workQueue is set to null - public void testNullPointerException2() { - try{ - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread()); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + /** + * Constructor throws if workQueue is set to null + */ + public void testConstructorNullPointerException2() { + try { + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); + shouldThrow(); } - catch (NullPointerException n2){} + catch (NullPointerException success){} } - //---- Tests if threadFactory is set to null - public void testNullPointerException3() { - try{ + /** + * Constructor throws if threadFactory is set to null + */ + public void testConstructorNullPointerException3() { + try { ThreadFactory f = null; - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f); + shouldThrow(); } - catch (NullPointerException n3){} + catch (NullPointerException success){} } - //---- Tests if corePoolSize argument is less than zero + /** + * Constructor throws if corePoolSize argument is less than zero + */ public void testConstructor11() { - try{ - new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i11){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero + */ public void testConstructor12() { - try{ - new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i12){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero + */ public void testConstructor13() { - try{ - new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i13){} + catch (IllegalArgumentException success){} } - //---- Tests if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero + */ public void testConstructor14() { - try{ - new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i14){} + catch (IllegalArgumentException success){} } - //---- Tests if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize + */ public void testConstructor15() { - try{ - new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i15){} + catch (IllegalArgumentException success){} } - //---- Tests if workQueue is set to null - public void testNullPointerException4() { - try{ - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testReject()); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + /** + * Constructor throws if workQueue is set to null + */ + public void testConstructorNullPointerException4() { + try { + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); + shouldThrow(); } - catch (NullPointerException n4){} + catch (NullPointerException success){} } - //---- Tests if handler is set to null - public void testNullPointerException5() { - try{ + /** + * Constructor throws if handler is set to null + */ + public void testConstructorNullPointerException5() { + try { RejectedExecutionHandler r = null; - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),r); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),r); + shouldThrow(); } - catch (NullPointerException n5){} + catch (NullPointerException success){} } - //---- Tests if corePoolSize argument is less than zero + /** + * Constructor throws if corePoolSize argument is less than zero + */ public void testConstructor16() { - try{ - new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread(),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i16){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero + */ public void testConstructor17() { - try{ - new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread(),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i17){} + catch (IllegalArgumentException success){} } - //---- Tests if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero + */ public void testConstructor18() { - try{ - new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread(),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i18){} + catch (IllegalArgumentException success){} } - //---- Tests if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero + */ public void testConstructor19() { - try{ - new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread(),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i19){} + catch (IllegalArgumentException success){} } - //---- Tests if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize + */ public void testConstructor20() { - try{ - new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new testThread(),new testReject()); - fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); + try { + new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); + shouldThrow(); } - catch (IllegalArgumentException i20){} + catch (IllegalArgumentException success){} } - //---- Tests if workQueue is set to null - public void testNullPointerException6() { - try{ - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread(),new testReject()); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + /** + * Constructor throws if workQueue is set to null + */ + public void testConstructorNullPointerException6() { + try { + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); + shouldThrow(); } - catch (NullPointerException n6){} + catch (NullPointerException success){} } - //---- Tests if handler is set to null - public void testNullPointerException7() { - try{ + /** + * Constructor throws if handler is set to null + */ + public void testConstructorNullPointerException7() { + try { RejectedExecutionHandler r = null; - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),new testThread(),r); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),new SimpleThreadFactory(),r); + shouldThrow(); } - catch (NullPointerException n7){} + catch (NullPointerException success){} } - //---- Tests if ThradFactory is set top null - public void testNullPointerException8() { - try{ + /** + * Constructor throws if ThreadFactory is set top null + */ + public void testConstructorNullPointerException8() { + try { ThreadFactory f = null; - new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f,new testReject()); - fail("ThreadPoolExecutor constructor should throw a NullPointerException"); + new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f,new NoOpREHandler()); + shouldThrow(); } - catch (NullPointerException n8){} + catch (NullPointerException successdn8){} } /** - * Test to verify execute will throw RejectedExcutionException - * ThreadPoolExecutor will throw one when more runnables are - * executed then will fit in the Queue. + * execute throws RejectedExecutionException + * if saturated. */ - public void testRejectedExecutedException(){ - ThreadPoolExecutor tpe = null; - try{ - tpe = new ThreadPoolExecutor(1,1,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(1)); - }catch(Exception e){} - tpe.shutdown(); - try{ - tpe.execute(new Runnable(){ - public void run(){ - try{ - Thread.sleep(1000); - }catch(InterruptedException e){} - } - }); - fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException"); - }catch(RejectedExecutionException sucess){} + public void testSaturatedExecute() { + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); + try { + + for(int i = 0; i < 5; ++i){ + p.execute(new MediumRunnable()); + } + shouldThrow(); + } catch(RejectedExecutionException success){} + joinPool(p); + } + + /** + * executor using CallerRunsPolicy runs task if saturated. + */ + public void testSaturatedExecute2() { + RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); + try { + + TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; + for(int i = 0; i < 5; ++i){ + tasks[i] = new TrackedNoOpRunnable(); + } + TrackedLongRunnable mr = new TrackedLongRunnable(); + p.execute(mr); + for(int i = 0; i < 5; ++i){ + p.execute(tasks[i]); + } + for(int i = 1; i < 5; ++i) { + assertTrue(tasks[i].done); + } + try { p.shutdownNow(); } catch(SecurityException ok) { return; } + } catch(RejectedExecutionException ex){ + unexpectedException(); + } finally { + joinPool(p); + } + } + + /** + * executor using DiscardPolicy drops task if saturated. + */ + public void testSaturatedExecute3() { + RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); + try { + + TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; + for(int i = 0; i < 5; ++i){ + tasks[i] = new TrackedNoOpRunnable(); + } + p.execute(new TrackedLongRunnable()); + for(int i = 0; i < 5; ++i){ + p.execute(tasks[i]); + } + for(int i = 0; i < 5; ++i){ + assertFalse(tasks[i].done); + } + try { p.shutdownNow(); } catch(SecurityException ok) { return; } + } catch(RejectedExecutionException ex){ + unexpectedException(); + } finally { + joinPool(p); + } + } + + /** + * executor using DiscardOldestPolicy drops oldest task if saturated. + */ + public void testSaturatedExecute4() { + RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); + try { + p.execute(new TrackedLongRunnable()); + TrackedLongRunnable r2 = new TrackedLongRunnable(); + p.execute(r2); + assertTrue(p.getQueue().contains(r2)); + TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); + p.execute(r3); + assertFalse(p.getQueue().contains(r2)); + assertTrue(p.getQueue().contains(r3)); + try { p.shutdownNow(); } catch(SecurityException ok) { return; } + } catch(RejectedExecutionException ex){ + unexpectedException(); + } finally { + joinPool(p); + } + } + + /** + * execute throws RejectedExecutionException if shutdown + */ + public void testRejectedExecutionExceptionOnShutdown() { + ThreadPoolExecutor tpe = + new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(1)); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } + try { + tpe.execute(new NoOpRunnable()); + shouldThrow(); + } catch(RejectedExecutionException success){} + joinPool(tpe); + } + + /** + * execute using CallerRunsPolicy drops task on shutdown + */ + public void testCallerRunsOnShutdown() { + RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); + + try { p.shutdown(); } catch(SecurityException ok) { return; } + try { + TrackedNoOpRunnable r = new TrackedNoOpRunnable(); + p.execute(r); + assertFalse(r.done); + } catch(RejectedExecutionException success){ + unexpectedException(); + } finally { + joinPool(p); + } + } + + /** + * execute using DiscardPolicy drops task on shutdown + */ + public void testDiscardOnShutdown() { + RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); + + try { p.shutdown(); } catch(SecurityException ok) { return; } + try { + TrackedNoOpRunnable r = new TrackedNoOpRunnable(); + p.execute(r); + assertFalse(r.done); + } catch(RejectedExecutionException success){ + unexpectedException(); + } finally { + joinPool(p); + } + } + + + /** + * execute using DiscardOldestPolicy drops task on shutdown + */ + public void testDiscardOldestOnShutdown() { + RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); + ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); + + try { p.shutdown(); } catch(SecurityException ok) { return; } + try { + TrackedNoOpRunnable r = new TrackedNoOpRunnable(); + p.execute(r); + assertFalse(r.done); + } catch(RejectedExecutionException success){ + unexpectedException(); + } finally { + joinPool(p); + } + } + + + /** + * execute (null) throws NPE + */ + public void testExecuteNull() { + ThreadPoolExecutor tpe = null; + try { + tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); + tpe.execute(null); + shouldThrow(); + } catch(NullPointerException success){} + joinPool(tpe); } /** - * Test to verify setCorePoolSize will throw IllegalArgumentException - * when given a negative + * setCorePoolSize of negative value throws IllegalArgumentException */ - public void testIllegalArgumentException1(){ + public void testCorePoolSizeIllegalArgumentException() { ThreadPoolExecutor tpe = null; - try{ - tpe = new ThreadPoolExecutor(1,2,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - }catch(Exception e){} - try{ + try { + tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); + } catch(Exception e){} + try { tpe.setCorePoolSize(-1); - fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException"); - }catch(IllegalArgumentException sucess){ + shouldThrow(); + } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } + joinPool(tpe); } - /** - * Test to verify setMaximumPoolSize(int) will throw IllegalArgumentException - * if given a value less the it's actual core pool size + * setMaximumPoolSize(int) throws IllegalArgumentException if + * given a value less the core pool size */ - public void testIllegalArgumentException2(){ + public void testMaximumPoolSizeIllegalArgumentException() { ThreadPoolExecutor tpe = null; - try{ - tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - }catch(Exception e){} - try{ + try { + tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); + } catch(Exception e){} + try { tpe.setMaximumPoolSize(1); - fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); - }catch(IllegalArgumentException sucess){ + shouldThrow(); + } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } + joinPool(tpe); } /** - * Test to verify that setMaximumPoolSize will throw IllegalArgumentException - * if given a negative number + * setMaximumPoolSize throws IllegalArgumentException + * if given a negative value */ - public void testIllegalArgumentException2SP(){ + public void testMaximumPoolSizeIllegalArgumentException2() { ThreadPoolExecutor tpe = null; - try{ - tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - }catch(Exception e){} - try{ + try { + tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); + } catch(Exception e){} + try { tpe.setMaximumPoolSize(-1); - fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); - }catch(IllegalArgumentException sucess){ + shouldThrow(); + } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } + joinPool(tpe); } /** - * Test to verify setKeepAliveTime will throw IllegalArgumentException + * setKeepAliveTime throws IllegalArgumentException * when given a negative value */ - public void testIllegalArgumentException3(){ + public void testKeepAliveTimeIllegalArgumentException() { ThreadPoolExecutor tpe = null; - try{ - tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - }catch(Exception e){} + try { + tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); + } catch(Exception e){} - try{ + try { tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); - fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException"); - }catch(IllegalArgumentException sucess){ + shouldThrow(); + } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } + joinPool(tpe); } - - - + + /** + * terminated() is called on termination + */ + public void testTerminated() { + ExtendedTPE tpe = new ExtendedTPE(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } + assertTrue(tpe.terminatedCalled); + joinPool(tpe); + } + + /** + * beforeExecute and afterExecute are called when executing task + */ + public void testBeforeAfter() { + ExtendedTPE tpe = new ExtendedTPE(); + try { + TrackedNoOpRunnable r = new TrackedNoOpRunnable(); + tpe.execute(r); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(r.done); + assertTrue(tpe.beforeCalled); + assertTrue(tpe.afterCalled); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } + } + catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(tpe); + } + } + + /** + * completed submit of callable returns result + */ + public void testSubmitCallable() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + Future future = e.submit(new StringTask()); + String result = future.get(); + assertSame(TEST_STRING, result); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * completed submit of runnable returns successfully + */ + public void testSubmitRunnable() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + Future future = e.submit(new NoOpRunnable()); + future.get(); + assertTrue(future.isDone()); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * completed submit of (runnable, result) returns result + */ + public void testSubmitRunnable2() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + Future future = e.submit(new NoOpRunnable(), TEST_STRING); + String result = future.get(); + assertSame(TEST_STRING, result); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + + + + + /** + * invokeAny(null) throws NPE + */ + public void testInvokeAny1() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + e.invokeAny(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(empty collection) throws IAE + */ + public void testInvokeAny2() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + e.invokeAny(new ArrayList>()); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws NPE if c has null elements + */ + public void testInvokeAny3() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws ExecutionException if no task completes + */ + public void testInvokeAny4() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) returns result of some task + */ + public void testInvokeAny5() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + String result = e.invokeAny(l); + assertSame(TEST_STRING, result); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(null) throws NPE + */ + public void testInvokeAll1() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + e.invokeAll(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(empty collection) returns empty collection + */ + public void testInvokeAll2() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + List> r = e.invokeAll(new ArrayList>()); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) throws NPE if c has null elements + */ + public void testInvokeAll3() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of element of invokeAll(c) throws exception on failed task + */ + public void testInvokeAll4() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + List> result = e.invokeAll(l); + assertEquals(1, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + it.next().get(); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) returns results of all completed tasks + */ + public void testInvokeAll5() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + List> result = e.invokeAll(l); + assertEquals(2, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + assertSame(TEST_STRING, it.next().get()); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + + + /** + * timed invokeAny(null) throws NPE + */ + public void testTimedInvokeAny1() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(,,null) throws NPE + */ + public void testTimedInvokeAnyNullTimeUnit() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + e.invokeAny(l, MEDIUM_DELAY_MS, null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(empty collection) throws IAE + */ + public void testTimedInvokeAny2() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) throws NPE if c has null elements + */ + public void testTimedInvokeAny3() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + ex.printStackTrace(); + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) throws ExecutionException if no task completes + */ + public void testTimedInvokeAny4() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) returns result of some task + */ + public void testTimedInvokeAny5() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertSame(TEST_STRING, result); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(null) throws NPE + */ + public void testTimedInvokeAll1() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(,,null) throws NPE + */ + public void testTimedInvokeAllNullTimeUnit() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + e.invokeAll(l, MEDIUM_DELAY_MS, null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(empty collection) returns empty collection + */ + public void testTimedInvokeAll2() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) throws NPE if c has null elements + */ + public void testTimedInvokeAll3() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of element of invokeAll(c) throws exception on failed task + */ + public void testTimedInvokeAll4() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertEquals(1, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + it.next().get(); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) returns results of all completed tasks + */ + public void testTimedInvokeAll5() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertEquals(2, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + assertSame(TEST_STRING, it.next().get()); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) cancels tasks not completed by timeout + */ + public void testTimedInvokeAll6() { + ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); + l.add(new StringTask()); + List> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + assertEquals(3, result.size()); + Iterator> it = result.iterator(); + Future f1 = it.next(); + Future f2 = it.next(); + Future f3 = it.next(); + assertTrue(f1.isDone()); + assertTrue(f2.isDone()); + assertTrue(f3.isDone()); + assertFalse(f1.isCancelled()); + assertTrue(f2.isCancelled()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * Execution continues if there is at least one thread even if + * thread factory fails to create more + */ + public void testFailingThreadFactory() { + ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new FailingThreadFactory()); + try { + ArrayList> l = new ArrayList>(); + for (int k = 0; k < 100; ++k) { + e.execute(new NoOpRunnable()); + } + Thread.sleep(LONG_DELAY_MS); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * allowsCoreThreadTimeOut is by default false. + */ + public void testAllowsCoreThreadTimeOut() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertFalse(tpe.allowsCoreThreadTimeOut()); + joinPool(tpe); + } + + /** + * allowCoreThreadTimeOut(true) causes idle threads to time out + */ + public void testAllowCoreThreadTimeOut_true() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + tpe.allowCoreThreadTimeOut(true); + tpe.execute(new NoOpRunnable()); + try { + Thread.sleep(MEDIUM_DELAY_MS); + assertEquals(0, tpe.getPoolSize()); + } catch(InterruptedException e){ + unexpectedException(); + } finally { + joinPool(tpe); + } + } + + /** + * allowCoreThreadTimeOut(false) causes idle threads not to time out + */ + public void testAllowCoreThreadTimeOut_false() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + tpe.allowCoreThreadTimeOut(false); + tpe.execute(new NoOpRunnable()); + try { + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(tpe.getPoolSize() >= 1); + } catch(InterruptedException e){ + unexpectedException(); + } finally { + joinPool(tpe); + } + } + }