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.93 by jsr166, Mon May 29 19:15:03 2017 UTC vs.
Revision 1.95 by jsr166, Mon Jul 17 22:27:31 2017 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import static java.util.concurrent.TimeUnit.SECONDS;
12  
13   import java.util.ArrayList;
14 + import java.util.Collection;
15 + import java.util.Collections;
16   import java.util.HashSet;
17   import java.util.List;
18   import java.util.concurrent.BlockingQueue;
# Line 222 | Line 224 | public class ScheduledExecutorTest exten
224      }
225  
226      /**
227 <     * execute(null) throws NPE
227 >     * Submitting null tasks throws NullPointerException
228       */
229 <    public void testExecuteNull() throws InterruptedException {
229 >    public void testNullTaskSubmission() {
230          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
231          try (PoolCleaner cleaner = cleaner(p)) {
232 <            try {
231 <                p.execute(null);
232 <                shouldThrow();
233 <            } catch (NullPointerException success) {}
232 >            assertNullTaskSubmissionThrowsNullPointerException(p);
233          }
234      }
235  
236      /**
237 <     * schedule(null) throws NPE
237 >     * Submitted tasks are rejected when shutdown
238       */
239 <    public void testScheduleNull() throws InterruptedException {
239 >    public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException {
240          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
241 <        try (PoolCleaner cleaner = cleaner(p)) {
242 <            try {
243 <                Future f = p.schedule((Callable)null,
244 <                                      randomTimeout(), randomTimeUnit());
245 <                shouldThrow();
246 <            } catch (NullPointerException success) {}
247 <        }
248 <    }
241 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
242 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize());
243 >        final CountDownLatch done = new CountDownLatch(1);
244 >        final Runnable r = () -> {
245 >            threadsStarted.countDown();
246 >            for (;;) {
247 >                try {
248 >                    done.await();
249 >                    return;
250 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
251 >            }};
252 >        final Callable<Boolean> c = () -> {
253 >            threadsStarted.countDown();
254 >            for (;;) {
255 >                try {
256 >                    done.await();
257 >                    return Boolean.TRUE;
258 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
259 >            }};
260  
261 <    /**
262 <     * execute throws RejectedExecutionException if shutdown
263 <     */
264 <    public void testSchedule1_RejectedExecutionException() throws InterruptedException {
265 <        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
266 <        try (PoolCleaner cleaner = cleaner(p)) {
267 <            try {
268 <                p.shutdown();
269 <                p.schedule(new NoOpRunnable(),
260 <                           MEDIUM_DELAY_MS, MILLISECONDS);
261 <                shouldThrow();
262 <            } catch (RejectedExecutionException success) {
263 <            } catch (SecurityException ok) {}
264 <        }
265 <    }
261 >        try (PoolCleaner cleaner = cleaner(p, done)) {
262 >            for (int i = p.getCorePoolSize(); i--> 0; ) {
263 >                switch (rnd.nextInt(4)) {
264 >                case 0: p.execute(r); break;
265 >                case 1: assertFalse(p.submit(r).isDone()); break;
266 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
267 >                case 3: assertFalse(p.submit(c).isDone()); break;
268 >                }
269 >            }
270  
271 <    /**
272 <     * schedule throws RejectedExecutionException if shutdown
269 <     */
270 <    public void testSchedule2_RejectedExecutionException() throws InterruptedException {
271 <        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
272 <        try (PoolCleaner cleaner = cleaner(p)) {
273 <            try {
274 <                p.shutdown();
275 <                p.schedule(new NoOpCallable(),
276 <                           MEDIUM_DELAY_MS, MILLISECONDS);
277 <                shouldThrow();
278 <            } catch (RejectedExecutionException success) {
279 <            } catch (SecurityException ok) {}
280 <        }
281 <    }
271 >            // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated.
272 >            await(threadsStarted);
273  
274 <    /**
275 <     * schedule callable throws RejectedExecutionException if shutdown
276 <     */
286 <    public void testSchedule3_RejectedExecutionException() throws InterruptedException {
287 <        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
288 <        try (PoolCleaner cleaner = cleaner(p)) {
289 <            try {
274 >            if (rnd.nextBoolean())
275 >                p.shutdownNow();
276 >            else
277                  p.shutdown();
278 <                p.schedule(new NoOpCallable(),
279 <                           MEDIUM_DELAY_MS, MILLISECONDS);
280 <                shouldThrow();
294 <            } catch (RejectedExecutionException success) {
295 <            } catch (SecurityException ok) {}
296 <        }
297 <    }
278 >            // Pool is shutdown, but not yet terminated
279 >            assertTaskSubmissionsAreRejected(p);
280 >            assertFalse(p.isTerminated());
281  
282 <    /**
283 <     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
301 <     */
302 <    public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
303 <        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
304 <        try (PoolCleaner cleaner = cleaner(p)) {
305 <            try {
306 <                p.shutdown();
307 <                p.scheduleAtFixedRate(new NoOpRunnable(),
308 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
309 <                shouldThrow();
310 <            } catch (RejectedExecutionException success) {
311 <            } catch (SecurityException ok) {}
312 <        }
313 <    }
282 >            done.countDown();   // release blocking tasks
283 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
284  
285 <    /**
316 <     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
317 <     */
318 <    public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
319 <        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
320 <        try (PoolCleaner cleaner = cleaner(p)) {
321 <            try {
322 <                p.shutdown();
323 <                p.scheduleWithFixedDelay(new NoOpRunnable(),
324 <                                         MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
325 <                shouldThrow();
326 <            } catch (RejectedExecutionException success) {
327 <            } catch (SecurityException ok) {}
285 >            assertTaskSubmissionsAreRejected(p);
286          }
287 +        assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount());
288      }
289  
290      /**
# Line 745 | Line 704 | public class ScheduledExecutorTest exten
704       * - setExecuteExistingDelayedTasksAfterShutdownPolicy
705       * - setContinueExistingPeriodicTasksAfterShutdownPolicy
706       */
707 +    @SuppressWarnings("FutureReturnValueIgnored")
708      public void testShutdown_cancellation() throws Exception {
709          final int poolSize = 4;
710          final ScheduledThreadPoolExecutor p
# Line 964 | Line 924 | public class ScheduledExecutorTest exten
924      }
925  
926      /**
927 <     * invokeAny(null) throws NPE
927 >     * invokeAny(null) throws NullPointerException
928       */
929      public void testInvokeAny1() throws Exception {
930          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
# Line 977 | Line 937 | public class ScheduledExecutorTest exten
937      }
938  
939      /**
940 <     * invokeAny(empty collection) throws IAE
940 >     * invokeAny(empty collection) throws IllegalArgumentException
941       */
942      public void testInvokeAny2() throws Exception {
943          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
# Line 990 | Line 950 | public class ScheduledExecutorTest exten
950      }
951  
952      /**
953 <     * invokeAny(c) throws NPE if c has null elements
953 >     * invokeAny(c) throws NullPointerException if c has null elements
954       */
955      public void testInvokeAny3() throws Exception {
956          CountDownLatch latch = new CountDownLatch(1);
# Line 1052 | Line 1012 | public class ScheduledExecutorTest exten
1012      }
1013  
1014      /**
1015 <     * invokeAll(empty collection) returns empty collection
1015 >     * invokeAll(empty collection) returns empty list
1016       */
1017      public void testInvokeAll2() throws Exception {
1018          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1019 +        final Collection<Callable<String>> emptyCollection
1020 +            = Collections.emptyList();
1021          try (PoolCleaner cleaner = cleaner(e)) {
1022 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1022 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1023              assertTrue(r.isEmpty());
1024          }
1025      }
# Line 1120 | Line 1082 | public class ScheduledExecutorTest exten
1082          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1083          try (PoolCleaner cleaner = cleaner(e)) {
1084              try {
1085 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1085 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1086                  shouldThrow();
1087              } catch (NullPointerException success) {}
1088          }
1089      }
1090  
1091      /**
1092 <     * timed invokeAny(,,null) throws NPE
1092 >     * timed invokeAny(,,null) throws NullPointerException
1093       */
1094      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1095          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
# Line 1135 | Line 1097 | public class ScheduledExecutorTest exten
1097              List<Callable<String>> l = new ArrayList<>();
1098              l.add(new StringTask());
1099              try {
1100 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1100 >                e.invokeAny(l, randomTimeout(), null);
1101                  shouldThrow();
1102              } catch (NullPointerException success) {}
1103          }
1104      }
1105  
1106      /**
1107 <     * timed invokeAny(empty collection) throws IAE
1107 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1108       */
1109      public void testTimedInvokeAny2() throws Exception {
1110          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1111 +        final Collection<Callable<String>> emptyCollection
1112 +            = Collections.emptyList();
1113          try (PoolCleaner cleaner = cleaner(e)) {
1114              try {
1115 <                e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1115 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1116                  shouldThrow();
1117              } catch (IllegalArgumentException success) {}
1118          }
# Line 1165 | Line 1129 | public class ScheduledExecutorTest exten
1129              l.add(latchAwaitingStringTask(latch));
1130              l.add(null);
1131              try {
1132 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1132 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1133                  shouldThrow();
1134              } catch (NullPointerException success) {}
1135              latch.countDown();
# Line 1214 | Line 1178 | public class ScheduledExecutorTest exten
1178          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1179          try (PoolCleaner cleaner = cleaner(e)) {
1180              try {
1181 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1181 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1182                  shouldThrow();
1183              } catch (NullPointerException success) {}
1184          }
# Line 1229 | Line 1193 | public class ScheduledExecutorTest exten
1193              List<Callable<String>> l = new ArrayList<>();
1194              l.add(new StringTask());
1195              try {
1196 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1196 >                e.invokeAll(l, randomTimeout(), null);
1197                  shouldThrow();
1198              } catch (NullPointerException success) {}
1199          }
1200      }
1201  
1202      /**
1203 <     * timed invokeAll(empty collection) returns empty collection
1203 >     * timed invokeAll(empty collection) returns empty list
1204       */
1205      public void testTimedInvokeAll2() throws Exception {
1206          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1207 +        final Collection<Callable<String>> emptyCollection
1208 +            = Collections.emptyList();
1209          try (PoolCleaner cleaner = cleaner(e)) {
1210 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1211 <                                                 MEDIUM_DELAY_MS, MILLISECONDS);
1210 >            List<Future<String>> r =
1211 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1212              assertTrue(r.isEmpty());
1213          }
1214      }
# Line 1257 | Line 1223 | public class ScheduledExecutorTest exten
1223              l.add(new StringTask());
1224              l.add(null);
1225              try {
1226 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1226 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1227                  shouldThrow();
1228              } catch (NullPointerException success) {}
1229          }
# Line 1343 | Line 1309 | public class ScheduledExecutorTest exten
1309       * one-shot task from executing.
1310       * https://bugs.openjdk.java.net/browse/JDK-8051859
1311       */
1312 +    @SuppressWarnings("FutureReturnValueIgnored")
1313      public void testScheduleWithFixedDelay_overflow() throws Exception {
1314          final CountDownLatch delayedDone = new CountDownLatch(1);
1315          final CountDownLatch immediateDone = new CountDownLatch(1);
1316          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
1317          try (PoolCleaner cleaner = cleaner(p)) {
1318 <            final Runnable immediate = new Runnable() { public void run() {
1352 <                immediateDone.countDown();
1353 <            }};
1354 <            final Runnable delayed = new Runnable() { public void run() {
1318 >            final Runnable delayed = () -> {
1319                  delayedDone.countDown();
1320 <                p.submit(immediate);
1321 <            }};
1320 >                p.submit(() -> immediateDone.countDown());
1321 >            };
1322              p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS);
1323              await(delayedDone);
1324              await(immediateDone);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines