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.40 by jsr166, Sat May 7 19:34:51 2011 UTC vs.
Revision 1.48 by jsr166, Wed Sep 25 06:59:34 2013 UTC

# Line 10 | Line 10 | import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 < import java.util.concurrent.atomic.*;
13 > import java.util.concurrent.atomic.AtomicInteger;
14  
15   public class ScheduledExecutorTest extends JSR166TestCase {
16      public static void main(String[] args) {
# 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       */
44      public void testSchedule1() throws Exception {
45          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
46 <        final long t0 = System.nanoTime();
49 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
46 >        final long startTime = System.nanoTime();
47          final CountDownLatch done = new CountDownLatch(1);
48          try {
49              Callable task = new CheckedCallable<Boolean>() {
50                  public Boolean realCall() {
51                      done.countDown();
52 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
52 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
53                      return Boolean.TRUE;
54                  }};
55 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
56 <            assertEquals(Boolean.TRUE, f.get());
57 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
55 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
56 >            assertSame(Boolean.TRUE, f.get());
57 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
58              assertTrue(done.await(0L, MILLISECONDS));
59          } finally {
60              joinPool(p);
# Line 69 | Line 66 | public class ScheduledExecutorTest exten
66       */
67      public void testSchedule3() throws Exception {
68          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
69 <        final long t0 = System.nanoTime();
73 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
69 >        final long startTime = System.nanoTime();
70          final CountDownLatch done = new CountDownLatch(1);
71          try {
72              Runnable task = new CheckedRunnable() {
73                  public void realRun() {
74                      done.countDown();
75 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
75 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
76                  }};
77 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
78 <            assertNull(f.get());
79 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
80 <            assertTrue(done.await(0L, MILLISECONDS));
77 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
78 >            await(done);
79 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
80 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
81          } finally {
82              joinPool(p);
83          }
# Line 92 | Line 88 | public class ScheduledExecutorTest exten
88       */
89      public void testSchedule4() throws Exception {
90          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
91 <        final long t0 = System.nanoTime();
96 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
91 >        final long startTime = System.nanoTime();
92          final CountDownLatch done = new CountDownLatch(1);
93          try {
94              Runnable task = new CheckedRunnable() {
95                  public void realRun() {
96                      done.countDown();
97 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
97 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
98                  }};
99              ScheduledFuture f =
100 <                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
101 <                                      SHORT_DELAY_MS, MILLISECONDS);
102 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
103 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
100 >                p.scheduleAtFixedRate(task, timeoutMillis(),
101 >                                      LONG_DELAY_MS, MILLISECONDS);
102 >            await(done);
103 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
104              f.cancel(true);
105          } finally {
106              joinPool(p);
# Line 115 | Line 110 | public class ScheduledExecutorTest exten
110      /**
111       * scheduleWithFixedDelay executes runnable after given initial delay
112       */
113 <    public void testSchedule5() throws InterruptedException {
113 >    public void testSchedule5() throws Exception {
114          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
115 <        final long t0 = System.nanoTime();
121 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
115 >        final long startTime = System.nanoTime();
116          final CountDownLatch done = new CountDownLatch(1);
117          try {
118              Runnable task = new CheckedRunnable() {
119                  public void realRun() {
120                      done.countDown();
121 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
121 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
122                  }};
123              ScheduledFuture f =
124 <                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
125 <                                         SHORT_DELAY_MS, MILLISECONDS);
126 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
127 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
124 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
125 >                                         LONG_DELAY_MS, MILLISECONDS);
126 >            await(done);
127 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
128              f.cancel(true);
129          } finally {
130              joinPool(p);
# Line 147 | Line 141 | public class ScheduledExecutorTest exten
141       */
142      public void testFixedRateSequence() throws InterruptedException {
143          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
144 <        RunnableCounter counter = new RunnableCounter();
145 <        ScheduledFuture h =
146 <            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
147 <        delay(SMALL_DELAY_MS);
148 <        h.cancel(true);
149 <        int c = counter.count.get();
150 <        // By time scaling conventions, we must have at least
151 <        // an execution per SHORT delay, but no more than one SHORT more
152 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
153 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
154 <        joinPool(p);
144 >        try {
145 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
146 >                long startTime = System.nanoTime();
147 >                int cycles = 10;
148 >                final CountDownLatch done = new CountDownLatch(cycles);
149 >                CheckedRunnable task = new CheckedRunnable() {
150 >                    public void realRun() { done.countDown(); }};
151 >                ScheduledFuture h =
152 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
153 >                done.await();
154 >                h.cancel(true);
155 >                double normalizedTime =
156 >                    (double) millisElapsedSince(startTime) / delay;
157 >                if (normalizedTime >= cycles - 1 &&
158 >                    normalizedTime <= cycles)
159 >                    return;
160 >            }
161 >            throw new AssertionError("unexpected execution rate");
162 >        } finally {
163 >            joinPool(p);
164 >        }
165      }
166  
167      /**
# Line 165 | Line 169 | public class ScheduledExecutorTest exten
169       */
170      public void testFixedDelaySequence() throws InterruptedException {
171          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
172 <        RunnableCounter counter = new RunnableCounter();
173 <        ScheduledFuture h =
174 <            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
175 <        delay(SMALL_DELAY_MS);
176 <        h.cancel(true);
177 <        int c = counter.count.get();
178 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
179 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
180 <        joinPool(p);
172 >        try {
173 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
174 >                long startTime = System.nanoTime();
175 >                int cycles = 10;
176 >                final CountDownLatch done = new CountDownLatch(cycles);
177 >                CheckedRunnable task = new CheckedRunnable() {
178 >                    public void realRun() { done.countDown(); }};
179 >                ScheduledFuture h =
180 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
181 >                done.await();
182 >                h.cancel(true);
183 >                double normalizedTime =
184 >                    (double) millisElapsedSince(startTime) / delay;
185 >                if (normalizedTime >= cycles - 1 &&
186 >                    normalizedTime <= cycles)
187 >                    return;
188 >            }
189 >            throw new AssertionError("unexpected execution rate");
190 >        } finally {
191 >            joinPool(p);
192 >        }
193      }
194  
179
195      /**
196       * execute(null) throws NPE
197       */
# Line 327 | Line 342 | public class ScheduledExecutorTest exten
342                      threadProceed.await();
343                      threadDone.countDown();
344                  }});
345 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
345 >            await(threadStarted);
346              assertEquals(0, p.getCompletedTaskCount());
347              threadProceed.countDown();
348              threadDone.await();
349 <            delay(SHORT_DELAY_MS);
350 <            assertEquals(1, p.getCompletedTaskCount());
349 >            long startTime = System.nanoTime();
350 >            while (p.getCompletedTaskCount() != 1) {
351 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
352 >                    fail("timed out");
353 >                Thread.yield();
354 >            }
355          } finally {
356              joinPool(p);
357          }
# Line 459 | Line 478 | public class ScheduledExecutorTest exten
478      }
479  
480      /**
481 <     * isShutDown is false before shutdown, true after
481 >     * isShutdown is false before shutdown, true after
482       */
483      public void testIsShutdown() {
484  
# Line 473 | Line 492 | public class ScheduledExecutorTest exten
492          assertTrue(p.isShutdown());
493      }
494  
476
495      /**
496       * isTerminated is false before termination, true after
497       */
# Line 617 | Line 635 | public class ScheduledExecutorTest exten
635      }
636  
637      /**
638 <     * shutDownNow returns a list containing tasks that were not run
638 >     * shutdownNow returns a list containing tasks that were not run
639       */
640      public void testShutdownNow() {
641          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
# Line 639 | Line 657 | public class ScheduledExecutorTest exten
657       * In default setting, shutdown cancels periodic but not delayed
658       * tasks at shutdown
659       */
660 <    public void testShutDown1() throws InterruptedException {
660 >    public void testShutdown1() throws InterruptedException {
661          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
662          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
663          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
# Line 664 | Line 682 | public class ScheduledExecutorTest exten
682          }
683      }
684  
667
685      /**
686       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
687       * delayed tasks are cancelled at shutdown
688       */
689 <    public void testShutDown2() throws InterruptedException {
689 >    public void testShutdown2() throws InterruptedException {
690          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
691          p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
692          assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
# Line 695 | Line 712 | public class ScheduledExecutorTest exten
712       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
713       * periodic tasks are cancelled at shutdown
714       */
715 <    public void testShutDown3() throws InterruptedException {
715 >    public void testShutdown3() throws InterruptedException {
716          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
717          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
718          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
719          p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
720          assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
721          assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
722 +        long initialDelay = LONG_DELAY_MS;
723          ScheduledFuture task =
724 <            p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
724 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
725 >                                  5, MILLISECONDS);
726          try { p.shutdown(); } catch (SecurityException ok) { return; }
727          assertTrue(p.isShutdown());
709        BlockingQueue q = p.getQueue();
728          assertTrue(p.getQueue().isEmpty());
729          assertTrue(task.isDone());
730          assertTrue(task.isCancelled());
731 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
714 <        assertTrue(p.isTerminated());
731 >        joinPool(p);
732      }
733  
734      /**
735       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
736       * periodic tasks are not cancelled at shutdown
737       */
738 <    public void testShutDown4() throws InterruptedException {
738 >    public void testShutdown4() throws InterruptedException {
739          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
740          final CountDownLatch counter = new CountDownLatch(2);
741          try {
# Line 1160 | Line 1177 | public class ScheduledExecutorTest exten
1177              l.add(new StringTask());
1178              List<Future<String>> futures =
1179                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1180 <            assertEquals(3, futures.size());
1181 <            Iterator<Future<String>> it = futures.iterator();
1182 <            Future<String> f1 = it.next();
1183 <            Future<String> f2 = it.next();
1184 <            Future<String> f3 = it.next();
1168 <            assertTrue(f1.isDone());
1169 <            assertTrue(f2.isDone());
1170 <            assertTrue(f3.isDone());
1171 <            assertFalse(f1.isCancelled());
1172 <            assertTrue(f2.isCancelled());
1180 >            assertEquals(l.size(), futures.size());
1181 >            for (Future future : futures)
1182 >                assertTrue(future.isDone());
1183 >            assertFalse(futures.get(0).isCancelled());
1184 >            assertTrue(futures.get(1).isCancelled());
1185          } finally {
1186              joinPool(e);
1187          }
1188      }
1189  
1178
1190   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines