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.67 by jsr166, Mon May 29 19:15:02 2017 UTC vs.
Revision 1.70 by jsr166, Mon Jan 8 02:04:15 2018 UTC

# Line 9 | Line 9 | import static java.util.concurrent.TimeU
9   import static java.util.concurrent.TimeUnit.SECONDS;
10  
11   import java.util.ArrayList;
12 + import java.util.Collection;
13 + import java.util.Collections;
14   import java.util.HashSet;
15   import java.util.List;
16   import java.util.concurrent.BlockingQueue;
# Line 278 | Line 280 | public class ScheduledExecutorSubclassTe
280      }
281  
282      /**
283 <     * execute(null) throws NPE
283 >     * Submitting null tasks throws NullPointerException
284       */
285 <    public void testExecuteNull() throws InterruptedException {
285 >    public void testNullTaskSubmission() {
286          final CustomExecutor p = new CustomExecutor(1);
287          try (PoolCleaner cleaner = cleaner(p)) {
288 <            try {
287 <                p.execute(null);
288 <                shouldThrow();
289 <            } catch (NullPointerException success) {}
288 >            assertNullTaskSubmissionThrowsNullPointerException(p);
289          }
290      }
291  
292      /**
293 <     * schedule(null) throws NPE
293 >     * Submitted tasks are rejected when shutdown
294       */
295 <    public void testScheduleNull() throws InterruptedException {
296 <        final CustomExecutor p = new CustomExecutor(1);
297 <        try (PoolCleaner cleaner = cleaner(p)) {
298 <            try {
299 <                Future f = p.schedule((Callable)null,
300 <                                      randomTimeout(), randomTimeUnit());
301 <                shouldThrow();
302 <            } catch (NullPointerException success) {}
303 <        }
304 <    }
295 >    public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException {
296 >        final CustomExecutor p = new CustomExecutor(2);
297 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
298 >        final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize());
299 >        final CountDownLatch done = new CountDownLatch(1);
300 >        final Runnable r = () -> {
301 >            threadsStarted.countDown();
302 >            for (;;) {
303 >                try {
304 >                    done.await();
305 >                    return;
306 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
307 >            }};
308 >        final Callable<Boolean> c = () -> {
309 >            threadsStarted.countDown();
310 >            for (;;) {
311 >                try {
312 >                    done.await();
313 >                    return Boolean.TRUE;
314 >                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
315 >            }};
316  
317 <    /**
318 <     * execute throws RejectedExecutionException if shutdown
319 <     */
320 <    public void testSchedule1_RejectedExecutionException() {
321 <        final CustomExecutor p = new CustomExecutor(1);
322 <        try (PoolCleaner cleaner = cleaner(p)) {
323 <            try {
324 <                p.shutdown();
325 <                p.schedule(new NoOpRunnable(),
316 <                           MEDIUM_DELAY_MS, MILLISECONDS);
317 <                shouldThrow();
318 <            } catch (RejectedExecutionException success) {
319 <            } catch (SecurityException ok) {}
320 <        }
321 <    }
317 >        try (PoolCleaner cleaner = cleaner(p, done)) {
318 >            for (int i = p.getCorePoolSize(); i--> 0; ) {
319 >                switch (rnd.nextInt(4)) {
320 >                case 0: p.execute(r); break;
321 >                case 1: assertFalse(p.submit(r).isDone()); break;
322 >                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
323 >                case 3: assertFalse(p.submit(c).isDone()); break;
324 >                }
325 >            }
326  
327 <    /**
328 <     * schedule throws RejectedExecutionException if shutdown
325 <     */
326 <    public void testSchedule2_RejectedExecutionException() {
327 <        final CustomExecutor p = new CustomExecutor(1);
328 <        try (PoolCleaner cleaner = cleaner(p)) {
329 <            try {
330 <                p.shutdown();
331 <                p.schedule(new NoOpCallable(),
332 <                           MEDIUM_DELAY_MS, MILLISECONDS);
333 <                shouldThrow();
334 <            } catch (RejectedExecutionException success) {
335 <            } catch (SecurityException ok) {}
336 <        }
337 <    }
327 >            // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated.
328 >            await(threadsStarted);
329  
330 <    /**
331 <     * schedule callable throws RejectedExecutionException if shutdown
332 <     */
342 <    public void testSchedule3_RejectedExecutionException() {
343 <        final CustomExecutor p = new CustomExecutor(1);
344 <        try (PoolCleaner cleaner = cleaner(p)) {
345 <            try {
330 >            if (rnd.nextBoolean())
331 >                p.shutdownNow();
332 >            else
333                  p.shutdown();
334 <                p.schedule(new NoOpCallable(),
335 <                           MEDIUM_DELAY_MS, MILLISECONDS);
336 <                shouldThrow();
350 <            } catch (RejectedExecutionException success) {
351 <            } catch (SecurityException ok) {}
352 <        }
353 <    }
334 >            // Pool is shutdown, but not yet terminated
335 >            assertTaskSubmissionsAreRejected(p);
336 >            assertFalse(p.isTerminated());
337  
338 <    /**
339 <     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
357 <     */
358 <    public void testScheduleAtFixedRate1_RejectedExecutionException() {
359 <        final CustomExecutor p = new CustomExecutor(1);
360 <        try (PoolCleaner cleaner = cleaner(p)) {
361 <            try {
362 <                p.shutdown();
363 <                p.scheduleAtFixedRate(new NoOpRunnable(),
364 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
365 <                shouldThrow();
366 <            } catch (RejectedExecutionException success) {
367 <            } catch (SecurityException ok) {}
368 <        }
369 <    }
338 >            done.countDown();   // release blocking tasks
339 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
340  
341 <    /**
372 <     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
373 <     */
374 <    public void testScheduleWithFixedDelay1_RejectedExecutionException() {
375 <        final CustomExecutor p = new CustomExecutor(1);
376 <        try (PoolCleaner cleaner = cleaner(p)) {
377 <            try {
378 <                p.shutdown();
379 <                p.scheduleWithFixedDelay(new NoOpRunnable(),
380 <                                         MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
381 <                shouldThrow();
382 <            } catch (RejectedExecutionException success) {
383 <            } catch (SecurityException ok) {}
341 >            assertTaskSubmissionsAreRejected(p);
342          }
343 +        assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount());
344      }
345  
346      /**
# Line 787 | Line 746 | public class ScheduledExecutorSubclassTe
746       * - setExecuteExistingDelayedTasksAfterShutdownPolicy
747       * - setContinueExistingPeriodicTasksAfterShutdownPolicy
748       */
749 +    @SuppressWarnings("FutureReturnValueIgnored")
750      public void testShutdown_cancellation() throws Exception {
751          final int poolSize = 4;
752          final CustomExecutor p = new CustomExecutor(poolSize);
# Line 885 | Line 845 | public class ScheduledExecutorSubclassTe
845          immediates.forEach(
846              f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L));
847  
848 <        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
848 >        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
849              .forEach(f -> assertFalse(f.isDone()));
850  
851          try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 939 | Line 899 | public class ScheduledExecutorSubclassTe
899  
900          assertTrue(q.isEmpty());
901  
902 <        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
902 >        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
903              .forEach(f -> assertTrue(f.isDone()));
904  
905          for (Future<?> f : immediates) assertNull(f.get());
# Line 1018 | Line 978 | public class ScheduledExecutorSubclassTe
978      }
979  
980      /**
981 <     * invokeAny(empty collection) throws IAE
981 >     * invokeAny(empty collection) throws IllegalArgumentException
982       */
983      public void testInvokeAny2() throws Exception {
984          final ExecutorService e = new CustomExecutor(2);
# Line 1093 | Line 1053 | public class ScheduledExecutorSubclassTe
1053      }
1054  
1055      /**
1056 <     * invokeAll(empty collection) returns empty collection
1056 >     * invokeAll(empty collection) returns empty list
1057       */
1058      public void testInvokeAll2() throws Exception {
1059          final ExecutorService e = new CustomExecutor(2);
1060 +        final Collection<Callable<String>> emptyCollection
1061 +            = Collections.emptyList();
1062          try (PoolCleaner cleaner = cleaner(e)) {
1063 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1063 >            List<Future<String>> r = e.invokeAll(emptyCollection);
1064              assertTrue(r.isEmpty());
1065          }
1066      }
# Line 1161 | Line 1123 | public class ScheduledExecutorSubclassTe
1123          final ExecutorService e = new CustomExecutor(2);
1124          try (PoolCleaner cleaner = cleaner(e)) {
1125              try {
1126 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1126 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
1127                  shouldThrow();
1128              } catch (NullPointerException success) {}
1129          }
# Line 1176 | Line 1138 | public class ScheduledExecutorSubclassTe
1138              List<Callable<String>> l = new ArrayList<>();
1139              l.add(new StringTask());
1140              try {
1141 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
1141 >                e.invokeAny(l, randomTimeout(), null);
1142                  shouldThrow();
1143              } catch (NullPointerException success) {}
1144          }
1145      }
1146  
1147      /**
1148 <     * timed invokeAny(empty collection) throws IAE
1148 >     * timed invokeAny(empty collection) throws IllegalArgumentException
1149       */
1150      public void testTimedInvokeAny2() throws Exception {
1151          final ExecutorService e = new CustomExecutor(2);
1152 +        final Collection<Callable<String>> emptyCollection
1153 +            = Collections.emptyList();
1154          try (PoolCleaner cleaner = cleaner(e)) {
1155              try {
1156 <                e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1156 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
1157                  shouldThrow();
1158              } catch (IllegalArgumentException success) {}
1159          }
# Line 1206 | Line 1170 | public class ScheduledExecutorSubclassTe
1170              l.add(latchAwaitingStringTask(latch));
1171              l.add(null);
1172              try {
1173 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1173 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
1174                  shouldThrow();
1175              } catch (NullPointerException success) {}
1176              latch.countDown();
# Line 1249 | Line 1213 | public class ScheduledExecutorSubclassTe
1213      }
1214  
1215      /**
1216 <     * timed invokeAll(null) throws NPE
1216 >     * timed invokeAll(null) throws NullPointerException
1217       */
1218      public void testTimedInvokeAll1() throws Exception {
1219          final ExecutorService e = new CustomExecutor(2);
1220          try (PoolCleaner cleaner = cleaner(e)) {
1221              try {
1222 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1222 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
1223                  shouldThrow();
1224              } catch (NullPointerException success) {}
1225          }
1226      }
1227  
1228      /**
1229 <     * timed invokeAll(,,null) throws NPE
1229 >     * timed invokeAll(,,null) throws NullPointerException
1230       */
1231      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1232          final ExecutorService e = new CustomExecutor(2);
# Line 1270 | Line 1234 | public class ScheduledExecutorSubclassTe
1234              List<Callable<String>> l = new ArrayList<>();
1235              l.add(new StringTask());
1236              try {
1237 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
1237 >                e.invokeAll(l, randomTimeout(), null);
1238                  shouldThrow();
1239              } catch (NullPointerException success) {}
1240          }
1241      }
1242  
1243      /**
1244 <     * timed invokeAll(empty collection) returns empty collection
1244 >     * timed invokeAll(empty collection) returns empty list
1245       */
1246      public void testTimedInvokeAll2() throws Exception {
1247          final ExecutorService e = new CustomExecutor(2);
1248 +        final Collection<Callable<String>> emptyCollection
1249 +            = Collections.emptyList();
1250          try (PoolCleaner cleaner = cleaner(e)) {
1251 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1251 >            List<Future<String>> r =
1252 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
1253              assertTrue(r.isEmpty());
1254          }
1255      }
# Line 1297 | Line 1264 | public class ScheduledExecutorSubclassTe
1264              l.add(new StringTask());
1265              l.add(null);
1266              try {
1267 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1267 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
1268                  shouldThrow();
1269              } catch (NullPointerException success) {}
1270          }
# Line 1308 | Line 1275 | public class ScheduledExecutorSubclassTe
1275       */
1276      public void testTimedInvokeAll4() throws Exception {
1277          final ExecutorService e = new CustomExecutor(2);
1278 +        final Collection<Callable<String>> c = new ArrayList<>();
1279 +        c.add(new NPETask());
1280          try (PoolCleaner cleaner = cleaner(e)) {
1312            List<Callable<String>> l = new ArrayList<>();
1313            l.add(new NPETask());
1281              List<Future<String>> futures =
1282 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1282 >                e.invokeAll(c, LONG_DELAY_MS, MILLISECONDS);
1283              assertEquals(1, futures.size());
1284              try {
1285                  futures.get(0).get();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines