--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/10/05 23:00:40 1.8 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2003/12/28 21:56:18 1.14 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -24,7 +25,7 @@ public class ScheduledExecutorTest exten public void testExecute() { try { TrackedShortRunnable runnable =new TrackedShortRunnable(); - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); p1.execute(runnable); assertFalse(runnable.done); Thread.sleep(SHORT_DELAY_MS); @@ -51,7 +52,7 @@ public class ScheduledExecutorTest exten public void testSchedule1() { try { TrackedCallable callable = new TrackedCallable(); - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); assertFalse(callable.done); Thread.sleep(MEDIUM_DELAY_MS); @@ -61,6 +62,7 @@ public class ScheduledExecutorTest exten joinPool(p1); } catch(RejectedExecutionException e){} catch(Exception e){ + e.printStackTrace(); unexpectedException(); } } @@ -71,7 +73,7 @@ public class ScheduledExecutorTest exten public void testSchedule3() { try { TrackedShortRunnable runnable = new TrackedShortRunnable(); - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); Thread.sleep(SHORT_DELAY_MS); assertFalse(runnable.done); @@ -90,8 +92,8 @@ public class ScheduledExecutorTest exten public void testSchedule4() { try { TrackedShortRunnable runnable = new TrackedShortRunnable(); - ScheduledExecutor p1 = new ScheduledExecutor(1); - ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + 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); @@ -109,8 +111,8 @@ public class ScheduledExecutorTest exten public void testSchedule5() { try { TrackedShortRunnable runnable = new TrackedShortRunnable(); - ScheduledExecutor p1 = new ScheduledExecutor(1); - ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + 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); @@ -126,9 +128,9 @@ public class ScheduledExecutorTest exten * execute (null) throws NPE */ public void testExecuteNull() { - ScheduledExecutor se = null; + ScheduledThreadPoolExecutor se = null; try { - se = new ScheduledExecutor(1); + se = new ScheduledThreadPoolExecutor(1); se.execute(null); shouldThrow(); } catch(NullPointerException success){} @@ -143,7 +145,7 @@ public class ScheduledExecutorTest exten * schedule (null) throws NPE */ public void testScheduleNull() { - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { TrackedCallable callable = null; Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); @@ -159,7 +161,7 @@ public class ScheduledExecutorTest exten * execute throws RejectedExecutionException if shutdown */ public void testSchedule1_RejectedExecutionException() { - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.schedule(new NoOpRunnable(), @@ -175,7 +177,7 @@ public class ScheduledExecutorTest exten * schedule throws RejectedExecutionException if shutdown */ public void testSchedule2_RejectedExecutionException() { - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.schedule(new NoOpCallable(), @@ -190,7 +192,7 @@ public class ScheduledExecutorTest exten * schedule callable throws RejectedExecutionException if shutdown */ public void testSchedule3_RejectedExecutionException() { - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.schedule(new NoOpCallable(), @@ -205,7 +207,7 @@ public class ScheduledExecutorTest exten * scheduleAtFixedRate throws RejectedExecutionException if shutdown */ public void testScheduleAtFixedRate1_RejectedExecutionException() { - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.scheduleAtFixedRate(new NoOpRunnable(), @@ -220,7 +222,7 @@ public class ScheduledExecutorTest exten * scheduleWithFixedDelay throws RejectedExecutionException if shutdown */ public void testScheduleWithFixedDelay1_RejectedExecutionException() { - ScheduledExecutor se = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); try { se.shutdown(); se.scheduleWithFixedDelay(new NoOpRunnable(), @@ -236,7 +238,7 @@ public class ScheduledExecutorTest exten * thread becomes active */ public void testGetActiveCount() { - ScheduledExecutor p2 = new ScheduledExecutor(2); + ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); assertEquals(0, p2.getActiveCount()); p2.execute(new SmallRunnable()); try { @@ -253,7 +255,7 @@ public class ScheduledExecutorTest exten * when tasks complete */ public void testGetCompletedTaskCount() { - ScheduledExecutor p2 = new ScheduledExecutor(2); + ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); assertEquals(0, p2.getCompletedTaskCount()); p2.execute(new SmallRunnable()); try { @@ -269,7 +271,7 @@ public class ScheduledExecutorTest exten * getCorePoolSize returns size given in constructor if not otherwise set */ public void testGetCorePoolSize() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertEquals(1, p1.getCorePoolSize()); joinPool(p1); } @@ -279,7 +281,7 @@ public class ScheduledExecutorTest exten * multiple threads active */ public void testGetLargestPoolSize() { - ScheduledExecutor p2 = new ScheduledExecutor(2); + ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); assertEquals(0, p2.getLargestPoolSize()); p2.execute(new SmallRunnable()); p2.execute(new SmallRunnable()); @@ -297,7 +299,7 @@ public class ScheduledExecutorTest exten * become active */ public void testGetPoolSize() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertEquals(0, p1.getPoolSize()); p1.execute(new SmallRunnable()); assertEquals(1, p1.getPoolSize()); @@ -309,7 +311,7 @@ public class ScheduledExecutorTest exten * submitted */ public void testGetTaskCount() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertEquals(0, p1.getTaskCount()); for(int i = 0; i < 5; i++) p1.execute(new SmallRunnable()); @@ -327,7 +329,7 @@ public class ScheduledExecutorTest exten */ public void testGetThreadFactory() { ThreadFactory tf = new SimpleThreadFactory(); - ScheduledExecutor p = new ScheduledExecutor(1, tf); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); assertSame(tf, p.getThreadFactory()); p.shutdown(); joinPool(p); @@ -338,7 +340,7 @@ public class ScheduledExecutorTest exten */ public void testSetThreadFactory() { ThreadFactory tf = new SimpleThreadFactory(); - ScheduledExecutor p = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); p.setThreadFactory(tf); assertSame(tf, p.getThreadFactory()); p.shutdown(); @@ -349,7 +351,7 @@ public class ScheduledExecutorTest exten * setThreadFactory(null) throws NPE */ public void testSetThreadFactoryNull() { - ScheduledExecutor p = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try { p.setThreadFactory(null); shouldThrow(); @@ -364,7 +366,7 @@ public class ScheduledExecutorTest exten */ public void testIsShutdown() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); try { assertFalse(p1.isShutdown()); } @@ -379,7 +381,7 @@ public class ScheduledExecutorTest exten * isTerminated is false before termination, true after */ public void testIsTerminated() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); try { p1.execute(new SmallRunnable()); } finally { @@ -397,7 +399,7 @@ public class ScheduledExecutorTest exten * isTerminating is not true when running or when terminated */ public void testIsTerminating() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertFalse(p1.isTerminating()); try { p1.execute(new SmallRunnable()); @@ -418,8 +420,8 @@ public class ScheduledExecutorTest exten * getQueue returns the work queue, which contains queued tasks */ public void testGetQueue() { - ScheduledExecutor p1 = new ScheduledExecutor(1); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + 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); } @@ -440,8 +442,8 @@ public class ScheduledExecutorTest exten * remove(task) removes queued task, and fails to remove active task */ public void testRemove() { - ScheduledExecutor p1 = new ScheduledExecutor(1); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + 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); } @@ -469,8 +471,8 @@ public class ScheduledExecutorTest exten * purge removes cancelled tasks from the queue */ public void testPurge() { - ScheduledExecutor p1 = new ScheduledExecutor(1); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + 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); } @@ -487,7 +489,7 @@ public class ScheduledExecutorTest exten * shutDownNow returns a list containing tasks that were not run */ public void testShutDownNow() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); for(int i = 0; i < 5; i++) p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); List l = p1.shutdownNow(); @@ -502,17 +504,17 @@ public class ScheduledExecutorTest exten */ public void testShutDown1() { try { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + ScheduledFuture[] tasks = new ScheduledFuture[5]; for(int i = 0; i < 5; i++) tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); p1.shutdown(); BlockingQueue q = p1.getQueue(); for (Iterator it = q.iterator(); it.hasNext();) { - ScheduledCancellable t = (ScheduledCancellable)it.next(); + ScheduledFuture t = (ScheduledFuture)it.next(); assertFalse(t.isCancelled()); } assertTrue(p1.isShutdown()); @@ -535,9 +537,9 @@ public class ScheduledExecutorTest exten */ public void testShutDown2() { try { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); - ScheduledCancellable[] tasks = new ScheduledCancellable[5]; + ScheduledFuture[] tasks = new ScheduledFuture[5]; for(int i = 0; i < 5; i++) tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); p1.shutdown(); @@ -559,9 +561,9 @@ public class ScheduledExecutorTest exten */ public void testShutDown3() { try { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); - ScheduledCancellable task = + ScheduledFuture task = p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); p1.shutdown(); assertTrue(p1.isShutdown()); @@ -580,10 +582,10 @@ public class ScheduledExecutorTest exten * periodic tasks are cancelled at shutdown */ public void testShutDown4() { - ScheduledExecutor p1 = new ScheduledExecutor(1); + ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); try { p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); - ScheduledCancellable task = + ScheduledFuture task = p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); assertFalse(task.isCancelled()); p1.shutdown(); @@ -605,4 +607,478 @@ public class ScheduledExecutorTest exten } } + /** + * completed submit of callable returns result + */ + public void testSubmitCallable() { + 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); + } + } + + /** + * completed submit of runnable returns successfully + */ + public void testSubmitRunnable() { + 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); + } + } + + /** + * completed submit of (runnable, result) returns result + */ + public void testSubmitRunnable2() { + 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); + } + } + + + + + + /** + * invokeAny(null) throws NPE + */ + public void testInvokeAny1() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAny(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(empty collection) throws IAE + */ + public void testInvokeAny2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAny(new ArrayList>()); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws NPE if c has null elements + */ + public void testInvokeAny3() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) throws ExecutionException if no task completes + */ + public void testInvokeAny4() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAny(c) returns result of some task + */ + public void testInvokeAny5() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + String result = e.invokeAny(l); + assertSame(TEST_STRING, result); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(null) throws NPE + */ + public void testInvokeAll1() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAll(null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(empty collection) returns empty collection + */ + public void testInvokeAll2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + List> r = e.invokeAll(new ArrayList>()); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) throws NPE if c has null elements + */ + public void testInvokeAll3() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of invokeAll(c) throws exception on failed task + */ + public void testInvokeAll4() { + 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(); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * invokeAll(c) returns results of all completed tasks + */ + public void testInvokeAll5() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(new StringTask()); + List> result = e.invokeAll(l); + assertEquals(2, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + assertSame(TEST_STRING, it.next().get()); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(null) throws NPE + */ + public void testTimedInvokeAny1() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(,,null) throws NPE + */ + public void testTimedInvokeAnyNullTimeUnit() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + e.invokeAny(l, MEDIUM_DELAY_MS, null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(empty collection) throws IAE + */ + public void testTimedInvokeAny2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (IllegalArgumentException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) throws NPE if c has null elements + */ + public void testTimedInvokeAny3() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + ex.printStackTrace(); + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) throws ExecutionException if no task completes + */ + public void testTimedInvokeAny4() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAny(c) returns result of some task + */ + public void testTimedInvokeAny5() { + ExecutorService e = new 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); + assertSame(TEST_STRING, result); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(null) throws NPE + */ + public void testTimedInvokeAll1() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(,,null) throws NPE + */ + public void testTimedInvokeAllNullTimeUnit() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + e.invokeAll(l, MEDIUM_DELAY_MS, null); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(empty collection) returns empty collection + */ + public void testTimedInvokeAll2() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertTrue(r.isEmpty()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) throws NPE if c has null elements + */ + public void testTimedInvokeAll3() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new StringTask()); + l.add(null); + e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + } catch (NullPointerException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * get of element of invokeAll(c) throws exception on failed task + */ + public void testTimedInvokeAll4() { + ExecutorService e = new ScheduledThreadPoolExecutor(2); + try { + ArrayList> l = new ArrayList>(); + l.add(new NPETask()); + List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); + assertEquals(1, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + it.next().get(); + } catch(ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) returns results of all completed tasks + */ + public void testTimedInvokeAll5() { + ExecutorService e = new 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); + assertEquals(2, result.size()); + for (Iterator> it = result.iterator(); it.hasNext();) + assertSame(TEST_STRING, it.next().get()); + } catch (ExecutionException success) { + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * timed invokeAll(c) cancels tasks not completed by timeout + */ + public void testTimedInvokeAll6() { + ExecutorService e = new 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); + assertEquals(3, result.size()); + Iterator> it = result.iterator(); + Future f1 = it.next(); + Future f2 = it.next(); + Future f3 = it.next(); + assertTrue(f1.isDone()); + assertTrue(f2.isDone()); + assertTrue(f3.isDone()); + assertFalse(f1.isCancelled()); + assertTrue(f2.isCancelled()); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + }