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.39 by jsr166, Mon Sep 28 02:32:57 2015 UTC vs.
Revision 1.42 by jsr166, Mon Sep 28 08:23:49 2015 UTC

# Line 710 | Line 710 | public class ScheduledExecutorSubclassTe
710       * shutdownNow returns a list containing tasks that were not run,
711       * and those tasks are drained from the queue
712       */
713 +    public void testShutdownNow() throws InterruptedException {
714 +        final int poolSize = 2;
715 +        final int count = 5;
716 +        final AtomicInteger ran = new AtomicInteger(0);
717 +        final CustomExecutor p = new CustomExecutor(poolSize);
718 +        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
719 +        Runnable waiter = new CheckedRunnable() { public void realRun() {
720 +            threadsStarted.countDown();
721 +            try {
722 +                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
723 +            } catch (InterruptedException success) {}
724 +            ran.getAndIncrement();
725 +        }};
726 +        for (int i = 0; i < count; i++)
727 +            p.execute(waiter);
728 +        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
729 +        assertEquals(poolSize, p.getActiveCount());
730 +        assertEquals(0, p.getCompletedTaskCount());
731 +        final List<Runnable> queuedTasks;
732 +        try {
733 +            queuedTasks = p.shutdownNow();
734 +        } catch (SecurityException ok) {
735 +            return; // Allowed in case test doesn't have privs
736 +        }
737 +        assertTrue(p.isShutdown());
738 +        assertTrue(p.getQueue().isEmpty());
739 +        assertEquals(count - poolSize, queuedTasks.size());
740 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
741 +        assertTrue(p.isTerminated());
742 +        assertEquals(poolSize, ran.get());
743 +        assertEquals(poolSize, p.getCompletedTaskCount());
744 +    }
745 +
746 +    /**
747 +     * shutdownNow returns a list containing tasks that were not run,
748 +     * and those tasks are drained from the queue
749 +     */
750      public void testShutdownNow_delayedTasks() throws InterruptedException {
751          CustomExecutor p = new CustomExecutor(1);
752          List<ScheduledFuture> tasks = new ArrayList<>();
# Line 742 | Line 779 | public class ScheduledExecutorSubclassTe
779      }
780  
781      /**
782 <     * In default setting, shutdown cancels periodic but not delayed
783 <     * tasks at shutdown
782 >     * By default, periodic tasks are cancelled at shutdown.
783 >     * By default, delayed tasks keep running after shutdown.
784 >     * Check that changing the default values work:
785 >     * - setExecuteExistingDelayedTasksAfterShutdownPolicy
786 >     * - setContinueExistingPeriodicTasksAfterShutdownPolicy
787       */
788 <    public void testShutdown1() throws InterruptedException {
789 <        CustomExecutor p = new CustomExecutor(1);
790 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
791 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
788 >    public void testShutdown_cancellation() throws Exception {
789 >        Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
790 >        for (Boolean policy : allBooleans)
791 >    {
792 >        final int poolSize = 2;
793 >        final CustomExecutor p = new CustomExecutor(poolSize);
794 >        final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
795 >        final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
796 >        final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
797 >        if (policy != null) {
798 >            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
799 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
800 >            p.setRemoveOnCancelPolicy(policy);
801 >        }
802 >        assertEquals(effectiveDelayedPolicy,
803 >                     p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
804 >        assertEquals(effectivePeriodicPolicy,
805 >                     p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
806 >        assertEquals(effectiveRemovePolicy,
807 >                     p.getRemoveOnCancelPolicy());
808 >        // Strategy: Wedge the pool with poolSize "blocker" threads
809 >        final AtomicInteger ran = new AtomicInteger(0);
810 >        final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
811 >        final CountDownLatch unblock = new CountDownLatch(1);
812 >        final CountDownLatch periodicLatch1 = new CountDownLatch(2);
813 >        final CountDownLatch periodicLatch2 = new CountDownLatch(2);
814 >        Runnable task = new CheckedRunnable() { public void realRun()
815 >                                                    throws InterruptedException {
816 >            poolBlocked.countDown();
817 >            assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
818 >            ran.getAndIncrement();
819 >        }};
820 >        List<Future<?>> blockers = new ArrayList<>();
821 >        List<Future<?>> periodics = new ArrayList<>();
822 >        List<Future<?>> delayeds = new ArrayList<>();
823 >        for (int i = 0; i < poolSize; i++)
824 >            blockers.add(p.submit(task));
825 >        assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
826 >
827 >        periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
828 >                                            1, 1, MILLISECONDS));
829 >        periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
830 >                                               1, 1, MILLISECONDS));
831 >        delayeds.add(p.schedule(task, 1, MILLISECONDS));
832  
833 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
834 <        for (int i = 0; i < tasks.length; i++)
755 <            tasks[i] = p.schedule(new NoOpRunnable(),
756 <                                  SHORT_DELAY_MS, MILLISECONDS);
833 >        assertTrue(p.getQueue().containsAll(periodics));
834 >        assertTrue(p.getQueue().containsAll(delayeds));
835          try { p.shutdown(); } catch (SecurityException ok) { return; }
758        BlockingQueue<Runnable> q = p.getQueue();
759        for (ScheduledFuture task : tasks) {
760            assertFalse(task.isDone());
761            assertFalse(task.isCancelled());
762            assertTrue(q.contains(task));
763        }
836          assertTrue(p.isShutdown());
837 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
838 <        assertTrue(p.isTerminated());
839 <        for (ScheduledFuture task : tasks) {
840 <            assertTrue(task.isDone());
841 <            assertFalse(task.isCancelled());
837 >        assertFalse(p.isTerminated());
838 >        for (Future<?> periodic : periodics) {
839 >            assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
840 >            assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
841 >        }
842 >        for (Future<?> delayed : delayeds) {
843 >            assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
844 >            assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
845 >        }
846 >        if (testImplementationDetails) {
847 >            assertEquals(effectivePeriodicPolicy,
848 >                         p.getQueue().containsAll(periodics));
849 >            assertEquals(effectiveDelayedPolicy,
850 >                         p.getQueue().containsAll(delayeds));
851 >        }
852 >        // Release all pool threads
853 >        unblock.countDown();
854 >
855 >        for (Future<?> delayed : delayeds) {
856 >            if (effectiveDelayedPolicy) {
857 >                assertNull(delayed.get());
858 >            }
859 >        }
860 >        if (effectivePeriodicPolicy) {
861 >            assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
862 >            assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
863 >            for (Future<?> periodic : periodics) {
864 >                assertTrue(periodic.cancel(false));
865 >                assertTrue(periodic.isCancelled());
866 >                assertTrue(periodic.isDone());
867 >            }
868          }
869 <    }
772 <
773 <    /**
774 <     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
775 <     * delayed tasks are cancelled at shutdown
776 <     */
777 <    public void testShutdown2() throws InterruptedException {
778 <        CustomExecutor p = new CustomExecutor(1);
779 <        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
780 <        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
781 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
782 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
783 <        for (int i = 0; i < tasks.length; i++)
784 <            tasks[i] = p.schedule(new NoOpRunnable(),
785 <                                  SHORT_DELAY_MS, MILLISECONDS);
786 <        BlockingQueue q = p.getQueue();
787 <        assertEquals(tasks.length, q.size());
788 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
789 <        assertTrue(p.isShutdown());
790 <        assertTrue(q.isEmpty());
791 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
869 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
870          assertTrue(p.isTerminated());
871 <        for (ScheduledFuture task : tasks) {
872 <            assertTrue(task.isDone());
795 <            assertTrue(task.isCancelled());
796 <        }
797 <    }
798 <
799 <    /**
800 <     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
801 <     * periodic tasks are cancelled at shutdown
802 <     */
803 <    public void testShutdown3() throws InterruptedException {
804 <        CustomExecutor p = new CustomExecutor(1);
805 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
806 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
807 <        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
808 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
809 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
810 <        long initialDelay = LONG_DELAY_MS;
811 <        ScheduledFuture task =
812 <            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
813 <                                  5, MILLISECONDS);
814 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
815 <        assertTrue(p.isShutdown());
816 <        assertTrue(p.getQueue().isEmpty());
817 <        assertTrue(task.isDone());
818 <        assertTrue(task.isCancelled());
819 <        joinPool(p);
820 <    }
821 <
822 <    /**
823 <     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
824 <     * periodic tasks are not cancelled at shutdown
825 <     */
826 <    public void testShutdown4() throws InterruptedException {
827 <        CustomExecutor p = new CustomExecutor(1);
828 <        final CountDownLatch counter = new CountDownLatch(2);
829 <        try {
830 <            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
831 <            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
832 <            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
833 <            final Runnable r = new CheckedRunnable() {
834 <                public void realRun() {
835 <                    counter.countDown();
836 <                }};
837 <            ScheduledFuture task =
838 <                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
839 <            assertFalse(task.isDone());
840 <            assertFalse(task.isCancelled());
841 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
842 <            assertFalse(task.isCancelled());
843 <            assertFalse(p.isTerminated());
844 <            assertTrue(p.isShutdown());
845 <            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
846 <            assertFalse(task.isCancelled());
847 <            assertTrue(task.cancel(false));
848 <            assertTrue(task.isDone());
849 <            assertTrue(task.isCancelled());
850 <            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
851 <            assertTrue(p.isTerminated());
852 <        }
853 <        finally {
854 <            joinPool(p);
855 <        }
856 <    }
871 >        assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
872 >    }}
873  
874      /**
875       * completed submit of callable returns result

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines