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.68 by jsr166, Mon May 29 22:44:27 2017 UTC vs.
Revision 1.74 by jsr166, Wed Jan 27 01:57:24 2021 UTC

# Line 123 | Line 123 | public class ScheduledExecutorSubclassTe
123          final CustomExecutor p = new CustomExecutor(1);
124          try (PoolCleaner cleaner = cleaner(p, done)) {
125              final long startTime = System.nanoTime();
126 <            Callable task = new CheckedCallable<Boolean>() {
126 >            Callable<Boolean> task = new CheckedCallable<>() {
127                  public Boolean realCall() {
128                      done.countDown();
129                      assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
130                      return Boolean.TRUE;
131                  }};
132 <            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
132 >            Future<Boolean> f = p.schedule(task, timeoutMillis(), MILLISECONDS);
133              assertSame(Boolean.TRUE, f.get());
134              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
135          }
# Line 148 | Line 148 | public class ScheduledExecutorSubclassTe
148                      done.countDown();
149                      assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
150                  }};
151 <            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
151 >            Future<?> f = p.schedule(task, timeoutMillis(), MILLISECONDS);
152              await(done);
153              assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
154              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 168 | Line 168 | public class ScheduledExecutorSubclassTe
168                      done.countDown();
169                      assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
170                  }};
171 <            ScheduledFuture f =
171 >            ScheduledFuture<?> f =
172                  p.scheduleAtFixedRate(task, timeoutMillis(),
173                                        LONG_DELAY_MS, MILLISECONDS);
174              await(done);
# Line 190 | Line 190 | public class ScheduledExecutorSubclassTe
190                      done.countDown();
191                      assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
192                  }};
193 <            ScheduledFuture f =
193 >            ScheduledFuture<?> f =
194                  p.scheduleWithFixedDelay(task, timeoutMillis(),
195                                           LONG_DELAY_MS, MILLISECONDS);
196              await(done);
# Line 218 | Line 218 | public class ScheduledExecutorSubclassTe
218                  final CountDownLatch done = new CountDownLatch(cycles);
219                  final Runnable task = new CheckedRunnable() {
220                      public void realRun() { done.countDown(); }};
221 <                final ScheduledFuture periodicTask =
221 >                final ScheduledFuture<?> periodicTask =
222                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
223                  final int totalDelayMillis = (cycles - 1) * delay;
224                  await(done, totalDelayMillis + LONG_DELAY_MS);
# Line 264 | Line 264 | public class ScheduledExecutorSubclassTe
264                          previous.set(now);
265                          done.countDown();
266                      }};
267 <                final ScheduledFuture periodicTask =
267 >                final ScheduledFuture<?> periodicTask =
268                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
269                  final int totalDelayMillis = (cycles - 1) * delay;
270                  await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
# Line 280 | 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 {
289 <                p.execute(null);
290 <                shouldThrow();
291 <            } 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(),
318 <                           randomTimeout(), randomTimeUnit());
319 <                shouldThrow();
320 <            } catch (RejectedExecutionException success) {
321 <            } catch (SecurityException ok) {}
322 <        }
323 <    }
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
327 <     */
328 <    public void testSchedule2_RejectedExecutionException() {
329 <        final CustomExecutor p = new CustomExecutor(1);
330 <        try (PoolCleaner cleaner = cleaner(p)) {
331 <            try {
332 <                p.shutdown();
333 <                p.schedule(new NoOpCallable(),
334 <                           randomTimeout(), randomTimeUnit());
335 <                shouldThrow();
336 <            } catch (RejectedExecutionException success) {
337 <            } catch (SecurityException ok) {}
338 <        }
339 <    }
327 >            // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated.
328 >            await(threadsStarted);
329  
330 <    /**
331 <     * schedule callable throws RejectedExecutionException if shutdown
332 <     */
344 <    public void testSchedule3_RejectedExecutionException() {
345 <        final CustomExecutor p = new CustomExecutor(1);
346 <        try (PoolCleaner cleaner = cleaner(p)) {
347 <            try {
330 >            if (rnd.nextBoolean())
331 >                p.shutdownNow();
332 >            else
333                  p.shutdown();
334 <                p.schedule(new NoOpCallable(),
335 <                           randomTimeout(), randomTimeUnit());
336 <                shouldThrow();
352 <            } catch (RejectedExecutionException success) {
353 <            } catch (SecurityException ok) {}
354 <        }
355 <    }
334 >            // Pool is shutdown, but not yet terminated
335 >            assertTaskSubmissionsAreRejected(p);
336 >            assertFalse(p.isTerminated());
337  
338 <    /**
339 <     * scheduleAtFixedRate throws RejectedExecutionException if shutdown
359 <     */
360 <    public void testScheduleAtFixedRate1_RejectedExecutionException() {
361 <        final CustomExecutor p = new CustomExecutor(1);
362 <        try (PoolCleaner cleaner = cleaner(p)) {
363 <            try {
364 <                p.shutdown();
365 <                p.scheduleAtFixedRate(new NoOpRunnable(),
366 <                                      MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
367 <                shouldThrow();
368 <            } catch (RejectedExecutionException success) {
369 <            } catch (SecurityException ok) {}
370 <        }
371 <    }
338 >            done.countDown();   // release blocking tasks
339 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
340  
341 <    /**
374 <     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
375 <     */
376 <    public void testScheduleWithFixedDelay1_RejectedExecutionException() {
377 <        final CustomExecutor p = new CustomExecutor(1);
378 <        try (PoolCleaner cleaner = cleaner(p)) {
379 <            try {
380 <                p.shutdown();
381 <                p.scheduleWithFixedDelay(new NoOpRunnable(),
382 <                                         MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
383 <                shouldThrow();
384 <            } catch (RejectedExecutionException success) {
385 <            } catch (SecurityException ok) {}
341 >            assertTaskSubmissionsAreRejected(p);
342          }
343 +        assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount());
344      }
345  
346      /**
# Line 634 | Line 591 | public class ScheduledExecutorSubclassTe
591          final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
592          try (PoolCleaner cleaner = cleaner(p, done)) {
593              final CountDownLatch threadStarted = new CountDownLatch(1);
594 <            ScheduledFuture[] tasks = new ScheduledFuture[5];
594 >            @SuppressWarnings("unchecked")
595 >            ScheduledFuture<?>[] tasks = (ScheduledFuture<?>[])new ScheduledFuture[5];
596              for (int i = 0; i < tasks.length; i++) {
597                  Runnable r = new CheckedRunnable() {
598                      public void realRun() throws InterruptedException {
# Line 657 | Line 615 | public class ScheduledExecutorSubclassTe
615          final CountDownLatch done = new CountDownLatch(1);
616          final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
617          try (PoolCleaner cleaner = cleaner(p, done)) {
618 <            ScheduledFuture[] tasks = new ScheduledFuture[5];
618 >            @SuppressWarnings("unchecked")
619 >            ScheduledFuture<?>[] tasks = (ScheduledFuture<?>[])new ScheduledFuture[5];
620              final CountDownLatch threadStarted = new CountDownLatch(1);
621              for (int i = 0; i < tasks.length; i++) {
622                  Runnable r = new CheckedRunnable() {
# Line 685 | Line 644 | public class ScheduledExecutorSubclassTe
644       * purge removes cancelled tasks from the queue
645       */
646      public void testPurge() throws InterruptedException {
647 <        final ScheduledFuture[] tasks = new ScheduledFuture[5];
647 >        @SuppressWarnings("unchecked")
648 >        ScheduledFuture<?>[] tasks = (ScheduledFuture<?>[])new ScheduledFuture[5];
649          final Runnable releaser = new Runnable() { public void run() {
650 <            for (ScheduledFuture task : tasks)
650 >            for (ScheduledFuture<?> task : tasks)
651                  if (task != null) task.cancel(true); }};
652          final CustomExecutor p = new CustomExecutor(1);
653          try (PoolCleaner cleaner = cleaner(p, releaser)) {
654              for (int i = 0; i < tasks.length; i++)
655 <                tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
655 >                tasks[i] = p.schedule(possiblyInterruptedRunnable(SMALL_DELAY_MS),
656                                        LONG_DELAY_MS, MILLISECONDS);
657              int max = tasks.length;
658              if (tasks[4].cancel(true)) --max;
# Line 723 | Line 683 | public class ScheduledExecutorSubclassTe
683          Runnable waiter = new CheckedRunnable() { public void realRun() {
684              threadsStarted.countDown();
685              try {
686 <                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
686 >                MILLISECONDS.sleep(LONGER_DELAY_MS);
687              } catch (InterruptedException success) {}
688              ran.getAndIncrement();
689          }};
# Line 753 | Line 713 | public class ScheduledExecutorSubclassTe
713       */
714      public void testShutdownNow_delayedTasks() throws InterruptedException {
715          final CustomExecutor p = new CustomExecutor(1);
716 <        List<ScheduledFuture> tasks = new ArrayList<>();
716 >        List<ScheduledFuture<?>> tasks = new ArrayList<>();
717          for (int i = 0; i < 3; i++) {
718              Runnable r = new NoOpRunnable();
719              tasks.add(p.schedule(r, 9, SECONDS));
# Line 761 | Line 721 | public class ScheduledExecutorSubclassTe
721              tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
722          }
723          if (testImplementationDetails)
724 <            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
724 >            assertEquals(new HashSet<Object>(tasks), new HashSet<Object>(p.getQueue()));
725          final List<Runnable> queuedTasks;
726          try {
727              queuedTasks = p.shutdownNow();
# Line 771 | Line 731 | public class ScheduledExecutorSubclassTe
731          assertTrue(p.isShutdown());
732          assertTrue(p.getQueue().isEmpty());
733          if (testImplementationDetails)
734 <            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
734 >            assertEquals(new HashSet<Object>(tasks), new HashSet<Object>(queuedTasks));
735          assertEquals(tasks.size(), queuedTasks.size());
736 <        for (ScheduledFuture task : tasks) {
736 >        for (ScheduledFuture<?> task : tasks) {
737              assertFalse(((CustomTask)task).ran);
738              assertFalse(task.isDone());
739              assertFalse(task.isCancelled());
# Line 789 | Line 749 | public class ScheduledExecutorSubclassTe
749       * - setExecuteExistingDelayedTasksAfterShutdownPolicy
750       * - setContinueExistingPeriodicTasksAfterShutdownPolicy
751       */
752 +    @SuppressWarnings("FutureReturnValueIgnored")
753      public void testShutdown_cancellation() throws Exception {
754          final int poolSize = 4;
755          final CustomExecutor p = new CustomExecutor(poolSize);
# Line 887 | Line 848 | public class ScheduledExecutorSubclassTe
848          immediates.forEach(
849              f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L));
850  
851 <        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
851 >        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
852              .forEach(f -> assertFalse(f.isDone()));
853  
854          try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 941 | Line 902 | public class ScheduledExecutorSubclassTe
902  
903          assertTrue(q.isEmpty());
904  
905 <        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
905 >        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
906              .forEach(f -> assertTrue(f.isDone()));
907  
908          for (Future<?> f : immediates) assertNull(f.get());
# Line 1355 | Line 1316 | public class ScheduledExecutorSubclassTe
1316      public void testTimedInvokeAll6() throws Exception {
1317          for (long timeout = timeoutMillis();;) {
1318              final CountDownLatch done = new CountDownLatch(1);
1319 <            final Callable<String> waiter = new CheckedCallable<String>() {
1319 >            final Callable<String> waiter = new CheckedCallable<>() {
1320                  public String realCall() {
1321                      try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1322                      catch (InterruptedException ok) {}
# Line 1371 | Line 1332 | public class ScheduledExecutorSubclassTe
1332                      p.invokeAll(tasks, timeout, MILLISECONDS);
1333                  assertEquals(tasks.size(), futures.size());
1334                  assertTrue(millisElapsedSince(startTime) >= timeout);
1335 <                for (Future future : futures)
1335 >                for (Future<?> future : futures)
1336                      assertTrue(future.isDone());
1337                  assertTrue(futures.get(1).isCancelled());
1338                  try {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines