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.33 by jsr166, Mon Oct 11 07:21:32 2010 UTC

# Line 25 | Line 25 | public class ScheduledExecutorTest exten
25       * execute successfully executes a runnable
26       */
27      public void testExecute() throws InterruptedException {
28 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
29 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
30 <        p1.execute(runnable);
31 <        assertFalse(runnable.done);
32 <        Thread.sleep(SHORT_DELAY_MS);
33 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
34 <        Thread.sleep(MEDIUM_DELAY_MS);
35 <        assertTrue(runnable.done);
36 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
37 <        joinPool(p1);
28 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
29 >        final CountDownLatch done = new CountDownLatch(1);
30 >        final Runnable task = new CheckedRunnable() {
31 >            public void realRun() {
32 >                done.countDown();
33 >            }};
34 >        try {
35 >            p.execute(task);
36 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
37 >        } finally {
38 >            joinPool(p);
39 >        }
40      }
41  
42  
# Line 42 | Line 44 | public class ScheduledExecutorTest exten
44       * delayed schedule of callable successfully executes after delay
45       */
46      public void testSchedule1() throws Exception {
47 <        TrackedCallable callable = new TrackedCallable();
48 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
49 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
50 <        assertFalse(callable.done);
51 <        Thread.sleep(MEDIUM_DELAY_MS);
52 <        assertTrue(callable.done);
53 <        assertEquals(Boolean.TRUE, f.get());
54 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
55 <        joinPool(p1);
47 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
48 >        final long t0 = System.nanoTime();
49 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
50 >        final CountDownLatch done = new CountDownLatch(1);
51 >        try {
52 >            Callable task = new CheckedCallable<Boolean>() {
53 >                public Boolean realCall() {
54 >                    done.countDown();
55 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
56 >                    return Boolean.TRUE;
57 >                }};
58 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
59 >            assertEquals(Boolean.TRUE, f.get());
60 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
61 >            assertTrue(done.await(0L, MILLISECONDS));
62 >        } finally {
63 >            joinPool(p);
64 >        }
65      }
66  
67      /**
68       * delayed schedule of runnable successfully executes after delay
69       */
70 <    public void testSchedule3() throws InterruptedException {
71 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
72 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
73 <        p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
74 <        Thread.sleep(SHORT_DELAY_MS);
75 <        assertFalse(runnable.done);
76 <        Thread.sleep(MEDIUM_DELAY_MS);
77 <        assertTrue(runnable.done);
78 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
79 <        joinPool(p1);
70 >    public void testSchedule3() throws Exception {
71 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
72 >        final long t0 = System.nanoTime();
73 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
74 >        final CountDownLatch done = new CountDownLatch(1);
75 >        try {
76 >            Runnable task = new CheckedRunnable() {
77 >                public void realRun() {
78 >                    done.countDown();
79 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
80 >                }};
81 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
82 >            assertNull(f.get());
83 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
84 >            assertTrue(done.await(0L, MILLISECONDS));
85 >        } finally {
86 >            joinPool(p);
87 >        }
88      }
89  
90      /**
91       * scheduleAtFixedRate executes runnable after given initial delay
92       */
93 <    public void testSchedule4() throws InterruptedException {
94 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
95 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
96 <        ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
97 <        assertFalse(runnable.done);
98 <        Thread.sleep(MEDIUM_DELAY_MS);
99 <        assertTrue(runnable.done);
100 <        h.cancel(true);
101 <        joinPool(p1);
102 <    }
103 <
104 <    static class RunnableCounter implements Runnable {
105 <        AtomicInteger count = new AtomicInteger(0);
106 <        public void run() { count.getAndIncrement(); }
93 >    public void testSchedule4() throws Exception {
94 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
95 >        final long t0 = System.nanoTime();
96 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
97 >        final CountDownLatch done = new CountDownLatch(1);
98 >        try {
99 >            Runnable task = new CheckedRunnable() {
100 >                public void realRun() {
101 >                    done.countDown();
102 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
103 >                }};
104 >            ScheduledFuture f =
105 >                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
106 >                                      SHORT_DELAY_MS, MILLISECONDS);
107 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
108 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
109 >            f.cancel(true);
110 >        } finally {
111 >            joinPool(p);
112 >        }
113      }
114  
115      /**
116       * scheduleWithFixedDelay executes runnable after given initial delay
117       */
118      public void testSchedule5() throws InterruptedException {
119 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
120 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
121 <        ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
122 <        assertFalse(runnable.done);
123 <        Thread.sleep(MEDIUM_DELAY_MS);
124 <        assertTrue(runnable.done);
125 <        h.cancel(true);
126 <        joinPool(p1);
119 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
120 >        final long t0 = System.nanoTime();
121 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
122 >        final CountDownLatch done = new CountDownLatch(1);
123 >        try {
124 >            Runnable task = new CheckedRunnable() {
125 >                public void realRun() {
126 >                    done.countDown();
127 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
128 >                }};
129 >            ScheduledFuture f =
130 >                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
131 >                                         SHORT_DELAY_MS, MILLISECONDS);
132 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
133 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
134 >            f.cancel(true);
135 >        } finally {
136 >            joinPool(p);
137 >        }
138 >    }
139 >
140 >    static class RunnableCounter implements Runnable {
141 >        AtomicInteger count = new AtomicInteger(0);
142 >        public void run() { count.getAndIncrement(); }
143      }
144  
145      /**
146       * scheduleAtFixedRate executes series of tasks at given rate
147       */
148      public void testFixedRateSequence() throws InterruptedException {
149 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
149 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
150          RunnableCounter counter = new RunnableCounter();
151          ScheduledFuture h =
152 <            p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
152 >            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
153          Thread.sleep(SMALL_DELAY_MS);
154          h.cancel(true);
155          int c = counter.count.get();
# Line 116 | Line 157 | public class ScheduledExecutorTest exten
157          // an execution per SHORT delay, but no more than one SHORT more
158          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
159          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
160 <        joinPool(p1);
160 >        joinPool(p);
161      }
162  
163      /**
164       * scheduleWithFixedDelay executes series of tasks with given period
165       */
166      public void testFixedDelaySequence() throws InterruptedException {
167 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
167 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
168          RunnableCounter counter = new RunnableCounter();
169          ScheduledFuture h =
170 <            p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
170 >            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
171          Thread.sleep(SMALL_DELAY_MS);
172          h.cancel(true);
173          int c = counter.count.get();
174          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
175          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
176 <        joinPool(p1);
176 >        joinPool(p);
177      }
178  
179  
# Line 249 | Line 290 | public class ScheduledExecutorTest exten
290       * thread becomes active
291       */
292      public void testGetActiveCount() throws InterruptedException {
293 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
294 <        assertEquals(0, p2.getActiveCount());
295 <        p2.execute(new SmallRunnable());
296 <        Thread.sleep(SHORT_DELAY_MS);
297 <        assertEquals(1, p2.getActiveCount());
298 <        joinPool(p2);
293 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
294 >        final CountDownLatch threadStarted = new CountDownLatch(1);
295 >        final CountDownLatch done = new CountDownLatch(1);
296 >        try {
297 >            assertEquals(0, p.getActiveCount());
298 >            p.execute(new CheckedRunnable() {
299 >                public void realRun() throws InterruptedException {
300 >                    threadStarted.countDown();
301 >                    assertEquals(1, p.getActiveCount());
302 >                    done.await();
303 >                }});
304 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
305 >            assertEquals(1, p.getActiveCount());
306 >        } finally {
307 >            done.countDown();
308 >            joinPool(p);
309 >        }
310      }
311  
312      /**
# Line 262 | Line 314 | public class ScheduledExecutorTest exten
314       * when tasks complete
315       */
316      public void testGetCompletedTaskCount() throws InterruptedException {
317 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
318 <        assertEquals(0, p2.getCompletedTaskCount());
319 <        p2.execute(new SmallRunnable());
320 <        Thread.sleep(MEDIUM_DELAY_MS);
321 <        assertEquals(1, p2.getCompletedTaskCount());
322 <        joinPool(p2);
317 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
318 >        final CountDownLatch threadStarted = new CountDownLatch(1);
319 >        final CountDownLatch threadProceed = new CountDownLatch(1);
320 >        final CountDownLatch threadDone = new CountDownLatch(1);
321 >        try {
322 >            assertEquals(0, p.getCompletedTaskCount());
323 >            p.execute(new CheckedRunnable() {
324 >                public void realRun() throws InterruptedException {
325 >                    threadStarted.countDown();
326 >                    assertEquals(0, p.getCompletedTaskCount());
327 >                    threadProceed.await();
328 >                    threadDone.countDown();
329 >                }});
330 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
331 >            assertEquals(0, p.getCompletedTaskCount());
332 >            threadProceed.countDown();
333 >            threadDone.await();
334 >            Thread.sleep(SHORT_DELAY_MS);
335 >            assertEquals(1, p.getCompletedTaskCount());
336 >        } finally {
337 >            joinPool(p);
338 >        }
339      }
340  
341      /**
342       * getCorePoolSize returns size given in constructor if not otherwise set
343       */
344      public void testGetCorePoolSize() throws InterruptedException {
345 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
346 <        assertEquals(1, p1.getCorePoolSize());
347 <        joinPool(p1);
345 >        ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
346 >        assertEquals(1, p.getCorePoolSize());
347 >        joinPool(p);
348      }
349  
350      /**
# Line 284 | Line 352 | public class ScheduledExecutorTest exten
352       * multiple threads active
353       */
354      public void testGetLargestPoolSize() throws InterruptedException {
355 <        ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
356 <        assertEquals(0, p2.getLargestPoolSize());
357 <        p2.execute(new SmallRunnable());
358 <        p2.execute(new SmallRunnable());
359 <        Thread.sleep(SHORT_DELAY_MS);
360 <        assertEquals(2, p2.getLargestPoolSize());
361 <        joinPool(p2);
355 >        final int THREADS = 3;
356 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
357 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
358 >        final CountDownLatch done = new CountDownLatch(1);
359 >        try {
360 >            assertEquals(0, p.getLargestPoolSize());
361 >            for (int i = 0; i < THREADS; i++)
362 >                p.execute(new CheckedRunnable() {
363 >                    public void realRun() throws InterruptedException {
364 >                        threadsStarted.countDown();
365 >                        done.await();
366 >                        assertEquals(THREADS, p.getLargestPoolSize());
367 >                    }});
368 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
369 >            assertEquals(THREADS, p.getLargestPoolSize());
370 >        } finally {
371 >            done.countDown();
372 >            joinPool(p);
373 >            assertEquals(THREADS, p.getLargestPoolSize());
374 >        }
375      }
376  
377      /**
# Line 298 | Line 379 | public class ScheduledExecutorTest exten
379       * become active
380       */
381      public void testGetPoolSize() throws InterruptedException {
382 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
383 <        assertEquals(0, p1.getPoolSize());
384 <        p1.execute(new SmallRunnable());
385 <        assertEquals(1, p1.getPoolSize());
386 <        joinPool(p1);
382 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
383 >        final CountDownLatch threadStarted = new CountDownLatch(1);
384 >        final CountDownLatch done = new CountDownLatch(1);
385 >        try {
386 >            assertEquals(0, p.getPoolSize());
387 >            p.execute(new CheckedRunnable() {
388 >                public void realRun() throws InterruptedException {
389 >                    threadStarted.countDown();
390 >                    assertEquals(1, p.getPoolSize());
391 >                    done.await();
392 >                }});
393 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
394 >            assertEquals(1, p.getPoolSize());
395 >        } finally {
396 >            done.countDown();
397 >            joinPool(p);
398 >        }
399      }
400  
401      /**
# Line 310 | Line 403 | public class ScheduledExecutorTest exten
403       * submitted
404       */
405      public void testGetTaskCount() throws InterruptedException {
406 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
407 <        assertEquals(0, p1.getTaskCount());
408 <        for (int i = 0; i < 5; i++)
409 <            p1.execute(new SmallRunnable());
410 <        Thread.sleep(SHORT_DELAY_MS);
411 <        assertEquals(5, p1.getTaskCount());
412 <        joinPool(p1);
406 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
407 >        final CountDownLatch threadStarted = new CountDownLatch(1);
408 >        final CountDownLatch done = new CountDownLatch(1);
409 >        final int TASKS = 5;
410 >        try {
411 >            assertEquals(0, p.getTaskCount());
412 >            for (int i = 0; i < TASKS; i++)
413 >                p.execute(new CheckedRunnable() {
414 >                    public void realRun() throws InterruptedException {
415 >                        threadStarted.countDown();
416 >                        done.await();
417 >                    }});
418 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
419 >            assertEquals(TASKS, p.getTaskCount());
420 >        } finally {
421 >            done.countDown();
422 >            joinPool(p);
423 >        }
424      }
425  
426      /**
# Line 359 | Line 463 | public class ScheduledExecutorTest exten
463       */
464      public void testIsShutdown() {
465  
466 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
466 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
467          try {
468 <            assertFalse(p1.isShutdown());
468 >            assertFalse(p.isShutdown());
469          }
470          finally {
471 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
471 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
472          }
473 <        assertTrue(p1.isShutdown());
473 >        assertTrue(p.isShutdown());
474      }
475  
476  
# Line 374 | Line 478 | public class ScheduledExecutorTest exten
478       * isTerminated is false before termination, true after
479       */
480      public void testIsTerminated() throws InterruptedException {
481 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
482 <        try {
483 <            p1.execute(new SmallRunnable());
481 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
482 >        final CountDownLatch threadStarted = new CountDownLatch(1);
483 >        final CountDownLatch done = new CountDownLatch(1);
484 >        assertFalse(p.isTerminated());
485 >        try {
486 >            p.execute(new CheckedRunnable() {
487 >                public void realRun() throws InterruptedException {
488 >                    threadStarted.countDown();
489 >                    assertFalse(p.isTerminated());
490 >                    done.await();
491 >                }});
492 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
493 >            done.countDown();
494          } finally {
495 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
495 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
496          }
497 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
498 <        assertTrue(p1.isTerminated());
497 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
498 >        assertTrue(p.isTerminated());
499      }
500  
501      /**
502       * isTerminating is not true when running or when terminated
503       */
504      public void testIsTerminating() throws InterruptedException {
505 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
506 <        assertFalse(p1.isTerminating());
507 <        try {
508 <            p1.execute(new SmallRunnable());
509 <            assertFalse(p1.isTerminating());
505 >        final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
506 >        final CountDownLatch threadStarted = new CountDownLatch(1);
507 >        final CountDownLatch done = new CountDownLatch(1);
508 >        try {
509 >            assertFalse(p.isTerminating());
510 >            p.execute(new CheckedRunnable() {
511 >                public void realRun() throws InterruptedException {
512 >                    threadStarted.countDown();
513 >                    assertFalse(p.isTerminating());
514 >                    done.await();
515 >                }});
516 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
517 >            assertFalse(p.isTerminating());
518 >            done.countDown();
519          } finally {
520 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
520 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
521          }
522 <
523 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
524 <        assertTrue(p1.isTerminated());
402 <        assertFalse(p1.isTerminating());
522 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
523 >        assertTrue(p.isTerminated());
524 >        assertFalse(p.isTerminating());
525      }
526  
527      /**
528       * getQueue returns the work queue, which contains queued tasks
529       */
530      public void testGetQueue() throws InterruptedException {
531 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
532 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
533 <        for (int i = 0; i < 5; i++) {
412 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
413 <        }
531 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
532 >        final CountDownLatch threadStarted = new CountDownLatch(1);
533 >        final CountDownLatch done = new CountDownLatch(1);
534          try {
535 <            Thread.sleep(SHORT_DELAY_MS);
536 <            BlockingQueue<Runnable> q = p1.getQueue();
537 <            assertTrue(q.contains(tasks[4]));
535 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
536 >            for (int i = 0; i < tasks.length; i++) {
537 >                Runnable r = new CheckedRunnable() {
538 >                    public void realRun() throws InterruptedException {
539 >                        threadStarted.countDown();
540 >                        done.await();
541 >                    }};
542 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
543 >            }
544 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
545 >            BlockingQueue<Runnable> q = p.getQueue();
546 >            assertTrue(q.contains(tasks[tasks.length - 1]));
547              assertFalse(q.contains(tasks[0]));
548          } finally {
549 <            joinPool(p1);
549 >            done.countDown();
550 >            joinPool(p);
551          }
552      }
553  
# Line 425 | Line 555 | public class ScheduledExecutorTest exten
555       * remove(task) removes queued task, and fails to remove active task
556       */
557      public void testRemove() throws InterruptedException {
558 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
558 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
559          ScheduledFuture[] tasks = new ScheduledFuture[5];
560 <        for (int i = 0; i < 5; i++) {
561 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
432 <        }
560 >        final CountDownLatch threadStarted = new CountDownLatch(1);
561 >        final CountDownLatch done = new CountDownLatch(1);
562          try {
563 <            Thread.sleep(SHORT_DELAY_MS);
564 <            BlockingQueue<Runnable> q = p1.getQueue();
565 <            assertFalse(p1.remove((Runnable)tasks[0]));
563 >            for (int i = 0; i < tasks.length; i++) {
564 >                Runnable r = new CheckedRunnable() {
565 >                    public void realRun() throws InterruptedException {
566 >                        threadStarted.countDown();
567 >                        done.await();
568 >                    }};
569 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
570 >            }
571 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
572 >            BlockingQueue<Runnable> q = p.getQueue();
573 >            assertFalse(p.remove((Runnable)tasks[0]));
574              assertTrue(q.contains((Runnable)tasks[4]));
575              assertTrue(q.contains((Runnable)tasks[3]));
576 <            assertTrue(p1.remove((Runnable)tasks[4]));
577 <            assertFalse(p1.remove((Runnable)tasks[4]));
576 >            assertTrue(p.remove((Runnable)tasks[4]));
577 >            assertFalse(p.remove((Runnable)tasks[4]));
578              assertFalse(q.contains((Runnable)tasks[4]));
579              assertTrue(q.contains((Runnable)tasks[3]));
580 <            assertTrue(p1.remove((Runnable)tasks[3]));
580 >            assertTrue(p.remove((Runnable)tasks[3]));
581              assertFalse(q.contains((Runnable)tasks[3]));
582          } finally {
583 <            joinPool(p1);
583 >            done.countDown();
584 >            joinPool(p);
585          }
586      }
587  
# Line 451 | Line 589 | public class ScheduledExecutorTest exten
589       * purge removes cancelled tasks from the queue
590       */
591      public void testPurge() throws InterruptedException {
592 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
592 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
593          ScheduledFuture[] tasks = new ScheduledFuture[5];
594 <        for (int i = 0; i < 5; i++) {
595 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
594 >        for (int i = 0; i < tasks.length; i++) {
595 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
596          }
597          try {
598 <            int max = 5;
598 >            int max = tasks.length;
599              if (tasks[4].cancel(true)) --max;
600              if (tasks[3].cancel(true)) --max;
601              // There must eventually be an interference-free point at
602              // which purge will not fail. (At worst, when queue is empty.)
603              int k;
604              for (k = 0; k < SMALL_DELAY_MS; ++k) {
605 <                p1.purge();
606 <                long count = p1.getTaskCount();
605 >                p.purge();
606 >                long count = p.getTaskCount();
607                  if (count >= 0 && count <= max)
608                      break;
609                  Thread.sleep(1);
610              }
611              assertTrue(k < SMALL_DELAY_MS);
612          } finally {
613 <            joinPool(p1);
613 >            for (ScheduledFuture task : tasks)
614 >                task.cancel(true);
615 >            joinPool(p);
616          }
617      }
618  
# Line 480 | Line 620 | public class ScheduledExecutorTest exten
620       * shutDownNow returns a list containing tasks that were not run
621       */
622      public void testShutDownNow() throws InterruptedException {
623 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
623 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
624          for (int i = 0; i < 5; i++)
625 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
625 >            p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
626          List l;
627          try {
628 <            l = p1.shutdownNow();
628 >            l = p.shutdownNow();
629          } catch (SecurityException ok) {
630              return;
631          }
632 <        assertTrue(p1.isShutdown());
632 >        assertTrue(p.isShutdown());
633          assertTrue(l.size() > 0 && l.size() <= 5);
634 <        joinPool(p1);
634 >        joinPool(p);
635      }
636  
637      /**
# Line 499 | Line 639 | public class ScheduledExecutorTest exten
639       * tasks at shutdown
640       */
641      public void testShutDown1() throws InterruptedException {
642 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
643 <        assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
644 <        assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
642 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
643 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
644 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
645  
646          ScheduledFuture[] tasks = new ScheduledFuture[5];
647 <        for (int i = 0; i < 5; i++)
648 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
649 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
650 <        BlockingQueue q = p1.getQueue();
651 <        for (Iterator it = q.iterator(); it.hasNext();) {
652 <            ScheduledFuture t = (ScheduledFuture)it.next();
653 <            assertFalse(t.isCancelled());
647 >        for (int i = 0; i < tasks.length; i++)
648 >            tasks[i] = p.schedule(new NoOpRunnable(),
649 >                                  SHORT_DELAY_MS, MILLISECONDS);
650 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
651 >        BlockingQueue<Runnable> q = p.getQueue();
652 >        for (ScheduledFuture task : tasks) {
653 >            assertFalse(task.isDone());
654 >            assertFalse(task.isCancelled());
655 >            assertTrue(q.contains(task));
656          }
657 <        assertTrue(p1.isShutdown());
658 <        Thread.sleep(SMALL_DELAY_MS);
659 <        for (int i = 0; i < 5; ++i) {
660 <            assertTrue(tasks[i].isDone());
661 <            assertFalse(tasks[i].isCancelled());
657 >        assertTrue(p.isShutdown());
658 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
659 >        assertTrue(p.isTerminated());
660 >        for (ScheduledFuture task : tasks) {
661 >            assertTrue(task.isDone());
662 >            assertFalse(task.isCancelled());
663          }
664      }
665  
# Line 526 | Line 669 | public class ScheduledExecutorTest exten
669       * delayed tasks are cancelled at shutdown
670       */
671      public void testShutDown2() throws InterruptedException {
672 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
673 <        p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
672 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
673 >        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
674          ScheduledFuture[] tasks = new ScheduledFuture[5];
675 <        for (int i = 0; i < 5; i++)
676 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
677 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
678 <        assertTrue(p1.isShutdown());
679 <        BlockingQueue q = p1.getQueue();
675 >        for (int i = 0; i < tasks.length; i++)
676 >            tasks[i] = p.schedule(new NoOpRunnable(),
677 >                                  SHORT_DELAY_MS, MILLISECONDS);
678 >        BlockingQueue q = p.getQueue();
679 >        assertEquals(tasks.length, q.size());
680 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
681 >        assertTrue(p.isShutdown());
682          assertTrue(q.isEmpty());
683 <        Thread.sleep(SMALL_DELAY_MS);
684 <        assertTrue(p1.isTerminated());
683 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
684 >        assertTrue(p.isTerminated());
685 >        for (ScheduledFuture task : tasks) {
686 >            assertTrue(task.isDone());
687 >            assertTrue(task.isCancelled());
688 >        }
689      }
690  
542
691      /**
692       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
693 <     * periodic tasks are not cancelled at shutdown
693 >     * periodic tasks are cancelled at shutdown
694       */
695      public void testShutDown3() throws InterruptedException {
696 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
697 <        p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
696 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
697 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
698          ScheduledFuture task =
699 <            p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
700 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
701 <        assertTrue(p1.isShutdown());
702 <        BlockingQueue q = p1.getQueue();
703 <        assertTrue(q.isEmpty());
704 <        Thread.sleep(SHORT_DELAY_MS);
705 <        assertTrue(p1.isTerminated());
699 >            p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
700 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
701 >        assertTrue(p.isShutdown());
702 >        BlockingQueue q = p.getQueue();
703 >        assertTrue(p.getQueue().isEmpty());
704 >        assertTrue(task.isDone());
705 >        assertTrue(task.isCancelled());
706 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
707 >        assertTrue(p.isTerminated());
708      }
709  
710      /**
711       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
712 <     * periodic tasks are cancelled at shutdown
712 >     * periodic tasks are not cancelled at shutdown
713       */
714      public void testShutDown4() throws InterruptedException {
715 <        ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
715 >        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
716 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
717 >        final CountDownLatch counter = new CountDownLatch(2);
718          try {
719 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
719 >            final Runnable r = new CheckedRunnable() {
720 >                public void realRun() {
721 >                    counter.countDown();
722 >                }};
723              ScheduledFuture task =
724 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
724 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
725 >            assertFalse(task.isDone());
726              assertFalse(task.isCancelled());
727 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
727 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
728              assertFalse(task.isCancelled());
729 <            assertFalse(p1.isTerminated());
730 <            assertTrue(p1.isShutdown());
731 <            Thread.sleep(SHORT_DELAY_MS);
729 >            assertFalse(p.isTerminated());
730 >            assertTrue(p.isShutdown());
731 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
732              assertFalse(task.isCancelled());
733 <            assertTrue(task.cancel(true));
733 >            assertTrue(task.cancel(false));
734              assertTrue(task.isDone());
735 <            Thread.sleep(SHORT_DELAY_MS);
736 <            assertTrue(p1.isTerminated());
735 >            assertTrue(task.isCancelled());
736 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
737 >            assertTrue(p.isTerminated());
738          }
739          finally {
740 <            joinPool(p1);
740 >            joinPool(p);
741          }
742      }
743  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines