--- jsr166/src/test/tck/ScheduledExecutorTest.java 2004/01/07 01:13:50 1.15 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2004/01/22 14:39:25 1.20 @@ -9,6 +9,7 @@ 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) { @@ -29,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){ @@ -58,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){ @@ -79,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(); @@ -98,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 */ @@ -117,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(); @@ -125,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() { @@ -168,7 +217,9 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } + joinPool(se); } @@ -184,6 +235,7 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } joinPool(se); } @@ -199,7 +251,8 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ - } + } catch (SecurityException ok) { + } joinPool(se); } @@ -214,6 +267,7 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } joinPool(se); } @@ -229,6 +283,7 @@ public class ScheduledExecutorTest exten MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch(RejectedExecutionException success){ + } catch (SecurityException ok) { } joinPool(se); } @@ -331,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); } @@ -343,7 +397,6 @@ public class ScheduledExecutorTest exten ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); p.setThreadFactory(tf); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } @@ -371,7 +424,7 @@ public class ScheduledExecutorTest exten assertFalse(p1.isShutdown()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } } assertTrue(p1.isShutdown()); } @@ -385,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)); @@ -405,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)); @@ -430,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 { @@ -459,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 { @@ -474,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); } /** @@ -492,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); @@ -511,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(); @@ -542,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()); @@ -565,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()); @@ -586,15 +655,15 @@ 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.cancel(true)); assertTrue(task.isDone()); Thread.sleep(SHORT_DELAY_MS); assertTrue(p1.isTerminated()); @@ -602,8 +671,8 @@ public class ScheduledExecutorTest exten catch(Exception ex) { unexpectedException(); } - finally { - p1.shutdownNow(); + finally { + joinPool(p1); } } @@ -667,10 +736,6 @@ public class ScheduledExecutorTest exten } } - - - - /** * invokeAny(null) throws NPE */