162 |
|
*/ |
163 |
|
public void testDefaultInitialState() { |
164 |
|
ForkJoinPool p = new ForkJoinPool(1); |
165 |
< |
try { |
165 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
166 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
167 |
|
p.getFactory()); |
168 |
|
assertFalse(p.getAsyncMode()); |
174 |
|
assertFalse(p.isShutdown()); |
175 |
|
assertFalse(p.isTerminating()); |
176 |
|
assertFalse(p.isTerminated()); |
177 |
– |
} finally { |
178 |
– |
joinPool(p); |
177 |
|
} |
178 |
|
} |
179 |
|
|
202 |
|
*/ |
203 |
|
public void testGetParallelism() { |
204 |
|
ForkJoinPool p = new ForkJoinPool(1); |
205 |
< |
try { |
205 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
206 |
|
assertEquals(1, p.getParallelism()); |
209 |
– |
} finally { |
210 |
– |
joinPool(p); |
207 |
|
} |
208 |
|
} |
209 |
|
|
212 |
|
*/ |
213 |
|
public void testGetPoolSize() { |
214 |
|
ForkJoinPool p = new ForkJoinPool(1); |
215 |
< |
try { |
215 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
216 |
|
assertEquals(0, p.getActiveThreadCount()); |
217 |
|
Future<String> future = p.submit(new StringTask()); |
218 |
|
assertEquals(1, p.getPoolSize()); |
223 |
– |
} finally { |
224 |
– |
joinPool(p); |
219 |
|
} |
220 |
|
} |
221 |
|
|
224 |
|
*/ |
225 |
|
public void testAwaitTermination_timesOut() throws InterruptedException { |
226 |
|
ForkJoinPool p = new ForkJoinPool(1); |
227 |
< |
assertFalse(p.isTerminated()); |
228 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
229 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
230 |
< |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
231 |
< |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
232 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
233 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
234 |
< |
long timeoutNanos = 999999L; |
235 |
< |
long startTime = System.nanoTime(); |
236 |
< |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
237 |
< |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
238 |
< |
assertFalse(p.isTerminated()); |
239 |
< |
startTime = System.nanoTime(); |
240 |
< |
long timeoutMillis = timeoutMillis(); |
241 |
< |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
242 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
243 |
< |
assertFalse(p.isTerminated()); |
244 |
< |
p.shutdown(); |
245 |
< |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
246 |
< |
assertTrue(p.isTerminated()); |
227 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
228 |
> |
assertFalse(p.isTerminated()); |
229 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
230 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
231 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
232 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
233 |
> |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
234 |
> |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
235 |
> |
long timeoutNanos = 999999L; |
236 |
> |
long startTime = System.nanoTime(); |
237 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
238 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
239 |
> |
assertFalse(p.isTerminated()); |
240 |
> |
startTime = System.nanoTime(); |
241 |
> |
long timeoutMillis = timeoutMillis(); |
242 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
243 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
244 |
> |
assertFalse(p.isTerminated()); |
245 |
> |
p.shutdown(); |
246 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
247 |
> |
assertTrue(p.isTerminated()); |
248 |
> |
} |
249 |
|
} |
250 |
|
|
251 |
|
/** |
256 |
|
*/ |
257 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
258 |
|
final CountDownLatch uehInvoked = new CountDownLatch(1); |
259 |
< |
final Thread.UncaughtExceptionHandler eh = |
259 |
> |
final Thread.UncaughtExceptionHandler ueh = |
260 |
|
new Thread.UncaughtExceptionHandler() { |
261 |
|
public void uncaughtException(Thread t, Throwable e) { |
262 |
|
uehInvoked.countDown(); |
263 |
|
}}; |
264 |
|
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
265 |
< |
eh, false); |
266 |
< |
try { |
267 |
< |
assertSame(eh, p.getUncaughtExceptionHandler()); |
265 |
> |
ueh, false); |
266 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
267 |
> |
assertSame(ueh, p.getUncaughtExceptionHandler()); |
268 |
|
try { |
269 |
< |
p.execute(new FibTask(8)); |
270 |
< |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
271 |
< |
} catch (RejectedExecutionException ok) { |
269 |
> |
try { |
270 |
> |
p.execute(new FibTask(8)); |
271 |
> |
await(uehInvoked); |
272 |
> |
} catch (RejectedExecutionException ok) {} |
273 |
> |
} finally { |
274 |
> |
p.shutdownNow(); // failure might have prevented processing task |
275 |
|
} |
277 |
– |
} finally { |
278 |
– |
p.shutdownNow(); // failure might have prevented processing task |
279 |
– |
joinPool(p); |
276 |
|
} |
277 |
|
} |
278 |
|
|
284 |
|
*/ |
285 |
|
public void testIsQuiescent() throws Exception { |
286 |
|
ForkJoinPool p = new ForkJoinPool(2); |
287 |
< |
try { |
287 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
288 |
|
assertTrue(p.isQuiescent()); |
289 |
|
long startTime = System.nanoTime(); |
290 |
|
FibTask f = new FibTask(20); |
312 |
|
assertFalse(p.isTerminated()); |
313 |
|
assertTrue(f.isDone()); |
314 |
|
assertEquals(6765, (int) f.get()); |
319 |
– |
} finally { |
320 |
– |
joinPool(p); |
315 |
|
} |
316 |
|
} |
317 |
|
|
320 |
|
*/ |
321 |
|
public void testSubmitForkJoinTask() throws Throwable { |
322 |
|
ForkJoinPool p = new ForkJoinPool(1); |
323 |
< |
try { |
323 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
324 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
325 |
|
assertEquals(21, (int) f.get()); |
332 |
– |
} finally { |
333 |
– |
joinPool(p); |
326 |
|
} |
327 |
|
} |
328 |
|
|
331 |
|
*/ |
332 |
|
public void testSubmitAfterShutdown() { |
333 |
|
ForkJoinPool p = new ForkJoinPool(1); |
334 |
< |
try { |
334 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
335 |
|
p.shutdown(); |
336 |
|
assertTrue(p.isShutdown()); |
337 |
|
try { |
338 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
339 |
|
shouldThrow(); |
340 |
|
} catch (RejectedExecutionException success) {} |
349 |
– |
} finally { |
350 |
– |
joinPool(p); |
341 |
|
} |
342 |
|
} |
343 |
|
|
363 |
|
public void testPollSubmission() { |
364 |
|
final CountDownLatch done = new CountDownLatch(1); |
365 |
|
SubFJP p = new SubFJP(); |
366 |
< |
try { |
366 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
367 |
|
ForkJoinTask a = p.submit(awaiter(done)); |
368 |
|
ForkJoinTask b = p.submit(awaiter(done)); |
369 |
|
ForkJoinTask c = p.submit(awaiter(done)); |
370 |
|
ForkJoinTask r = p.pollSubmission(); |
371 |
|
assertTrue(r == a || r == b || r == c); |
372 |
|
assertFalse(r.isDone()); |
383 |
– |
} finally { |
373 |
|
done.countDown(); |
385 |
– |
joinPool(p); |
374 |
|
} |
375 |
|
} |
376 |
|
|
380 |
|
public void testDrainTasksTo() { |
381 |
|
final CountDownLatch done = new CountDownLatch(1); |
382 |
|
SubFJP p = new SubFJP(); |
383 |
< |
try { |
383 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
384 |
|
ForkJoinTask a = p.submit(awaiter(done)); |
385 |
|
ForkJoinTask b = p.submit(awaiter(done)); |
386 |
|
ForkJoinTask c = p.submit(awaiter(done)); |
391 |
|
assertTrue(r == a || r == b || r == c); |
392 |
|
assertFalse(r.isDone()); |
393 |
|
} |
406 |
– |
} finally { |
394 |
|
done.countDown(); |
408 |
– |
joinPool(p); |
395 |
|
} |
396 |
|
} |
397 |
|
|
402 |
|
*/ |
403 |
|
public void testExecuteRunnable() throws Throwable { |
404 |
|
ExecutorService e = new ForkJoinPool(1); |
405 |
< |
try { |
405 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
406 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
407 |
|
Future<?> future = e.submit(new CheckedRunnable() { |
408 |
|
public void realRun() { |
413 |
|
assertTrue(done.get()); |
414 |
|
assertTrue(future.isDone()); |
415 |
|
assertFalse(future.isCancelled()); |
430 |
– |
} finally { |
431 |
– |
joinPool(e); |
416 |
|
} |
417 |
|
} |
418 |
|
|
421 |
|
*/ |
422 |
|
public void testSubmitCallable() throws Throwable { |
423 |
|
ExecutorService e = new ForkJoinPool(1); |
424 |
< |
try { |
424 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
425 |
|
Future<String> future = e.submit(new StringTask()); |
426 |
|
assertSame(TEST_STRING, future.get()); |
427 |
|
assertTrue(future.isDone()); |
428 |
|
assertFalse(future.isCancelled()); |
445 |
– |
} finally { |
446 |
– |
joinPool(e); |
429 |
|
} |
430 |
|
} |
431 |
|
|
434 |
|
*/ |
435 |
|
public void testSubmitRunnable() throws Throwable { |
436 |
|
ExecutorService e = new ForkJoinPool(1); |
437 |
< |
try { |
437 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
438 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
439 |
|
assertNull(future.get()); |
440 |
|
assertTrue(future.isDone()); |
441 |
|
assertFalse(future.isCancelled()); |
460 |
– |
} finally { |
461 |
– |
joinPool(e); |
442 |
|
} |
443 |
|
} |
444 |
|
|
447 |
|
*/ |
448 |
|
public void testSubmitRunnable2() throws Throwable { |
449 |
|
ExecutorService e = new ForkJoinPool(1); |
450 |
< |
try { |
450 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
451 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
452 |
|
assertSame(TEST_STRING, future.get()); |
453 |
|
assertTrue(future.isDone()); |
454 |
|
assertFalse(future.isCancelled()); |
475 |
– |
} finally { |
476 |
– |
joinPool(e); |
455 |
|
} |
456 |
|
} |
457 |
|
|
464 |
|
Runnable r = new CheckedRunnable() { |
465 |
|
public void realRun() throws Exception { |
466 |
|
ExecutorService e = new ForkJoinPool(1); |
467 |
< |
try { |
467 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
468 |
|
Future future = e.submit(callable); |
469 |
|
assertSame(TEST_STRING, future.get()); |
492 |
– |
} finally { |
493 |
– |
joinPool(e); |
470 |
|
} |
471 |
|
}}; |
472 |
|
|
483 |
|
Runnable r = new CheckedRunnable() { |
484 |
|
public void realRun() throws Exception { |
485 |
|
ExecutorService e = new ForkJoinPool(1); |
486 |
< |
try { |
486 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
487 |
|
Future future = e.submit(callable); |
488 |
|
assertSame(TEST_STRING, future.get()); |
513 |
– |
} finally { |
514 |
– |
joinPool(e); |
489 |
|
} |
490 |
|
}}; |
491 |
|
|
502 |
|
Runnable r = new CheckedRunnable() { |
503 |
|
public void realRun() throws Exception { |
504 |
|
ExecutorService e = new ForkJoinPool(1); |
505 |
< |
try { |
505 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
506 |
|
Future future = e.submit(callable); |
507 |
|
try { |
508 |
|
future.get(); |
510 |
|
} catch (ExecutionException success) { |
511 |
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
512 |
|
} |
539 |
– |
} finally { |
540 |
– |
joinPool(e); |
513 |
|
} |
514 |
|
}}; |
515 |
|
|
521 |
|
*/ |
522 |
|
public void testExecuteNullRunnable() { |
523 |
|
ExecutorService e = new ForkJoinPool(1); |
524 |
< |
try { |
525 |
< |
Future<?> future = e.submit((Runnable) null); |
526 |
< |
shouldThrow(); |
527 |
< |
} catch (NullPointerException success) { |
528 |
< |
} finally { |
557 |
< |
joinPool(e); |
524 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
525 |
> |
try { |
526 |
> |
Future<?> future = e.submit((Runnable) null); |
527 |
> |
shouldThrow(); |
528 |
> |
} catch (NullPointerException success) {} |
529 |
|
} |
530 |
|
} |
531 |
|
|
534 |
|
*/ |
535 |
|
public void testSubmitNullCallable() { |
536 |
|
ExecutorService e = new ForkJoinPool(1); |
537 |
< |
try { |
538 |
< |
Future<String> future = e.submit((Callable) null); |
539 |
< |
shouldThrow(); |
540 |
< |
} catch (NullPointerException success) { |
541 |
< |
} finally { |
571 |
< |
joinPool(e); |
537 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
538 |
> |
try { |
539 |
> |
Future<String> future = e.submit((Callable) null); |
540 |
> |
shouldThrow(); |
541 |
> |
} catch (NullPointerException success) {} |
542 |
|
} |
543 |
|
} |
544 |
|
|
548 |
|
public void testInterruptedSubmit() throws InterruptedException { |
549 |
|
final CountDownLatch submitted = new CountDownLatch(1); |
550 |
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
581 |
– |
final ExecutorService p = new ForkJoinPool(1); |
551 |
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
552 |
|
public Void realCall() throws InterruptedException { |
553 |
< |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
553 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
554 |
|
return null; |
555 |
|
}}; |
556 |
< |
try { |
556 |
> |
final ExecutorService p = new ForkJoinPool(1); |
557 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
558 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
559 |
|
public void realRun() throws Exception { |
560 |
|
Future<Void> future = p.submit(awaiter); |
562 |
|
future.get(); |
563 |
|
}}); |
564 |
|
t.start(); |
565 |
< |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
565 |
> |
await(submitted); |
566 |
|
t.interrupt(); |
567 |
< |
t.join(); |
598 |
< |
} finally { |
599 |
< |
quittingTime.countDown(); |
600 |
< |
joinPool(p); |
567 |
> |
awaitTermination(t); |
568 |
|
} |
569 |
|
} |
570 |
|
|
574 |
|
*/ |
575 |
|
public void testSubmitEE() throws Throwable { |
576 |
|
ForkJoinPool p = new ForkJoinPool(1); |
577 |
< |
try { |
578 |
< |
p.submit(new Callable() { |
579 |
< |
public Object call() { throw new ArithmeticException(); }}) |
580 |
< |
.get(); |
581 |
< |
shouldThrow(); |
582 |
< |
} catch (ExecutionException success) { |
583 |
< |
assertTrue(success.getCause() instanceof ArithmeticException); |
584 |
< |
} finally { |
585 |
< |
joinPool(p); |
577 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
578 |
> |
try { |
579 |
> |
p.submit(new Callable() { |
580 |
> |
public Object call() { throw new ArithmeticException(); }}) |
581 |
> |
.get(); |
582 |
> |
shouldThrow(); |
583 |
> |
} catch (ExecutionException success) { |
584 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
585 |
> |
} |
586 |
|
} |
587 |
|
} |
588 |
|
|
591 |
|
*/ |
592 |
|
public void testInvokeAny1() throws Throwable { |
593 |
|
ExecutorService e = new ForkJoinPool(1); |
594 |
< |
try { |
595 |
< |
e.invokeAny(null); |
596 |
< |
shouldThrow(); |
597 |
< |
} catch (NullPointerException success) { |
598 |
< |
} finally { |
632 |
< |
joinPool(e); |
594 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
595 |
> |
try { |
596 |
> |
e.invokeAny(null); |
597 |
> |
shouldThrow(); |
598 |
> |
} catch (NullPointerException success) {} |
599 |
|
} |
600 |
|
} |
601 |
|
|
604 |
|
*/ |
605 |
|
public void testInvokeAny2() throws Throwable { |
606 |
|
ExecutorService e = new ForkJoinPool(1); |
607 |
< |
try { |
608 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
609 |
< |
shouldThrow(); |
610 |
< |
} catch (IllegalArgumentException success) { |
611 |
< |
} finally { |
646 |
< |
joinPool(e); |
607 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
608 |
> |
try { |
609 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
610 |
> |
shouldThrow(); |
611 |
> |
} catch (IllegalArgumentException success) {} |
612 |
|
} |
613 |
|
} |
614 |
|
|
617 |
|
*/ |
618 |
|
public void testInvokeAny3() throws Throwable { |
619 |
|
ExecutorService e = new ForkJoinPool(1); |
620 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
621 |
< |
l.add(null); |
622 |
< |
try { |
623 |
< |
e.invokeAny(l); |
624 |
< |
shouldThrow(); |
625 |
< |
} catch (NullPointerException success) { |
626 |
< |
} finally { |
662 |
< |
joinPool(e); |
620 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
621 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
622 |
> |
l.add(null); |
623 |
> |
try { |
624 |
> |
e.invokeAny(l); |
625 |
> |
shouldThrow(); |
626 |
> |
} catch (NullPointerException success) {} |
627 |
|
} |
628 |
|
} |
629 |
|
|
633 |
|
public void testInvokeAny4() throws Throwable { |
634 |
|
CountDownLatch latch = new CountDownLatch(1); |
635 |
|
ExecutorService e = new ForkJoinPool(1); |
636 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
637 |
< |
l.add(latchAwaitingStringTask(latch)); |
638 |
< |
l.add(null); |
639 |
< |
try { |
640 |
< |
e.invokeAny(l); |
641 |
< |
shouldThrow(); |
642 |
< |
} catch (NullPointerException success) { |
643 |
< |
} finally { |
636 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
637 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
638 |
> |
l.add(latchAwaitingStringTask(latch)); |
639 |
> |
l.add(null); |
640 |
> |
try { |
641 |
> |
e.invokeAny(l); |
642 |
> |
shouldThrow(); |
643 |
> |
} catch (NullPointerException success) {} |
644 |
|
latch.countDown(); |
681 |
– |
joinPool(e); |
645 |
|
} |
646 |
|
} |
647 |
|
|
650 |
|
*/ |
651 |
|
public void testInvokeAny5() throws Throwable { |
652 |
|
ExecutorService e = new ForkJoinPool(1); |
653 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
654 |
< |
l.add(new NPETask()); |
655 |
< |
try { |
656 |
< |
e.invokeAny(l); |
657 |
< |
shouldThrow(); |
658 |
< |
} catch (ExecutionException success) { |
659 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
660 |
< |
} finally { |
661 |
< |
joinPool(e); |
653 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
654 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
655 |
> |
l.add(new NPETask()); |
656 |
> |
try { |
657 |
> |
e.invokeAny(l); |
658 |
> |
shouldThrow(); |
659 |
> |
} catch (ExecutionException success) { |
660 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
661 |
> |
} |
662 |
|
} |
663 |
|
} |
664 |
|
|
667 |
|
*/ |
668 |
|
public void testInvokeAny6() throws Throwable { |
669 |
|
ExecutorService e = new ForkJoinPool(1); |
670 |
< |
try { |
670 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
671 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
672 |
|
l.add(new StringTask()); |
673 |
|
l.add(new StringTask()); |
674 |
|
String result = e.invokeAny(l); |
675 |
|
assertSame(TEST_STRING, result); |
713 |
– |
} finally { |
714 |
– |
joinPool(e); |
676 |
|
} |
677 |
|
} |
678 |
|
|
681 |
|
*/ |
682 |
|
public void testInvokeAll1() throws Throwable { |
683 |
|
ExecutorService e = new ForkJoinPool(1); |
684 |
< |
try { |
685 |
< |
e.invokeAll(null); |
686 |
< |
shouldThrow(); |
687 |
< |
} catch (NullPointerException success) { |
688 |
< |
} finally { |
728 |
< |
joinPool(e); |
684 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
685 |
> |
try { |
686 |
> |
e.invokeAll(null); |
687 |
> |
shouldThrow(); |
688 |
> |
} catch (NullPointerException success) {} |
689 |
|
} |
690 |
|
} |
691 |
|
|
694 |
|
*/ |
695 |
|
public void testInvokeAll2() throws InterruptedException { |
696 |
|
ExecutorService e = new ForkJoinPool(1); |
697 |
< |
try { |
697 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
698 |
|
List<Future<String>> r |
699 |
|
= e.invokeAll(new ArrayList<Callable<String>>()); |
700 |
|
assertTrue(r.isEmpty()); |
741 |
– |
} finally { |
742 |
– |
joinPool(e); |
701 |
|
} |
702 |
|
} |
703 |
|
|
706 |
|
*/ |
707 |
|
public void testInvokeAll3() throws InterruptedException { |
708 |
|
ExecutorService e = new ForkJoinPool(1); |
709 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
710 |
< |
l.add(new StringTask()); |
711 |
< |
l.add(null); |
712 |
< |
try { |
713 |
< |
e.invokeAll(l); |
714 |
< |
shouldThrow(); |
715 |
< |
} catch (NullPointerException success) { |
716 |
< |
} finally { |
759 |
< |
joinPool(e); |
709 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
710 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
711 |
> |
l.add(new StringTask()); |
712 |
> |
l.add(null); |
713 |
> |
try { |
714 |
> |
e.invokeAll(l); |
715 |
> |
shouldThrow(); |
716 |
> |
} catch (NullPointerException success) {} |
717 |
|
} |
718 |
|
} |
719 |
|
|
723 |
|
*/ |
724 |
|
public void testInvokeAll4() throws Throwable { |
725 |
|
ExecutorService e = new ForkJoinPool(1); |
726 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
727 |
< |
l.add(new NPETask()); |
728 |
< |
List<Future<String>> futures = e.invokeAll(l); |
729 |
< |
assertEquals(1, futures.size()); |
730 |
< |
try { |
731 |
< |
futures.get(0).get(); |
732 |
< |
shouldThrow(); |
733 |
< |
} catch (ExecutionException success) { |
734 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
735 |
< |
} finally { |
736 |
< |
joinPool(e); |
726 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
727 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
728 |
> |
l.add(new NPETask()); |
729 |
> |
List<Future<String>> futures = e.invokeAll(l); |
730 |
> |
assertEquals(1, futures.size()); |
731 |
> |
try { |
732 |
> |
futures.get(0).get(); |
733 |
> |
shouldThrow(); |
734 |
> |
} catch (ExecutionException success) { |
735 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
736 |
> |
} |
737 |
|
} |
738 |
|
} |
739 |
|
|
742 |
|
*/ |
743 |
|
public void testInvokeAll5() throws Throwable { |
744 |
|
ExecutorService e = new ForkJoinPool(1); |
745 |
< |
try { |
745 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
746 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
747 |
|
l.add(new StringTask()); |
748 |
|
l.add(new StringTask()); |
750 |
|
assertEquals(2, futures.size()); |
751 |
|
for (Future<String> future : futures) |
752 |
|
assertSame(TEST_STRING, future.get()); |
796 |
– |
} finally { |
797 |
– |
joinPool(e); |
753 |
|
} |
754 |
|
} |
755 |
|
|
758 |
|
*/ |
759 |
|
public void testTimedInvokeAny1() throws Throwable { |
760 |
|
ExecutorService e = new ForkJoinPool(1); |
761 |
< |
try { |
762 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
763 |
< |
shouldThrow(); |
764 |
< |
} catch (NullPointerException success) { |
765 |
< |
} finally { |
811 |
< |
joinPool(e); |
761 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
762 |
> |
try { |
763 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
764 |
> |
shouldThrow(); |
765 |
> |
} catch (NullPointerException success) {} |
766 |
|
} |
767 |
|
} |
768 |
|
|
771 |
|
*/ |
772 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
773 |
|
ExecutorService e = new ForkJoinPool(1); |
774 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
775 |
< |
l.add(new StringTask()); |
776 |
< |
try { |
777 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
778 |
< |
shouldThrow(); |
779 |
< |
} catch (NullPointerException success) { |
780 |
< |
} finally { |
827 |
< |
joinPool(e); |
774 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
775 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
776 |
> |
l.add(new StringTask()); |
777 |
> |
try { |
778 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
779 |
> |
shouldThrow(); |
780 |
> |
} catch (NullPointerException success) {} |
781 |
|
} |
782 |
|
} |
783 |
|
|
786 |
|
*/ |
787 |
|
public void testTimedInvokeAny2() throws Throwable { |
788 |
|
ExecutorService e = new ForkJoinPool(1); |
789 |
< |
try { |
790 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), |
791 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
792 |
< |
shouldThrow(); |
793 |
< |
} catch (IllegalArgumentException success) { |
794 |
< |
} finally { |
842 |
< |
joinPool(e); |
789 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
790 |
> |
try { |
791 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
792 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
793 |
> |
shouldThrow(); |
794 |
> |
} catch (IllegalArgumentException success) {} |
795 |
|
} |
796 |
|
} |
797 |
|
|
801 |
|
public void testTimedInvokeAny3() throws Throwable { |
802 |
|
CountDownLatch latch = new CountDownLatch(1); |
803 |
|
ExecutorService e = new ForkJoinPool(1); |
804 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
805 |
< |
l.add(latchAwaitingStringTask(latch)); |
806 |
< |
l.add(null); |
807 |
< |
try { |
808 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
809 |
< |
shouldThrow(); |
810 |
< |
} catch (NullPointerException success) { |
811 |
< |
} finally { |
804 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
805 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
806 |
> |
l.add(latchAwaitingStringTask(latch)); |
807 |
> |
l.add(null); |
808 |
> |
try { |
809 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
810 |
> |
shouldThrow(); |
811 |
> |
} catch (NullPointerException success) {} |
812 |
|
latch.countDown(); |
861 |
– |
joinPool(e); |
813 |
|
} |
814 |
|
} |
815 |
|
|
818 |
|
*/ |
819 |
|
public void testTimedInvokeAny4() throws Throwable { |
820 |
|
ExecutorService e = new ForkJoinPool(1); |
821 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
822 |
< |
l.add(new NPETask()); |
823 |
< |
try { |
824 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
825 |
< |
shouldThrow(); |
826 |
< |
} catch (ExecutionException success) { |
827 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
828 |
< |
} finally { |
829 |
< |
joinPool(e); |
821 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
822 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
823 |
> |
l.add(new NPETask()); |
824 |
> |
try { |
825 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
826 |
> |
shouldThrow(); |
827 |
> |
} catch (ExecutionException success) { |
828 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
829 |
> |
} |
830 |
|
} |
831 |
|
} |
832 |
|
|
835 |
|
*/ |
836 |
|
public void testTimedInvokeAny5() throws Throwable { |
837 |
|
ExecutorService e = new ForkJoinPool(1); |
838 |
< |
try { |
838 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
839 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
840 |
|
l.add(new StringTask()); |
841 |
|
l.add(new StringTask()); |
842 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
843 |
|
assertSame(TEST_STRING, result); |
893 |
– |
} finally { |
894 |
– |
joinPool(e); |
844 |
|
} |
845 |
|
} |
846 |
|
|
849 |
|
*/ |
850 |
|
public void testTimedInvokeAll1() throws Throwable { |
851 |
|
ExecutorService e = new ForkJoinPool(1); |
852 |
< |
try { |
853 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
854 |
< |
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) { |
856 |
< |
} finally { |
908 |
< |
joinPool(e); |
852 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
853 |
> |
try { |
854 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
855 |
> |
shouldThrow(); |
856 |
> |
} catch (NullPointerException success) {} |
857 |
|
} |
858 |
|
} |
859 |
|
|
862 |
|
*/ |
863 |
|
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
864 |
|
ExecutorService e = new ForkJoinPool(1); |
865 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
866 |
< |
l.add(new StringTask()); |
867 |
< |
try { |
868 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
869 |
< |
shouldThrow(); |
870 |
< |
} catch (NullPointerException success) { |
871 |
< |
} finally { |
924 |
< |
joinPool(e); |
865 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
866 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
867 |
> |
l.add(new StringTask()); |
868 |
> |
try { |
869 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
870 |
> |
shouldThrow(); |
871 |
> |
} catch (NullPointerException success) {} |
872 |
|
} |
873 |
|
} |
874 |
|
|
877 |
|
*/ |
878 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
879 |
|
ExecutorService e = new ForkJoinPool(1); |
880 |
< |
try { |
880 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
881 |
|
List<Future<String>> r |
882 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
883 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
884 |
|
assertTrue(r.isEmpty()); |
938 |
– |
} finally { |
939 |
– |
joinPool(e); |
885 |
|
} |
886 |
|
} |
887 |
|
|
890 |
|
*/ |
891 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
892 |
|
ExecutorService e = new ForkJoinPool(1); |
893 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
894 |
< |
l.add(new StringTask()); |
895 |
< |
l.add(null); |
896 |
< |
try { |
897 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
898 |
< |
shouldThrow(); |
899 |
< |
} catch (NullPointerException success) { |
900 |
< |
} finally { |
956 |
< |
joinPool(e); |
893 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
894 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
895 |
> |
l.add(new StringTask()); |
896 |
> |
l.add(null); |
897 |
> |
try { |
898 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
899 |
> |
shouldThrow(); |
900 |
> |
} catch (NullPointerException success) {} |
901 |
|
} |
902 |
|
} |
903 |
|
|
906 |
|
*/ |
907 |
|
public void testTimedInvokeAll4() throws Throwable { |
908 |
|
ExecutorService e = new ForkJoinPool(1); |
909 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
910 |
< |
l.add(new NPETask()); |
911 |
< |
List<Future<String>> futures |
912 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
913 |
< |
assertEquals(1, futures.size()); |
914 |
< |
try { |
915 |
< |
futures.get(0).get(); |
916 |
< |
shouldThrow(); |
917 |
< |
} catch (ExecutionException success) { |
918 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
919 |
< |
} finally { |
920 |
< |
joinPool(e); |
909 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
910 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
911 |
> |
l.add(new NPETask()); |
912 |
> |
List<Future<String>> futures |
913 |
> |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
914 |
> |
assertEquals(1, futures.size()); |
915 |
> |
try { |
916 |
> |
futures.get(0).get(); |
917 |
> |
shouldThrow(); |
918 |
> |
} catch (ExecutionException success) { |
919 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
920 |
> |
} |
921 |
|
} |
922 |
|
} |
923 |
|
|
925 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
926 |
|
*/ |
927 |
|
public void testTimedInvokeAll5() throws Throwable { |
928 |
< |
ExecutorService e = new ForkJoinPool(1); |
929 |
< |
try { |
928 |
> |
ForkJoinPool e = new ForkJoinPool(1); |
929 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
930 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
931 |
|
l.add(new StringTask()); |
932 |
|
l.add(new StringTask()); |
935 |
|
assertEquals(2, futures.size()); |
936 |
|
for (Future<String> future : futures) |
937 |
|
assertSame(TEST_STRING, future.get()); |
994 |
– |
} finally { |
995 |
– |
joinPool(e); |
938 |
|
} |
939 |
|
} |
940 |
|
|