--- jsr166/src/test/tck/ScheduledExecutorTest.java 2003/12/22 00:48:56 1.10 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2004/01/22 14:07:50 1.19 @@ -1,13 +1,15 @@ /* - * 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.*; import java.util.*; import java.util.concurrent.*; +import java.util.concurrent.atomic.*; public class ScheduledExecutorTest extends JSR166TestCase { public static void main(String[] args) { @@ -28,14 +30,14 @@ public class ScheduledExecutorTest exten p1.execute(runnable); assertFalse(runnable.done); Thread.sleep(SHORT_DELAY_MS); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } try { Thread.sleep(MEDIUM_DELAY_MS); } catch(InterruptedException e){ unexpectedException(); } assertTrue(runnable.done); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } joinPool(p1); } catch(Exception e){ @@ -57,7 +59,7 @@ public class ScheduledExecutorTest exten Thread.sleep(MEDIUM_DELAY_MS); assertTrue(callable.done); assertEquals(Boolean.TRUE, f.get()); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } joinPool(p1); } catch(RejectedExecutionException e){} catch(Exception e){ @@ -78,7 +80,7 @@ public class ScheduledExecutorTest exten assertFalse(runnable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } joinPool(p1); } catch(Exception e){ unexpectedException(); @@ -97,13 +99,17 @@ public class ScheduledExecutorTest exten Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); h.cancel(true); - p1.shutdown(); joinPool(p1); } catch(Exception e){ unexpectedException(); } } + static class RunnableCounter implements Runnable { + AtomicInteger count = new AtomicInteger(0); + public void run() { count.getAndIncrement(); } + } + /** * scheduleWithFixedDelay executes runnable after given initial delay */ @@ -116,7 +122,6 @@ public class ScheduledExecutorTest exten Thread.sleep(MEDIUM_DELAY_MS); assertTrue(runnable.done); h.cancel(true); - p1.shutdown(); joinPool(p1); } catch(Exception e){ unexpectedException(); @@ -124,6 +129,51 @@ public class ScheduledExecutorTest exten } /** + * 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); + assertTrue(h.isDone()); + joinPool(p1); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * 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); + assertTrue(h.isDone()); + joinPool(p1); + } catch(Exception e){ + unexpectedException(); + } + } + + + /** * execute (null) throws NPE */ public void testExecuteNull() { @@ -167,7 +217,9 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } + joinPool(se); } @@ -183,6 +235,7 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } joinPool(se); } @@ -198,7 +251,8 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ - } + } catch (SecurityException ok) { + } joinPool(se); } @@ -213,6 +267,7 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } joinPool(se); } @@ -228,6 +283,7 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } joinPool(se); } @@ -330,7 +386,6 @@ public class ScheduledExecutorTest exten ThreadFactory tf = new SimpleThreadFactory(); ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } @@ -342,7 +397,6 @@ public class ScheduledExecutorTest exten ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); p.setThreadFactory(tf); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } @@ -370,7 +424,7 @@ public class ScheduledExecutorTest exten assertFalse(p1.isShutdown()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } } assertTrue(p1.isShutdown()); } @@ -384,7 +438,7 @@ public class ScheduledExecutorTest exten try { p1.execute(new SmallRunnable()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } } try { assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); @@ -404,7 +458,7 @@ public class ScheduledExecutorTest exten p1.execute(new SmallRunnable()); assertFalse(p1.isTerminating()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } } try { assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); @@ -429,7 +483,6 @@ public class ScheduledExecutorTest exten BlockingQueue q = p1.getQueue(); assertTrue(q.contains(tasks[4])); assertFalse(q.contains(tasks[0])); - p1.shutdownNow(); } catch(Exception e) { unexpectedException(); } finally { @@ -444,7 +497,7 @@ public class ScheduledExecutorTest exten 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(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); } try { Thread.sleep(SHORT_DELAY_MS); @@ -458,7 +511,6 @@ public class ScheduledExecutorTest exten assertTrue(q.contains((Runnable)tasks[3])); assertTrue(p1.remove((Runnable)tasks[3])); assertFalse(q.contains((Runnable)tasks[3])); - p1.shutdownNow(); } catch(Exception e) { unexpectedException(); } finally { @@ -473,15 +525,28 @@ public class ScheduledExecutorTest exten 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(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + } + try { + int max = 5; + if (tasks[4].cancel(true)) --max; + if (tasks[3].cancel(true)) --max; + // There must eventually be an interference-free point at + // which purge will not fail. (At worst, when queue is empty.) + int k; + for (k = 0; k < SMALL_DELAY_MS; ++k) { + p1.purge(); + long count = p1.getTaskCount(); + if (count >= 0 && count <= max) + break; + Thread.sleep(1); + } + assertTrue(k < SMALL_DELAY_MS); + } catch(Exception e) { + unexpectedException(); + } finally { + joinPool(p1); } - int max = 5; - if (tasks[4].cancel(true)) --max; - if (tasks[3].cancel(true)) --max; - p1.purge(); - long count = p1.getTaskCount(); - assertTrue(count > 0 && count <= max); - joinPool(p1); } /** @@ -491,7 +556,12 @@ public class ScheduledExecutorTest exten 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(); + List l; + try { + l = p1.shutdownNow(); + } catch (SecurityException ok) { + return; + } assertTrue(p1.isShutdown()); assertTrue(l.size() > 0 && l.size() <= 5); joinPool(p1); @@ -510,7 +580,7 @@ public class ScheduledExecutorTest exten 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(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } BlockingQueue q = p1.getQueue(); for (Iterator it = q.iterator(); it.hasNext();) { ScheduledFuture t = (ScheduledFuture)it.next(); @@ -541,7 +611,7 @@ public class ScheduledExecutorTest exten 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(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } assertTrue(p1.isShutdown()); BlockingQueue q = p1.getQueue(); assertTrue(q.isEmpty()); @@ -564,7 +634,7 @@ public class ScheduledExecutorTest exten p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); ScheduledFuture task = p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } assertTrue(p1.isShutdown()); BlockingQueue q = p1.getQueue(); assertTrue(q.isEmpty()); @@ -585,24 +655,24 @@ public class ScheduledExecutorTest exten try { p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); ScheduledFuture task = - p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); + p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); assertFalse(task.isCancelled()); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } assertFalse(task.isCancelled()); assertFalse(p1.isTerminated()); assertTrue(p1.isShutdown()); Thread.sleep(SHORT_DELAY_MS); assertFalse(task.isCancelled()); - task.cancel(true); - assertTrue(task.isCancelled()); + assertTrue(task.cancel(true)); + assertTrue(task.isDone()); Thread.sleep(SHORT_DELAY_MS); assertTrue(p1.isTerminated()); } catch(Exception ex) { unexpectedException(); } - finally { - p1.shutdownNow(); + finally { + joinPool(p1); } } @@ -666,10 +736,6 @@ public class ScheduledExecutorTest exten } } - - - - /** * invokeAny(null) throws NPE */ @@ -839,6 +905,242 @@ public class ScheduledExecutorTest exten } 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); } }