--- jsr166/src/test/tck/ScheduledExecutorTest.java 2017/05/29 22:44:27 1.94 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2017/07/17 22:27:31 1.95 @@ -224,110 +224,67 @@ public class ScheduledExecutorTest exten } /** - * execute(null) throws NPE + * Submitting null tasks throws NullPointerException */ - public void testExecuteNull() throws InterruptedException { + public void testNullTaskSubmission() { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { - try { - p.execute(null); - shouldThrow(); - } catch (NullPointerException success) {} + assertNullTaskSubmissionThrowsNullPointerException(p); } } /** - * schedule(null) throws NPE + * Submitted tasks are rejected when shutdown */ - public void testScheduleNull() throws InterruptedException { + public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - Future f = p.schedule((Callable)null, - randomTimeout(), randomTimeUnit()); - shouldThrow(); - } catch (NullPointerException success) {} - } - } + final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize()); + final CountDownLatch done = new CountDownLatch(1); + final Runnable r = () -> { + threadsStarted.countDown(); + for (;;) { + try { + done.await(); + return; + } catch (InterruptedException shutdownNowDeliberatelyIgnored) {} + }}; + final Callable c = () -> { + threadsStarted.countDown(); + for (;;) { + try { + done.await(); + return Boolean.TRUE; + } catch (InterruptedException shutdownNowDeliberatelyIgnored) {} + }}; - /** - * execute throws RejectedExecutionException if shutdown - */ - public void testSchedule1_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.schedule(new NoOpRunnable(), - randomTimeout(), randomTimeUnit()); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + try (PoolCleaner cleaner = cleaner(p, done)) { + for (int i = p.getCorePoolSize(); i--> 0; ) { + switch (rnd.nextInt(4)) { + case 0: p.execute(r); break; + case 1: assertFalse(p.submit(r).isDone()); break; + case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; + case 3: assertFalse(p.submit(c).isDone()); break; + } + } - /** - * schedule throws RejectedExecutionException if shutdown - */ - public void testSchedule2_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.schedule(new NoOpCallable(), - randomTimeout(), randomTimeUnit()); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated. + await(threadsStarted); - /** - * schedule callable throws RejectedExecutionException if shutdown - */ - public void testSchedule3_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { + if (rnd.nextBoolean()) + p.shutdownNow(); + else p.shutdown(); - p.schedule(new NoOpCallable(), - randomTimeout(), randomTimeUnit()); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + // Pool is shutdown, but not yet terminated + assertTaskSubmissionsAreRejected(p); + assertFalse(p.isTerminated()); - /** - * scheduleAtFixedRate throws RejectedExecutionException if shutdown - */ - public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.scheduleAtFixedRate(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + done.countDown(); // release blocking tasks + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); - /** - * scheduleWithFixedDelay throws RejectedExecutionException if shutdown - */ - public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.scheduleWithFixedDelay(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} + assertTaskSubmissionsAreRejected(p); } + assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount()); } /** @@ -747,6 +704,7 @@ public class ScheduledExecutorTest exten * - setExecuteExistingDelayedTasksAfterShutdownPolicy * - setContinueExistingPeriodicTasksAfterShutdownPolicy */ + @SuppressWarnings("FutureReturnValueIgnored") public void testShutdown_cancellation() throws Exception { final int poolSize = 4; final ScheduledThreadPoolExecutor p @@ -1351,18 +1309,16 @@ public class ScheduledExecutorTest exten * one-shot task from executing. * https://bugs.openjdk.java.net/browse/JDK-8051859 */ + @SuppressWarnings("FutureReturnValueIgnored") public void testScheduleWithFixedDelay_overflow() throws Exception { final CountDownLatch delayedDone = new CountDownLatch(1); final CountDownLatch immediateDone = new CountDownLatch(1); final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { - final Runnable immediate = new Runnable() { public void run() { - immediateDone.countDown(); - }}; - final Runnable delayed = new Runnable() { public void run() { + final Runnable delayed = () -> { delayedDone.countDown(); - p.submit(immediate); - }}; + p.submit(() -> immediateDone.countDown()); + }; p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); await(delayedDone); await(immediateDone);