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.32 by jsr166, Sat Oct 9 19:30:35 2010 UTC vs.
Revision 1.44 by jsr166, Sat May 28 15:33:20 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       */
26      public void testExecute() throws InterruptedException {
27 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
28 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
29 <        p1.execute(runnable);
30 <        assertFalse(runnable.done);
31 <        Thread.sleep(SHORT_DELAY_MS);
32 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
33 <        Thread.sleep(MEDIUM_DELAY_MS);
34 <        assertTrue(runnable.done);
35 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
36 <        joinPool(p1);
27 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
28 >        final CountDownLatch done = new CountDownLatch(1);
29 >        final Runnable task = new CheckedRunnable() {
30 >            public void realRun() {
31 >                done.countDown();
32 >            }};
33 >        try {
34 >            p.execute(task);
35 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
36 >        } finally {
37 >            joinPool(p);
38 >        }
39      }
40  
40
41      /**
42       * delayed schedule of callable successfully executes after delay
43       */
44      public void testSchedule1() throws Exception {
45 <        TrackedCallable callable = new TrackedCallable();
46 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
47 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
48 <        assertFalse(callable.done);
49 <        Thread.sleep(MEDIUM_DELAY_MS);
50 <        assertTrue(callable.done);
51 <        assertEquals(Boolean.TRUE, f.get());
52 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
53 <        joinPool(p1);
45 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
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(millisElapsedSince(startTime) >= timeoutMillis());
53 >                    return Boolean.TRUE;
54 >                }};
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);
61 >        }
62      }
63  
64      /**
65       * delayed schedule of runnable successfully executes after delay
66       */
67 <    public void testSchedule3() throws InterruptedException {
68 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
69 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
70 <        p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
71 <        Thread.sleep(SHORT_DELAY_MS);
72 <        assertFalse(runnable.done);
73 <        Thread.sleep(MEDIUM_DELAY_MS);
74 <        assertTrue(runnable.done);
75 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
76 <        joinPool(p1);
67 >    public void testSchedule3() throws Exception {
68 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
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(millisElapsedSince(startTime) >= timeoutMillis());
76 >                }};
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 >        }
84      }
85  
86      /**
87       * scheduleAtFixedRate executes runnable after given initial delay
88       */
89 <    public void testSchedule4() throws InterruptedException {
90 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
91 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
92 <        ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
93 <        assertFalse(runnable.done);
94 <        Thread.sleep(MEDIUM_DELAY_MS);
95 <        assertTrue(runnable.done);
96 <        h.cancel(true);
97 <        joinPool(p1);
98 <    }
99 <
100 <    static class RunnableCounter implements Runnable {
101 <        AtomicInteger count = new AtomicInteger(0);
102 <        public void run() { count.getAndIncrement(); }
89 >    public void testSchedule4() throws Exception {
90 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
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(millisElapsedSince(startTime) >= timeoutMillis());
98 >                }};
99 >            ScheduledFuture f =
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);
107 >        }
108      }
109  
110      /**
111       * scheduleWithFixedDelay executes runnable after given initial delay
112       */
113 <    public void testSchedule5() throws InterruptedException {
114 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
115 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
116 <        ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
117 <        assertFalse(runnable.done);
118 <        Thread.sleep(MEDIUM_DELAY_MS);
119 <        assertTrue(runnable.done);
120 <        h.cancel(true);
121 <        joinPool(p1);
113 >    public void testSchedule5() throws Exception {
114 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
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(millisElapsedSince(startTime) >= timeoutMillis());
122 >                }};
123 >            ScheduledFuture f =
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);
131 >        }
132 >    }
133 >
134 >    static class RunnableCounter implements Runnable {
135 >        AtomicInteger count = new AtomicInteger(0);
136 >        public void run() { count.getAndIncrement(); }
137      }
138  
139      /**
140       * scheduleAtFixedRate executes series of tasks at given rate
141       */
142      public void testFixedRateSequence() throws InterruptedException {
143 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
143 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
144          RunnableCounter counter = new RunnableCounter();
145          ScheduledFuture h =
146 <            p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
147 <        Thread.sleep(SMALL_DELAY_MS);
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(p1);
154 >        joinPool(p);
155      }
156  
157      /**
158       * scheduleWithFixedDelay executes series of tasks with given period
159       */
160      public void testFixedDelaySequence() throws InterruptedException {
161 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
161 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
162          RunnableCounter counter = new RunnableCounter();
163          ScheduledFuture h =
164 <            p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
165 <        Thread.sleep(SMALL_DELAY_MS);
164 >            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
165 >        delay(SMALL_DELAY_MS);
166          h.cancel(true);
167          int c = counter.count.get();
168          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
169          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
170 <        joinPool(p1);
170 >        joinPool(p);
171      }
172  
138
173      /**
174       * execute(null) throws NPE
175       */
# Line 199 | Line 233 | public class ScheduledExecutorTest exten
233      /**
234       * schedule callable throws RejectedExecutionException if shutdown
235       */
236 <     public void testSchedule3_RejectedExecutionException() throws InterruptedException {
237 <         ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
238 <         try {
236 >    public void testSchedule3_RejectedExecutionException() throws InterruptedException {
237 >        ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
238 >        try {
239              se.shutdown();
240              se.schedule(new NoOpCallable(),
241                          MEDIUM_DELAY_MS, MILLISECONDS);
# Line 209 | Line 243 | public class ScheduledExecutorTest exten
243          } catch (RejectedExecutionException success) {
244          } catch (SecurityException ok) {
245          }
246 <         joinPool(se);
246 >        joinPool(se);
247      }
248  
249      /**
# Line 249 | Line 283 | public class ScheduledExecutorTest exten
283       * thread becomes active
284       */
285      public void testGetActiveCount() throws InterruptedException {
286 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
287 <        assertEquals(0, p2.getActiveCount());
288 <        p2.execute(new SmallRunnable());
289 <        Thread.sleep(SHORT_DELAY_MS);
290 <        assertEquals(1, p2.getActiveCount());
291 <        joinPool(p2);
286 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
287 >        final CountDownLatch threadStarted = new CountDownLatch(1);
288 >        final CountDownLatch done = new CountDownLatch(1);
289 >        try {
290 >            assertEquals(0, p.getActiveCount());
291 >            p.execute(new CheckedRunnable() {
292 >                public void realRun() throws InterruptedException {
293 >                    threadStarted.countDown();
294 >                    assertEquals(1, p.getActiveCount());
295 >                    done.await();
296 >                }});
297 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
298 >            assertEquals(1, p.getActiveCount());
299 >        } finally {
300 >            done.countDown();
301 >            joinPool(p);
302 >        }
303      }
304  
305      /**
# Line 262 | Line 307 | public class ScheduledExecutorTest exten
307       * when tasks complete
308       */
309      public void testGetCompletedTaskCount() throws InterruptedException {
310 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
311 <        assertEquals(0, p2.getCompletedTaskCount());
312 <        p2.execute(new SmallRunnable());
313 <        Thread.sleep(MEDIUM_DELAY_MS);
314 <        assertEquals(1, p2.getCompletedTaskCount());
315 <        joinPool(p2);
310 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
311 >        final CountDownLatch threadStarted = new CountDownLatch(1);
312 >        final CountDownLatch threadProceed = new CountDownLatch(1);
313 >        final CountDownLatch threadDone = new CountDownLatch(1);
314 >        try {
315 >            assertEquals(0, p.getCompletedTaskCount());
316 >            p.execute(new CheckedRunnable() {
317 >                public void realRun() throws InterruptedException {
318 >                    threadStarted.countDown();
319 >                    assertEquals(0, p.getCompletedTaskCount());
320 >                    threadProceed.await();
321 >                    threadDone.countDown();
322 >                }});
323 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
324 >            assertEquals(0, p.getCompletedTaskCount());
325 >            threadProceed.countDown();
326 >            threadDone.await();
327 >            long startTime = System.nanoTime();
328 >            while (p.getCompletedTaskCount() != 1) {
329 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
330 >                    fail("timed out");
331 >                Thread.yield();
332 >            }
333 >        } finally {
334 >            joinPool(p);
335 >        }
336      }
337  
338      /**
339       * getCorePoolSize returns size given in constructor if not otherwise set
340       */
341      public void testGetCorePoolSize() throws InterruptedException {
342 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
343 <        assertEquals(1, p1.getCorePoolSize());
344 <        joinPool(p1);
342 >        ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
343 >        assertEquals(1, p.getCorePoolSize());
344 >        joinPool(p);
345      }
346  
347      /**
# Line 284 | Line 349 | public class ScheduledExecutorTest exten
349       * multiple threads active
350       */
351      public void testGetLargestPoolSize() throws InterruptedException {
352 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
353 <        assertEquals(0, p2.getLargestPoolSize());
354 <        p2.execute(new SmallRunnable());
355 <        p2.execute(new SmallRunnable());
356 <        Thread.sleep(SHORT_DELAY_MS);
357 <        assertEquals(2, p2.getLargestPoolSize());
358 <        joinPool(p2);
352 >        final int THREADS = 3;
353 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
354 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
355 >        final CountDownLatch done = new CountDownLatch(1);
356 >        try {
357 >            assertEquals(0, p.getLargestPoolSize());
358 >            for (int i = 0; i < THREADS; i++)
359 >                p.execute(new CheckedRunnable() {
360 >                    public void realRun() throws InterruptedException {
361 >                        threadsStarted.countDown();
362 >                        done.await();
363 >                        assertEquals(THREADS, p.getLargestPoolSize());
364 >                    }});
365 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
366 >            assertEquals(THREADS, p.getLargestPoolSize());
367 >        } finally {
368 >            done.countDown();
369 >            joinPool(p);
370 >            assertEquals(THREADS, p.getLargestPoolSize());
371 >        }
372      }
373  
374      /**
# Line 298 | Line 376 | public class ScheduledExecutorTest exten
376       * become active
377       */
378      public void testGetPoolSize() throws InterruptedException {
379 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
380 <        assertEquals(0, p1.getPoolSize());
381 <        p1.execute(new SmallRunnable());
382 <        assertEquals(1, p1.getPoolSize());
383 <        joinPool(p1);
379 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
380 >        final CountDownLatch threadStarted = new CountDownLatch(1);
381 >        final CountDownLatch done = new CountDownLatch(1);
382 >        try {
383 >            assertEquals(0, p.getPoolSize());
384 >            p.execute(new CheckedRunnable() {
385 >                public void realRun() throws InterruptedException {
386 >                    threadStarted.countDown();
387 >                    assertEquals(1, p.getPoolSize());
388 >                    done.await();
389 >                }});
390 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
391 >            assertEquals(1, p.getPoolSize());
392 >        } finally {
393 >            done.countDown();
394 >            joinPool(p);
395 >        }
396      }
397  
398      /**
# Line 310 | Line 400 | public class ScheduledExecutorTest exten
400       * submitted
401       */
402      public void testGetTaskCount() throws InterruptedException {
403 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
404 <        assertEquals(0, p1.getTaskCount());
405 <        for (int i = 0; i < 5; i++)
406 <            p1.execute(new SmallRunnable());
407 <        Thread.sleep(SHORT_DELAY_MS);
408 <        assertEquals(5, p1.getTaskCount());
409 <        joinPool(p1);
403 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
404 >        final CountDownLatch threadStarted = new CountDownLatch(1);
405 >        final CountDownLatch done = new CountDownLatch(1);
406 >        final int TASKS = 5;
407 >        try {
408 >            assertEquals(0, p.getTaskCount());
409 >            for (int i = 0; i < TASKS; i++)
410 >                p.execute(new CheckedRunnable() {
411 >                    public void realRun() throws InterruptedException {
412 >                        threadStarted.countDown();
413 >                        done.await();
414 >                    }});
415 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
416 >            assertEquals(TASKS, p.getTaskCount());
417 >        } finally {
418 >            done.countDown();
419 >            joinPool(p);
420 >        }
421      }
422  
423      /**
# Line 355 | 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  
463 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
463 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
464          try {
465 <            assertFalse(p1.isShutdown());
465 >            assertFalse(p.isShutdown());
466          }
467          finally {
468 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
468 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
469          }
470 <        assertTrue(p1.isShutdown());
470 >        assertTrue(p.isShutdown());
471      }
472  
372
473      /**
474       * isTerminated is false before termination, true after
475       */
476      public void testIsTerminated() throws InterruptedException {
477 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
478 <        try {
479 <            p1.execute(new SmallRunnable());
477 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
478 >        final CountDownLatch threadStarted = new CountDownLatch(1);
479 >        final CountDownLatch done = new CountDownLatch(1);
480 >        assertFalse(p.isTerminated());
481 >        try {
482 >            p.execute(new CheckedRunnable() {
483 >                public void realRun() throws InterruptedException {
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 { p1.shutdown(); } catch (SecurityException ok) { return; }
492 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
493          }
494 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
495 <        assertTrue(p1.isTerminated());
494 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
495 >        assertTrue(p.isTerminated());
496      }
497  
498      /**
499       * isTerminating is not true when running or when terminated
500       */
501      public void testIsTerminating() throws InterruptedException {
502 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
503 <        assertFalse(p1.isTerminating());
504 <        try {
505 <            p1.execute(new SmallRunnable());
506 <            assertFalse(p1.isTerminating());
507 <        } finally {
508 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
509 <        }
510 <
511 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
512 <        assertTrue(p1.isTerminated());
513 <        assertFalse(p1.isTerminating());
502 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
503 >        final CountDownLatch threadStarted = new CountDownLatch(1);
504 >        final CountDownLatch done = new CountDownLatch(1);
505 >        try {
506 >            assertFalse(p.isTerminating());
507 >            p.execute(new CheckedRunnable() {
508 >                public void realRun() throws InterruptedException {
509 >                    assertFalse(p.isTerminating());
510 >                    threadStarted.countDown();
511 >                    done.await();
512 >                }});
513 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
514 >            assertFalse(p.isTerminating());
515 >            done.countDown();
516 >        } finally {
517 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
518 >        }
519 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
520 >        assertTrue(p.isTerminated());
521 >        assertFalse(p.isTerminating());
522      }
523  
524      /**
525       * getQueue returns the work queue, which contains queued tasks
526       */
527      public void testGetQueue() throws InterruptedException {
528 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
529 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
530 <        for (int i = 0; i < 5; i++) {
412 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
413 <        }
528 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
529 >        final CountDownLatch threadStarted = new CountDownLatch(1);
530 >        final CountDownLatch done = new CountDownLatch(1);
531          try {
532 <            Thread.sleep(SHORT_DELAY_MS);
533 <            BlockingQueue<Runnable> q = p1.getQueue();
534 <            assertTrue(q.contains(tasks[4]));
532 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
533 >            for (int i = 0; i < tasks.length; i++) {
534 >                Runnable r = new CheckedRunnable() {
535 >                    public void realRun() throws InterruptedException {
536 >                        threadStarted.countDown();
537 >                        done.await();
538 >                    }};
539 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
540 >            }
541 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
542 >            BlockingQueue<Runnable> q = p.getQueue();
543 >            assertTrue(q.contains(tasks[tasks.length - 1]));
544              assertFalse(q.contains(tasks[0]));
545          } finally {
546 <            joinPool(p1);
546 >            done.countDown();
547 >            joinPool(p);
548          }
549      }
550  
# Line 425 | Line 552 | public class ScheduledExecutorTest exten
552       * remove(task) removes queued task, and fails to remove active task
553       */
554      public void testRemove() throws InterruptedException {
555 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
555 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
556          ScheduledFuture[] tasks = new ScheduledFuture[5];
557 <        for (int i = 0; i < 5; i++) {
558 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
432 <        }
557 >        final CountDownLatch threadStarted = new CountDownLatch(1);
558 >        final CountDownLatch done = new CountDownLatch(1);
559          try {
560 <            Thread.sleep(SHORT_DELAY_MS);
561 <            BlockingQueue<Runnable> q = p1.getQueue();
562 <            assertFalse(p1.remove((Runnable)tasks[0]));
560 >            for (int i = 0; i < tasks.length; i++) {
561 >                Runnable r = new CheckedRunnable() {
562 >                    public void realRun() throws InterruptedException {
563 >                        threadStarted.countDown();
564 >                        done.await();
565 >                    }};
566 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
567 >            }
568 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
569 >            BlockingQueue<Runnable> q = p.getQueue();
570 >            assertFalse(p.remove((Runnable)tasks[0]));
571              assertTrue(q.contains((Runnable)tasks[4]));
572              assertTrue(q.contains((Runnable)tasks[3]));
573 <            assertTrue(p1.remove((Runnable)tasks[4]));
574 <            assertFalse(p1.remove((Runnable)tasks[4]));
573 >            assertTrue(p.remove((Runnable)tasks[4]));
574 >            assertFalse(p.remove((Runnable)tasks[4]));
575              assertFalse(q.contains((Runnable)tasks[4]));
576              assertTrue(q.contains((Runnable)tasks[3]));
577 <            assertTrue(p1.remove((Runnable)tasks[3]));
577 >            assertTrue(p.remove((Runnable)tasks[3]));
578              assertFalse(q.contains((Runnable)tasks[3]));
579          } finally {
580 <            joinPool(p1);
580 >            done.countDown();
581 >            joinPool(p);
582          }
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 p1 = new ScheduledThreadPoolExecutor(1);
589 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
590          ScheduledFuture[] tasks = new ScheduledFuture[5];
591 <        for (int i = 0; i < 5; i++) {
592 <            tasks[i] = p1.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 = 5;
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) {
602 <                p1.purge();
603 <                long count = p1.getTaskCount();
604 <                if (count >= 0 && count <= max)
605 <                    break;
606 <                Thread.sleep(1);
607 <            }
473 <            assertTrue(k < SMALL_DELAY_MS);
600 >            long startTime = System.nanoTime();
601 >            do {
602 >                p.purge();
603 >                long count = p.getTaskCount();
604 >                if (count == max)
605 >                    return;
606 >            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
607 >            fail("Purge failed to remove cancelled tasks");
608          } finally {
609 <            joinPool(p1);
609 >            for (ScheduledFuture task : tasks)
610 >                task.cancel(true);
611 >            joinPool(p);
612          }
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 {
619 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
618 >    public void testShutdownNow() {
619 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
620          for (int i = 0; i < 5; i++)
621 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
622 <        List l;
621 >            p.schedule(new SmallPossiblyInterruptedRunnable(),
622 >                       LONG_DELAY_MS, MILLISECONDS);
623          try {
624 <            l = p1.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          }
492        assertTrue(p1.isShutdown());
493        assertTrue(l.size() > 0 && l.size() <= 5);
494        joinPool(p1);
632      }
633  
634      /**
635       * In default setting, shutdown cancels periodic but not delayed
636       * tasks at shutdown
637       */
638 <    public void testShutDown1() throws InterruptedException {
639 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
640 <        assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
641 <        assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
638 >    public void testShutdown1() throws InterruptedException {
639 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
640 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
641 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
642  
643          ScheduledFuture[] tasks = new ScheduledFuture[5];
644 <        for (int i = 0; i < 5; i++)
645 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
646 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
647 <        BlockingQueue q = p1.getQueue();
648 <        for (Iterator it = q.iterator(); it.hasNext();) {
649 <            ScheduledFuture t = (ScheduledFuture)it.next();
650 <            assertFalse(t.isCancelled());
651 <        }
652 <        assertTrue(p1.isShutdown());
653 <        Thread.sleep(SMALL_DELAY_MS);
654 <        for (int i = 0; i < 5; ++i) {
655 <            assertTrue(tasks[i].isDone());
656 <            assertFalse(tasks[i].isCancelled());
644 >        for (int i = 0; i < tasks.length; i++)
645 >            tasks[i] = p.schedule(new NoOpRunnable(),
646 >                                  SHORT_DELAY_MS, MILLISECONDS);
647 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
648 >        BlockingQueue<Runnable> q = p.getQueue();
649 >        for (ScheduledFuture task : tasks) {
650 >            assertFalse(task.isDone());
651 >            assertFalse(task.isCancelled());
652 >            assertTrue(q.contains(task));
653 >        }
654 >        assertTrue(p.isShutdown());
655 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
656 >        assertTrue(p.isTerminated());
657 >        for (ScheduledFuture task : tasks) {
658 >            assertTrue(task.isDone());
659 >            assertFalse(task.isCancelled());
660          }
661      }
662  
523
663      /**
664       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
665       * delayed tasks are cancelled at shutdown
666       */
667 <    public void testShutDown2() throws InterruptedException {
668 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
669 <        p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
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 < 5; i++)
674 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
675 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
676 <        assertTrue(p1.isShutdown());
677 <        BlockingQueue q = p1.getQueue();
673 >        for (int i = 0; i < tasks.length; i++)
674 >            tasks[i] = p.schedule(new NoOpRunnable(),
675 >                                  SHORT_DELAY_MS, MILLISECONDS);
676 >        BlockingQueue q = p.getQueue();
677 >        assertEquals(tasks.length, q.size());
678 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
679 >        assertTrue(p.isShutdown());
680          assertTrue(q.isEmpty());
681 <        Thread.sleep(SMALL_DELAY_MS);
682 <        assertTrue(p1.isTerminated());
681 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
682 >        assertTrue(p.isTerminated());
683 >        for (ScheduledFuture task : tasks) {
684 >            assertTrue(task.isDone());
685 >            assertTrue(task.isCancelled());
686 >        }
687      }
688  
542
689      /**
690       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
691 <     * periodic tasks are not cancelled at shutdown
691 >     * periodic tasks are cancelled at shutdown
692       */
693 <    public void testShutDown3() throws InterruptedException {
694 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
695 <        p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
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 <            p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
703 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
704 <        assertTrue(p1.isShutdown());
705 <        BlockingQueue q = p1.getQueue();
706 <        assertTrue(q.isEmpty());
707 <        Thread.sleep(SHORT_DELAY_MS);
708 <        assertTrue(p1.isTerminated());
702 >            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
703 >                                  5, MILLISECONDS);
704 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
705 >        assertTrue(p.isShutdown());
706 >        assertTrue(p.getQueue().isEmpty());
707 >        assertTrue(task.isDone());
708 >        assertTrue(task.isCancelled());
709 >        joinPool(p);
710      }
711  
712      /**
713       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
714 <     * periodic tasks are cancelled at shutdown
714 >     * periodic tasks are not cancelled at shutdown
715       */
716 <    public void testShutDown4() throws InterruptedException {
717 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
716 >    public void testShutdown4() throws InterruptedException {
717 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
718 >        final CountDownLatch counter = new CountDownLatch(2);
719          try {
720 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
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();
726 >                }};
727              ScheduledFuture task =
728 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
728 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
729 >            assertFalse(task.isDone());
730              assertFalse(task.isCancelled());
731 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
731 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
732              assertFalse(task.isCancelled());
733 <            assertFalse(p1.isTerminated());
734 <            assertTrue(p1.isShutdown());
735 <            Thread.sleep(SHORT_DELAY_MS);
733 >            assertFalse(p.isTerminated());
734 >            assertTrue(p.isShutdown());
735 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
736              assertFalse(task.isCancelled());
737 <            assertTrue(task.cancel(true));
737 >            assertTrue(task.cancel(false));
738              assertTrue(task.isDone());
739 <            Thread.sleep(SHORT_DELAY_MS);
740 <            assertTrue(p1.isTerminated());
739 >            assertTrue(task.isCancelled());
740 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
741 >            assertTrue(p.isTerminated());
742          }
743          finally {
744 <            joinPool(p1);
744 >            joinPool(p);
745          }
746      }
747  
# Line 1009 | Line 1170 | public class ScheduledExecutorTest exten
1170          }
1171      }
1172  
1012
1173   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines