--- jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2009/11/16 05:30:08 1.4 +++ jsr166/src/test/tck/ScheduledExecutorSubclassTest.java 2010/10/09 18:40:41 1.13 @@ -7,14 +7,15 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.concurrent.atomic.*; public class ScheduledExecutorSubclassTest 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(ScheduledExecutorTest.class); + return new TestSuite(ScheduledExecutorSubclassTest.class); } static class CustomTask implements RunnableScheduledFuture { @@ -73,90 +74,65 @@ public class ScheduledExecutorSubclassTe } - /** * execute successfully executes a runnable */ - public void testExecute() { - try { - TrackedShortRunnable runnable =new TrackedShortRunnable(); - CustomExecutor p1 = new CustomExecutor(1); - p1.execute(runnable); - assertFalse(runnable.done); - Thread.sleep(SHORT_DELAY_MS); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - try { - Thread.sleep(MEDIUM_DELAY_MS); - } catch (InterruptedException e) { - unexpectedException(); - } - assertTrue(runnable.done); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - joinPool(p1); - } - catch (Exception e) { - unexpectedException(); - } - + public void testExecute() throws InterruptedException { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + CustomExecutor p1 = new CustomExecutor(1); + p1.execute(runnable); + assertFalse(runnable.done); + Thread.sleep(SHORT_DELAY_MS); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(runnable.done); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + joinPool(p1); } /** * delayed schedule of callable successfully executes after delay */ - public void testSchedule1() { - try { - TrackedCallable callable = new TrackedCallable(); - CustomExecutor p1 = new CustomExecutor(1); - Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - assertFalse(callable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(callable.done); - assertEquals(Boolean.TRUE, f.get()); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - joinPool(p1); - } catch (RejectedExecutionException e) {} - catch (Exception e) { - e.printStackTrace(); - unexpectedException(); - } + public void testSchedule1() throws Exception { + TrackedCallable callable = new TrackedCallable(); + CustomExecutor p1 = new CustomExecutor(1); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); + assertFalse(callable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(callable.done); + assertEquals(Boolean.TRUE, f.get()); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + joinPool(p1); } /** * delayed schedule of runnable successfully executes after delay */ - public void testSchedule3() { - try { - TrackedShortRunnable runnable = new TrackedShortRunnable(); - CustomExecutor p1 = new CustomExecutor(1); - p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); - Thread.sleep(SHORT_DELAY_MS); - assertFalse(runnable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(runnable.done); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - joinPool(p1); - } catch (Exception e) { - unexpectedException(); - } + public void testSchedule3() throws InterruptedException { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + CustomExecutor p1 = new CustomExecutor(1); + p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); + Thread.sleep(SHORT_DELAY_MS); + assertFalse(runnable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(runnable.done); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + joinPool(p1); } /** * scheduleAtFixedRate executes runnable after given initial delay */ - public void testSchedule4() { - try { - TrackedShortRunnable runnable = new TrackedShortRunnable(); - CustomExecutor p1 = new CustomExecutor(1); - ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - assertFalse(runnable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(runnable.done); - h.cancel(true); - joinPool(p1); - } catch (Exception e) { - unexpectedException(); - } + public void testSchedule4() throws InterruptedException { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + CustomExecutor p1 = new CustomExecutor(1); + ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); + assertFalse(runnable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(runnable.done); + h.cancel(true); + joinPool(p1); } static class RunnableCounter implements Runnable { @@ -167,95 +143,75 @@ public class ScheduledExecutorSubclassTe /** * scheduleWithFixedDelay executes runnable after given initial delay */ - public void testSchedule5() { - try { - TrackedShortRunnable runnable = new TrackedShortRunnable(); - CustomExecutor p1 = new CustomExecutor(1); - ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - assertFalse(runnable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(runnable.done); - h.cancel(true); - joinPool(p1); - } catch (Exception e) { - unexpectedException(); - } + public void testSchedule5() throws InterruptedException { + TrackedShortRunnable runnable = new TrackedShortRunnable(); + CustomExecutor p1 = new CustomExecutor(1); + ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); + assertFalse(runnable.done); + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(runnable.done); + h.cancel(true); + joinPool(p1); } /** * scheduleAtFixedRate executes series of tasks at given rate */ - public void testFixedRateSequence() { - try { - CustomExecutor p1 = new CustomExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); - Thread.sleep(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - // By time scaling conventions, we must have at least - // an execution per SHORT delay, but no more than one SHORT more - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p1); - } catch (Exception e) { - unexpectedException(); - } + public void testFixedRateSequence() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); + RunnableCounter counter = new RunnableCounter(); + ScheduledFuture h = + p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); + Thread.sleep(SMALL_DELAY_MS); + h.cancel(true); + int c = counter.count.get(); + // By time scaling conventions, we must have at least + // an execution per SHORT delay, but no more than one SHORT more + assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); + assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); + joinPool(p1); } /** * scheduleWithFixedDelay executes series of tasks with given period */ - public void testFixedDelaySequence() { - try { - CustomExecutor p1 = new CustomExecutor(1); - RunnableCounter counter = new RunnableCounter(); - ScheduledFuture h = - p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); - Thread.sleep(SMALL_DELAY_MS); - h.cancel(true); - int c = counter.count.get(); - assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); - assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); - joinPool(p1); - } catch (Exception e) { - unexpectedException(); - } + public void testFixedDelaySequence() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); + RunnableCounter counter = new RunnableCounter(); + ScheduledFuture h = + p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); + Thread.sleep(SMALL_DELAY_MS); + h.cancel(true); + int c = counter.count.get(); + assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); + assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); + joinPool(p1); } /** - * execute (null) throws NPE + * execute(null) throws NPE */ - public void testExecuteNull() { - CustomExecutor se = null; + public void testExecuteNull() throws InterruptedException { + CustomExecutor se = new CustomExecutor(1); try { - se = new CustomExecutor(1); - se.execute(null); + se.execute(null); shouldThrow(); - } catch (NullPointerException success) {} - catch (Exception e) { - unexpectedException(); - } - - joinPool(se); + } catch (NullPointerException success) {} + joinPool(se); } /** - * schedule (null) throws NPE + * schedule(null) throws NPE */ - public void testScheduleNull() { + public void testScheduleNull() throws InterruptedException { CustomExecutor se = new CustomExecutor(1); - try { + try { TrackedCallable callable = null; - Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (NullPointerException success) {} - catch (Exception e) { - unexpectedException(); - } - joinPool(se); + } catch (NullPointerException success) {} + joinPool(se); } /** @@ -266,14 +222,13 @@ public class ScheduledExecutorSubclassTe try { se.shutdown(); se.schedule(new NoOpRunnable(), - MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { } joinPool(se); - } /** @@ -284,7 +239,7 @@ public class ScheduledExecutorSubclassTe try { se.shutdown(); se.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { @@ -298,13 +253,13 @@ public class ScheduledExecutorSubclassTe public void testSchedule3_RejectedExecutionException() { CustomExecutor se = new CustomExecutor(1); try { - se.shutdown(); - se.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) { - } + se.shutdown(); + se.schedule(new NoOpCallable(), + MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (RejectedExecutionException success) { + } catch (SecurityException ok) { + } joinPool(se); } @@ -316,7 +271,7 @@ public class ScheduledExecutorSubclassTe try { se.shutdown(); se.scheduleAtFixedRate(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { @@ -332,7 +287,7 @@ public class ScheduledExecutorSubclassTe try { se.shutdown(); se.scheduleWithFixedDelay(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { @@ -344,15 +299,11 @@ public class ScheduledExecutorSubclassTe * getActiveCount increases but doesn't overestimate, when a * thread becomes active */ - public void testGetActiveCount() { + public void testGetActiveCount() throws InterruptedException { CustomExecutor p2 = new CustomExecutor(2); assertEquals(0, p2.getActiveCount()); p2.execute(new SmallRunnable()); - try { - Thread.sleep(SHORT_DELAY_MS); - } catch (Exception e) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); assertEquals(1, p2.getActiveCount()); joinPool(p2); } @@ -361,15 +312,11 @@ public class ScheduledExecutorSubclassTe * getCompletedTaskCount increases, but doesn't overestimate, * when tasks complete */ - public void testGetCompletedTaskCount() { + public void testGetCompletedTaskCount() throws InterruptedException { CustomExecutor p2 = new CustomExecutor(2); assertEquals(0, p2.getCompletedTaskCount()); p2.execute(new SmallRunnable()); - try { - Thread.sleep(MEDIUM_DELAY_MS); - } catch (Exception e) { - unexpectedException(); - } + Thread.sleep(MEDIUM_DELAY_MS); assertEquals(1, p2.getCompletedTaskCount()); joinPool(p2); } @@ -387,16 +334,12 @@ public class ScheduledExecutorSubclassTe * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active */ - public void testGetLargestPoolSize() { + public void testGetLargestPoolSize() throws InterruptedException { CustomExecutor p2 = new CustomExecutor(2); assertEquals(0, p2.getLargestPoolSize()); p2.execute(new SmallRunnable()); p2.execute(new SmallRunnable()); - try { - Thread.sleep(SHORT_DELAY_MS); - } catch (Exception e) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); assertEquals(2, p2.getLargestPoolSize()); joinPool(p2); } @@ -417,16 +360,12 @@ public class ScheduledExecutorSubclassTe * getTaskCount increases, but doesn't overestimate, when tasks * submitted */ - public void testGetTaskCount() { + public void testGetTaskCount() throws InterruptedException { CustomExecutor p1 = new CustomExecutor(1); assertEquals(0, p1.getTaskCount()); for (int i = 0; i < 5; i++) p1.execute(new SmallRunnable()); - try { - Thread.sleep(SHORT_DELAY_MS); - } catch (Exception e) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); assertEquals(5, p1.getTaskCount()); joinPool(p1); } @@ -436,7 +375,7 @@ public class ScheduledExecutorSubclassTe */ public void testGetThreadFactory() { ThreadFactory tf = new SimpleThreadFactory(); - CustomExecutor p = new CustomExecutor(1, tf); + CustomExecutor p = new CustomExecutor(1, tf); assertSame(tf, p.getThreadFactory()); joinPool(p); } @@ -446,7 +385,7 @@ public class ScheduledExecutorSubclassTe */ public void testSetThreadFactory() { ThreadFactory tf = new SimpleThreadFactory(); - CustomExecutor p = new CustomExecutor(1); + CustomExecutor p = new CustomExecutor(1); p.setThreadFactory(tf); assertSame(tf, p.getThreadFactory()); joinPool(p); @@ -456,7 +395,7 @@ public class ScheduledExecutorSubclassTe * setThreadFactory(null) throws NPE */ public void testSetThreadFactoryNull() { - CustomExecutor p = new CustomExecutor(1); + CustomExecutor p = new CustomExecutor(1); try { p.setThreadFactory(null); shouldThrow(); @@ -470,41 +409,36 @@ public class ScheduledExecutorSubclassTe * is isShutDown is false before shutdown, true after */ public void testIsShutdown() { - - CustomExecutor p1 = new CustomExecutor(1); + CustomExecutor p1 = new CustomExecutor(1); try { assertFalse(p1.isShutdown()); } finally { try { p1.shutdown(); } catch (SecurityException ok) { return; } } - assertTrue(p1.isShutdown()); + assertTrue(p1.isShutdown()); } /** - * isTerminated is false before termination, true after + * isTerminated is false before termination, true after */ - public void testIsTerminated() { - CustomExecutor p1 = new CustomExecutor(1); + public void testIsTerminated() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); try { p1.execute(new SmallRunnable()); } finally { try { p1.shutdown(); } catch (SecurityException ok) { return; } } - try { - assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - assertTrue(p1.isTerminated()); - } catch (Exception e) { - unexpectedException(); - } + assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p1.isTerminated()); } /** * isTerminating is not true when running or when terminated */ - public void testIsTerminating() { - CustomExecutor p1 = new CustomExecutor(1); + public void testIsTerminating() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); assertFalse(p1.isTerminating()); try { p1.execute(new SmallRunnable()); @@ -512,31 +446,25 @@ public class ScheduledExecutorSubclassTe } finally { try { p1.shutdown(); } catch (SecurityException ok) { return; } } - try { - assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - assertTrue(p1.isTerminated()); - assertFalse(p1.isTerminating()); - } catch (Exception e) { - unexpectedException(); - } + assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p1.isTerminated()); + assertFalse(p1.isTerminating()); } /** * getQueue returns the work queue, which contains queued tasks */ - public void testGetQueue() { + public void testGetQueue() throws InterruptedException { CustomExecutor p1 = new CustomExecutor(1); ScheduledFuture[] tasks = new ScheduledFuture[5]; for (int i = 0; i < 5; i++) { - tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); } try { Thread.sleep(SHORT_DELAY_MS); BlockingQueue q = p1.getQueue(); assertTrue(q.contains(tasks[4])); assertFalse(q.contains(tasks[0])); - } catch (Exception e) { - unexpectedException(); } finally { joinPool(p1); } @@ -545,11 +473,11 @@ public class ScheduledExecutorSubclassTe /** * remove(task) removes queued task, and fails to remove active task */ - public void testRemove() { + public void testRemove() throws InterruptedException { CustomExecutor p1 = new CustomExecutor(1); ScheduledFuture[] tasks = new ScheduledFuture[5]; for (int i = 0; i < 5; i++) { - tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); } try { Thread.sleep(SHORT_DELAY_MS); @@ -563,8 +491,6 @@ public class ScheduledExecutorSubclassTe assertTrue(q.contains((Runnable)tasks[3])); assertTrue(p1.remove((Runnable)tasks[3])); assertFalse(q.contains((Runnable)tasks[3])); - } catch (Exception e) { - unexpectedException(); } finally { joinPool(p1); } @@ -573,11 +499,11 @@ public class ScheduledExecutorSubclassTe /** * purge removes cancelled tasks from the queue */ - public void testPurge() { + public void testPurge() throws InterruptedException { CustomExecutor p1 = new CustomExecutor(1); ScheduledFuture[] tasks = new ScheduledFuture[5]; for (int i = 0; i < 5; i++) { - tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); } try { int max = 5; @@ -594,8 +520,6 @@ public class ScheduledExecutorSubclassTe Thread.sleep(1); } assertTrue(k < SMALL_DELAY_MS); - } catch (Exception e) { - unexpectedException(); } finally { joinPool(p1); } @@ -605,17 +529,17 @@ public class ScheduledExecutorSubclassTe * shutDownNow returns a list containing tasks that were not run */ public void testShutDownNow() { - CustomExecutor p1 = new CustomExecutor(1); + CustomExecutor p1 = new CustomExecutor(1); for (int i = 0; i < 5; i++) - p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); List l; try { l = p1.shutdownNow(); } catch (SecurityException ok) { return; } - assertTrue(p1.isShutdown()); - assertTrue(l.size() > 0 && l.size() <= 5); + assertTrue(p1.isShutdown()); + assertTrue(l.size() > 0 && l.size() <= 5); joinPool(p1); } @@ -623,31 +547,25 @@ public class ScheduledExecutorSubclassTe * In default setting, shutdown cancels periodic but not delayed * tasks at shutdown */ - public void testShutDown1() { - try { - CustomExecutor p1 = new CustomExecutor(1); - assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); - assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); - - ScheduledFuture[] tasks = new ScheduledFuture[5]; - for (int i = 0; i < 5; i++) - tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - BlockingQueue q = p1.getQueue(); - for (Iterator it = q.iterator(); it.hasNext();) { - ScheduledFuture t = (ScheduledFuture)it.next(); - assertFalse(t.isCancelled()); - } - assertTrue(p1.isShutdown()); - Thread.sleep(SMALL_DELAY_MS); - for (int i = 0; i < 5; ++i) { - assertTrue(tasks[i].isDone()); - assertFalse(tasks[i].isCancelled()); - } + public void testShutDown1() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); + assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); + assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); - } - catch (Exception ex) { - unexpectedException(); + ScheduledFuture[] tasks = new ScheduledFuture[5]; + for (int i = 0; i < 5; i++) + tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + BlockingQueue q = p1.getQueue(); + for (Iterator it = q.iterator(); it.hasNext();) { + ScheduledFuture t = (ScheduledFuture)it.next(); + assertFalse(t.isCancelled()); + } + assertTrue(p1.isShutdown()); + Thread.sleep(SMALL_DELAY_MS); + for (int i = 0; i < 5; ++i) { + assertTrue(tasks[i].isDone()); + assertFalse(tasks[i].isCancelled()); } } @@ -656,23 +574,18 @@ public class ScheduledExecutorSubclassTe * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, * delayed tasks are cancelled at shutdown */ - public void testShutDown2() { - try { - CustomExecutor p1 = new CustomExecutor(1); - p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); - ScheduledFuture[] tasks = new ScheduledFuture[5]; - for (int i = 0; i < 5; i++) - tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - assertTrue(p1.isShutdown()); - BlockingQueue q = p1.getQueue(); - assertTrue(q.isEmpty()); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(p1.isTerminated()); - } - catch (Exception ex) { - unexpectedException(); - } + public void testShutDown2() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); + p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + ScheduledFuture[] tasks = new ScheduledFuture[5]; + for (int i = 0; i < 5; i++) + tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + assertTrue(p1.isShutdown()); + BlockingQueue q = p1.getQueue(); + assertTrue(q.isEmpty()); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(p1.isTerminated()); } @@ -680,34 +593,29 @@ public class ScheduledExecutorSubclassTe * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, * periodic tasks are not cancelled at shutdown */ - public void testShutDown3() { - try { - CustomExecutor p1 = new CustomExecutor(1); - p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); - ScheduledFuture task = - p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); - try { p1.shutdown(); } catch (SecurityException ok) { return; } - assertTrue(p1.isShutdown()); - BlockingQueue q = p1.getQueue(); - assertTrue(q.isEmpty()); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(p1.isTerminated()); - } - catch (Exception ex) { - unexpectedException(); - } + public void testShutDown3() throws InterruptedException { + CustomExecutor p1 = new CustomExecutor(1); + p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + ScheduledFuture task = + p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); + try { p1.shutdown(); } catch (SecurityException ok) { return; } + assertTrue(p1.isShutdown()); + BlockingQueue q = p1.getQueue(); + assertTrue(q.isEmpty()); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(p1.isTerminated()); } /** * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, * periodic tasks are cancelled at shutdown */ - public void testShutDown4() { + public void testShutDown4() throws InterruptedException { CustomExecutor p1 = new CustomExecutor(1); try { p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); ScheduledFuture task = - p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); + p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); assertFalse(task.isCancelled()); try { p1.shutdown(); } catch (SecurityException ok) { return; } assertFalse(task.isCancelled()); @@ -720,9 +628,6 @@ public class ScheduledExecutorSubclassTe Thread.sleep(SHORT_DELAY_MS); assertTrue(p1.isTerminated()); } - catch (Exception ex) { - unexpectedException(); - } finally { joinPool(p1); } @@ -731,18 +636,12 @@ public class ScheduledExecutorSubclassTe /** * completed submit of callable returns result */ - public void testSubmitCallable() { + public void testSubmitCallable() throws Exception { ExecutorService e = new CustomExecutor(2); 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); } @@ -751,18 +650,12 @@ public class ScheduledExecutorSubclassTe /** * completed submit of runnable returns successfully */ - public void testSubmitRunnable() { + public void testSubmitRunnable() throws Exception { ExecutorService e = new CustomExecutor(2); try { Future future = e.submit(new NoOpRunnable()); future.get(); assertTrue(future.isDone()); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); } finally { joinPool(e); } @@ -771,18 +664,12 @@ public class ScheduledExecutorSubclassTe /** * completed submit of (runnable, result) returns result */ - public void testSubmitRunnable2() { + public void testSubmitRunnable2() throws Exception { ExecutorService e = new CustomExecutor(2); 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); } @@ -791,13 +678,12 @@ public class ScheduledExecutorSubclassTe /** * invokeAny(null) throws NPE */ - public void testInvokeAny1() { + public void testInvokeAny1() throws Exception { ExecutorService e = new CustomExecutor(2); try { e.invokeAny(null); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -806,13 +692,12 @@ public class ScheduledExecutorSubclassTe /** * invokeAny(empty collection) throws IAE */ - public void testInvokeAny2() { + public void testInvokeAny2() throws Exception { ExecutorService e = new CustomExecutor(2); try { e.invokeAny(new ArrayList>()); + shouldThrow(); } catch (IllegalArgumentException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -821,17 +706,18 @@ public class ScheduledExecutorSubclassTe /** * invokeAny(c) throws NPE if c has null elements */ - public void testInvokeAny3() { + public void testInvokeAny3() throws Exception { + CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); e.invokeAny(l); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { + latch.countDown(); joinPool(e); } } @@ -839,15 +725,15 @@ public class ScheduledExecutorSubclassTe /** * invokeAny(c) throws ExecutionException if no task completes */ - public void testInvokeAny4() { + public void testInvokeAny4() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new NPETask()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); e.invokeAny(l); + shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -856,17 +742,14 @@ public class ScheduledExecutorSubclassTe /** * invokeAny(c) returns result of some task */ - public void testInvokeAny5() { + public void testInvokeAny5() throws Exception { ExecutorService e = new CustomExecutor(2); try { - ArrayList> l = new ArrayList>(); + List> 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); } @@ -875,13 +758,12 @@ public class ScheduledExecutorSubclassTe /** * invokeAll(null) throws NPE */ - public void testInvokeAll1() { + public void testInvokeAll1() throws Exception { ExecutorService e = new CustomExecutor(2); try { e.invokeAll(null); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -890,13 +772,11 @@ public class ScheduledExecutorSubclassTe /** * invokeAll(empty collection) returns empty collection */ - public void testInvokeAll2() { + public void testInvokeAll2() throws Exception { ExecutorService e = new CustomExecutor(2); try { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -905,16 +785,15 @@ public class ScheduledExecutorSubclassTe /** * invokeAll(c) throws NPE if c has null elements */ - public void testInvokeAll3() { + public void testInvokeAll3() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); e.invokeAll(l); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -923,18 +802,17 @@ public class ScheduledExecutorSubclassTe /** * get of invokeAll(c) throws exception on failed task */ - public void testInvokeAll4() { + public void testInvokeAll4() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new NPETask()); + List> futures = e.invokeAll(l); + assertEquals(1, futures.size()); 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(); + futures.get(0).get(); + shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -943,19 +821,16 @@ public class ScheduledExecutorSubclassTe /** * invokeAll(c) returns results of all completed tasks */ - public void testInvokeAll5() { + public void testInvokeAll5() throws Exception { ExecutorService e = new CustomExecutor(2); try { - ArrayList> l = new ArrayList>(); + List> 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(); + List> futures = e.invokeAll(l); + assertEquals(2, futures.size()); + for (Future future : futures) + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } @@ -964,13 +839,12 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAny(null) throws NPE */ - public void testTimedInvokeAny1() { + public void testTimedInvokeAny1() throws Exception { ExecutorService e = new CustomExecutor(2); try { - e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -979,15 +853,14 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAny(,,null) throws NPE */ - public void testTimedInvokeAnyNullTimeUnit() { + public void testTimedInvokeAnyNullTimeUnit() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new StringTask()); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); e.invokeAny(l, MEDIUM_DELAY_MS, null); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -996,13 +869,12 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAny(empty collection) throws IAE */ - public void testTimedInvokeAny2() { + public void testTimedInvokeAny2() throws Exception { ExecutorService e = new CustomExecutor(2); try { - e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (IllegalArgumentException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -1011,18 +883,18 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAny(c) throws NPE if c has null elements */ - public void testTimedInvokeAny3() { + public void testTimedInvokeAny3() throws Exception { + CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); - e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - ex.printStackTrace(); - unexpectedException(); } finally { + latch.countDown(); joinPool(e); } } @@ -1030,15 +902,15 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAny(c) throws ExecutionException if no task completes */ - public void testTimedInvokeAny4() { + public void testTimedInvokeAny4() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new NPETask()); try { - ArrayList> l = new ArrayList>(); - l.add(new NPETask()); - e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -1047,17 +919,14 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAny(c) returns result of some task */ - public void testTimedInvokeAny5() { + public void testTimedInvokeAny5() throws Exception { ExecutorService e = new CustomExecutor(2); try { - ArrayList> l = new ArrayList>(); + List> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); - String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -1066,13 +935,12 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAll(null) throws NPE */ - public void testTimedInvokeAll1() { + public void testTimedInvokeAll1() throws Exception { ExecutorService e = new CustomExecutor(2); try { - e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -1081,15 +949,14 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAll(,,null) throws NPE */ - public void testTimedInvokeAllNullTimeUnit() { + public void testTimedInvokeAllNullTimeUnit() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new StringTask()); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); e.invokeAll(l, MEDIUM_DELAY_MS, null); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -1098,13 +965,11 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAll(empty collection) returns empty collection */ - public void testTimedInvokeAll2() { + public void testTimedInvokeAll2() throws Exception { ExecutorService e = new CustomExecutor(2); try { - List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); assertTrue(r.isEmpty()); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -1113,16 +978,15 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAll(c) throws NPE if c has null elements */ - public void testTimedInvokeAll3() { + public void testTimedInvokeAll3() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); try { - ArrayList> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); - e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -1131,18 +995,18 @@ public class ScheduledExecutorSubclassTe /** * get of element of invokeAll(c) throws exception on failed task */ - public void testTimedInvokeAll4() { + public void testTimedInvokeAll4() throws Exception { ExecutorService e = new CustomExecutor(2); + List> l = new ArrayList>(); + l.add(new NPETask()); + List> futures = + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + assertEquals(1, futures.size()); 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(); + futures.get(0).get(); + shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -1151,19 +1015,17 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAll(c) returns results of all completed tasks */ - public void testTimedInvokeAll5() { + public void testTimedInvokeAll5() throws Exception { ExecutorService e = new CustomExecutor(2); try { - ArrayList> l = new ArrayList>(); + List> 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(); + List> futures = + e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + assertEquals(2, futures.size()); + for (Future future : futures) + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } @@ -1172,16 +1034,17 @@ public class ScheduledExecutorSubclassTe /** * timed invokeAll(c) cancels tasks not completed by timeout */ - public void testTimedInvokeAll6() { + public void testTimedInvokeAll6() throws Exception { ExecutorService e = new CustomExecutor(2); try { - ArrayList> l = new ArrayList>(); + List> 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(); + List> futures = + e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); + assertEquals(3, futures.size()); + Iterator> it = futures.iterator(); Future f1 = it.next(); Future f2 = it.next(); Future f3 = it.next(); @@ -1190,12 +1053,9 @@ public class ScheduledExecutorSubclassTe assertTrue(f3.isDone()); assertFalse(f1.isCancelled()); assertTrue(f2.isCancelled()); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } } - }