8 |
|
import junit.framework.*; |
9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
import java.util.concurrent.locks.*; |
13 |
|
import java.security.*; |
14 |
|
|
15 |
< |
public class ForkJoinPoolTest extends JSR166TestCase{ |
15 |
> |
public class ForkJoinPoolTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
|
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
51 |
|
protected void onStart() { throw new Error(); } |
52 |
|
} |
53 |
|
|
54 |
< |
static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
54 |
> |
static class FailingThreadFactory |
55 |
> |
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
56 |
|
int calls = 0; |
57 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
58 |
|
if (++calls > 1) return null; |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* Succesfully constructed pool reports default factory, |
140 |
> |
* Successfully constructed pool reports default factory, |
141 |
|
* parallelism and async mode policies, no active threads or |
142 |
|
* tasks, and quiescent running state. |
143 |
|
*/ |
145 |
|
ForkJoinPool p = null; |
146 |
|
try { |
147 |
|
p = new ForkJoinPool(1); |
148 |
< |
assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
148 |
> |
assertTrue(p.getFactory() == |
149 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
150 |
|
assertTrue(p.isQuiescent()); |
151 |
|
assertTrue(p.getMaintainsParallelism()); |
152 |
|
assertFalse(p.getAsyncMode()); |
170 |
|
try { |
171 |
|
new ForkJoinPool(-1); |
172 |
|
shouldThrow(); |
173 |
< |
} |
171 |
< |
catch (IllegalArgumentException success) {} |
173 |
> |
} catch (IllegalArgumentException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
180 |
|
try { |
181 |
|
new ForkJoinPool(1, null); |
182 |
|
shouldThrow(); |
183 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
183 |
> |
} catch (NullPointerException success) {} |
184 |
|
} |
185 |
|
|
186 |
|
|
309 |
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
310 |
|
* performs its defined action |
311 |
|
*/ |
312 |
< |
public void testSetUncaughtExceptionHandler() { |
312 |
> |
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
313 |
|
ForkJoinPool p = null; |
314 |
|
try { |
315 |
|
p = new ForkJoinPool(1, new FailingThreadFactory()); |
319 |
|
p.execute(new FailingTask()); |
320 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
321 |
|
assertTrue(eh.catches > 0); |
321 |
– |
} catch (InterruptedException e) { |
322 |
– |
unexpectedException(); |
322 |
|
} finally { |
323 |
|
joinPool(p); |
324 |
|
} |
344 |
|
* queues are empty, threads are not active, and |
345 |
|
* construction parameters continue to hold |
346 |
|
*/ |
347 |
< |
public void testisQuiescent() { |
347 |
> |
public void testisQuiescent() throws InterruptedException { |
348 |
|
ForkJoinPool p = null; |
349 |
|
try { |
350 |
|
p = new ForkJoinPool(2); |
351 |
|
p.invoke(new FibTask(20)); |
352 |
< |
assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
352 |
> |
assertTrue(p.getFactory() == |
353 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
354 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
355 |
|
assertTrue(p.isQuiescent()); |
356 |
|
assertTrue(p.getMaintainsParallelism()); |
362 |
|
assertFalse(p.isShutdown()); |
363 |
|
assertFalse(p.isTerminating()); |
364 |
|
assertFalse(p.isTerminated()); |
365 |
– |
} catch (InterruptedException e) { |
366 |
– |
unexpectedException(); |
365 |
|
} finally { |
366 |
|
joinPool(p); |
367 |
|
} |
370 |
|
/** |
371 |
|
* Completed submit(ForkJoinTask) returns result |
372 |
|
*/ |
373 |
< |
public void testSubmitForkJoinTask() { |
373 |
> |
public void testSubmitForkJoinTask() throws Throwable { |
374 |
|
ForkJoinPool p = null; |
375 |
|
try { |
376 |
|
p = new ForkJoinPool(1); |
377 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
378 |
|
int r = f.get(); |
379 |
|
assertTrue(r == 21); |
382 |
– |
} catch (ExecutionException ex) { |
383 |
– |
unexpectedException(); |
384 |
– |
} catch (InterruptedException ex) { |
385 |
– |
unexpectedException(); |
380 |
|
} finally { |
381 |
|
joinPool(p); |
382 |
|
} |
402 |
|
/** |
403 |
|
* Pool maintains parallelism when using ManagedBlocker |
404 |
|
*/ |
405 |
< |
public void testBlockingForkJoinTask() { |
405 |
> |
public void testBlockingForkJoinTask() throws Throwable { |
406 |
|
ForkJoinPool p = null; |
407 |
|
try { |
408 |
|
p = new ForkJoinPool(4); |
413 |
|
assertTrue(p.getPoolSize() >= 4); |
414 |
|
int r = f.get(); |
415 |
|
assertTrue(r == 832040); |
422 |
– |
} catch (ExecutionException ex) { |
423 |
– |
unexpectedException(); |
424 |
– |
} catch (InterruptedException ex) { |
425 |
– |
unexpectedException(); |
416 |
|
} finally { |
417 |
< |
joinPool(p); |
417 |
> |
p.shutdownNow(); // don't wait out shutdown |
418 |
|
} |
419 |
|
} |
420 |
|
|
464 |
|
/** |
465 |
|
* execute(runnable) runs it to completion |
466 |
|
*/ |
467 |
< |
public void testExecuteRunnable() { |
468 |
< |
try { |
469 |
< |
ExecutorService e = new ForkJoinPool(1); |
470 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
471 |
< |
assertFalse(task.done); |
472 |
< |
Future<?> future = e.submit(task); |
473 |
< |
future.get(); |
484 |
< |
assertTrue(task.done); |
485 |
< |
} |
486 |
< |
catch (ExecutionException ex) { |
487 |
< |
unexpectedException(); |
488 |
< |
} |
489 |
< |
catch (InterruptedException ex) { |
490 |
< |
unexpectedException(); |
491 |
< |
} |
467 |
> |
public void testExecuteRunnable() throws Throwable { |
468 |
> |
ExecutorService e = new ForkJoinPool(1); |
469 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
470 |
> |
assertFalse(task.done); |
471 |
> |
Future<?> future = e.submit(task); |
472 |
> |
future.get(); |
473 |
> |
assertTrue(task.done); |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
|
/** |
478 |
|
* Completed submit(callable) returns result |
479 |
|
*/ |
480 |
< |
public void testSubmitCallable() { |
481 |
< |
try { |
482 |
< |
ExecutorService e = new ForkJoinPool(1); |
483 |
< |
Future<String> future = e.submit(new StringTask()); |
484 |
< |
String result = future.get(); |
503 |
< |
assertSame(TEST_STRING, result); |
504 |
< |
} |
505 |
< |
catch (ExecutionException ex) { |
506 |
< |
unexpectedException(); |
507 |
< |
} |
508 |
< |
catch (InterruptedException ex) { |
509 |
< |
unexpectedException(); |
510 |
< |
} |
480 |
> |
public void testSubmitCallable() throws Throwable { |
481 |
> |
ExecutorService e = new ForkJoinPool(1); |
482 |
> |
Future<String> future = e.submit(new StringTask()); |
483 |
> |
String result = future.get(); |
484 |
> |
assertSame(TEST_STRING, result); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
|
* Completed submit(runnable) returns successfully |
489 |
|
*/ |
490 |
< |
public void testSubmitRunnable() { |
491 |
< |
try { |
492 |
< |
ExecutorService e = new ForkJoinPool(1); |
493 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
494 |
< |
future.get(); |
521 |
< |
assertTrue(future.isDone()); |
522 |
< |
} |
523 |
< |
catch (ExecutionException ex) { |
524 |
< |
unexpectedException(); |
525 |
< |
} |
526 |
< |
catch (InterruptedException ex) { |
527 |
< |
unexpectedException(); |
528 |
< |
} |
490 |
> |
public void testSubmitRunnable() throws Throwable { |
491 |
> |
ExecutorService e = new ForkJoinPool(1); |
492 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
493 |
> |
future.get(); |
494 |
> |
assertTrue(future.isDone()); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
|
* Completed submit(runnable, result) returns result |
499 |
|
*/ |
500 |
< |
public void testSubmitRunnable2() { |
501 |
< |
try { |
502 |
< |
ExecutorService e = new ForkJoinPool(1); |
503 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
504 |
< |
String result = future.get(); |
539 |
< |
assertSame(TEST_STRING, result); |
540 |
< |
} |
541 |
< |
catch (ExecutionException ex) { |
542 |
< |
unexpectedException(); |
543 |
< |
} |
544 |
< |
catch (InterruptedException ex) { |
545 |
< |
unexpectedException(); |
546 |
< |
} |
500 |
> |
public void testSubmitRunnable2() throws Throwable { |
501 |
> |
ExecutorService e = new ForkJoinPool(1); |
502 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
503 |
> |
String result = future.get(); |
504 |
> |
assertSame(TEST_STRING, result); |
505 |
|
} |
506 |
|
|
507 |
|
|
508 |
|
/** |
509 |
|
* A submitted privileged action to completion |
510 |
|
*/ |
511 |
< |
public void testSubmitPrivilegedAction() { |
511 |
> |
public void testSubmitPrivilegedAction() throws Throwable { |
512 |
|
Policy savedPolicy = null; |
513 |
|
try { |
514 |
|
savedPolicy = Policy.getPolicy(); |
529 |
|
Object result = future.get(); |
530 |
|
assertSame(TEST_STRING, result); |
531 |
|
} |
574 |
– |
catch (ExecutionException ex) { |
575 |
– |
unexpectedException(); |
576 |
– |
} |
577 |
– |
catch (InterruptedException ex) { |
578 |
– |
unexpectedException(); |
579 |
– |
} |
532 |
|
finally { |
533 |
< |
try { |
582 |
< |
Policy.setPolicy(savedPolicy); |
583 |
< |
} catch (AccessControlException ok) { |
584 |
< |
return; |
585 |
< |
} |
533 |
> |
Policy.setPolicy(savedPolicy); |
534 |
|
} |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
|
* A submitted a privileged exception action runs to completion |
539 |
|
*/ |
540 |
< |
public void testSubmitPrivilegedExceptionAction() { |
540 |
> |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
541 |
|
Policy savedPolicy = null; |
542 |
|
try { |
543 |
|
savedPolicy = Policy.getPolicy(); |
559 |
|
Object result = future.get(); |
560 |
|
assertSame(TEST_STRING, result); |
561 |
|
} |
614 |
– |
catch (ExecutionException ex) { |
615 |
– |
unexpectedException(); |
616 |
– |
} |
617 |
– |
catch (InterruptedException ex) { |
618 |
– |
unexpectedException(); |
619 |
– |
} |
562 |
|
finally { |
563 |
|
Policy.setPolicy(savedPolicy); |
564 |
|
} |
567 |
|
/** |
568 |
|
* A submitted failed privileged exception action reports exception |
569 |
|
*/ |
570 |
< |
public void testSubmitFailedPrivilegedExceptionAction() { |
570 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
571 |
|
Policy savedPolicy = null; |
572 |
|
try { |
573 |
|
savedPolicy = Policy.getPolicy(); |
589 |
|
|
590 |
|
Object result = future.get(); |
591 |
|
shouldThrow(); |
592 |
< |
} |
593 |
< |
catch (ExecutionException success) { |
594 |
< |
} catch (CancellationException success) { |
653 |
< |
} catch (InterruptedException ex) { |
654 |
< |
unexpectedException(); |
655 |
< |
} |
656 |
< |
finally { |
592 |
> |
} catch (ExecutionException success) { |
593 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
594 |
> |
} finally { |
595 |
|
Policy.setPolicy(savedPolicy); |
596 |
|
} |
597 |
|
} |
598 |
|
|
599 |
|
/** |
600 |
< |
* execute(null runnable) throws NPE |
600 |
> |
* execute(null runnable) throws NullPointerException |
601 |
|
*/ |
602 |
|
public void testExecuteNullRunnable() { |
603 |
|
try { |
605 |
|
TrackedShortRunnable task = null; |
606 |
|
Future<?> future = e.submit(task); |
607 |
|
shouldThrow(); |
608 |
< |
} |
671 |
< |
catch (NullPointerException success) { |
672 |
< |
} |
673 |
< |
catch (Exception ex) { |
674 |
< |
unexpectedException(); |
675 |
< |
} |
608 |
> |
} catch (NullPointerException success) {} |
609 |
|
} |
610 |
|
|
611 |
|
|
612 |
|
/** |
613 |
< |
* submit(null callable) throws NPE |
613 |
> |
* submit(null callable) throws NullPointerException |
614 |
|
*/ |
615 |
|
public void testSubmitNullCallable() { |
616 |
|
try { |
618 |
|
StringTask t = null; |
619 |
|
Future<String> future = e.submit(t); |
620 |
|
shouldThrow(); |
621 |
< |
} |
689 |
< |
catch (NullPointerException success) { |
690 |
< |
} |
691 |
< |
catch (Exception ex) { |
692 |
< |
unexpectedException(); |
693 |
< |
} |
621 |
> |
} catch (NullPointerException success) {} |
622 |
|
} |
623 |
|
|
624 |
|
|
625 |
|
/** |
626 |
< |
* Blocking on submit(callable) throws InterruptedException if |
627 |
< |
* caller interrupted. |
626 |
> |
* Blocking on submit(callable) throws InterruptedException if |
627 |
> |
* caller interrupted. |
628 |
|
*/ |
629 |
< |
public void testInterruptedSubmit() { |
629 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
630 |
|
final ForkJoinPool p = new ForkJoinPool(1); |
631 |
< |
Thread t = new Thread(new Runnable() { |
632 |
< |
public void run() { |
633 |
< |
try { |
634 |
< |
p.submit(new Callable<Object>() { |
635 |
< |
public Object call() { |
636 |
< |
try { |
637 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
638 |
< |
shouldThrow(); |
639 |
< |
} catch (InterruptedException e) { |
640 |
< |
} |
641 |
< |
return null; |
642 |
< |
} |
643 |
< |
}).get(); |
644 |
< |
} catch (InterruptedException success) { |
645 |
< |
} catch (Exception e) { |
646 |
< |
unexpectedException(); |
647 |
< |
} |
648 |
< |
|
721 |
< |
} |
722 |
< |
}); |
723 |
< |
try { |
724 |
< |
t.start(); |
725 |
< |
Thread.sleep(SHORT_DELAY_MS); |
726 |
< |
t.interrupt(); |
727 |
< |
} catch (Exception e) { |
728 |
< |
unexpectedException(); |
729 |
< |
} |
631 |
> |
|
632 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
633 |
> |
void realRun() throws Throwable { |
634 |
> |
p.submit(new CheckedCallable<Object>() { |
635 |
> |
public Object realCall() throws Throwable { |
636 |
> |
try { |
637 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
638 |
> |
} catch (InterruptedException ok) { |
639 |
> |
} |
640 |
> |
return null; |
641 |
> |
}}).get(); |
642 |
> |
}}); |
643 |
> |
|
644 |
> |
t.start(); |
645 |
> |
Thread.sleep(SHORT_DELAY_MS); |
646 |
> |
t.interrupt(); |
647 |
> |
t.join(); |
648 |
> |
p.shutdownNow(); |
649 |
|
joinPool(p); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
< |
* get of submit(callable) throws ExecutionException if callable |
654 |
< |
* throws exception |
653 |
> |
* get of submit(callable) throws ExecutionException if callable |
654 |
> |
* throws exception |
655 |
|
*/ |
656 |
< |
public void testSubmitEE() { |
656 |
> |
public void testSubmitEE() throws Throwable { |
657 |
|
ForkJoinPool p = new ForkJoinPool(1); |
739 |
– |
|
658 |
|
try { |
659 |
< |
Callable c = new Callable() { |
660 |
< |
public Object call() { |
661 |
< |
int i = 5/0; |
662 |
< |
return Boolean.TRUE; |
663 |
< |
} |
746 |
< |
}; |
747 |
< |
|
748 |
< |
for (int i = 0; i < 5; i++) { |
749 |
< |
p.submit(c).get(); |
750 |
< |
} |
751 |
< |
|
659 |
> |
p.submit(new Callable() { |
660 |
> |
public Object call() { |
661 |
> |
int i = 5/0; |
662 |
> |
return Boolean.TRUE; |
663 |
> |
}}).get(); |
664 |
|
shouldThrow(); |
665 |
|
} catch (ExecutionException success) { |
666 |
< |
} catch (CancellationException success) { |
755 |
< |
} catch (Exception e) { |
756 |
< |
unexpectedException(); |
666 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
667 |
|
} |
668 |
+ |
|
669 |
|
joinPool(p); |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
< |
* invokeAny(null) throws NPE |
673 |
> |
* invokeAny(null) throws NullPointerException |
674 |
|
*/ |
675 |
< |
public void testInvokeAny1() { |
675 |
> |
public void testInvokeAny1() throws Throwable { |
676 |
|
ExecutorService e = new ForkJoinPool(1); |
677 |
|
try { |
678 |
|
e.invokeAny(null); |
679 |
+ |
shouldThrow(); |
680 |
|
} catch (NullPointerException success) { |
769 |
– |
} catch (Exception ex) { |
770 |
– |
unexpectedException(); |
681 |
|
} finally { |
682 |
|
joinPool(e); |
683 |
|
} |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
< |
* invokeAny(empty collection) throws IAE |
687 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
688 |
|
*/ |
689 |
< |
public void testInvokeAny2() { |
689 |
> |
public void testInvokeAny2() throws Throwable { |
690 |
|
ExecutorService e = new ForkJoinPool(1); |
691 |
|
try { |
692 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
693 |
+ |
shouldThrow(); |
694 |
|
} catch (IllegalArgumentException success) { |
784 |
– |
} catch (Exception ex) { |
785 |
– |
unexpectedException(); |
695 |
|
} finally { |
696 |
|
joinPool(e); |
697 |
|
} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
< |
* invokeAny(c) throws NPE if c has null elements |
701 |
> |
* invokeAny(c) throws NullPointerException if c has a single null element |
702 |
|
*/ |
703 |
< |
public void testInvokeAny3() { |
703 |
> |
public void testInvokeAny3() throws Throwable { |
704 |
|
ExecutorService e = new ForkJoinPool(1); |
705 |
|
try { |
706 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
798 |
– |
l.add(new StringTask()); |
707 |
|
l.add(null); |
708 |
|
e.invokeAny(l); |
709 |
+ |
shouldThrow(); |
710 |
+ |
} catch (NullPointerException success) { |
711 |
+ |
} finally { |
712 |
+ |
joinPool(e); |
713 |
+ |
} |
714 |
+ |
} |
715 |
+ |
|
716 |
+ |
/** |
717 |
+ |
* invokeAny(c) throws NullPointerException if c has null elements |
718 |
+ |
*/ |
719 |
+ |
public void testInvokeAny4() throws Throwable { |
720 |
+ |
ExecutorService e = new ForkJoinPool(1); |
721 |
+ |
try { |
722 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
723 |
+ |
l.add(new Callable<String>() { |
724 |
+ |
public String call() { |
725 |
+ |
// The delay gives the pool a chance to notice |
726 |
+ |
// the null element. |
727 |
+ |
sleepTillInterrupted(SMALL_DELAY_MS); |
728 |
+ |
return "foo"; |
729 |
+ |
}}); |
730 |
+ |
l.add(null); |
731 |
+ |
e.invokeAny(l); |
732 |
+ |
shouldThrow(); |
733 |
|
} catch (NullPointerException success) { |
802 |
– |
} catch (Exception ex) { |
803 |
– |
ex.printStackTrace(); |
804 |
– |
unexpectedException(); |
734 |
|
} finally { |
735 |
|
joinPool(e); |
736 |
|
} |
739 |
|
/** |
740 |
|
* invokeAny(c) throws ExecutionException if no task in c completes |
741 |
|
*/ |
742 |
< |
public void testInvokeAny4() { |
742 |
> |
public void testInvokeAny5() throws Throwable { |
743 |
|
ExecutorService e = new ForkJoinPool(1); |
744 |
|
try { |
745 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
746 |
|
l.add(new NPETask()); |
747 |
|
e.invokeAny(l); |
748 |
+ |
shouldThrow(); |
749 |
|
} catch (ExecutionException success) { |
750 |
< |
} catch (CancellationException success) { |
821 |
< |
} catch (Exception ex) { |
822 |
< |
unexpectedException(); |
750 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
751 |
|
} finally { |
752 |
|
joinPool(e); |
753 |
|
} |
756 |
|
/** |
757 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
758 |
|
*/ |
759 |
< |
public void testInvokeAny5() { |
759 |
> |
public void testInvokeAny6() throws Throwable { |
760 |
|
ExecutorService e = new ForkJoinPool(1); |
761 |
|
try { |
762 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
764 |
|
l.add(new StringTask()); |
765 |
|
String result = e.invokeAny(l); |
766 |
|
assertSame(TEST_STRING, result); |
839 |
– |
} catch (ExecutionException success) { |
840 |
– |
} catch (CancellationException success) { |
841 |
– |
} catch (Exception ex) { |
842 |
– |
unexpectedException(); |
767 |
|
} finally { |
768 |
|
joinPool(e); |
769 |
|
} |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* invokeAll(null) throws NPE |
773 |
> |
* invokeAll(null) throws NullPointerException |
774 |
|
*/ |
775 |
< |
public void testInvokeAll1() { |
775 |
> |
public void testInvokeAll1() throws Throwable { |
776 |
|
ExecutorService e = new ForkJoinPool(1); |
777 |
|
try { |
778 |
|
e.invokeAll(null); |
779 |
+ |
shouldThrow(); |
780 |
|
} catch (NullPointerException success) { |
856 |
– |
} catch (Exception ex) { |
857 |
– |
unexpectedException(); |
781 |
|
} finally { |
782 |
|
joinPool(e); |
783 |
|
} |
786 |
|
/** |
787 |
|
* invokeAll(empty collection) returns empty collection |
788 |
|
*/ |
789 |
< |
public void testInvokeAll2() { |
789 |
> |
public void testInvokeAll2() throws InterruptedException { |
790 |
|
ExecutorService e = new ForkJoinPool(1); |
791 |
|
try { |
792 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
792 |
> |
List<Future<String>> r |
793 |
> |
= e.invokeAll(new ArrayList<Callable<String>>()); |
794 |
|
assertTrue(r.isEmpty()); |
871 |
– |
} catch (Exception ex) { |
872 |
– |
unexpectedException(); |
795 |
|
} finally { |
796 |
|
joinPool(e); |
797 |
|
} |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
< |
* invokeAll(c) throws NPE if c has null elements |
801 |
> |
* invokeAll(c) throws NullPointerException if c has null elements |
802 |
|
*/ |
803 |
< |
public void testInvokeAll3() { |
803 |
> |
public void testInvokeAll3() throws InterruptedException { |
804 |
|
ExecutorService e = new ForkJoinPool(1); |
805 |
|
try { |
806 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
807 |
|
l.add(new StringTask()); |
808 |
|
l.add(null); |
809 |
|
e.invokeAll(l); |
810 |
+ |
shouldThrow(); |
811 |
|
} catch (NullPointerException success) { |
889 |
– |
} catch (Exception ex) { |
890 |
– |
unexpectedException(); |
812 |
|
} finally { |
813 |
|
joinPool(e); |
814 |
|
} |
815 |
|
} |
816 |
|
|
817 |
|
/** |
818 |
< |
* get of returned element of invokeAll(c) throws exception on failed task |
818 |
> |
* get of returned element of invokeAll(c) throws |
819 |
> |
* ExecutionException on failed task |
820 |
|
*/ |
821 |
< |
public void testInvokeAll4() { |
821 |
> |
public void testInvokeAll4() throws Throwable { |
822 |
|
ExecutorService e = new ForkJoinPool(1); |
823 |
|
try { |
824 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
825 |
|
l.add(new NPETask()); |
826 |
|
List<Future<String>> result = e.invokeAll(l); |
827 |
|
assertEquals(1, result.size()); |
828 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
829 |
< |
it.next().get(); |
828 |
> |
for (Future<String> future : result) |
829 |
> |
future.get(); |
830 |
> |
shouldThrow(); |
831 |
|
} catch (ExecutionException success) { |
832 |
< |
} catch (CancellationException success) { |
910 |
< |
} catch (Exception ex) { |
911 |
< |
ex.printStackTrace(); |
912 |
< |
unexpectedException(); |
832 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
833 |
|
} finally { |
834 |
|
joinPool(e); |
835 |
|
} |
838 |
|
/** |
839 |
|
* invokeAll(c) returns results of all completed tasks in c |
840 |
|
*/ |
841 |
< |
public void testInvokeAll5() { |
841 |
> |
public void testInvokeAll5() throws Throwable { |
842 |
|
ExecutorService e = new ForkJoinPool(1); |
843 |
|
try { |
844 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
846 |
|
l.add(new StringTask()); |
847 |
|
List<Future<String>> result = e.invokeAll(l); |
848 |
|
assertEquals(2, result.size()); |
849 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
850 |
< |
assertSame(TEST_STRING, it.next().get()); |
931 |
< |
} catch (ExecutionException success) { |
932 |
< |
} catch (CancellationException success) { |
933 |
< |
} catch (Exception ex) { |
934 |
< |
ex.printStackTrace(); |
935 |
< |
unexpectedException(); |
849 |
> |
for (Future<String> future : result) |
850 |
> |
assertSame(TEST_STRING, future.get()); |
851 |
|
} finally { |
852 |
|
joinPool(e); |
853 |
|
} |
855 |
|
|
856 |
|
|
857 |
|
/** |
858 |
< |
* timed invokeAny(null) throws NPE |
858 |
> |
* timed invokeAny(null) throws NullPointerException |
859 |
|
*/ |
860 |
< |
public void testTimedInvokeAny1() { |
860 |
> |
public void testTimedInvokeAny1() throws Throwable { |
861 |
|
ExecutorService e = new ForkJoinPool(1); |
862 |
|
try { |
863 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
863 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
864 |
> |
shouldThrow(); |
865 |
|
} catch (NullPointerException success) { |
950 |
– |
} catch (Exception ex) { |
951 |
– |
ex.printStackTrace(); |
952 |
– |
unexpectedException(); |
866 |
|
} finally { |
867 |
|
joinPool(e); |
868 |
|
} |
869 |
|
} |
870 |
|
|
871 |
|
/** |
872 |
< |
* timed invokeAny(null time unit) throws NPE |
872 |
> |
* timed invokeAny(null time unit) throws NullPointerException |
873 |
|
*/ |
874 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
874 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
875 |
|
ExecutorService e = new ForkJoinPool(1); |
876 |
|
try { |
877 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
878 |
|
l.add(new StringTask()); |
879 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
880 |
+ |
shouldThrow(); |
881 |
|
} catch (NullPointerException success) { |
968 |
– |
} catch (Exception ex) { |
969 |
– |
ex.printStackTrace(); |
970 |
– |
unexpectedException(); |
882 |
|
} finally { |
883 |
|
joinPool(e); |
884 |
|
} |
885 |
|
} |
886 |
|
|
887 |
|
/** |
888 |
< |
* timed invokeAny(empty collection) throws IAE |
888 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
889 |
|
*/ |
890 |
< |
public void testTimedInvokeAny2() { |
890 |
> |
public void testTimedInvokeAny2() throws Throwable { |
891 |
|
ExecutorService e = new ForkJoinPool(1); |
892 |
|
try { |
893 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
893 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
894 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
895 |
> |
shouldThrow(); |
896 |
|
} catch (IllegalArgumentException success) { |
984 |
– |
} catch (Exception ex) { |
985 |
– |
ex.printStackTrace(); |
986 |
– |
unexpectedException(); |
897 |
|
} finally { |
898 |
|
joinPool(e); |
899 |
|
} |
900 |
|
} |
901 |
|
|
902 |
|
/** |
903 |
< |
* timed invokeAny(c) throws NPE if c has null elements |
903 |
> |
* timed invokeAny(c) throws NullPointerException if c has null elements |
904 |
|
*/ |
905 |
< |
public void testTimedInvokeAny3() { |
905 |
> |
public void testTimedInvokeAny3() throws Throwable { |
906 |
|
ExecutorService e = new ForkJoinPool(1); |
907 |
|
try { |
908 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
909 |
|
l.add(new StringTask()); |
910 |
|
l.add(null); |
911 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
911 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
912 |
> |
shouldThrow(); |
913 |
|
} catch (NullPointerException success) { |
1003 |
– |
} catch (Exception ex) { |
1004 |
– |
ex.printStackTrace(); |
1005 |
– |
unexpectedException(); |
914 |
|
} finally { |
915 |
|
joinPool(e); |
916 |
|
} |
919 |
|
/** |
920 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
921 |
|
*/ |
922 |
< |
public void testTimedInvokeAny4() { |
922 |
> |
public void testTimedInvokeAny4() throws Throwable { |
923 |
|
ExecutorService e = new ForkJoinPool(1); |
924 |
|
try { |
925 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
926 |
|
l.add(new NPETask()); |
927 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
927 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
928 |
> |
shouldThrow(); |
929 |
|
} catch (ExecutionException success) { |
930 |
< |
} catch (CancellationException success) { |
1022 |
< |
} catch (Exception ex) { |
1023 |
< |
ex.printStackTrace(); |
1024 |
< |
unexpectedException(); |
930 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
931 |
|
} finally { |
932 |
|
joinPool(e); |
933 |
|
} |
936 |
|
/** |
937 |
|
* timed invokeAny(c) returns result of some task in c |
938 |
|
*/ |
939 |
< |
public void testTimedInvokeAny5() { |
939 |
> |
public void testTimedInvokeAny5() throws Throwable { |
940 |
|
ExecutorService e = new ForkJoinPool(1); |
941 |
|
try { |
942 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
943 |
|
l.add(new StringTask()); |
944 |
|
l.add(new StringTask()); |
945 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
945 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
946 |
|
assertSame(TEST_STRING, result); |
1041 |
– |
} catch (ExecutionException success) { |
1042 |
– |
} catch (CancellationException success) { |
1043 |
– |
} catch (Exception ex) { |
1044 |
– |
ex.printStackTrace(); |
1045 |
– |
unexpectedException(); |
947 |
|
} finally { |
948 |
|
joinPool(e); |
949 |
|
} |
950 |
|
} |
951 |
|
|
952 |
|
/** |
953 |
< |
* timed invokeAll(null) throws NPE |
953 |
> |
* timed invokeAll(null) throws NullPointerException |
954 |
|
*/ |
955 |
< |
public void testTimedInvokeAll1() { |
955 |
> |
public void testTimedInvokeAll1() throws Throwable { |
956 |
|
ExecutorService e = new ForkJoinPool(1); |
957 |
|
try { |
958 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
958 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
959 |
> |
shouldThrow(); |
960 |
|
} catch (NullPointerException success) { |
1059 |
– |
} catch (Exception ex) { |
1060 |
– |
ex.printStackTrace(); |
1061 |
– |
unexpectedException(); |
961 |
|
} finally { |
962 |
|
joinPool(e); |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
< |
* timed invokeAll(null time unit) throws NPE |
967 |
> |
* timed invokeAll(null time unit) throws NullPointerException |
968 |
|
*/ |
969 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
969 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
970 |
|
ExecutorService e = new ForkJoinPool(1); |
971 |
|
try { |
972 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
973 |
|
l.add(new StringTask()); |
974 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
975 |
+ |
shouldThrow(); |
976 |
|
} catch (NullPointerException success) { |
1077 |
– |
} catch (Exception ex) { |
1078 |
– |
ex.printStackTrace(); |
1079 |
– |
unexpectedException(); |
977 |
|
} finally { |
978 |
|
joinPool(e); |
979 |
|
} |
982 |
|
/** |
983 |
|
* timed invokeAll(empty collection) returns empty collection |
984 |
|
*/ |
985 |
< |
public void testTimedInvokeAll2() { |
985 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
986 |
|
ExecutorService e = new ForkJoinPool(1); |
987 |
|
try { |
988 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
988 |
> |
List<Future<String>> r |
989 |
> |
= e.invokeAll(new ArrayList<Callable<String>>(), |
990 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
991 |
|
assertTrue(r.isEmpty()); |
1093 |
– |
} catch (Exception ex) { |
1094 |
– |
ex.printStackTrace(); |
1095 |
– |
unexpectedException(); |
992 |
|
} finally { |
993 |
|
joinPool(e); |
994 |
|
} |
995 |
|
} |
996 |
|
|
997 |
|
/** |
998 |
< |
* timed invokeAll(c) throws NPE if c has null elements |
998 |
> |
* timed invokeAll(c) throws NullPointerException if c has null elements |
999 |
|
*/ |
1000 |
< |
public void testTimedInvokeAll3() { |
1000 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
1001 |
|
ExecutorService e = new ForkJoinPool(1); |
1002 |
|
try { |
1003 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1004 |
|
l.add(new StringTask()); |
1005 |
|
l.add(null); |
1006 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1006 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1007 |
> |
shouldThrow(); |
1008 |
|
} catch (NullPointerException success) { |
1112 |
– |
} catch (Exception ex) { |
1113 |
– |
ex.printStackTrace(); |
1114 |
– |
unexpectedException(); |
1009 |
|
} finally { |
1010 |
|
joinPool(e); |
1011 |
|
} |
1014 |
|
/** |
1015 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
1016 |
|
*/ |
1017 |
< |
public void testTimedInvokeAll4() { |
1017 |
> |
public void testTimedInvokeAll4() throws Throwable { |
1018 |
|
ExecutorService e = new ForkJoinPool(1); |
1019 |
|
try { |
1020 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1021 |
|
l.add(new NPETask()); |
1022 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1022 |
> |
List<Future<String>> result |
1023 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1024 |
|
assertEquals(1, result.size()); |
1025 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1026 |
< |
it.next().get(); |
1025 |
> |
for (Future<String> future : result) |
1026 |
> |
future.get(); |
1027 |
> |
shouldThrow(); |
1028 |
|
} catch (ExecutionException success) { |
1029 |
< |
} catch (CancellationException success) { |
1134 |
< |
} catch (Exception ex) { |
1135 |
< |
ex.printStackTrace(); |
1136 |
< |
unexpectedException(); |
1029 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1030 |
|
} finally { |
1031 |
|
joinPool(e); |
1032 |
|
} |
1035 |
|
/** |
1036 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
1037 |
|
*/ |
1038 |
< |
public void testTimedInvokeAll5() { |
1038 |
> |
public void testTimedInvokeAll5() throws Throwable { |
1039 |
|
ExecutorService e = new ForkJoinPool(1); |
1040 |
|
try { |
1041 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1042 |
|
l.add(new StringTask()); |
1043 |
|
l.add(new StringTask()); |
1044 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1044 |
> |
List<Future<String>> result |
1045 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1046 |
|
assertEquals(2, result.size()); |
1047 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1048 |
< |
assertSame(TEST_STRING, it.next().get()); |
1155 |
< |
} catch (ExecutionException success) { |
1156 |
< |
} catch (CancellationException success) { |
1157 |
< |
} catch (Exception ex) { |
1158 |
< |
ex.printStackTrace(); |
1159 |
< |
unexpectedException(); |
1047 |
> |
for (Future<String> future : result) |
1048 |
> |
assertSame(TEST_STRING, future.get()); |
1049 |
|
} finally { |
1050 |
|
joinPool(e); |
1051 |
|
} |