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.82 by jsr166, Thu Sep 15 17:31:16 2016 UTC vs.
Revision 1.87 by jsr166, Sat Mar 25 23:35:15 2017 UTC

# Line 50 | Line 50 | public class ScheduledExecutorTest exten
50              final Runnable task = new CheckedRunnable() {
51                  public void realRun() { done.countDown(); }};
52              p.execute(task);
53 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
53 >            await(done);
54          }
55      }
56  
# Line 362 | Line 362 | public class ScheduledExecutorTest exten
362                  public void realRun() throws InterruptedException {
363                      threadStarted.countDown();
364                      assertEquals(0, p.getCompletedTaskCount());
365 <                    threadProceed.await();
365 >                    await(threadProceed);
366                      threadDone.countDown();
367                  }});
368              await(threadStarted);
369              assertEquals(0, p.getCompletedTaskCount());
370              threadProceed.countDown();
371 <            threadDone.await();
371 >            await(threadDone);
372              long startTime = System.nanoTime();
373              while (p.getCompletedTaskCount() != 1) {
374                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 507 | Line 507 | public class ScheduledExecutorTest exten
507      }
508  
509      /**
510 +     * The default rejected execution handler is AbortPolicy.
511 +     */
512 +    public void testDefaultRejectedExecutionHandler() {
513 +        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
514 +        try (PoolCleaner cleaner = cleaner(p)) {
515 +            assertTrue(p.getRejectedExecutionHandler()
516 +                       instanceof ThreadPoolExecutor.AbortPolicy);
517 +        }
518 +    }
519 +
520 +    /**
521       * isShutdown is false before shutdown, true after
522       */
523      public void testIsShutdown() {
# Line 733 | Line 744 | public class ScheduledExecutorTest exten
744       * - setContinueExistingPeriodicTasksAfterShutdownPolicy
745       */
746      public void testShutdown_cancellation() throws Exception {
747 <        Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
748 <        for (Boolean policy : allBooleans)
747 >        final int UNSPECIFIED = 0, YES = 1, NO = 2;
748 >        for (int maybe : new int[] { UNSPECIFIED, YES, NO })
749      {
750          final int poolSize = 2;
751          final ScheduledThreadPoolExecutor p
752              = new ScheduledThreadPoolExecutor(poolSize);
753 <        final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
754 <        final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
755 <        final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
756 <        if (policy != null) {
757 <            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
758 <            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
759 <            p.setRemoveOnCancelPolicy(policy);
753 >        final boolean effectiveDelayedPolicy  = (maybe != NO);
754 >        final boolean effectivePeriodicPolicy = (maybe == YES);
755 >        final boolean effectiveRemovePolicy   = (maybe == YES);
756 >        if (maybe != UNSPECIFIED) {
757 >            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(maybe == YES);
758 >            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(maybe == YES);
759 >            p.setRemoveOnCancelPolicy(maybe == YES);
760          }
761          assertEquals(effectiveDelayedPolicy,
762                       p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
# Line 762 | Line 773 | public class ScheduledExecutorTest exten
773          Runnable task = new CheckedRunnable() { public void realRun()
774                                                      throws InterruptedException {
775              poolBlocked.countDown();
776 <            assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
776 >            await(unblock);
777              ran.getAndIncrement();
778          }};
779          List<Future<?>> blockers = new ArrayList<>();
# Line 770 | Line 781 | public class ScheduledExecutorTest exten
781          List<Future<?>> delayeds = new ArrayList<>();
782          for (int i = 0; i < poolSize; i++)
783              blockers.add(p.submit(task));
784 <        assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
784 >        await(poolBlocked);
785  
786 <        periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
787 <                                            1, 1, MILLISECONDS));
788 <        periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
789 <                                               1, 1, MILLISECONDS));
786 >        periodics.add(p.scheduleAtFixedRate(
787 >                          countDowner(periodicLatch1), 1, 1, MILLISECONDS));
788 >        periodics.add(p.scheduleWithFixedDelay(
789 >                          countDowner(periodicLatch2), 1, 1, MILLISECONDS));
790          delayeds.add(p.schedule(task, 1, MILLISECONDS));
791  
792          assertTrue(p.getQueue().containsAll(periodics));
# Line 797 | Line 808 | public class ScheduledExecutorTest exten
808              assertEquals(effectiveDelayedPolicy,
809                           p.getQueue().containsAll(delayeds));
810          }
811 <        // Release all pool threads
801 <        unblock.countDown();
811 >        unblock.countDown();    // Release all pool threads
812  
813 <        for (Future<?> delayed : delayeds) {
814 <            if (effectiveDelayedPolicy) {
805 <                assertNull(delayed.get());
806 <            }
807 <        }
813 >        if (effectiveDelayedPolicy)
814 >            for (Future<?> delayed : delayeds) assertNull(delayed.get());
815          if (effectivePeriodicPolicy) {
816 <            assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
817 <            assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
816 >            await(periodicLatch1);
817 >            await(periodicLatch2);
818              for (Future<?> periodic : periodics) {
819                  assertTrue(periodic.cancel(false));
820                  assertTrue(periodic.isCancelled());
821                  assertTrue(periodic.isDone());
822              }
823          }
824 +        for (Future<?> blocker : blockers) assertNull(blocker.get());
825          assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
826          assertTrue(p.isTerminated());
827 +
828 +        for (Future<?> future : delayeds) {
829 +            assertTrue(effectiveDelayedPolicy ^ future.isCancelled());
830 +            assertTrue(future.isDone());
831 +        }
832 +        for (Future<?> future : periodics)
833 +            assertTrue(future.isCancelled());
834 +        for (Future<?> future : blockers)
835 +            assertNull(future.get());
836          assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
837      }}
838  
# Line 888 | Line 905 | public class ScheduledExecutorTest exten
905          CountDownLatch latch = new CountDownLatch(1);
906          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
907          try (PoolCleaner cleaner = cleaner(e)) {
908 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
908 >            List<Callable<String>> l = new ArrayList<>();
909              l.add(latchAwaitingStringTask(latch));
910              l.add(null);
911              try {
# Line 905 | Line 922 | public class ScheduledExecutorTest exten
922      public void testInvokeAny4() throws Exception {
923          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
924          try (PoolCleaner cleaner = cleaner(e)) {
925 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
925 >            List<Callable<String>> l = new ArrayList<>();
926              l.add(new NPETask());
927              try {
928                  e.invokeAny(l);
# Line 922 | Line 939 | public class ScheduledExecutorTest exten
939      public void testInvokeAny5() throws Exception {
940          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
941          try (PoolCleaner cleaner = cleaner(e)) {
942 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
942 >            List<Callable<String>> l = new ArrayList<>();
943              l.add(new StringTask());
944              l.add(new StringTask());
945              String result = e.invokeAny(l);
# Line 960 | Line 977 | public class ScheduledExecutorTest exten
977      public void testInvokeAll3() throws Exception {
978          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
979          try (PoolCleaner cleaner = cleaner(e)) {
980 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
980 >            List<Callable<String>> l = new ArrayList<>();
981              l.add(new StringTask());
982              l.add(null);
983              try {
# Line 976 | Line 993 | public class ScheduledExecutorTest exten
993      public void testInvokeAll4() throws Exception {
994          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
995          try (PoolCleaner cleaner = cleaner(e)) {
996 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
996 >            List<Callable<String>> l = new ArrayList<>();
997              l.add(new NPETask());
998              List<Future<String>> futures = e.invokeAll(l);
999              assertEquals(1, futures.size());
# Line 995 | Line 1012 | public class ScheduledExecutorTest exten
1012      public void testInvokeAll5() throws Exception {
1013          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1014          try (PoolCleaner cleaner = cleaner(e)) {
1015 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1015 >            List<Callable<String>> l = new ArrayList<>();
1016              l.add(new StringTask());
1017              l.add(new StringTask());
1018              List<Future<String>> futures = e.invokeAll(l);
# Line 1024 | Line 1041 | public class ScheduledExecutorTest exten
1041      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1042          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1043          try (PoolCleaner cleaner = cleaner(e)) {
1044 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1044 >            List<Callable<String>> l = new ArrayList<>();
1045              l.add(new StringTask());
1046              try {
1047                  e.invokeAny(l, MEDIUM_DELAY_MS, null);
# Line 1053 | Line 1070 | public class ScheduledExecutorTest exten
1070          CountDownLatch latch = new CountDownLatch(1);
1071          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1072          try (PoolCleaner cleaner = cleaner(e)) {
1073 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1073 >            List<Callable<String>> l = new ArrayList<>();
1074              l.add(latchAwaitingStringTask(latch));
1075              l.add(null);
1076              try {
# Line 1071 | Line 1088 | public class ScheduledExecutorTest exten
1088          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1089          try (PoolCleaner cleaner = cleaner(e)) {
1090              long startTime = System.nanoTime();
1091 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1091 >            List<Callable<String>> l = new ArrayList<>();
1092              l.add(new NPETask());
1093              try {
1094                  e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1090 | Line 1107 | public class ScheduledExecutorTest exten
1107          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1108          try (PoolCleaner cleaner = cleaner(e)) {
1109              long startTime = System.nanoTime();
1110 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1110 >            List<Callable<String>> l = new ArrayList<>();
1111              l.add(new StringTask());
1112              l.add(new StringTask());
1113              String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1118 | Line 1135 | public class ScheduledExecutorTest exten
1135      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1136          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1137          try (PoolCleaner cleaner = cleaner(e)) {
1138 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1138 >            List<Callable<String>> l = new ArrayList<>();
1139              l.add(new StringTask());
1140              try {
1141                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 1145 | Line 1162 | public class ScheduledExecutorTest exten
1162      public void testTimedInvokeAll3() throws Exception {
1163          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1164          try (PoolCleaner cleaner = cleaner(e)) {
1165 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1165 >            List<Callable<String>> l = new ArrayList<>();
1166              l.add(new StringTask());
1167              l.add(null);
1168              try {
# Line 1161 | Line 1178 | public class ScheduledExecutorTest exten
1178      public void testTimedInvokeAll4() throws Exception {
1179          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1180          try (PoolCleaner cleaner = cleaner(e)) {
1181 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1181 >            List<Callable<String>> l = new ArrayList<>();
1182              l.add(new NPETask());
1183              List<Future<String>> futures =
1184                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1181 | Line 1198 | public class ScheduledExecutorTest exten
1198      public void testTimedInvokeAll5() throws Exception {
1199          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1200          try (PoolCleaner cleaner = cleaner(e)) {
1201 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1201 >            List<Callable<String>> l = new ArrayList<>();
1202              l.add(new StringTask());
1203              l.add(new StringTask());
1204              List<Future<String>> futures =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines