--- jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2005/05/20 16:30:17 1.1 +++ jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2009/11/02 20:28:32 1.2 @@ -2,8 +2,8 @@ * 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. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import java.util.concurrent.*; @@ -14,7 +14,7 @@ import java.util.*; public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { return new TestSuite(ThreadPoolExecutorTest.class); @@ -31,7 +31,7 @@ public class ThreadPoolExecutorSubclassT Exception exception; CustomTask(Callable c) { callable = c; } CustomTask(final Runnable r, final V res) { callable = new Callable() { - public V call() throws Exception { r.run(); return res; }}; + public V call() throws Exception { r.run(); return res; }}; } public boolean isDone() { lock.lock(); try { return done; } finally { lock.unlock() ; } @@ -45,7 +45,7 @@ public class ThreadPoolExecutorSubclassT if (!done) { cancelled = true; done = true; - if (mayInterrupt && thread != null) + if (mayInterrupt && thread != null) thread.interrupt(); return true; } @@ -84,7 +84,7 @@ public class ThreadPoolExecutorSubclassT public V get() throws InterruptedException, ExecutionException { lock.lock(); try { - while (!done) + while (!done) cond.await(); if (exception != null) throw new ExecutionException(exception); @@ -109,23 +109,23 @@ public class ThreadPoolExecutorSubclassT } finally { lock.unlock(); } } - } + } + - static class CustomTPE extends ThreadPoolExecutor { protected RunnableFuture newTaskFor(Callable c) { return new CustomTask(c); } protected RunnableFuture newTaskFor(Runnable r, V v) { return new CustomTask(r, v); - } - + } + CustomTPE(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } CustomTPE(int corePoolSize, @@ -154,7 +154,7 @@ public class ThreadPoolExecutorSubclassT BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); } @@ -173,7 +173,7 @@ public class ThreadPoolExecutorSubclassT protected void terminated() { terminatedCalled = true; } - + } static class FailingThreadFactory implements ThreadFactory{ @@ -181,9 +181,9 @@ public class ThreadPoolExecutorSubclassT public Thread newThread(Runnable r){ if (++calls > 1) return null; return new Thread(r); - } + } } - + /** * execute successfully executes a runnable @@ -203,7 +203,7 @@ public class ThreadPoolExecutorSubclassT Thread.sleep(SMALL_DELAY_MS); } catch(InterruptedException e){ unexpectedException(); - } + } joinPool(p1); } @@ -251,7 +251,7 @@ public class ThreadPoolExecutorSubclassT assertEquals(2, p2.getPoolSize()); joinPool(p2); } - + /** * getCompletedTaskCount increases, but doesn't overestimate, * when tasks complete @@ -269,7 +269,7 @@ public class ThreadPoolExecutorSubclassT try { p2.shutdown(); } catch(SecurityException ok) { return; } joinPool(p2); } - + /** * getCorePoolSize returns size given in constructor if not otherwise set */ @@ -278,7 +278,7 @@ public class ThreadPoolExecutorSubclassT assertEquals(1, p1.getCorePoolSize()); joinPool(p1); } - + /** * getKeepAliveTime returns value given in constructor if not otherwise set */ @@ -289,7 +289,7 @@ public class ThreadPoolExecutorSubclassT } - /** + /** * getThreadFactory returns factory in constructor if not set */ public void testGetThreadFactory() { @@ -299,7 +299,7 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** + /** * setThreadFactory sets the thread factory returned by getThreadFactory */ public void testSetThreadFactory() { @@ -311,7 +311,7 @@ public class ThreadPoolExecutorSubclassT } - /** + /** * setThreadFactory(null) throws NPE */ public void testSetThreadFactoryNull() { @@ -325,7 +325,7 @@ public class ThreadPoolExecutorSubclassT } } - /** + /** * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { @@ -335,7 +335,7 @@ public class ThreadPoolExecutorSubclassT joinPool(p); } - /** + /** * setRejectedExecutionHandler sets the handler returned by * getRejectedExecutionHandler */ @@ -348,7 +348,7 @@ public class ThreadPoolExecutorSubclassT } - /** + /** * setRejectedExecutionHandler(null) throws NPE */ public void testSetRejectedExecutionHandlerNull() { @@ -362,7 +362,7 @@ public class ThreadPoolExecutorSubclassT } } - + /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -377,10 +377,10 @@ public class ThreadPoolExecutorSubclassT assertEquals(2, p2.getLargestPoolSize()); } catch(Exception e){ unexpectedException(); - } + } joinPool(p2); } - + /** * getMaximumPoolSize returns value given in constructor if not * otherwise set @@ -390,7 +390,7 @@ public class ThreadPoolExecutorSubclassT assertEquals(2, p2.getMaximumPoolSize()); joinPool(p2); } - + /** * getPoolSize increases, but doesn't overestimate, when threads * become active @@ -402,7 +402,7 @@ public class ThreadPoolExecutorSubclassT assertEquals(1, p1.getPoolSize()); joinPool(p1); } - + /** * getTaskCount increases, but doesn't overestimate, when tasks submitted */ @@ -415,15 +415,15 @@ public class ThreadPoolExecutorSubclassT assertEquals(1, p1.getTaskCount()); } catch(Exception e){ unexpectedException(); - } + } joinPool(p1); } - + /** * isShutDown is false before shutdown, true after */ public void testIsShutdown() { - + ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); assertFalse(p1.isShutdown()); try { p1.shutdown(); } catch(SecurityException ok) { return; } @@ -431,7 +431,7 @@ public class ThreadPoolExecutorSubclassT joinPool(p1); } - + /** * isTerminated is false before termination, true after */ @@ -448,7 +448,7 @@ public class ThreadPoolExecutorSubclassT assertTrue(p1.isTerminated()); } catch(Exception e){ unexpectedException(); - } + } } /** @@ -469,7 +469,7 @@ public class ThreadPoolExecutorSubclassT assertFalse(p1.isTerminating()); } catch(Exception e){ unexpectedException(); - } + } } /** @@ -560,17 +560,17 @@ public class ThreadPoolExecutorSubclassT try { l = p1.shutdownNow(); } catch (SecurityException ok) { return; } - + } assertTrue(p1.isShutdown()); assertTrue(l.size() <= 4); } // Exception Tests - - /** - * Constructor throws if corePoolSize argument is less than zero + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor1() { try { @@ -579,9 +579,9 @@ public class ThreadPoolExecutorSubclassT } catch (IllegalArgumentException success){} } - - /** - * Constructor throws if maximumPoolSize is less than zero + + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor2() { try { @@ -590,9 +590,9 @@ public class ThreadPoolExecutorSubclassT } catch (IllegalArgumentException success){} } - - /** - * Constructor throws if maximumPoolSize is equal to zero + + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor3() { try { @@ -602,8 +602,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor4() { try { @@ -613,8 +613,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor5() { try { @@ -623,22 +623,22 @@ public class ThreadPoolExecutorSubclassT } catch (IllegalArgumentException success){} } - - /** - * Constructor throws if workQueue is set to null + + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException() { try { new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - - - /** - * Constructor throws if corePoolSize argument is less than zero + + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor6() { try { @@ -646,9 +646,9 @@ public class ThreadPoolExecutorSubclassT shouldThrow(); } catch (IllegalArgumentException success){} } - - /** - * Constructor throws if maximumPoolSize is less than zero + + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor7() { try { @@ -658,8 +658,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor8() { try { @@ -669,8 +669,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor9() { try { @@ -680,8 +680,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor10() { try { @@ -691,19 +691,19 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if workQueue is set to null + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException2() { try { new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - /** - * Constructor throws if threadFactory is set to null + /** + * Constructor throws if threadFactory is set to null */ public void testConstructorNullPointerException3() { try { @@ -711,12 +711,12 @@ public class ThreadPoolExecutorSubclassT new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - - - /** - * Constructor throws if corePoolSize argument is less than zero + + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor11() { try { @@ -726,8 +726,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor12() { try { @@ -737,8 +737,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor13() { try { @@ -748,8 +748,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor14() { try { @@ -759,8 +759,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor15() { try { @@ -770,19 +770,19 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if workQueue is set to null + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException4() { try { new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - /** - * Constructor throws if handler is set to null + /** + * Constructor throws if handler is set to null */ public void testConstructorNullPointerException5() { try { @@ -790,12 +790,12 @@ public class ThreadPoolExecutorSubclassT new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),r); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - - /** - * Constructor throws if corePoolSize argument is less than zero + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor16() { try { @@ -805,8 +805,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor17() { try { @@ -816,8 +816,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor18() { try { @@ -827,8 +827,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor19() { try { @@ -838,8 +838,8 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor20() { try { @@ -849,19 +849,19 @@ public class ThreadPoolExecutorSubclassT catch (IllegalArgumentException success){} } - /** - * Constructor throws if workQueue is set to null + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException6() { try { new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - /** - * Constructor throws if handler is set to null + /** + * Constructor throws if handler is set to null */ public void testConstructorNullPointerException7() { try { @@ -869,11 +869,11 @@ public class ThreadPoolExecutorSubclassT new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),new SimpleThreadFactory(),r); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success){} } - /** - * Constructor throws if ThreadFactory is set top null + /** + * Constructor throws if ThreadFactory is set top null */ public void testConstructorNullPointerException8() { try { @@ -881,9 +881,9 @@ public class ThreadPoolExecutorSubclassT new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f,new NoOpREHandler()); shouldThrow(); } - catch (NullPointerException successdn8){} + catch (NullPointerException successdn8){} } - + /** * execute throws RejectedExecutionException @@ -892,7 +892,7 @@ public class ThreadPoolExecutorSubclassT public void testSaturatedExecute() { ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); try { - + for(int i = 0; i < 5; ++i){ p.execute(new MediumRunnable()); } @@ -908,7 +908,7 @@ public class ThreadPoolExecutorSubclassT RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); ThreadPoolExecutor p = new CustomTPE(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(); @@ -936,7 +936,7 @@ public class ThreadPoolExecutorSubclassT RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); ThreadPoolExecutor p = new CustomTPE(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(); @@ -983,14 +983,14 @@ public class ThreadPoolExecutorSubclassT * execute throws RejectedExecutionException if shutdown */ public void testRejectedExecutionExceptionOnShutdown() { - ThreadPoolExecutor tpe = + ThreadPoolExecutor tpe = new CustomTPE(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); } @@ -1063,10 +1063,10 @@ public class ThreadPoolExecutorSubclassT tpe.execute(null); shouldThrow(); } catch(NullPointerException success){} - + joinPool(tpe); } - + /** * setCorePoolSize of negative value throws IllegalArgumentException */ @@ -1083,12 +1083,12 @@ public class ThreadPoolExecutorSubclassT try { tpe.shutdown(); } catch(SecurityException ok) { return; } } joinPool(tpe); - } + } /** * setMaximumPoolSize(int) throws IllegalArgumentException if * given a value less the core pool size - */ + */ public void testMaximumPoolSizeIllegalArgumentException() { ThreadPoolExecutor tpe = null; try { @@ -1103,7 +1103,7 @@ public class ThreadPoolExecutorSubclassT } joinPool(tpe); } - + /** * setMaximumPoolSize throws IllegalArgumentException * if given a negative value @@ -1122,7 +1122,7 @@ public class ThreadPoolExecutorSubclassT } joinPool(tpe); } - + /** * setKeepAliveTime throws IllegalArgumentException @@ -1133,7 +1133,7 @@ public class ThreadPoolExecutorSubclassT try { tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); } catch(Exception e){} - + try { tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); shouldThrow(); @@ -1381,7 +1381,7 @@ public class ThreadPoolExecutorSubclassT l.add(new NPETask()); List> result = e.invokeAll(l); assertEquals(1, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) it.next().get(); } catch(ExecutionException success) { } catch(Exception ex) { @@ -1402,7 +1402,7 @@ public class ThreadPoolExecutorSubclassT l.add(new StringTask()); List> result = e.invokeAll(l); assertEquals(2, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) assertSame(TEST_STRING, it.next().get()); } catch (ExecutionException success) { } catch(Exception ex) { @@ -1591,7 +1591,7 @@ public class ThreadPoolExecutorSubclassT 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();) + for (Iterator> it = result.iterator(); it.hasNext();) it.next().get(); } catch(ExecutionException success) { } catch(Exception ex) { @@ -1612,7 +1612,7 @@ public class ThreadPoolExecutorSubclassT 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();) + for (Iterator> it = result.iterator(); it.hasNext();) assertSame(TEST_STRING, it.next().get()); } catch (ExecutionException success) { } catch(Exception ex) { @@ -1634,7 +1634,7 @@ public class ThreadPoolExecutorSubclassT l.add(new StringTask()); List> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); assertEquals(3, result.size()); - Iterator> it = result.iterator(); + Iterator> it = result.iterator(); Future f1 = it.next(); Future f2 = it.next(); Future f3 = it.next();