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

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.33 by jsr166, Mon Oct 11 07:21:32 2010 UTC vs.
Revision 1.43 by jsr166, Fri May 27 19:48:58 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   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 20 | Line 20 | public class ScheduledExecutorTest exten
20          return new TestSuite(ScheduledExecutorTest.class);
21      }
22  
23
23      /**
24       * execute successfully executes a runnable
25       */
# Line 39 | Line 38 | public class ScheduledExecutorTest exten
38          }
39      }
40  
42
41      /**
42       * delayed schedule of callable successfully executes after delay
43       */
# Line 150 | Line 148 | public class ScheduledExecutorTest exten
148          RunnableCounter counter = new RunnableCounter();
149          ScheduledFuture h =
150              p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
151 <        Thread.sleep(SMALL_DELAY_MS);
151 >        delay(SMALL_DELAY_MS);
152          h.cancel(true);
153          int c = counter.count.get();
154          // By time scaling conventions, we must have at least
# Line 168 | Line 166 | public class ScheduledExecutorTest exten
166          RunnableCounter counter = new RunnableCounter();
167          ScheduledFuture h =
168              p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
169 <        Thread.sleep(SMALL_DELAY_MS);
169 >        delay(SMALL_DELAY_MS);
170          h.cancel(true);
171          int c = counter.count.get();
172          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
# Line 176 | Line 174 | public class ScheduledExecutorTest exten
174          joinPool(p);
175      }
176  
179
177      /**
178       * execute(null) throws NPE
179       */
# Line 240 | Line 237 | public class ScheduledExecutorTest exten
237      /**
238       * schedule callable throws RejectedExecutionException if shutdown
239       */
240 <     public void testSchedule3_RejectedExecutionException() throws InterruptedException {
241 <         ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
242 <         try {
240 >    public void testSchedule3_RejectedExecutionException() throws InterruptedException {
241 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
242 >        try {
243              se.shutdown();
244              se.schedule(new NoOpCallable(),
245                          MEDIUM_DELAY_MS, MILLISECONDS);
# Line 250 | Line 247 | public class ScheduledExecutorTest exten
247          } catch (RejectedExecutionException success) {
248          } catch (SecurityException ok) {
249          }
250 <         joinPool(se);
250 >        joinPool(se);
251      }
252  
253      /**
# Line 331 | Line 328 | public class ScheduledExecutorTest exten
328              assertEquals(0, p.getCompletedTaskCount());
329              threadProceed.countDown();
330              threadDone.await();
331 <            Thread.sleep(SHORT_DELAY_MS);
331 >            delay(SHORT_DELAY_MS);
332              assertEquals(1, p.getCompletedTaskCount());
333          } finally {
334              joinPool(p);
# Line 459 | Line 456 | public class ScheduledExecutorTest exten
456      }
457  
458      /**
459 <     * isShutDown is false before shutdown, true after
459 >     * isShutdown is false before shutdown, true after
460       */
461      public void testIsShutdown() {
462  
# Line 473 | Line 470 | public class ScheduledExecutorTest exten
470          assertTrue(p.isShutdown());
471      }
472  
476
473      /**
474       * isTerminated is false before termination, true after
475       */
# Line 485 | Line 481 | public class ScheduledExecutorTest exten
481          try {
482              p.execute(new CheckedRunnable() {
483                  public void realRun() throws InterruptedException {
488                    threadStarted.countDown();
484                      assertFalse(p.isTerminated());
485 +                    threadStarted.countDown();
486                      done.await();
487                  }});
488              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
489 +            assertFalse(p.isTerminating());
490              done.countDown();
491          } finally {
492              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 509 | Line 506 | public class ScheduledExecutorTest exten
506              assertFalse(p.isTerminating());
507              p.execute(new CheckedRunnable() {
508                  public void realRun() throws InterruptedException {
512                    threadStarted.countDown();
509                      assertFalse(p.isTerminating());
510 +                    threadStarted.countDown();
511                      done.await();
512                  }});
513              assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
# Line 586 | Line 583 | public class ScheduledExecutorTest exten
583      }
584  
585      /**
586 <     * purge removes cancelled tasks from the queue
586 >     * purge eventually removes cancelled tasks from the queue
587       */
588      public void testPurge() throws InterruptedException {
589          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
590          ScheduledFuture[] tasks = new ScheduledFuture[5];
591 <        for (int i = 0; i < tasks.length; i++) {
592 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
593 <        }
591 >        for (int i = 0; i < tasks.length; i++)
592 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
593 >                                  LONG_DELAY_MS, MILLISECONDS);
594          try {
595              int max = tasks.length;
596              if (tasks[4].cancel(true)) --max;
597              if (tasks[3].cancel(true)) --max;
598              // There must eventually be an interference-free point at
599              // which purge will not fail. (At worst, when queue is empty.)
600 <            int k;
601 <            for (k = 0; k < SMALL_DELAY_MS; ++k) {
600 >            long startTime = System.nanoTime();
601 >            do {
602                  p.purge();
603                  long count = p.getTaskCount();
604 <                if (count >= 0 && count <= max)
605 <                    break;
606 <                Thread.sleep(1);
607 <            }
611 <            assertTrue(k < SMALL_DELAY_MS);
604 >                if (count == max)
605 >                    return;
606 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
607 >            fail("Purge failed to remove cancelled tasks");
608          } finally {
609              for (ScheduledFuture task : tasks)
610                  task.cancel(true);
# Line 617 | Line 613 | public class ScheduledExecutorTest exten
613      }
614  
615      /**
616 <     * shutDownNow returns a list containing tasks that were not run
616 >     * shutdownNow returns a list containing tasks that were not run
617       */
618 <    public void testShutDownNow() throws InterruptedException {
618 >    public void testShutdownNow() {
619          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
620          for (int i = 0; i < 5; i++)
621 <            p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
622 <        List l;
621 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
622 >                       LONG_DELAY_MS, MILLISECONDS);
623          try {
624 <            l = p.shutdownNow();
624 >            List<Runnable> l = p.shutdownNow();
625 >            assertTrue(p.isShutdown());
626 >            assertEquals(5, l.size());
627          } catch (SecurityException ok) {
628 <            return;
628 >            // Allowed in case test doesn't have privs
629 >        } finally {
630 >            joinPool(p);
631          }
632        assertTrue(p.isShutdown());
633        assertTrue(l.size() > 0 && l.size() <= 5);
634        joinPool(p);
632      }
633  
634      /**
635       * In default setting, shutdown cancels periodic but not delayed
636       * tasks at shutdown
637       */
638 <    public void testShutDown1() throws InterruptedException {
638 >    public void testShutdown1() throws InterruptedException {
639          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
640          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
641          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
# Line 663 | Line 660 | public class ScheduledExecutorTest exten
660          }
661      }
662  
666
663      /**
664       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
665       * delayed tasks are cancelled at shutdown
666       */
667 <    public void testShutDown2() throws InterruptedException {
667 >    public void testShutdown2() throws InterruptedException {
668          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
669          p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
670 +        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
671 +        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
672          ScheduledFuture[] tasks = new ScheduledFuture[5];
673          for (int i = 0; i < tasks.length; i++)
674              tasks[i] = p.schedule(new NoOpRunnable(),
# Line 692 | Line 690 | public class ScheduledExecutorTest exten
690       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
691       * periodic tasks are cancelled at shutdown
692       */
693 <    public void testShutDown3() throws InterruptedException {
693 >    public void testShutdown3() throws InterruptedException {
694          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
695 +        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
696 +        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
697          p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
698 +        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
699 +        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
700 +        long initialDelay = LONG_DELAY_MS;
701          ScheduledFuture task =
702 <            p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
702 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
703 >                                  5, MILLISECONDS);
704          try { p.shutdown(); } catch (SecurityException ok) { return; }
705          assertTrue(p.isShutdown());
702        BlockingQueue q = p.getQueue();
706          assertTrue(p.getQueue().isEmpty());
707          assertTrue(task.isDone());
708          assertTrue(task.isCancelled());
709 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
707 <        assertTrue(p.isTerminated());
709 >        joinPool(p);
710      }
711  
712      /**
713       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
714       * periodic tasks are not cancelled at shutdown
715       */
716 <    public void testShutDown4() throws InterruptedException {
716 >    public void testShutdown4() throws InterruptedException {
717          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
716        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
718          final CountDownLatch counter = new CountDownLatch(2);
719          try {
720 +            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
721 +            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
722 +            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
723              final Runnable r = new CheckedRunnable() {
724                  public void realRun() {
725                      counter.countDown();
# Line 1166 | Line 1170 | public class ScheduledExecutorTest exten
1170          }
1171      }
1172  
1169
1173   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines