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.40 by jsr166, Mon Sep 28 02:41:29 2015 UTC vs.
Revision 1.45 by jsr166, Sun Oct 4 02:15:08 2015 UTC

# Line 389 | Line 389 | public class ScheduledExecutorSubclassTe
389                      assertEquals(1, p.getActiveCount());
390                      done.await();
391                  }});
392 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
392 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
393              assertEquals(1, p.getActiveCount());
394          } finally {
395              done.countDown();
# Line 457 | Line 457 | public class ScheduledExecutorSubclassTe
457                          done.await();
458                          assertEquals(THREADS, p.getLargestPoolSize());
459                      }});
460 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
460 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
461              assertEquals(THREADS, p.getLargestPoolSize());
462          } finally {
463              done.countDown();
# Line 482 | Line 482 | public class ScheduledExecutorSubclassTe
482                      assertEquals(1, p.getPoolSize());
483                      done.await();
484                  }});
485 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
485 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
486              assertEquals(1, p.getPoolSize());
487          } finally {
488              done.countDown();
# Line 507 | Line 507 | public class ScheduledExecutorSubclassTe
507                          threadStarted.countDown();
508                          done.await();
509                      }});
510 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
510 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
511              assertEquals(TASKS, p.getTaskCount());
512          } finally {
513              done.countDown();
# Line 579 | Line 579 | public class ScheduledExecutorSubclassTe
579                      threadStarted.countDown();
580                      done.await();
581                  }});
582 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
582 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
583              assertFalse(p.isTerminating());
584              done.countDown();
585          } finally {
# Line 604 | Line 604 | public class ScheduledExecutorSubclassTe
604                      threadStarted.countDown();
605                      done.await();
606                  }});
607 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
607 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
608              assertFalse(p.isTerminating());
609              done.countDown();
610          } finally {
# Line 632 | Line 632 | public class ScheduledExecutorSubclassTe
632                      }};
633                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
634              }
635 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
635 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
636              BlockingQueue<Runnable> q = p.getQueue();
637              assertTrue(q.contains(tasks[tasks.length - 1]));
638              assertFalse(q.contains(tasks[0]));
# Line 659 | Line 659 | public class ScheduledExecutorSubclassTe
659                      }};
660                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
661              }
662 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
662 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
663              BlockingQueue<Runnable> q = p.getQueue();
664              assertFalse(p.remove((Runnable)tasks[0]));
665              assertTrue(q.contains((Runnable)tasks[4]));
# Line 716 | Line 716 | public class ScheduledExecutorSubclassTe
716          final AtomicInteger ran = new AtomicInteger(0);
717          final CustomExecutor p = new CustomExecutor(poolSize);
718          CountDownLatch threadsStarted = new CountDownLatch(poolSize);
719 <        CheckedRunnable waiter = new CheckedRunnable() { public void realRun() {
719 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
720              threadsStarted.countDown();
721              try {
722                  MILLISECONDS.sleep(2 * LONG_DELAY_MS);
# Line 726 | Line 726 | public class ScheduledExecutorSubclassTe
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();
# Line 738 | Line 740 | public class ScheduledExecutorSubclassTe
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      /**
# Line 776 | Line 779 | public class ScheduledExecutorSubclassTe
779      }
780  
781      /**
782 <     * In default setting, shutdown cancels periodic but not delayed
783 <     * tasks at shutdown
784 <     */
785 <    public void testShutdown1() throws InterruptedException {
786 <        CustomExecutor p = new CustomExecutor(1);
787 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
788 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
789 <
790 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
791 <        for (int i = 0; i < tasks.length; i++)
792 <            tasks[i] = p.schedule(new NoOpRunnable(),
793 <                                  SHORT_DELAY_MS, MILLISECONDS);
794 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
795 <        BlockingQueue<Runnable> q = p.getQueue();
796 <        for (ScheduledFuture task : tasks) {
797 <            assertFalse(task.isDone());
798 <            assertFalse(task.isCancelled());
799 <            assertTrue(q.contains(task));
800 <        }
801 <        assertTrue(p.isShutdown());
802 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
803 <        assertTrue(p.isTerminated());
804 <        for (ScheduledFuture task : tasks) {
805 <            assertTrue(task.isDone());
806 <            assertFalse(task.isCancelled());
807 <        }
808 <    }
809 <
810 <    /**
811 <     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
812 <     * delayed tasks are cancelled at shutdown
813 <     */
814 <    public void testShutdown2() throws InterruptedException {
815 <        CustomExecutor p = new CustomExecutor(1);
816 <        p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
817 <        assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
818 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
819 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
820 <        for (int i = 0; i < tasks.length; i++)
821 <            tasks[i] = p.schedule(new NoOpRunnable(),
822 <                                  SHORT_DELAY_MS, MILLISECONDS);
823 <        BlockingQueue q = p.getQueue();
824 <        assertEquals(tasks.length, q.size());
825 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
826 <        assertTrue(p.isShutdown());
827 <        assertTrue(q.isEmpty());
828 <        assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
829 <        assertTrue(p.isTerminated());
830 <        for (ScheduledFuture task : tasks) {
831 <            assertTrue(task.isDone());
829 <            assertTrue(task.isCancelled());
830 <        }
831 <    }
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 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 <    /**
834 <     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
835 <     * periodic tasks are cancelled at shutdown
836 <     */
837 <    public void testShutdown3() throws InterruptedException {
838 <        CustomExecutor p = new CustomExecutor(1);
839 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
840 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
841 <        p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
842 <        assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
843 <        assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
844 <        long initialDelay = LONG_DELAY_MS;
845 <        ScheduledFuture task =
846 <            p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay,
847 <                                  5, MILLISECONDS);
833 >        assertTrue(p.getQueue().containsAll(periodics));
834 >        assertTrue(p.getQueue().containsAll(delayeds));
835          try { p.shutdown(); } catch (SecurityException ok) { return; }
836          assertTrue(p.isShutdown());
837 <        assertTrue(p.getQueue().isEmpty());
838 <        assertTrue(task.isDone());
839 <        assertTrue(task.isCancelled());
840 <        joinPool(p);
841 <    }
842 <
843 <    /**
844 <     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
845 <     * periodic tasks are not cancelled at shutdown
846 <     */
847 <    public void testShutdown4() throws InterruptedException {
848 <        CustomExecutor p = new CustomExecutor(1);
849 <        final CountDownLatch counter = new CountDownLatch(2);
850 <        try {
851 <            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
852 <            assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
853 <            assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
854 <            final Runnable r = new CheckedRunnable() {
855 <                public void realRun() {
856 <                    counter.countDown();
857 <                }};
858 <            ScheduledFuture task =
872 <                p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS);
873 <            assertFalse(task.isDone());
874 <            assertFalse(task.isCancelled());
875 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
876 <            assertFalse(task.isCancelled());
877 <            assertFalse(p.isTerminated());
878 <            assertTrue(p.isShutdown());
879 <            assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS));
880 <            assertFalse(task.isCancelled());
881 <            assertTrue(task.cancel(false));
882 <            assertTrue(task.isDone());
883 <            assertTrue(task.isCancelled());
884 <            assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS));
885 <            assertTrue(p.isTerminated());
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 <        finally {
861 <            joinPool(p);
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 <    }
869 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
870 >        assertTrue(p.isTerminated());
871 >        assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
872 >    }}
873  
874      /**
875       * completed submit of callable returns result
# Line 1278 | Line 1260 | public class ScheduledExecutorSubclassTe
1260              l.add(new StringTask());
1261              l.add(new StringTask());
1262              List<Future<String>> futures =
1263 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1263 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1264              assertEquals(2, futures.size());
1265              for (Future<String> future : futures)
1266                  assertSame(TEST_STRING, future.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines