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 { |
233 |
< |
p.execute(null); |
234 |
< |
shouldThrow(); |
235 |
< |
} 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(), |
262 |
< |
randomTimeout(), randomTimeUnit()); |
263 |
< |
shouldThrow(); |
264 |
< |
} catch (RejectedExecutionException success) { |
265 |
< |
} catch (SecurityException ok) {} |
266 |
< |
} |
267 |
< |
} |
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 |
271 |
< |
*/ |
272 |
< |
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
273 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
274 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
275 |
< |
try { |
276 |
< |
p.shutdown(); |
277 |
< |
p.schedule(new NoOpCallable(), |
278 |
< |
randomTimeout(), randomTimeUnit()); |
279 |
< |
shouldThrow(); |
280 |
< |
} catch (RejectedExecutionException success) { |
281 |
< |
} catch (SecurityException ok) {} |
282 |
< |
} |
283 |
< |
} |
271 |
> |
// ScheduledThreadPoolExecutor has an unbounded queue, so never saturated. |
272 |
> |
await(threadsStarted); |
273 |
|
|
274 |
< |
/** |
275 |
< |
* schedule callable throws RejectedExecutionException if shutdown |
276 |
< |
*/ |
288 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
289 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
290 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
291 |
< |
try { |
274 |
> |
if (rnd.nextBoolean()) |
275 |
> |
p.shutdownNow(); |
276 |
> |
else |
277 |
|
p.shutdown(); |
278 |
< |
p.schedule(new NoOpCallable(), |
279 |
< |
randomTimeout(), randomTimeUnit()); |
280 |
< |
shouldThrow(); |
296 |
< |
} catch (RejectedExecutionException success) { |
297 |
< |
} catch (SecurityException ok) {} |
298 |
< |
} |
299 |
< |
} |
278 |
> |
// Pool is shutdown, but not yet terminated |
279 |
> |
assertTaskSubmissionsAreRejected(p); |
280 |
> |
assertFalse(p.isTerminated()); |
281 |
|
|
282 |
< |
/** |
283 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
303 |
< |
*/ |
304 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
305 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
306 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
307 |
< |
try { |
308 |
< |
p.shutdown(); |
309 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), |
310 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
311 |
< |
shouldThrow(); |
312 |
< |
} catch (RejectedExecutionException success) { |
313 |
< |
} catch (SecurityException ok) {} |
314 |
< |
} |
315 |
< |
} |
282 |
> |
done.countDown(); // release blocking tasks |
283 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
284 |
|
|
285 |
< |
/** |
318 |
< |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
319 |
< |
*/ |
320 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
321 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
322 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
323 |
< |
try { |
324 |
< |
p.shutdown(); |
325 |
< |
p.scheduleWithFixedDelay(new NoOpRunnable(), |
326 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
327 |
< |
shouldThrow(); |
328 |
< |
} catch (RejectedExecutionException success) { |
329 |
< |
} catch (SecurityException ok) {} |
285 |
> |
assertTaskSubmissionsAreRejected(p); |
286 |
|
} |
287 |
+ |
assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount()); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
607 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
608 |
|
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
609 |
|
for (int i = 0; i < tasks.length; i++) |
610 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
610 |
> |
tasks[i] = p.schedule(possiblyInterruptedRunnable(SMALL_DELAY_MS), |
611 |
|
LONG_DELAY_MS, MILLISECONDS); |
612 |
|
int max = tasks.length; |
613 |
|
if (tasks[4].cancel(true)) --max; |
639 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
640 |
|
threadsStarted.countDown(); |
641 |
|
try { |
642 |
< |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
642 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
643 |
|
} catch (InterruptedException success) {} |
644 |
|
ran.getAndIncrement(); |
645 |
|
}}; |
704 |
|
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
705 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
706 |
|
*/ |
707 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
708 |
|
public void testShutdown_cancellation() throws Exception { |
709 |
|
final int poolSize = 4; |
710 |
|
final ScheduledThreadPoolExecutor p |
804 |
|
immediates.forEach( |
805 |
|
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
806 |
|
|
807 |
< |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
807 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) |
808 |
|
.forEach(f -> assertFalse(f.isDone())); |
809 |
|
|
810 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
858 |
|
|
859 |
|
assertTrue(q.isEmpty()); |
860 |
|
|
861 |
< |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
861 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) |
862 |
|
.forEach(f -> assertTrue(f.isDone())); |
863 |
|
|
864 |
|
for (Future<?> f : immediates) assertNull(f.get()); |
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() { |
1360 |
< |
immediateDone.countDown(); |
1361 |
< |
}}; |
1362 |
< |
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); |