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.76 by jsr166, Thu Oct 8 03:08:37 2015 UTC vs.
Revision 1.93 by jsr166, Mon May 29 19:15:03 2017 UTC

# Line 7 | Line 7
7   */
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
11   import static java.util.concurrent.TimeUnit.SECONDS;
12  
13   import java.util.ArrayList;
# Line 17 | Line 18 | import java.util.concurrent.Callable;
18   import java.util.concurrent.CancellationException;
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.Future;
23   import java.util.concurrent.RejectedExecutionException;
24   import java.util.concurrent.ScheduledFuture;
25   import java.util.concurrent.ScheduledThreadPoolExecutor;
26   import java.util.concurrent.ThreadFactory;
27 + import java.util.concurrent.ThreadLocalRandom;
28   import java.util.concurrent.ThreadPoolExecutor;
29 + import java.util.concurrent.atomic.AtomicBoolean;
30   import java.util.concurrent.atomic.AtomicInteger;
31 + import java.util.concurrent.atomic.AtomicLong;
32 + import java.util.stream.Stream;
33  
34   import junit.framework.Test;
35   import junit.framework.TestSuite;
# Line 48 | Line 52 | public class ScheduledExecutorTest exten
52              final Runnable task = new CheckedRunnable() {
53                  public void realRun() { done.countDown(); }};
54              p.execute(task);
55 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
55 >            await(done);
56          }
57      }
58  
# Line 69 | Line 73 | public class ScheduledExecutorTest exten
73              Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
74              assertSame(Boolean.TRUE, f.get());
75              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
76 <            assertTrue(done.await(0L, MILLISECONDS));
76 >            assertEquals(0L, done.getCount());
77          }
78      }
79  
# Line 143 | Line 147 | public class ScheduledExecutorTest exten
147      }
148  
149      /**
150 <     * scheduleAtFixedRate executes series of tasks at given rate
150 >     * scheduleAtFixedRate executes series of tasks at given rate.
151 >     * Eventually, it must hold that:
152 >     *   cycles - 1 <= elapsedMillis/delay < cycles
153       */
154      public void testFixedRateSequence() throws InterruptedException {
155          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
156          try (PoolCleaner cleaner = cleaner(p)) {
157              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
158 <                long startTime = System.nanoTime();
159 <                int cycles = 10;
158 >                final long startTime = System.nanoTime();
159 >                final int cycles = 8;
160                  final CountDownLatch done = new CountDownLatch(cycles);
161 <                Runnable task = new CheckedRunnable() {
161 >                final Runnable task = new CheckedRunnable() {
162                      public void realRun() { done.countDown(); }};
163 <                ScheduledFuture h =
163 >                final ScheduledFuture periodicTask =
164                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
165 <                await(done);
166 <                h.cancel(true);
167 <                double normalizedTime =
168 <                    (double) millisElapsedSince(startTime) / delay;
169 <                if (normalizedTime >= cycles - 1 &&
170 <                    normalizedTime <= cycles)
165 >                final int totalDelayMillis = (cycles - 1) * delay;
166 >                await(done, totalDelayMillis + LONG_DELAY_MS);
167 >                periodicTask.cancel(true);
168 >                final long elapsedMillis = millisElapsedSince(startTime);
169 >                assertTrue(elapsedMillis >= totalDelayMillis);
170 >                if (elapsedMillis <= cycles * delay)
171                      return;
172 +                // else retry with longer delay
173              }
174 <            throw new AssertionError("unexpected execution rate");
174 >            fail("unexpected execution rate");
175          }
176      }
177  
178      /**
179 <     * scheduleWithFixedDelay executes series of tasks with given period
179 >     * scheduleWithFixedDelay executes series of tasks with given period.
180 >     * Eventually, it must hold that each task starts at least delay and at
181 >     * most 2 * delay after the termination of the previous task.
182       */
183      public void testFixedDelaySequence() throws InterruptedException {
184          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
185          try (PoolCleaner cleaner = cleaner(p)) {
186              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
187 <                long startTime = System.nanoTime();
188 <                int cycles = 10;
187 >                final long startTime = System.nanoTime();
188 >                final AtomicLong previous = new AtomicLong(startTime);
189 >                final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
190 >                final int cycles = 8;
191                  final CountDownLatch done = new CountDownLatch(cycles);
192 <                Runnable task = new CheckedRunnable() {
193 <                    public void realRun() { done.countDown(); }};
194 <                ScheduledFuture h =
192 >                final int d = delay;
193 >                final Runnable task = new CheckedRunnable() {
194 >                    public void realRun() {
195 >                        long now = System.nanoTime();
196 >                        long elapsedMillis
197 >                            = NANOSECONDS.toMillis(now - previous.get());
198 >                        if (done.getCount() == cycles) { // first execution
199 >                            if (elapsedMillis >= d)
200 >                                tryLongerDelay.set(true);
201 >                        } else {
202 >                            assertTrue(elapsedMillis >= d);
203 >                            if (elapsedMillis >= 2 * d)
204 >                                tryLongerDelay.set(true);
205 >                        }
206 >                        previous.set(now);
207 >                        done.countDown();
208 >                    }};
209 >                final ScheduledFuture periodicTask =
210                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
211 <                await(done);
212 <                h.cancel(true);
213 <                double normalizedTime =
214 <                    (double) millisElapsedSince(startTime) / delay;
215 <                if (normalizedTime >= cycles - 1 &&
216 <                    normalizedTime <= cycles)
211 >                final int totalDelayMillis = (cycles - 1) * delay;
212 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
213 >                periodicTask.cancel(true);
214 >                final long elapsedMillis = millisElapsedSince(startTime);
215 >                assertTrue(elapsedMillis >= totalDelayMillis);
216 >                if (!tryLongerDelay.get())
217                      return;
218 +                // else retry with longer delay
219              }
220 <            throw new AssertionError("unexpected execution rate");
220 >            fail("unexpected execution rate");
221          }
222      }
223  
# Line 214 | Line 241 | public class ScheduledExecutorTest exten
241          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
242          try (PoolCleaner cleaner = cleaner(p)) {
243              try {
244 <                TrackedCallable callable = null;
245 <                Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
244 >                Future f = p.schedule((Callable)null,
245 >                                      randomTimeout(), randomTimeUnit());
246                  shouldThrow();
247              } catch (NullPointerException success) {}
248          }
# Line 337 | Line 364 | public class ScheduledExecutorTest exten
364                  public void realRun() throws InterruptedException {
365                      threadStarted.countDown();
366                      assertEquals(0, p.getCompletedTaskCount());
367 <                    threadProceed.await();
367 >                    await(threadProceed);
368                      threadDone.countDown();
369                  }});
370              await(threadStarted);
371              assertEquals(0, p.getCompletedTaskCount());
372              threadProceed.countDown();
373 <            threadDone.await();
373 >            await(threadDone);
374              long startTime = System.nanoTime();
375              while (p.getCompletedTaskCount() != 1) {
376                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 482 | Line 509 | public class ScheduledExecutorTest exten
509      }
510  
511      /**
512 +     * The default rejected execution handler is AbortPolicy.
513 +     */
514 +    public void testDefaultRejectedExecutionHandler() {
515 +        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
516 +        try (PoolCleaner cleaner = cleaner(p)) {
517 +            assertTrue(p.getRejectedExecutionHandler()
518 +                       instanceof ThreadPoolExecutor.AbortPolicy);
519 +        }
520 +    }
521 +
522 +    /**
523       * isShutdown is false before shutdown, true after
524       */
525      public void testIsShutdown() {
488
526          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
527 <        try {
528 <            assertFalse(p.isShutdown());
529 <        }
530 <        finally {
531 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
527 >        assertFalse(p.isShutdown());
528 >        try (PoolCleaner cleaner = cleaner(p)) {
529 >            try {
530 >                p.shutdown();
531 >                assertTrue(p.isShutdown());
532 >            } catch (SecurityException ok) {}
533          }
496        assertTrue(p.isShutdown());
534      }
535  
536      /**
# Line 709 | Line 746 | public class ScheduledExecutorTest exten
746       * - setContinueExistingPeriodicTasksAfterShutdownPolicy
747       */
748      public void testShutdown_cancellation() throws Exception {
749 <        Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
713 <        for (Boolean policy : allBooleans)
714 <    {
715 <        final int poolSize = 2;
749 >        final int poolSize = 4;
750          final ScheduledThreadPoolExecutor p
751              = new ScheduledThreadPoolExecutor(poolSize);
752 <        final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
753 <        final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
754 <        final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
755 <        if (policy != null) {
756 <            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
757 <            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
758 <            p.setRemoveOnCancelPolicy(policy);
759 <        }
752 >        final BlockingQueue<Runnable> q = p.getQueue();
753 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
754 >        final long delay = rnd.nextInt(2);
755 >        final int rounds = rnd.nextInt(1, 3);
756 >        final boolean effectiveDelayedPolicy;
757 >        final boolean effectivePeriodicPolicy;
758 >        final boolean effectiveRemovePolicy;
759 >
760 >        if (rnd.nextBoolean())
761 >            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(
762 >                effectiveDelayedPolicy = rnd.nextBoolean());
763 >        else
764 >            effectiveDelayedPolicy = true;
765          assertEquals(effectiveDelayedPolicy,
766                       p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
767 +
768 +        if (rnd.nextBoolean())
769 +            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(
770 +                effectivePeriodicPolicy = rnd.nextBoolean());
771 +        else
772 +            effectivePeriodicPolicy = false;
773          assertEquals(effectivePeriodicPolicy,
774                       p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
775 +
776 +        if (rnd.nextBoolean())
777 +            p.setRemoveOnCancelPolicy(
778 +                effectiveRemovePolicy = rnd.nextBoolean());
779 +        else
780 +            effectiveRemovePolicy = false;
781          assertEquals(effectiveRemovePolicy,
782                       p.getRemoveOnCancelPolicy());
783 <        // Strategy: Wedge the pool with poolSize "blocker" threads
783 >
784 >        final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean();
785 >
786 >        // Strategy: Wedge the pool with one wave of "blocker" tasks,
787 >        // then add a second wave that waits in the queue until unblocked.
788          final AtomicInteger ran = new AtomicInteger(0);
789          final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
790          final CountDownLatch unblock = new CountDownLatch(1);
791 <        final CountDownLatch periodicLatch1 = new CountDownLatch(2);
737 <        final CountDownLatch periodicLatch2 = new CountDownLatch(2);
738 <        Runnable task = new CheckedRunnable() { public void realRun()
739 <                                                    throws InterruptedException {
740 <            poolBlocked.countDown();
741 <            assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
742 <            ran.getAndIncrement();
743 <        }};
744 <        List<Future<?>> blockers = new ArrayList<>();
745 <        List<Future<?>> periodics = new ArrayList<>();
746 <        List<Future<?>> delayeds = new ArrayList<>();
747 <        for (int i = 0; i < poolSize; i++)
748 <            blockers.add(p.submit(task));
749 <        assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
750 <
751 <        periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
752 <                                            1, 1, MILLISECONDS));
753 <        periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
754 <                                               1, 1, MILLISECONDS));
755 <        delayeds.add(p.schedule(task, 1, MILLISECONDS));
791 >        final RuntimeException exception = new RuntimeException();
792  
793 <        assertTrue(p.getQueue().containsAll(periodics));
794 <        assertTrue(p.getQueue().containsAll(delayeds));
795 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
796 <        assertTrue(p.isShutdown());
797 <        assertFalse(p.isTerminated());
798 <        for (Future<?> periodic : periodics) {
799 <            assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
764 <            assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
765 <        }
766 <        for (Future<?> delayed : delayeds) {
767 <            assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
768 <            assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
769 <        }
770 <        if (testImplementationDetails) {
771 <            assertEquals(effectivePeriodicPolicy,
772 <                         p.getQueue().containsAll(periodics));
773 <            assertEquals(effectiveDelayedPolicy,
774 <                         p.getQueue().containsAll(delayeds));
775 <        }
776 <        // Release all pool threads
777 <        unblock.countDown();
778 <
779 <        for (Future<?> delayed : delayeds) {
780 <            if (effectiveDelayedPolicy) {
781 <                assertNull(delayed.get());
793 >        class Task implements Runnable {
794 >            public void run() {
795 >                try {
796 >                    ran.getAndIncrement();
797 >                    poolBlocked.countDown();
798 >                    await(unblock);
799 >                } catch (Throwable fail) { threadUnexpectedException(fail); }
800              }
801          }
802 <        if (effectivePeriodicPolicy) {
803 <            assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
804 <            assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
805 <            for (Future<?> periodic : periodics) {
806 <                assertTrue(periodic.cancel(false));
807 <                assertTrue(periodic.isCancelled());
808 <                assertTrue(periodic.isDone());
802 >
803 >        class PeriodicTask extends Task {
804 >            PeriodicTask(int rounds) { this.rounds = rounds; }
805 >            int rounds;
806 >            public void run() {
807 >                if (--rounds == 0) super.run();
808 >                // throw exception to surely terminate this periodic task,
809 >                // but in a separate execution and in a detectable way.
810 >                if (rounds == -1) throw exception;
811              }
812          }
813 +
814 +        Runnable task = new Task();
815 +
816 +        List<Future<?>> immediates = new ArrayList<>();
817 +        List<Future<?>> delayeds   = new ArrayList<>();
818 +        List<Future<?>> periodics  = new ArrayList<>();
819 +
820 +        immediates.add(p.submit(task));
821 +        delayeds.add(p.schedule(task, delay, MILLISECONDS));
822 +        periodics.add(p.scheduleAtFixedRate(
823 +                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
824 +        periodics.add(p.scheduleWithFixedDelay(
825 +                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
826 +
827 +        await(poolBlocked);
828 +
829 +        assertEquals(poolSize, ran.get());
830 +        assertEquals(poolSize, p.getActiveCount());
831 +        assertTrue(q.isEmpty());
832 +
833 +        // Add second wave of tasks.
834 +        immediates.add(p.submit(task));
835 +        delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS));
836 +        periodics.add(p.scheduleAtFixedRate(
837 +                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
838 +        periodics.add(p.scheduleWithFixedDelay(
839 +                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
840 +
841 +        assertEquals(poolSize, q.size());
842 +        assertEquals(poolSize, ran.get());
843 +
844 +        immediates.forEach(
845 +            f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L));
846 +
847 +        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
848 +            .forEach(f -> assertFalse(f.isDone()));
849 +
850 +        try { p.shutdown(); } catch (SecurityException ok) { return; }
851 +        assertTrue(p.isShutdown());
852 +        assertTrue(p.isTerminating());
853 +        assertFalse(p.isTerminated());
854 +
855 +        if (rnd.nextBoolean())
856 +            assertThrows(
857 +                RejectedExecutionException.class,
858 +                () -> p.submit(task),
859 +                () -> p.schedule(task, 1, SECONDS),
860 +                () -> p.scheduleAtFixedRate(
861 +                    new PeriodicTask(1), 1, 1, SECONDS),
862 +                () -> p.scheduleWithFixedDelay(
863 +                    new PeriodicTask(2), 1, 1, SECONDS));
864 +
865 +        assertTrue(q.contains(immediates.get(1)));
866 +        assertTrue(!effectiveDelayedPolicy
867 +                   ^ q.contains(delayeds.get(1)));
868 +        assertTrue(!effectivePeriodicPolicy
869 +                   ^ q.containsAll(periodics.subList(2, 4)));
870 +
871 +        immediates.forEach(f -> assertFalse(f.isDone()));
872 +
873 +        assertFalse(delayeds.get(0).isDone());
874 +        if (effectiveDelayedPolicy)
875 +            assertFalse(delayeds.get(1).isDone());
876 +        else
877 +            assertTrue(delayeds.get(1).isCancelled());
878 +
879 +        if (effectivePeriodicPolicy)
880 +            periodics.forEach(
881 +                f -> {
882 +                    assertFalse(f.isDone());
883 +                    if (!periodicTasksContinue) {
884 +                        assertTrue(f.cancel(false));
885 +                        assertTrue(f.isCancelled());
886 +                    }
887 +                });
888 +        else {
889 +            periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone()));
890 +            periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled()));
891 +        }
892 +
893 +        unblock.countDown();    // Release all pool threads
894 +
895          assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
896 +        assertFalse(p.isTerminating());
897          assertTrue(p.isTerminated());
898 <        assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
899 <    }}
898 >
899 >        assertTrue(q.isEmpty());
900 >
901 >        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
902 >            .forEach(f -> assertTrue(f.isDone()));
903 >
904 >        for (Future<?> f : immediates) assertNull(f.get());
905 >
906 >        assertNull(delayeds.get(0).get());
907 >        if (effectiveDelayedPolicy)
908 >            assertNull(delayeds.get(1).get());
909 >        else
910 >            assertTrue(delayeds.get(1).isCancelled());
911 >
912 >        if (periodicTasksContinue)
913 >            periodics.forEach(
914 >                f -> {
915 >                    try { f.get(); }
916 >                    catch (ExecutionException success) {
917 >                        assertSame(exception, success.getCause());
918 >                    }
919 >                    catch (Throwable fail) { threadUnexpectedException(fail); }
920 >                });
921 >        else
922 >            periodics.forEach(f -> assertTrue(f.isCancelled()));
923 >
924 >        assertEquals(poolSize + 1
925 >                     + (effectiveDelayedPolicy ? 1 : 0)
926 >                     + (periodicTasksContinue ? 2 : 0),
927 >                     ran.get());
928 >    }
929  
930      /**
931       * completed submit of callable returns result
# Line 864 | Line 996 | public class ScheduledExecutorTest exten
996          CountDownLatch latch = new CountDownLatch(1);
997          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
998          try (PoolCleaner cleaner = cleaner(e)) {
999 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
999 >            List<Callable<String>> l = new ArrayList<>();
1000              l.add(latchAwaitingStringTask(latch));
1001              l.add(null);
1002              try {
# Line 881 | Line 1013 | public class ScheduledExecutorTest exten
1013      public void testInvokeAny4() throws Exception {
1014          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1015          try (PoolCleaner cleaner = cleaner(e)) {
1016 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1016 >            List<Callable<String>> l = new ArrayList<>();
1017              l.add(new NPETask());
1018              try {
1019                  e.invokeAny(l);
# Line 898 | Line 1030 | public class ScheduledExecutorTest exten
1030      public void testInvokeAny5() throws Exception {
1031          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1032          try (PoolCleaner cleaner = cleaner(e)) {
1033 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1033 >            List<Callable<String>> l = new ArrayList<>();
1034              l.add(new StringTask());
1035              l.add(new StringTask());
1036              String result = e.invokeAny(l);
# Line 936 | Line 1068 | public class ScheduledExecutorTest exten
1068      public void testInvokeAll3() throws Exception {
1069          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1070          try (PoolCleaner cleaner = cleaner(e)) {
1071 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1071 >            List<Callable<String>> l = new ArrayList<>();
1072              l.add(new StringTask());
1073              l.add(null);
1074              try {
# Line 952 | Line 1084 | public class ScheduledExecutorTest exten
1084      public void testInvokeAll4() throws Exception {
1085          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1086          try (PoolCleaner cleaner = cleaner(e)) {
1087 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1087 >            List<Callable<String>> l = new ArrayList<>();
1088              l.add(new NPETask());
1089              List<Future<String>> futures = e.invokeAll(l);
1090              assertEquals(1, futures.size());
# Line 971 | Line 1103 | public class ScheduledExecutorTest exten
1103      public void testInvokeAll5() throws Exception {
1104          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1105          try (PoolCleaner cleaner = cleaner(e)) {
1106 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1106 >            List<Callable<String>> l = new ArrayList<>();
1107              l.add(new StringTask());
1108              l.add(new StringTask());
1109              List<Future<String>> futures = e.invokeAll(l);
# Line 1000 | Line 1132 | public class ScheduledExecutorTest exten
1132      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1133          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1134          try (PoolCleaner cleaner = cleaner(e)) {
1135 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1135 >            List<Callable<String>> l = new ArrayList<>();
1136              l.add(new StringTask());
1137              try {
1138                  e.invokeAny(l, MEDIUM_DELAY_MS, null);
# Line 1029 | Line 1161 | public class ScheduledExecutorTest exten
1161          CountDownLatch latch = new CountDownLatch(1);
1162          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1163          try (PoolCleaner cleaner = cleaner(e)) {
1164 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1164 >            List<Callable<String>> l = new ArrayList<>();
1165              l.add(latchAwaitingStringTask(latch));
1166              l.add(null);
1167              try {
# Line 1047 | Line 1179 | public class ScheduledExecutorTest exten
1179          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1180          try (PoolCleaner cleaner = cleaner(e)) {
1181              long startTime = System.nanoTime();
1182 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1182 >            List<Callable<String>> l = new ArrayList<>();
1183              l.add(new NPETask());
1184              try {
1185                  e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1066 | Line 1198 | public class ScheduledExecutorTest exten
1198          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1199          try (PoolCleaner cleaner = cleaner(e)) {
1200              long startTime = System.nanoTime();
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              String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1094 | Line 1226 | public class ScheduledExecutorTest exten
1226      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1227          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1228          try (PoolCleaner cleaner = cleaner(e)) {
1229 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1229 >            List<Callable<String>> l = new ArrayList<>();
1230              l.add(new StringTask());
1231              try {
1232                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 1121 | Line 1253 | public class ScheduledExecutorTest exten
1253      public void testTimedInvokeAll3() throws Exception {
1254          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1255          try (PoolCleaner cleaner = cleaner(e)) {
1256 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1256 >            List<Callable<String>> l = new ArrayList<>();
1257              l.add(new StringTask());
1258              l.add(null);
1259              try {
# Line 1137 | Line 1269 | public class ScheduledExecutorTest exten
1269      public void testTimedInvokeAll4() throws Exception {
1270          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1271          try (PoolCleaner cleaner = cleaner(e)) {
1272 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1272 >            List<Callable<String>> l = new ArrayList<>();
1273              l.add(new NPETask());
1274              List<Future<String>> futures =
1275                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1157 | Line 1289 | public class ScheduledExecutorTest exten
1289      public void testTimedInvokeAll5() throws Exception {
1290          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1291          try (PoolCleaner cleaner = cleaner(e)) {
1292 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1292 >            List<Callable<String>> l = new ArrayList<>();
1293              l.add(new StringTask());
1294              l.add(new StringTask());
1295              List<Future<String>> futures =
# Line 1206 | Line 1338 | public class ScheduledExecutorTest exten
1338          }
1339      }
1340  
1341 +    /**
1342 +     * A fixed delay task with overflowing period should not prevent a
1343 +     * one-shot task from executing.
1344 +     * https://bugs.openjdk.java.net/browse/JDK-8051859
1345 +     */
1346 +    public void testScheduleWithFixedDelay_overflow() throws Exception {
1347 +        final CountDownLatch delayedDone = new CountDownLatch(1);
1348 +        final CountDownLatch immediateDone = new CountDownLatch(1);
1349 +        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
1350 +        try (PoolCleaner cleaner = cleaner(p)) {
1351 +            final Runnable immediate = new Runnable() { public void run() {
1352 +                immediateDone.countDown();
1353 +            }};
1354 +            final Runnable delayed = new Runnable() { public void run() {
1355 +                delayedDone.countDown();
1356 +                p.submit(immediate);
1357 +            }};
1358 +            p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS);
1359 +            await(delayedDone);
1360 +            await(immediateDone);
1361 +        }
1362 +    }
1363 +
1364   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines