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.54 by jsr166, Sun Sep 27 20:17:39 2015 UTC vs.
Revision 1.61 by jsr166, Sun Oct 4 02:15:08 2015 UTC

# Line 334 | Line 334 | public class ScheduledExecutorTest exten
334                      assertEquals(1, p.getActiveCount());
335                      done.await();
336                  }});
337 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
337 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
338              assertEquals(1, p.getActiveCount());
339          } finally {
340              done.countDown();
# Line 402 | Line 402 | public class ScheduledExecutorTest exten
402                          done.await();
403                          assertEquals(THREADS, p.getLargestPoolSize());
404                      }});
405 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
405 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
406              assertEquals(THREADS, p.getLargestPoolSize());
407          } finally {
408              done.countDown();
# Line 427 | Line 427 | public class ScheduledExecutorTest exten
427                      assertEquals(1, p.getPoolSize());
428                      done.await();
429                  }});
430 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
430 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
431              assertEquals(1, p.getPoolSize());
432          } finally {
433              done.countDown();
# Line 452 | Line 452 | public class ScheduledExecutorTest exten
452                          threadStarted.countDown();
453                          done.await();
454                      }});
455 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
455 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
456              assertEquals(TASKS, p.getTaskCount());
457          } finally {
458              done.countDown();
# Line 525 | Line 525 | public class ScheduledExecutorTest exten
525                      threadStarted.countDown();
526                      done.await();
527                  }});
528 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
528 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
529              assertFalse(p.isTerminating());
530              done.countDown();
531          } finally {
# Line 550 | Line 550 | public class ScheduledExecutorTest exten
550                      threadStarted.countDown();
551                      done.await();
552                  }});
553 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
553 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
554              assertFalse(p.isTerminating());
555              done.countDown();
556          } finally {
# Line 578 | Line 578 | public class ScheduledExecutorTest exten
578                      }};
579                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
580              }
581 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
581 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
582              BlockingQueue<Runnable> q = p.getQueue();
583              assertTrue(q.contains(tasks[tasks.length - 1]));
584              assertFalse(q.contains(tasks[0]));
# Line 605 | Line 605 | public class ScheduledExecutorTest exten
605                      }};
606                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
607              }
608 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
608 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
609              BlockingQueue<Runnable> q = p.getQueue();
610              assertFalse(p.remove((Runnable)tasks[0]));
611              assertTrue(q.contains((Runnable)tasks[4]));
# Line 656 | Line 656 | public class ScheduledExecutorTest exten
656       * shutdownNow returns a list containing tasks that were not run,
657       * and those tasks are drained from the queue
658       */
659 +    public void testShutdownNow() throws InterruptedException {
660 +        final int poolSize = 2;
661 +        final int count = 5;
662 +        final AtomicInteger ran = new AtomicInteger(0);
663 +        final ScheduledThreadPoolExecutor p =
664 +            new ScheduledThreadPoolExecutor(poolSize);
665 +        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
666 +        Runnable waiter = new CheckedRunnable() { public void realRun() {
667 +            threadsStarted.countDown();
668 +            try {
669 +                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
670 +            } catch (InterruptedException success) {}
671 +            ran.getAndIncrement();
672 +        }};
673 +        for (int i = 0; i < count; i++)
674 +            p.execute(waiter);
675 +        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
676 +        assertEquals(poolSize, p.getActiveCount());
677 +        assertEquals(0, p.getCompletedTaskCount());
678 +        final List<Runnable> queuedTasks;
679 +        try {
680 +            queuedTasks = p.shutdownNow();
681 +        } catch (SecurityException ok) {
682 +            return; // Allowed in case test doesn't have privs
683 +        }
684 +        assertTrue(p.isShutdown());
685 +        assertTrue(p.getQueue().isEmpty());
686 +        assertEquals(count - poolSize, queuedTasks.size());
687 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
688 +        assertTrue(p.isTerminated());
689 +        assertEquals(poolSize, ran.get());
690 +        assertEquals(poolSize, p.getCompletedTaskCount());
691 +    }
692 +
693 +    /**
694 +     * shutdownNow returns a list containing tasks that were not run,
695 +     * and those tasks are drained from the queue
696 +     */
697      public void testShutdownNow_delayedTasks() throws InterruptedException {
698          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
699          List<ScheduledFuture> tasks = new ArrayList<>();
# Line 665 | Line 703 | public class ScheduledExecutorTest exten
703              tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
704              tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
705          }
706 <        assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
706 >        if (testImplementationDetails)
707 >            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
708          final List<Runnable> queuedTasks;
709          try {
710              queuedTasks = p.shutdownNow();
# Line 674 | Line 713 | public class ScheduledExecutorTest exten
713          }
714          assertTrue(p.isShutdown());
715          assertTrue(p.getQueue().isEmpty());
716 <        assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
716 >        if (testImplementationDetails)
717 >            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
718          assertEquals(tasks.size(), queuedTasks.size());
719          for (ScheduledFuture task : tasks) {
720              assertFalse(task.isDone());
# Line 685 | Line 725 | public class ScheduledExecutorTest exten
725      }
726  
727      /**
728 <     * In default setting, shutdown cancels periodic but not delayed
729 <     * tasks at shutdown
730 <     */
731 <    public void testShutdown1() throws InterruptedException {
732 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
733 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
734 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
735 <
736 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
737 <        for (int i = 0; i < tasks.length; i++)
738 <            tasks[i] = p.schedule(new NoOpRunnable(),
739 <                                  SHORT_DELAY_MS, MILLISECONDS);
740 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
741 <        BlockingQueue<Runnable> q = p.getQueue();
742 <        for (ScheduledFuture task : tasks) {
743 <            assertFalse(task.isDone());
744 <            assertFalse(task.isCancelled());
745 <            assertTrue(q.contains(task));
746 <        }
747 <        assertTrue(p.isShutdown());
748 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
749 <        assertTrue(p.isTerminated());
750 <        for (ScheduledFuture task : tasks) {
751 <            assertTrue(task.isDone());
752 <            assertFalse(task.isCancelled());
753 <        }
754 <    }
755 <
756 <    /**
757 <     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
758 <     * delayed tasks are cancelled at shutdown
759 <     */
760 <    public void testShutdown2() throws InterruptedException {
761 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
762 <        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
763 <        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
764 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
765 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
766 <        for (int i = 0; i < tasks.length; i++)
767 <            tasks[i] = p.schedule(new NoOpRunnable(),
768 <                                  SHORT_DELAY_MS, MILLISECONDS);
769 <        BlockingQueue q = p.getQueue();
770 <        assertEquals(tasks.length, q.size());
771 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
772 <        assertTrue(p.isShutdown());
773 <        assertTrue(q.isEmpty());
774 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
775 <        assertTrue(p.isTerminated());
776 <        for (ScheduledFuture task : tasks) {
777 <            assertTrue(task.isDone());
778 <            assertTrue(task.isCancelled());
739 <        }
740 <    }
728 >     * By default, periodic tasks are cancelled at shutdown.
729 >     * By default, delayed tasks keep running after shutdown.
730 >     * Check that changing the default values work:
731 >     * - setExecuteExistingDelayedTasksAfterShutdownPolicy
732 >     * - setContinueExistingPeriodicTasksAfterShutdownPolicy
733 >     */
734 >    public void testShutdown_cancellation() throws Exception {
735 >        Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
736 >        for (Boolean policy : allBooleans)
737 >    {
738 >        final int poolSize = 2;
739 >        final ScheduledThreadPoolExecutor p
740 >            = new ScheduledThreadPoolExecutor(poolSize);
741 >        final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
742 >        final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
743 >        final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
744 >        if (policy != null) {
745 >            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
746 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
747 >            p.setRemoveOnCancelPolicy(policy);
748 >        }
749 >        assertEquals(effectiveDelayedPolicy,
750 >                     p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
751 >        assertEquals(effectivePeriodicPolicy,
752 >                     p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
753 >        assertEquals(effectiveRemovePolicy,
754 >                     p.getRemoveOnCancelPolicy());
755 >        // Strategy: Wedge the pool with poolSize "blocker" threads
756 >        final AtomicInteger ran = new AtomicInteger(0);
757 >        final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
758 >        final CountDownLatch unblock = new CountDownLatch(1);
759 >        final CountDownLatch periodicLatch1 = new CountDownLatch(2);
760 >        final CountDownLatch periodicLatch2 = new CountDownLatch(2);
761 >        Runnable task = new CheckedRunnable() { public void realRun()
762 >                                                    throws InterruptedException {
763 >            poolBlocked.countDown();
764 >            assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
765 >            ran.getAndIncrement();
766 >        }};
767 >        List<Future<?>> blockers = new ArrayList<>();
768 >        List<Future<?>> periodics = new ArrayList<>();
769 >        List<Future<?>> delayeds = new ArrayList<>();
770 >        for (int i = 0; i < poolSize; i++)
771 >            blockers.add(p.submit(task));
772 >        assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
773 >
774 >        periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
775 >                                            1, 1, MILLISECONDS));
776 >        periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
777 >                                               1, 1, MILLISECONDS));
778 >        delayeds.add(p.schedule(task, 1, MILLISECONDS));
779  
780 <    /**
781 <     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
744 <     * periodic tasks are cancelled at shutdown
745 <     */
746 <    public void testShutdown3() throws InterruptedException {
747 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
748 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
749 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
750 <        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
751 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
752 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
753 <        long initialDelay = LONG_DELAY_MS;
754 <        ScheduledFuture task =
755 <            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
756 <                                  5, MILLISECONDS);
780 >        assertTrue(p.getQueue().containsAll(periodics));
781 >        assertTrue(p.getQueue().containsAll(delayeds));
782          try { p.shutdown(); } catch (SecurityException ok) { return; }
783          assertTrue(p.isShutdown());
784 <        assertTrue(p.getQueue().isEmpty());
785 <        assertTrue(task.isDone());
786 <        assertTrue(task.isCancelled());
787 <        joinPool(p);
788 <    }
789 <
790 <    /**
791 <     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
792 <     * periodic tasks are not cancelled at shutdown
793 <     */
794 <    public void testShutdown4() throws InterruptedException {
795 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
796 <        final CountDownLatch counter = new CountDownLatch(2);
797 <        try {
798 <            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
799 <            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
800 <            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
801 <            final Runnable r = new CheckedRunnable() {
802 <                public void realRun() {
803 <                    counter.countDown();
804 <                }};
805 <            ScheduledFuture task =
781 <                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
782 <            assertFalse(task.isDone());
783 <            assertFalse(task.isCancelled());
784 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
785 <            assertFalse(task.isCancelled());
786 <            assertFalse(p.isTerminated());
787 <            assertTrue(p.isShutdown());
788 <            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
789 <            assertFalse(task.isCancelled());
790 <            assertTrue(task.cancel(false));
791 <            assertTrue(task.isDone());
792 <            assertTrue(task.isCancelled());
793 <            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
794 <            assertTrue(p.isTerminated());
784 >        assertFalse(p.isTerminated());
785 >        for (Future<?> periodic : periodics) {
786 >            assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
787 >            assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
788 >        }
789 >        for (Future<?> delayed : delayeds) {
790 >            assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
791 >            assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
792 >        }
793 >        if (testImplementationDetails) {
794 >            assertEquals(effectivePeriodicPolicy,
795 >                         p.getQueue().containsAll(periodics));
796 >            assertEquals(effectiveDelayedPolicy,
797 >                         p.getQueue().containsAll(delayeds));
798 >        }
799 >        // Release all pool threads
800 >        unblock.countDown();
801 >
802 >        for (Future<?> delayed : delayeds) {
803 >            if (effectiveDelayedPolicy) {
804 >                assertNull(delayed.get());
805 >            }
806          }
807 <        finally {
808 <            joinPool(p);
807 >        if (effectivePeriodicPolicy) {
808 >            assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
809 >            assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
810 >            for (Future<?> periodic : periodics) {
811 >                assertTrue(periodic.cancel(false));
812 >                assertTrue(periodic.isCancelled());
813 >                assertTrue(periodic.isDone());
814 >            }
815          }
816 <    }
816 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
817 >        assertTrue(p.isTerminated());
818 >        assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
819 >    }}
820  
821      /**
822       * completed submit of callable returns result
# Line 1187 | Line 1207 | public class ScheduledExecutorTest exten
1207              l.add(new StringTask());
1208              l.add(new StringTask());
1209              List<Future<String>> futures =
1210 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1210 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1211              assertEquals(2, futures.size());
1212              for (Future<String> future : futures)
1213                  assertSame(TEST_STRING, future.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines