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 |
|
/** |
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); |
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; } |
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()); |