--- jsr166/src/test/tck/ScheduledExecutorTest.java 2009/11/16 05:30:08 1.24 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2009/11/20 22:58:48 1.25 @@ -9,6 +9,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.concurrent.atomic.*; public class ScheduledExecutorTest extends JSR166TestCase { @@ -23,86 +24,62 @@ public class ScheduledExecutorTest exten /** * execute successfully executes a runnable */ - public void testExecute() { - try { - TrackedShortRunnable runnable =new TrackedShortRunnable(); - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { @@ -113,77 +90,62 @@ public class ScheduledExecutorTest exten /** * scheduleWithFixedDelay executes runnable after given initial delay */ - public void testSchedule5() { - try { - TrackedShortRunnable runnable = new TrackedShortRunnable(); - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 */ - public void testExecuteNull() { + public void testExecuteNull() throws InterruptedException { ScheduledThreadPoolExecutor se = null; try { se = new ScheduledThreadPoolExecutor(1); se.execute(null); shouldThrow(); } catch (NullPointerException success) {} - catch (Exception e) { - unexpectedException(); - } joinPool(se); } @@ -191,28 +153,25 @@ public class ScheduledExecutorTest exten /** * schedule (null) throws NPE */ - public void testScheduleNull() { + public void testScheduleNull() throws InterruptedException { ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); 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); } /** * execute throws RejectedExecutionException if shutdown */ - public void testSchedule1_RejectedExecutionException() { + public void testSchedule1_RejectedExecutionException() throws InterruptedException { ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.schedule(new NoOpRunnable(), - MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { @@ -225,12 +184,12 @@ public class ScheduledExecutorTest exten /** * schedule throws RejectedExecutionException if shutdown */ - public void testSchedule2_RejectedExecutionException() { + public void testSchedule2_RejectedExecutionException() throws InterruptedException { ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { @@ -241,12 +200,12 @@ public class ScheduledExecutorTest exten /** * schedule callable throws RejectedExecutionException if shutdown */ - public void testSchedule3_RejectedExecutionException() { + public void testSchedule3_RejectedExecutionException() throws InterruptedException { ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (RejectedExecutionException success) { } catch (SecurityException ok) { @@ -257,12 +216,12 @@ public class ScheduledExecutorTest exten /** * scheduleAtFixedRate throws RejectedExecutionException if shutdown */ - public void testScheduleAtFixedRate1_RejectedExecutionException() { + public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); 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) { @@ -273,12 +232,12 @@ public class ScheduledExecutorTest exten /** * scheduleWithFixedDelay throws RejectedExecutionException if shutdown */ - public void testScheduleWithFixedDelay1_RejectedExecutionException() { + public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); 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) { @@ -290,15 +249,11 @@ public class ScheduledExecutorTest exten * getActiveCount increases but doesn't overestimate, when a * thread becomes active */ - public void testGetActiveCount() { + public void testGetActiveCount() throws InterruptedException { ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(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); } @@ -307,15 +262,11 @@ public class ScheduledExecutorTest exten * getCompletedTaskCount increases, but doesn't overestimate, * when tasks complete */ - public void testGetCompletedTaskCount() { + public void testGetCompletedTaskCount() throws InterruptedException { ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(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); } @@ -323,7 +274,7 @@ public class ScheduledExecutorTest exten /** * getCorePoolSize returns size given in constructor if not otherwise set */ - public void testGetCorePoolSize() { + public void testGetCorePoolSize() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertEquals(1, p1.getCorePoolSize()); joinPool(p1); @@ -333,16 +284,12 @@ public class ScheduledExecutorTest exten * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active */ - public void testGetLargestPoolSize() { + public void testGetLargestPoolSize() throws InterruptedException { ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(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); } @@ -351,7 +298,7 @@ public class ScheduledExecutorTest exten * getPoolSize increases, but doesn't overestimate, when threads * become active */ - public void testGetPoolSize() { + public void testGetPoolSize() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertEquals(0, p1.getPoolSize()); p1.execute(new SmallRunnable()); @@ -363,16 +310,12 @@ public class ScheduledExecutorTest exten * getTaskCount increases, but doesn't overestimate, when tasks * submitted */ - public void testGetTaskCount() { + public void testGetTaskCount() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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); } @@ -380,7 +323,7 @@ public class ScheduledExecutorTest exten /** * getThreadFactory returns factory in constructor if not set */ - public void testGetThreadFactory() { + public void testGetThreadFactory() throws InterruptedException { ThreadFactory tf = new SimpleThreadFactory(); ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); assertSame(tf, p.getThreadFactory()); @@ -390,7 +333,7 @@ public class ScheduledExecutorTest exten /** * setThreadFactory sets the thread factory returned by getThreadFactory */ - public void testSetThreadFactory() { + public void testSetThreadFactory() throws InterruptedException { ThreadFactory tf = new SimpleThreadFactory(); ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); p.setThreadFactory(tf); @@ -401,7 +344,7 @@ public class ScheduledExecutorTest exten /** * setThreadFactory(null) throws NPE */ - public void testSetThreadFactoryNull() { + public void testSetThreadFactoryNull() throws InterruptedException { ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try { p.setThreadFactory(null); @@ -431,25 +374,21 @@ public class ScheduledExecutorTest exten /** * isTerminated is false before termination, true after */ - public void testIsTerminated() { + public void testIsTerminated() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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() { + public void testIsTerminating() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertFalse(p1.isTerminating()); try { @@ -458,31 +397,26 @@ public class ScheduledExecutorTest exten } 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 { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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); } @@ -491,11 +425,11 @@ public class ScheduledExecutorTest exten /** * remove(task) removes queued task, and fails to remove active task */ - public void testRemove() { + public void testRemove() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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); @@ -509,8 +443,6 @@ public class ScheduledExecutorTest exten 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); } @@ -519,11 +451,11 @@ public class ScheduledExecutorTest exten /** * purge removes cancelled tasks from the queue */ - public void testPurge() { + public void testPurge() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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; @@ -540,8 +472,6 @@ public class ScheduledExecutorTest exten Thread.sleep(1); } assertTrue(k < SMALL_DELAY_MS); - } catch (Exception e) { - unexpectedException(); } finally { joinPool(p1); } @@ -550,10 +480,10 @@ public class ScheduledExecutorTest exten /** * shutDownNow returns a list containing tasks that were not run */ - public void testShutDownNow() { + public void testShutDownNow() throws InterruptedException { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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(); @@ -569,31 +499,25 @@ public class ScheduledExecutorTest exten * In default setting, shutdown cancels periodic but not delayed * tasks at shutdown */ - public void testShutDown1() { - try { - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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()); } } @@ -602,23 +526,18 @@ public class ScheduledExecutorTest exten * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, * delayed tasks are cancelled at shutdown */ - public void testShutDown2() { - try { - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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()); } @@ -626,34 +545,29 @@ public class ScheduledExecutorTest exten * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, * periodic tasks are not cancelled at shutdown */ - public void testShutDown3() { - try { - ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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 { ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(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()); @@ -666,9 +580,6 @@ public class ScheduledExecutorTest exten Thread.sleep(SHORT_DELAY_MS); assertTrue(p1.isTerminated()); } - catch (Exception ex) { - unexpectedException(); - } finally { joinPool(p1); } @@ -677,18 +588,12 @@ public class ScheduledExecutorTest exten /** * completed submit of callable returns result */ - public void testSubmitCallable() { + public void testSubmitCallable() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(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); } @@ -697,18 +602,12 @@ public class ScheduledExecutorTest exten /** * completed submit of runnable returns successfully */ - public void testSubmitRunnable() { + public void testSubmitRunnable() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { Future future = e.submit(new NoOpRunnable()); future.get(); assertTrue(future.isDone()); - } - catch (ExecutionException ex) { - unexpectedException(); - } - catch (InterruptedException ex) { - unexpectedException(); } finally { joinPool(e); } @@ -717,18 +616,12 @@ public class ScheduledExecutorTest exten /** * completed submit of (runnable, result) returns result */ - public void testSubmitRunnable2() { + public void testSubmitRunnable2() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(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); } @@ -737,13 +630,12 @@ public class ScheduledExecutorTest exten /** * invokeAny(null) throws NPE */ - public void testInvokeAny1() { + public void testInvokeAny1() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { e.invokeAny(null); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -752,13 +644,12 @@ public class ScheduledExecutorTest exten /** * invokeAny(empty collection) throws IAE */ - public void testInvokeAny2() { + public void testInvokeAny2() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { e.invokeAny(new ArrayList>()); + shouldThrow(); } catch (IllegalArgumentException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -767,17 +658,24 @@ public class ScheduledExecutorTest exten /** * invokeAny(c) throws NPE if c has null elements */ - public void testInvokeAny3() { + public void testInvokeAny3() throws Exception { + final CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> l = new ArrayList>(); - l.add(new StringTask()); + l.add(new Callable() { + public String call() { + try { + latch.await(); + } catch (InterruptedException ok) {} + return TEST_STRING; + }}); l.add(null); e.invokeAny(l); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { + latch.countDown(); joinPool(e); } } @@ -785,15 +683,15 @@ public class ScheduledExecutorTest exten /** * invokeAny(c) throws ExecutionException if no task completes */ - public void testInvokeAny4() { + public void testInvokeAny4() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); } @@ -802,7 +700,7 @@ public class ScheduledExecutorTest exten /** * invokeAny(c) returns result of some task */ - public void testInvokeAny5() { + public void testInvokeAny5() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> l = new ArrayList>(); @@ -810,9 +708,6 @@ public class ScheduledExecutorTest exten l.add(new StringTask()); String result = e.invokeAny(l); assertSame(TEST_STRING, result); - } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -821,13 +716,12 @@ public class ScheduledExecutorTest exten /** * invokeAll(null) throws NPE */ - public void testInvokeAll1() { + public void testInvokeAll1() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { e.invokeAll(null); + shouldThrow(); } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -836,13 +730,11 @@ public class ScheduledExecutorTest exten /** * invokeAll(empty collection) returns empty collection */ - public void testInvokeAll2() { + public void testInvokeAll2() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); } @@ -851,16 +743,15 @@ public class ScheduledExecutorTest exten /** * invokeAll(c) throws NPE if c has null elements */ - public void testInvokeAll3() { + public void testInvokeAll3() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); } @@ -869,18 +760,18 @@ public class ScheduledExecutorTest exten /** * get of invokeAll(c) throws exception on failed task */ - public void testInvokeAll4() { + public void testInvokeAll4() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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(); + for (Future future : result) + future.get(); + shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -889,7 +780,7 @@ public class ScheduledExecutorTest exten /** * invokeAll(c) returns results of all completed tasks */ - public void testInvokeAll5() { + public void testInvokeAll5() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> l = new ArrayList>(); @@ -897,11 +788,8 @@ public class ScheduledExecutorTest exten 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(); + for (Future future : result) + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } @@ -910,13 +798,12 @@ public class ScheduledExecutorTest exten /** * timed invokeAny(null) throws NPE */ - public void testTimedInvokeAny1() { + public void testTimedInvokeAny1() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(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); } @@ -925,15 +812,14 @@ public class ScheduledExecutorTest exten /** * timed invokeAny(,,null) throws NPE */ - public void testTimedInvokeAnyNullTimeUnit() { + public void testTimedInvokeAnyNullTimeUnit() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); } @@ -942,13 +828,12 @@ public class ScheduledExecutorTest exten /** * timed invokeAny(empty collection) throws IAE */ - public void testTimedInvokeAny2() { + public void testTimedInvokeAny2() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(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); } @@ -957,18 +842,24 @@ public class ScheduledExecutorTest exten /** * timed invokeAny(c) throws NPE if c has null elements */ - public void testTimedInvokeAny3() { + public void testTimedInvokeAny3() throws Exception { + final CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> l = new ArrayList>(); - l.add(new StringTask()); + l.add(new Callable() { + public String call() { + try { + latch.await(); + } catch (InterruptedException ok) {} + return TEST_STRING; + }}); 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); } } @@ -976,15 +867,15 @@ public class ScheduledExecutorTest exten /** * timed invokeAny(c) throws ExecutionException if no task completes */ - public void testTimedInvokeAny4() { + public void testTimedInvokeAny4() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); } @@ -993,17 +884,14 @@ public class ScheduledExecutorTest exten /** * timed invokeAny(c) returns result of some task */ - public void testTimedInvokeAny5() { + public void testTimedInvokeAny5() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> 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); } @@ -1012,13 +900,12 @@ public class ScheduledExecutorTest exten /** * timed invokeAll(null) throws NPE */ - public void testTimedInvokeAll1() { + public void testTimedInvokeAll1() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(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); } @@ -1027,15 +914,14 @@ public class ScheduledExecutorTest exten /** * timed invokeAll(,,null) throws NPE */ - public void testTimedInvokeAllNullTimeUnit() { + public void testTimedInvokeAllNullTimeUnit() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); } @@ -1044,13 +930,11 @@ public class ScheduledExecutorTest exten /** * timed invokeAll(empty collection) returns empty collection */ - public void testTimedInvokeAll2() { + public void testTimedInvokeAll2() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(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); } @@ -1059,16 +943,15 @@ public class ScheduledExecutorTest exten /** * timed invokeAll(c) throws NPE if c has null elements */ - public void testTimedInvokeAll3() { + public void testTimedInvokeAll3() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); } @@ -1077,18 +960,18 @@ public class ScheduledExecutorTest exten /** * get of element of invokeAll(c) throws exception on failed task */ - public void testTimedInvokeAll4() { + public void testTimedInvokeAll4() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> l = new ArrayList>(); l.add(new NPETask()); - List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + List> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); assertEquals(1, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) - it.next().get(); + for (Future future : result) + future.get(); + shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(); + assertTrue(success.getCause() instanceof NullPointerException); } finally { joinPool(e); } @@ -1097,19 +980,16 @@ public class ScheduledExecutorTest exten /** * timed invokeAll(c) returns results of all completed tasks */ - public void testTimedInvokeAll5() { + public void testTimedInvokeAll5() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); try { ArrayList> l = new ArrayList>(); l.add(new StringTask()); l.add(new StringTask()); - List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + List> result = e.invokeAll(l, MEDIUM_DELAY_MS, 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(); + for (Future future : result) + assertSame(TEST_STRING, future.get()); } finally { joinPool(e); } @@ -1118,14 +998,14 @@ public class ScheduledExecutorTest exten /** * timed invokeAll(c) cancels tasks not completed by timeout */ - public void testTimedInvokeAll6() { + public void testTimedInvokeAll6() throws Exception { ExecutorService e = new ScheduledThreadPoolExecutor(2); 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); + List> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); assertEquals(3, result.size()); Iterator> it = result.iterator(); Future f1 = it.next(); @@ -1136,8 +1016,6 @@ public class ScheduledExecutorTest exten assertTrue(f3.isDone()); assertFalse(f1.isCancelled()); assertTrue(f2.isCancelled()); - } catch (Exception ex) { - unexpectedException(); } finally { joinPool(e); }