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

Comparing jsr166/src/test/tck/ScheduledExecutorSubclassTest.java (file contents):
Revision 1.14 by jsr166, Sat Oct 9 19:30:35 2010 UTC vs.
Revision 1.15 by jsr166, Mon Oct 11 07:21:32 2010 UTC

# Line 78 | Line 78 | public class ScheduledExecutorSubclassTe
78       * execute successfully executes a runnable
79       */
80      public void testExecute() throws InterruptedException {
81 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
82 <        CustomExecutor p1 = new CustomExecutor(1);
83 <        p1.execute(runnable);
84 <        assertFalse(runnable.done);
85 <        Thread.sleep(SHORT_DELAY_MS);
86 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
87 <        Thread.sleep(MEDIUM_DELAY_MS);
88 <        assertTrue(runnable.done);
89 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
90 <        joinPool(p1);
81 >        CustomExecutor p = new CustomExecutor(1);
82 >        final CountDownLatch done = new CountDownLatch(1);
83 >        final Runnable task = new CheckedRunnable() {
84 >            public void realRun() {
85 >                done.countDown();
86 >            }};
87 >        try {
88 >            p.execute(task);
89 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
90 >        } finally {
91 >            joinPool(p);
92 >        }
93      }
94  
95  
# Line 95 | Line 97 | public class ScheduledExecutorSubclassTe
97       * delayed schedule of callable successfully executes after delay
98       */
99      public void testSchedule1() throws Exception {
100 <        TrackedCallable callable = new TrackedCallable();
101 <        CustomExecutor p1 = new CustomExecutor(1);
102 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
103 <        assertFalse(callable.done);
104 <        Thread.sleep(MEDIUM_DELAY_MS);
105 <        assertTrue(callable.done);
106 <        assertEquals(Boolean.TRUE, f.get());
107 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
108 <        joinPool(p1);
100 >        CustomExecutor p = new CustomExecutor(1);
101 >        final long t0 = System.nanoTime();
102 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
103 >        final CountDownLatch done = new CountDownLatch(1);
104 >        try {
105 >            Callable task = new CheckedCallable<Boolean>() {
106 >                public Boolean realCall() {
107 >                    done.countDown();
108 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
109 >                    return Boolean.TRUE;
110 >                }};
111 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
112 >            assertEquals(Boolean.TRUE, f.get());
113 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
114 >            assertTrue(done.await(0L, MILLISECONDS));
115 >        } finally {
116 >            joinPool(p);
117 >        }
118      }
119  
120      /**
121       * delayed schedule of runnable successfully executes after delay
122       */
123 <    public void testSchedule3() throws InterruptedException {
124 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
125 <        CustomExecutor p1 = new CustomExecutor(1);
126 <        p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
127 <        Thread.sleep(SHORT_DELAY_MS);
128 <        assertFalse(runnable.done);
129 <        Thread.sleep(MEDIUM_DELAY_MS);
130 <        assertTrue(runnable.done);
131 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
132 <        joinPool(p1);
123 >    public void testSchedule3() throws Exception {
124 >        CustomExecutor p = new CustomExecutor(1);
125 >        final long t0 = System.nanoTime();
126 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
127 >        final CountDownLatch done = new CountDownLatch(1);
128 >        try {
129 >            Runnable task = new CheckedRunnable() {
130 >                public void realRun() {
131 >                    done.countDown();
132 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
133 >                }};
134 >            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
135 >            assertNull(f.get());
136 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
137 >            assertTrue(done.await(0L, MILLISECONDS));
138 >        } finally {
139 >            joinPool(p);
140 >        }
141      }
142  
143      /**
144       * scheduleAtFixedRate executes runnable after given initial delay
145       */
146      public void testSchedule4() throws InterruptedException {
147 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
148 <        CustomExecutor p1 = new CustomExecutor(1);
149 <        ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
150 <        assertFalse(runnable.done);
151 <        Thread.sleep(MEDIUM_DELAY_MS);
152 <        assertTrue(runnable.done);
153 <        h.cancel(true);
154 <        joinPool(p1);
155 <    }
156 <
157 <    static class RunnableCounter implements Runnable {
158 <        AtomicInteger count = new AtomicInteger(0);
159 <        public void run() { count.getAndIncrement(); }
147 >        CustomExecutor p = new CustomExecutor(1);
148 >        final long t0 = System.nanoTime();
149 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
150 >        final CountDownLatch done = new CountDownLatch(1);
151 >        try {
152 >            Runnable task = new CheckedRunnable() {
153 >                public void realRun() {
154 >                    done.countDown();
155 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
156 >                }};
157 >            ScheduledFuture f =
158 >                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
159 >                                      SHORT_DELAY_MS, MILLISECONDS);
160 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
161 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
162 >            f.cancel(true);
163 >        } finally {
164 >            joinPool(p);
165 >        }
166      }
167  
168      /**
169       * scheduleWithFixedDelay executes runnable after given initial delay
170       */
171      public void testSchedule5() throws InterruptedException {
172 <        TrackedShortRunnable runnable = new TrackedShortRunnable();
173 <        CustomExecutor p1 = new CustomExecutor(1);
174 <        ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
175 <        assertFalse(runnable.done);
176 <        Thread.sleep(MEDIUM_DELAY_MS);
177 <        assertTrue(runnable.done);
178 <        h.cancel(true);
179 <        joinPool(p1);
172 >        CustomExecutor p = new CustomExecutor(1);
173 >        final long t0 = System.nanoTime();
174 >        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
175 >        final CountDownLatch done = new CountDownLatch(1);
176 >        try {
177 >            Runnable task = new CheckedRunnable() {
178 >                public void realRun() {
179 >                    done.countDown();
180 >                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
181 >                }};
182 >            ScheduledFuture f =
183 >                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
184 >                                         SHORT_DELAY_MS, MILLISECONDS);
185 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
186 >            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
187 >            f.cancel(true);
188 >        } finally {
189 >            joinPool(p);
190 >        }
191 >    }
192 >
193 >    static class RunnableCounter implements Runnable {
194 >        AtomicInteger count = new AtomicInteger(0);
195 >        public void run() { count.getAndIncrement(); }
196      }
197  
198      /**
199       * scheduleAtFixedRate executes series of tasks at given rate
200       */
201      public void testFixedRateSequence() throws InterruptedException {
202 <        CustomExecutor p1 = new CustomExecutor(1);
202 >        CustomExecutor p = new CustomExecutor(1);
203          RunnableCounter counter = new RunnableCounter();
204          ScheduledFuture h =
205 <            p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
205 >            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
206          Thread.sleep(SMALL_DELAY_MS);
207          h.cancel(true);
208          int c = counter.count.get();
# Line 169 | Line 210 | public class ScheduledExecutorSubclassTe
210          // an execution per SHORT delay, but no more than one SHORT more
211          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
212          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
213 <        joinPool(p1);
213 >        joinPool(p);
214      }
215  
216      /**
217       * scheduleWithFixedDelay executes series of tasks with given period
218       */
219      public void testFixedDelaySequence() throws InterruptedException {
220 <        CustomExecutor p1 = new CustomExecutor(1);
220 >        CustomExecutor p = new CustomExecutor(1);
221          RunnableCounter counter = new RunnableCounter();
222          ScheduledFuture h =
223 <            p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
223 >            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
224          Thread.sleep(SMALL_DELAY_MS);
225          h.cancel(true);
226          int c = counter.count.get();
227          assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
228          assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
229 <        joinPool(p1);
229 >        joinPool(p);
230      }
231  
232  
# Line 300 | Line 341 | public class ScheduledExecutorSubclassTe
341       * thread becomes active
342       */
343      public void testGetActiveCount() throws InterruptedException {
344 <        CustomExecutor p2 = new CustomExecutor(2);
345 <        assertEquals(0, p2.getActiveCount());
346 <        p2.execute(new SmallRunnable());
347 <        Thread.sleep(SHORT_DELAY_MS);
348 <        assertEquals(1, p2.getActiveCount());
349 <        joinPool(p2);
344 >        final ThreadPoolExecutor p = new CustomExecutor(2);
345 >        final CountDownLatch threadStarted = new CountDownLatch(1);
346 >        final CountDownLatch done = new CountDownLatch(1);
347 >        try {
348 >            assertEquals(0, p.getActiveCount());
349 >            p.execute(new CheckedRunnable() {
350 >                public void realRun() throws InterruptedException {
351 >                    threadStarted.countDown();
352 >                    assertEquals(1, p.getActiveCount());
353 >                    done.await();
354 >                }});
355 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
356 >            assertEquals(1, p.getActiveCount());
357 >        } finally {
358 >            done.countDown();
359 >            joinPool(p);
360 >        }
361      }
362  
363      /**
# Line 313 | Line 365 | public class ScheduledExecutorSubclassTe
365       * when tasks complete
366       */
367      public void testGetCompletedTaskCount() throws InterruptedException {
368 <        CustomExecutor p2 = new CustomExecutor(2);
369 <        assertEquals(0, p2.getCompletedTaskCount());
370 <        p2.execute(new SmallRunnable());
371 <        Thread.sleep(MEDIUM_DELAY_MS);
372 <        assertEquals(1, p2.getCompletedTaskCount());
373 <        joinPool(p2);
368 >        final ThreadPoolExecutor p = new CustomExecutor(2);
369 >        final CountDownLatch threadStarted = new CountDownLatch(1);
370 >        final CountDownLatch threadProceed = new CountDownLatch(1);
371 >        final CountDownLatch threadDone = new CountDownLatch(1);
372 >        try {
373 >            assertEquals(0, p.getCompletedTaskCount());
374 >            p.execute(new CheckedRunnable() {
375 >                public void realRun() throws InterruptedException {
376 >                    threadStarted.countDown();
377 >                    assertEquals(0, p.getCompletedTaskCount());
378 >                    threadProceed.await();
379 >                    threadDone.countDown();
380 >                }});
381 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
382 >            assertEquals(0, p.getCompletedTaskCount());
383 >            threadProceed.countDown();
384 >            threadDone.await();
385 >            Thread.sleep(SHORT_DELAY_MS);
386 >            assertEquals(1, p.getCompletedTaskCount());
387 >        } finally {
388 >            joinPool(p);
389 >        }
390      }
391  
392      /**
393       * getCorePoolSize returns size given in constructor if not otherwise set
394       */
395      public void testGetCorePoolSize() {
396 <        CustomExecutor p1 = new CustomExecutor(1);
397 <        assertEquals(1, p1.getCorePoolSize());
398 <        joinPool(p1);
396 >        CustomExecutor p = new CustomExecutor(1);
397 >        assertEquals(1, p.getCorePoolSize());
398 >        joinPool(p);
399      }
400  
401      /**
# Line 335 | Line 403 | public class ScheduledExecutorSubclassTe
403       * multiple threads active
404       */
405      public void testGetLargestPoolSize() throws InterruptedException {
406 <        CustomExecutor p2 = new CustomExecutor(2);
407 <        assertEquals(0, p2.getLargestPoolSize());
408 <        p2.execute(new SmallRunnable());
409 <        p2.execute(new SmallRunnable());
410 <        Thread.sleep(SHORT_DELAY_MS);
411 <        assertEquals(2, p2.getLargestPoolSize());
412 <        joinPool(p2);
406 >        final int THREADS = 3;
407 >        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
408 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
409 >        final CountDownLatch done = new CountDownLatch(1);
410 >        try {
411 >            assertEquals(0, p.getLargestPoolSize());
412 >            for (int i = 0; i < THREADS; i++)
413 >                p.execute(new CheckedRunnable() {
414 >                    public void realRun() throws InterruptedException {
415 >                        threadsStarted.countDown();
416 >                        done.await();
417 >                        assertEquals(THREADS, p.getLargestPoolSize());
418 >                    }});
419 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
420 >            assertEquals(THREADS, p.getLargestPoolSize());
421 >        } finally {
422 >            done.countDown();
423 >            joinPool(p);
424 >            assertEquals(THREADS, p.getLargestPoolSize());
425 >        }
426      }
427  
428      /**
429       * getPoolSize increases, but doesn't overestimate, when threads
430       * become active
431       */
432 <    public void testGetPoolSize() {
433 <        CustomExecutor p1 = new CustomExecutor(1);
434 <        assertEquals(0, p1.getPoolSize());
435 <        p1.execute(new SmallRunnable());
436 <        assertEquals(1, p1.getPoolSize());
437 <        joinPool(p1);
432 >    public void testGetPoolSize() throws InterruptedException {
433 >        final ThreadPoolExecutor p = new CustomExecutor(1);
434 >        final CountDownLatch threadStarted = new CountDownLatch(1);
435 >        final CountDownLatch done = new CountDownLatch(1);
436 >        try {
437 >            assertEquals(0, p.getPoolSize());
438 >            p.execute(new CheckedRunnable() {
439 >                public void realRun() throws InterruptedException {
440 >                    threadStarted.countDown();
441 >                    assertEquals(1, p.getPoolSize());
442 >                    done.await();
443 >                }});
444 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
445 >            assertEquals(1, p.getPoolSize());
446 >        } finally {
447 >            done.countDown();
448 >            joinPool(p);
449 >        }
450      }
451  
452      /**
# Line 361 | Line 454 | public class ScheduledExecutorSubclassTe
454       * submitted
455       */
456      public void testGetTaskCount() throws InterruptedException {
457 <        CustomExecutor p1 = new CustomExecutor(1);
458 <        assertEquals(0, p1.getTaskCount());
459 <        for (int i = 0; i < 5; i++)
460 <            p1.execute(new SmallRunnable());
461 <        Thread.sleep(SHORT_DELAY_MS);
462 <        assertEquals(5, p1.getTaskCount());
463 <        joinPool(p1);
457 >        final ThreadPoolExecutor p = new CustomExecutor(1);
458 >        final CountDownLatch threadStarted = new CountDownLatch(1);
459 >        final CountDownLatch done = new CountDownLatch(1);
460 >        final int TASKS = 5;
461 >        try {
462 >            assertEquals(0, p.getTaskCount());
463 >            for (int i = 0; i < TASKS; i++)
464 >                p.execute(new CheckedRunnable() {
465 >                    public void realRun() throws InterruptedException {
466 >                        threadStarted.countDown();
467 >                        done.await();
468 >                    }});
469 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
470 >            assertEquals(TASKS, p.getTaskCount());
471 >        } finally {
472 >            done.countDown();
473 >            joinPool(p);
474 >        }
475      }
476  
477      /**
# Line 409 | Line 513 | public class ScheduledExecutorSubclassTe
513       * isShutDown is false before shutdown, true after
514       */
515      public void testIsShutdown() {
516 <        CustomExecutor p1 = new CustomExecutor(1);
516 >        CustomExecutor p = new CustomExecutor(1);
517          try {
518 <            assertFalse(p1.isShutdown());
518 >            assertFalse(p.isShutdown());
519          }
520          finally {
521 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
521 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
522          }
523 <        assertTrue(p1.isShutdown());
523 >        assertTrue(p.isShutdown());
524      }
525  
526  
# Line 424 | Line 528 | public class ScheduledExecutorSubclassTe
528       * isTerminated is false before termination, true after
529       */
530      public void testIsTerminated() throws InterruptedException {
531 <        CustomExecutor p1 = new CustomExecutor(1);
532 <        try {
533 <            p1.execute(new SmallRunnable());
531 >        final ThreadPoolExecutor p = new CustomExecutor(1);
532 >        final CountDownLatch threadStarted = new CountDownLatch(1);
533 >        final CountDownLatch done = new CountDownLatch(1);
534 >        assertFalse(p.isTerminated());
535 >        try {
536 >            p.execute(new CheckedRunnable() {
537 >                public void realRun() throws InterruptedException {
538 >                    threadStarted.countDown();
539 >                    assertFalse(p.isTerminated());
540 >                    done.await();
541 >                }});
542 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
543 >            done.countDown();
544          } finally {
545 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
545 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
546          }
547 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
548 <        assertTrue(p1.isTerminated());
547 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
548 >        assertTrue(p.isTerminated());
549      }
550  
551      /**
552       * isTerminating is not true when running or when terminated
553       */
554      public void testIsTerminating() throws InterruptedException {
555 <        CustomExecutor p1 = new CustomExecutor(1);
556 <        assertFalse(p1.isTerminating());
557 <        try {
558 <            p1.execute(new SmallRunnable());
559 <            assertFalse(p1.isTerminating());
555 >        final ThreadPoolExecutor p = new CustomExecutor(1);
556 >        final CountDownLatch threadStarted = new CountDownLatch(1);
557 >        final CountDownLatch done = new CountDownLatch(1);
558 >        try {
559 >            assertFalse(p.isTerminating());
560 >            p.execute(new CheckedRunnable() {
561 >                public void realRun() throws InterruptedException {
562 >                    threadStarted.countDown();
563 >                    assertFalse(p.isTerminating());
564 >                    done.await();
565 >                }});
566 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
567 >            assertFalse(p.isTerminating());
568 >            done.countDown();
569          } finally {
570 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
570 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
571          }
572 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
573 <        assertTrue(p1.isTerminated());
574 <        assertFalse(p1.isTerminating());
572 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
573 >        assertTrue(p.isTerminated());
574 >        assertFalse(p.isTerminating());
575      }
576  
577      /**
578       * getQueue returns the work queue, which contains queued tasks
579       */
580      public void testGetQueue() throws InterruptedException {
581 <        CustomExecutor p1 = new CustomExecutor(1);
582 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
583 <        for (int i = 0; i < 5; i++) {
584 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
585 <        }
586 <        try {
587 <            Thread.sleep(SHORT_DELAY_MS);
588 <            BlockingQueue<Runnable> q = p1.getQueue();
589 <            assertTrue(q.contains(tasks[4]));
581 >        ScheduledThreadPoolExecutor p = new CustomExecutor(1);
582 >        final CountDownLatch threadStarted = new CountDownLatch(1);
583 >        final CountDownLatch done = new CountDownLatch(1);
584 >        try {
585 >            ScheduledFuture[] tasks = new ScheduledFuture[5];
586 >            for (int i = 0; i < tasks.length; i++) {
587 >                Runnable r = new CheckedRunnable() {
588 >                    public void realRun() throws InterruptedException {
589 >                        threadStarted.countDown();
590 >                        done.await();
591 >                    }};
592 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
593 >            }
594 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
595 >            BlockingQueue<Runnable> q = p.getQueue();
596 >            assertTrue(q.contains(tasks[tasks.length - 1]));
597              assertFalse(q.contains(tasks[0]));
598          } finally {
599 <            joinPool(p1);
599 >            done.countDown();
600 >            joinPool(p);
601          }
602      }
603  
# Line 474 | Line 605 | public class ScheduledExecutorSubclassTe
605       * remove(task) removes queued task, and fails to remove active task
606       */
607      public void testRemove() throws InterruptedException {
608 <        CustomExecutor p1 = new CustomExecutor(1);
608 >        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
609          ScheduledFuture[] tasks = new ScheduledFuture[5];
610 <        for (int i = 0; i < 5; i++) {
611 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
481 <        }
610 >        final CountDownLatch threadStarted = new CountDownLatch(1);
611 >        final CountDownLatch done = new CountDownLatch(1);
612          try {
613 <            Thread.sleep(SHORT_DELAY_MS);
614 <            BlockingQueue<Runnable> q = p1.getQueue();
615 <            assertFalse(p1.remove((Runnable)tasks[0]));
613 >            for (int i = 0; i < tasks.length; i++) {
614 >                Runnable r = new CheckedRunnable() {
615 >                    public void realRun() throws InterruptedException {
616 >                        threadStarted.countDown();
617 >                        done.await();
618 >                    }};
619 >                tasks[i] = p.schedule(r, 1, MILLISECONDS);
620 >            }
621 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
622 >            BlockingQueue<Runnable> q = p.getQueue();
623 >            assertFalse(p.remove((Runnable)tasks[0]));
624              assertTrue(q.contains((Runnable)tasks[4]));
625              assertTrue(q.contains((Runnable)tasks[3]));
626 <            assertTrue(p1.remove((Runnable)tasks[4]));
627 <            assertFalse(p1.remove((Runnable)tasks[4]));
626 >            assertTrue(p.remove((Runnable)tasks[4]));
627 >            assertFalse(p.remove((Runnable)tasks[4]));
628              assertFalse(q.contains((Runnable)tasks[4]));
629              assertTrue(q.contains((Runnable)tasks[3]));
630 <            assertTrue(p1.remove((Runnable)tasks[3]));
630 >            assertTrue(p.remove((Runnable)tasks[3]));
631              assertFalse(q.contains((Runnable)tasks[3]));
632          } finally {
633 <            joinPool(p1);
633 >            done.countDown();
634 >            joinPool(p);
635          }
636      }
637  
# Line 500 | Line 639 | public class ScheduledExecutorSubclassTe
639       * purge removes cancelled tasks from the queue
640       */
641      public void testPurge() throws InterruptedException {
642 <        CustomExecutor p1 = new CustomExecutor(1);
642 >        CustomExecutor p = new CustomExecutor(1);
643          ScheduledFuture[] tasks = new ScheduledFuture[5];
644 <        for (int i = 0; i < 5; i++) {
645 <            tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
644 >        for (int i = 0; i < tasks.length; i++) {
645 >            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
646          }
647          try {
648 <            int max = 5;
648 >            int max = tasks.length;
649              if (tasks[4].cancel(true)) --max;
650              if (tasks[3].cancel(true)) --max;
651              // There must eventually be an interference-free point at
652              // which purge will not fail. (At worst, when queue is empty.)
653              int k;
654              for (k = 0; k < SMALL_DELAY_MS; ++k) {
655 <                p1.purge();
656 <                long count = p1.getTaskCount();
655 >                p.purge();
656 >                long count = p.getTaskCount();
657                  if (count >= 0 && count <= max)
658                      break;
659                  Thread.sleep(1);
660              }
661              assertTrue(k < SMALL_DELAY_MS);
662          } finally {
663 <            joinPool(p1);
663 >            for (ScheduledFuture task : tasks)
664 >                task.cancel(true);
665 >            joinPool(p);
666          }
667      }
668  
# Line 529 | Line 670 | public class ScheduledExecutorSubclassTe
670       * shutDownNow returns a list containing tasks that were not run
671       */
672      public void testShutDownNow() {
673 <        CustomExecutor p1 = new CustomExecutor(1);
673 >        CustomExecutor p = new CustomExecutor(1);
674          for (int i = 0; i < 5; i++)
675 <            p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
675 >            p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
676          List l;
677          try {
678 <            l = p1.shutdownNow();
678 >            l = p.shutdownNow();
679          } catch (SecurityException ok) {
680              return;
681          }
682 <        assertTrue(p1.isShutdown());
682 >        assertTrue(p.isShutdown());
683          assertTrue(l.size() > 0 && l.size() <= 5);
684 <        joinPool(p1);
684 >        joinPool(p);
685      }
686  
687      /**
# Line 548 | Line 689 | public class ScheduledExecutorSubclassTe
689       * tasks at shutdown
690       */
691      public void testShutDown1() throws InterruptedException {
692 <        CustomExecutor p1 = new CustomExecutor(1);
693 <        assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
694 <        assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
692 >        CustomExecutor p = new CustomExecutor(1);
693 >        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
694 >        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
695  
696          ScheduledFuture[] tasks = new ScheduledFuture[5];
697 <        for (int i = 0; i < 5; i++)
698 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
699 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
700 <        BlockingQueue q = p1.getQueue();
701 <        for (Iterator it = q.iterator(); it.hasNext();) {
702 <            ScheduledFuture t = (ScheduledFuture)it.next();
703 <            assertFalse(t.isCancelled());
697 >        for (int i = 0; i < tasks.length; i++)
698 >            tasks[i] = p.schedule(new NoOpRunnable(),
699 >                                  SHORT_DELAY_MS, MILLISECONDS);
700 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
701 >        BlockingQueue<Runnable> q = p.getQueue();
702 >        for (ScheduledFuture task : tasks) {
703 >            assertFalse(task.isDone());
704 >            assertFalse(task.isCancelled());
705 >            assertTrue(q.contains(task));
706          }
707 <        assertTrue(p1.isShutdown());
708 <        Thread.sleep(SMALL_DELAY_MS);
709 <        for (int i = 0; i < 5; ++i) {
710 <            assertTrue(tasks[i].isDone());
711 <            assertFalse(tasks[i].isCancelled());
707 >        assertTrue(p.isShutdown());
708 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
709 >        assertTrue(p.isTerminated());
710 >        for (ScheduledFuture task : tasks) {
711 >            assertTrue(task.isDone());
712 >            assertFalse(task.isCancelled());
713          }
714      }
715  
# Line 575 | Line 719 | public class ScheduledExecutorSubclassTe
719       * delayed tasks are cancelled at shutdown
720       */
721      public void testShutDown2() throws InterruptedException {
722 <        CustomExecutor p1 = new CustomExecutor(1);
723 <        p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
722 >        CustomExecutor p = new CustomExecutor(1);
723 >        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
724          ScheduledFuture[] tasks = new ScheduledFuture[5];
725 <        for (int i = 0; i < 5; i++)
726 <            tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
727 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
728 <        assertTrue(p1.isShutdown());
729 <        BlockingQueue q = p1.getQueue();
725 >        for (int i = 0; i < tasks.length; i++)
726 >            tasks[i] = p.schedule(new NoOpRunnable(),
727 >                                  SHORT_DELAY_MS, MILLISECONDS);
728 >        BlockingQueue q = p.getQueue();
729 >        assertEquals(tasks.length, q.size());
730 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
731 >        assertTrue(p.isShutdown());
732          assertTrue(q.isEmpty());
733 <        Thread.sleep(SMALL_DELAY_MS);
734 <        assertTrue(p1.isTerminated());
733 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
734 >        assertTrue(p.isTerminated());
735 >        for (ScheduledFuture task : tasks) {
736 >            assertTrue(task.isDone());
737 >            assertTrue(task.isCancelled());
738 >        }
739      }
740  
741  
742      /**
743       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
744 <     * periodic tasks are not cancelled at shutdown
744 >     * periodic tasks are cancelled at shutdown
745       */
746      public void testShutDown3() throws InterruptedException {
747 <        CustomExecutor p1 = new CustomExecutor(1);
748 <        p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
747 >        CustomExecutor p = new CustomExecutor(1);
748 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
749          ScheduledFuture task =
750 <            p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
751 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
752 <        assertTrue(p1.isShutdown());
753 <        BlockingQueue q = p1.getQueue();
754 <        assertTrue(q.isEmpty());
755 <        Thread.sleep(SHORT_DELAY_MS);
756 <        assertTrue(p1.isTerminated());
750 >            p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
751 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
752 >        assertTrue(p.isShutdown());
753 >        BlockingQueue q = p.getQueue();
754 >        assertTrue(p.getQueue().isEmpty());
755 >        assertTrue(task.isDone());
756 >        assertTrue(task.isCancelled());
757 >        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
758 >        assertTrue(p.isTerminated());
759      }
760  
761      /**
762       * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
763 <     * periodic tasks are cancelled at shutdown
763 >     * periodic tasks are not cancelled at shutdown
764       */
765      public void testShutDown4() throws InterruptedException {
766 <        CustomExecutor p1 = new CustomExecutor(1);
766 >        CustomExecutor p = new CustomExecutor(1);
767 >        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
768 >        final CountDownLatch counter = new CountDownLatch(2);
769          try {
770 <            p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
770 >            final Runnable r = new CheckedRunnable() {
771 >                public void realRun() {
772 >                    counter.countDown();
773 >                }};
774              ScheduledFuture task =
775 <                p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
775 >                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
776 >            assertFalse(task.isDone());
777              assertFalse(task.isCancelled());
778 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
778 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
779              assertFalse(task.isCancelled());
780 <            assertFalse(p1.isTerminated());
781 <            assertTrue(p1.isShutdown());
782 <            Thread.sleep(SHORT_DELAY_MS);
780 >            assertFalse(p.isTerminated());
781 >            assertTrue(p.isShutdown());
782 >            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
783              assertFalse(task.isCancelled());
784 <            assertTrue(task.cancel(true));
784 >            assertTrue(task.cancel(false));
785              assertTrue(task.isDone());
786 <            Thread.sleep(SHORT_DELAY_MS);
787 <            assertTrue(p1.isTerminated());
786 >            assertTrue(task.isCancelled());
787 >            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
788 >            assertTrue(p.isTerminated());
789          }
790          finally {
791 <            joinPool(p1);
791 >            joinPool(p);
792          }
793      }
794  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines