ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorSubclassTest.java (file contents):
Revision 1.16 by jsr166, Mon Oct 11 15:46:40 2010 UTC vs.
Revision 1.24 by jsr166, Fri May 27 16:26:29 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# Line 203 | Line 203 | public class ScheduledExecutorSubclassTe
203          RunnableCounter counter = new RunnableCounter();
204          ScheduledFuture h =
205              p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
206 <        Thread.sleep(SMALL_DELAY_MS);
206 >        delay(SMALL_DELAY_MS);
207          h.cancel(true);
208          int c = counter.count.get();
209          // By time scaling conventions, we must have at least
# Line 221 | Line 221 | public class ScheduledExecutorSubclassTe
221          RunnableCounter counter = new RunnableCounter();
222          ScheduledFuture h =
223              p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
224 <        Thread.sleep(SMALL_DELAY_MS);
224 >        delay(SMALL_DELAY_MS);
225          h.cancel(true);
226          int c = counter.count.get();
227          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
# Line 291 | Line 291 | public class ScheduledExecutorSubclassTe
291      /**
292       * schedule callable throws RejectedExecutionException if shutdown
293       */
294 <     public void testSchedule3_RejectedExecutionException() {
295 <         CustomExecutor se = new CustomExecutor(1);
296 <         try {
297 <             se.shutdown();
298 <             se.schedule(new NoOpCallable(),
299 <                         MEDIUM_DELAY_MS, MILLISECONDS);
300 <             shouldThrow();
301 <         } catch (RejectedExecutionException success) {
302 <         } catch (SecurityException ok) {
303 <         }
304 <         joinPool(se);
294 >    public void testSchedule3_RejectedExecutionException() {
295 >        CustomExecutor se = new CustomExecutor(1);
296 >        try {
297 >            se.shutdown();
298 >            se.schedule(new NoOpCallable(),
299 >                        MEDIUM_DELAY_MS, MILLISECONDS);
300 >            shouldThrow();
301 >        } catch (RejectedExecutionException success) {
302 >        } catch (SecurityException ok) {
303 >        }
304 >        joinPool(se);
305      }
306  
307      /**
# Line 382 | Line 382 | public class ScheduledExecutorSubclassTe
382              assertEquals(0, p.getCompletedTaskCount());
383              threadProceed.countDown();
384              threadDone.await();
385 <            Thread.sleep(SHORT_DELAY_MS);
385 >            delay(SHORT_DELAY_MS);
386              assertEquals(1, p.getCompletedTaskCount());
387          } finally {
388              joinPool(p);
# Line 510 | Line 510 | public class ScheduledExecutorSubclassTe
510      }
511  
512      /**
513 <     * isShutDown is false before shutdown, true after
513 >     * isShutdown is false before shutdown, true after
514       */
515      public void testIsShutdown() {
516          CustomExecutor p = new CustomExecutor(1);
# Line 535 | Line 535 | public class ScheduledExecutorSubclassTe
535          try {
536              p.execute(new CheckedRunnable() {
537                  public void realRun() throws InterruptedException {
538                    threadStarted.countDown();
538                      assertFalse(p.isTerminated());
539 +                    threadStarted.countDown();
540                      done.await();
541                  }});
542              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
543 +            assertFalse(p.isTerminating());
544              done.countDown();
545          } finally {
546              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 559 | Line 560 | public class ScheduledExecutorSubclassTe
560              assertFalse(p.isTerminating());
561              p.execute(new CheckedRunnable() {
562                  public void realRun() throws InterruptedException {
562                    threadStarted.countDown();
563                      assertFalse(p.isTerminating());
564 +                    threadStarted.countDown();
565                      done.await();
566                  }});
567              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 641 | Line 642 | public class ScheduledExecutorSubclassTe
642      public void testPurge() throws InterruptedException {
643          CustomExecutor p = new CustomExecutor(1);
644          ScheduledFuture[] tasks = new ScheduledFuture[5];
645 <        for (int i = 0; i < tasks.length; i++) {
646 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
647 <        }
645 >        for (int i = 0; i < tasks.length; i++)
646 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
647 >                                  LONG_DELAY_MS, MILLISECONDS);
648          try {
649              int max = tasks.length;
650              if (tasks[4].cancel(true)) --max;
651              if (tasks[3].cancel(true)) --max;
652              // There must eventually be an interference-free point at
653              // which purge will not fail. (At worst, when queue is empty.)
654 <            int k;
655 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
654 >            long startTime = System.nanoTime();
655 >            do {
656                  p.purge();
657                  long count = p.getTaskCount();
658 <                if (count >= 0 && count <= max)
659 <                    break;
660 <                Thread.sleep(1);
661 <            }
661 <            assertTrue(k < SMALL_DELAY_MS);
658 >                if (count == max)
659 >                    return;
660 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
661 >            fail("Purge failed to remove cancelled tasks");
662          } finally {
663              for (ScheduledFuture task : tasks)
664                  task.cancel(true);
# Line 667 | Line 667 | public class ScheduledExecutorSubclassTe
667      }
668  
669      /**
670 <     * shutDownNow returns a list containing tasks that were not run
670 >     * shutdownNow returns a list containing tasks that were not run
671       */
672 <    public void testShutDownNow() {
672 >    public void testShutdownNow() {
673          CustomExecutor p = new CustomExecutor(1);
674          for (int i = 0; i < 5; i++)
675 <            p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
676 <        List l;
675 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
676 >                       LONG_DELAY_MS, MILLISECONDS);
677          try {
678 <            l = p.shutdownNow();
678 >            List<Runnable> l = p.shutdownNow();
679 >            assertTrue(p.isShutdown());
680 >            assertEquals(5, l.size());
681          } catch (SecurityException ok) {
682 <            return;
682 >            // Allowed in case test doesn't have privs
683 >        } finally {
684 >            joinPool(p);
685          }
682        assertTrue(p.isShutdown());
683        assertTrue(l.size() > 0 && l.size() <= 5);
684        joinPool(p);
686      }
687  
688      /**
689       * In default setting, shutdown cancels periodic but not delayed
690       * tasks at shutdown
691       */
692 <    public void testShutDown1() throws InterruptedException {
692 >    public void testShutdown1() throws InterruptedException {
693          CustomExecutor p = new CustomExecutor(1);
694          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
695          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
# Line 718 | Line 719 | public class ScheduledExecutorSubclassTe
719       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
720       * delayed tasks are cancelled at shutdown
721       */
722 <    public void testShutDown2() throws InterruptedException {
722 >    public void testShutdown2() throws InterruptedException {
723          CustomExecutor p = new CustomExecutor(1);
724          p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
725          assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
# Line 745 | Line 746 | public class ScheduledExecutorSubclassTe
746       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
747       * periodic tasks are cancelled at shutdown
748       */
749 <    public void testShutDown3() throws InterruptedException {
749 >    public void testShutdown3() throws InterruptedException {
750          CustomExecutor p = new CustomExecutor(1);
751          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
752          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
753          p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
754          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
755          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
756 +        long initialDelay = LONG_DELAY_MS;
757          ScheduledFuture task =
758 <            p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
758 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
759 >                                  5, MILLISECONDS);
760          try { p.shutdown(); } catch (SecurityException ok) { return; }
761          assertTrue(p.isShutdown());
759        BlockingQueue q = p.getQueue();
762          assertTrue(p.getQueue().isEmpty());
763          assertTrue(task.isDone());
764          assertTrue(task.isCancelled());
765 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
764 <        assertTrue(p.isTerminated());
765 >        joinPool(p);
766      }
767  
768      /**
769       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
770       * periodic tasks are not cancelled at shutdown
771       */
772 <    public void testShutDown4() throws InterruptedException {
772 >    public void testShutdown4() throws InterruptedException {
773          CustomExecutor p = new CustomExecutor(1);
774          final CountDownLatch counter = new CountDownLatch(2);
775          try {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines