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 |
|
|
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) { |
652 |
< |
} catch (CancellationException success) { |
653 |
< |
} catch (InterruptedException ex) { |
654 |
< |
unexpectedException(); |
655 |
< |
} |
656 |
< |
finally { |
592 |
> |
} catch (ExecutionException success) { |
593 |
> |
} finally { |
594 |
|
Policy.setPolicy(savedPolicy); |
595 |
|
} |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* execute(null runnable) throws NPE |
599 |
> |
* execute(null runnable) throws NullPointerException |
600 |
|
*/ |
601 |
|
public void testExecuteNullRunnable() { |
602 |
|
try { |
604 |
|
TrackedShortRunnable task = null; |
605 |
|
Future<?> future = e.submit(task); |
606 |
|
shouldThrow(); |
607 |
< |
} |
671 |
< |
catch (NullPointerException success) { |
672 |
< |
} |
673 |
< |
catch (Exception ex) { |
674 |
< |
unexpectedException(); |
675 |
< |
} |
607 |
> |
} catch (NullPointerException success) {} |
608 |
|
} |
609 |
|
|
610 |
|
|
611 |
|
/** |
612 |
< |
* submit(null callable) throws NPE |
612 |
> |
* submit(null callable) throws NullPointerException |
613 |
|
*/ |
614 |
|
public void testSubmitNullCallable() { |
615 |
|
try { |
617 |
|
StringTask t = null; |
618 |
|
Future<String> future = e.submit(t); |
619 |
|
shouldThrow(); |
620 |
< |
} |
689 |
< |
catch (NullPointerException success) { |
690 |
< |
} |
691 |
< |
catch (Exception ex) { |
692 |
< |
unexpectedException(); |
693 |
< |
} |
620 |
> |
} catch (NullPointerException success) {} |
621 |
|
} |
622 |
|
|
623 |
|
|
625 |
|
* Blocking on submit(callable) throws InterruptedException if |
626 |
|
* caller interrupted. |
627 |
|
*/ |
628 |
< |
public void testInterruptedSubmit() { |
628 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
629 |
|
final ForkJoinPool p = new ForkJoinPool(1); |
630 |
< |
Thread t = new Thread(new Runnable() { |
631 |
< |
public void run() { |
632 |
< |
try { |
633 |
< |
p.submit(new Callable<Object>() { |
634 |
< |
public Object call() { |
635 |
< |
try { |
636 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
637 |
< |
shouldThrow(); |
638 |
< |
} catch (InterruptedException e) { |
639 |
< |
} |
640 |
< |
return null; |
641 |
< |
} |
642 |
< |
}).get(); |
643 |
< |
} catch (InterruptedException success) { |
644 |
< |
} catch (Exception e) { |
645 |
< |
unexpectedException(); |
646 |
< |
} |
647 |
< |
|
721 |
< |
} |
722 |
< |
}); |
723 |
< |
try { |
724 |
< |
t.start(); |
725 |
< |
Thread.sleep(SHORT_DELAY_MS); |
726 |
< |
t.interrupt(); |
727 |
< |
} catch (Exception e) { |
728 |
< |
unexpectedException(); |
729 |
< |
} |
630 |
> |
|
631 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
632 |
> |
void realRun() throws Throwable { |
633 |
> |
p.submit(new CheckedCallable<Object>() { |
634 |
> |
public Object realCall() throws Throwable { |
635 |
> |
try { |
636 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
637 |
> |
} catch (InterruptedException ok) { |
638 |
> |
} |
639 |
> |
return null; |
640 |
> |
}}).get(); |
641 |
> |
}}); |
642 |
> |
|
643 |
> |
t.start(); |
644 |
> |
Thread.sleep(SHORT_DELAY_MS); |
645 |
> |
t.interrupt(); |
646 |
> |
t.join(); |
647 |
> |
p.shutdownNow(); |
648 |
|
joinPool(p); |
649 |
|
} |
650 |
|
|
652 |
|
* get of submit(callable) throws ExecutionException if callable |
653 |
|
* throws exception |
654 |
|
*/ |
655 |
< |
public void testSubmitEE() { |
655 |
> |
public void testSubmitEE() throws Throwable { |
656 |
|
ForkJoinPool p = new ForkJoinPool(1); |
739 |
– |
|
657 |
|
try { |
658 |
< |
Callable c = new Callable() { |
659 |
< |
public Object call() { |
660 |
< |
int i = 5/0; |
661 |
< |
return Boolean.TRUE; |
662 |
< |
} |
746 |
< |
}; |
747 |
< |
|
748 |
< |
for (int i = 0; i < 5; i++) { |
749 |
< |
p.submit(c).get(); |
750 |
< |
} |
751 |
< |
|
658 |
> |
p.submit(new Callable() { |
659 |
> |
public Object call() { |
660 |
> |
int i = 5/0; |
661 |
> |
return Boolean.TRUE; |
662 |
> |
}}).get(); |
663 |
|
shouldThrow(); |
664 |
< |
} catch (ExecutionException success) { |
665 |
< |
} catch (CancellationException success) { |
755 |
< |
} catch (Exception e) { |
756 |
< |
unexpectedException(); |
757 |
< |
} |
664 |
> |
} catch (ExecutionException success) {} |
665 |
> |
|
666 |
|
joinPool(p); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* invokeAny(null) throws NPE |
670 |
> |
* invokeAny(null) throws NullPointerException |
671 |
|
*/ |
672 |
< |
public void testInvokeAny1() { |
672 |
> |
public void testInvokeAny1() throws Throwable { |
673 |
|
ExecutorService e = new ForkJoinPool(1); |
674 |
|
try { |
675 |
|
e.invokeAny(null); |
676 |
+ |
shouldThrow(); |
677 |
|
} catch (NullPointerException success) { |
769 |
– |
} catch (Exception ex) { |
770 |
– |
unexpectedException(); |
678 |
|
} finally { |
679 |
|
joinPool(e); |
680 |
|
} |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
< |
* invokeAny(empty collection) throws IAE |
684 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
685 |
|
*/ |
686 |
< |
public void testInvokeAny2() { |
686 |
> |
public void testInvokeAny2() throws Throwable { |
687 |
|
ExecutorService e = new ForkJoinPool(1); |
688 |
|
try { |
689 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
690 |
+ |
shouldThrow(); |
691 |
|
} catch (IllegalArgumentException success) { |
784 |
– |
} catch (Exception ex) { |
785 |
– |
unexpectedException(); |
692 |
|
} finally { |
693 |
|
joinPool(e); |
694 |
|
} |
695 |
|
} |
696 |
|
|
697 |
|
/** |
698 |
< |
* invokeAny(c) throws NPE if c has null elements |
698 |
> |
* invokeAny(c) throws NullPointerException if c has a single null element |
699 |
|
*/ |
700 |
< |
public void testInvokeAny3() { |
700 |
> |
public void testInvokeAny3() throws Throwable { |
701 |
|
ExecutorService e = new ForkJoinPool(1); |
702 |
|
try { |
703 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
798 |
– |
l.add(new StringTask()); |
704 |
|
l.add(null); |
705 |
|
e.invokeAny(l); |
706 |
+ |
shouldThrow(); |
707 |
+ |
} catch (NullPointerException success) { |
708 |
+ |
} finally { |
709 |
+ |
joinPool(e); |
710 |
+ |
} |
711 |
+ |
} |
712 |
+ |
|
713 |
+ |
/** |
714 |
+ |
* invokeAny(c) throws NullPointerException if c has null elements |
715 |
+ |
*/ |
716 |
+ |
public void testInvokeAny4() throws Throwable { |
717 |
+ |
ExecutorService e = new ForkJoinPool(1); |
718 |
+ |
try { |
719 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
720 |
+ |
l.add(new Callable<String>() { |
721 |
+ |
public String call() { |
722 |
+ |
// The delay gives the pool a chance to notice |
723 |
+ |
// the null element. |
724 |
+ |
sleepTillInterrupted(SMALL_DELAY_MS); |
725 |
+ |
return "foo"; |
726 |
+ |
}}); |
727 |
+ |
l.add(null); |
728 |
+ |
e.invokeAny(l); |
729 |
+ |
shouldThrow(); |
730 |
|
} catch (NullPointerException success) { |
802 |
– |
} catch (Exception ex) { |
803 |
– |
ex.printStackTrace(); |
804 |
– |
unexpectedException(); |
731 |
|
} finally { |
732 |
|
joinPool(e); |
733 |
|
} |
736 |
|
/** |
737 |
|
* invokeAny(c) throws ExecutionException if no task in c completes |
738 |
|
*/ |
739 |
< |
public void testInvokeAny4() { |
739 |
> |
public void testInvokeAny5() throws Throwable { |
740 |
|
ExecutorService e = new ForkJoinPool(1); |
741 |
|
try { |
742 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
743 |
|
l.add(new NPETask()); |
744 |
|
e.invokeAny(l); |
745 |
+ |
shouldThrow(); |
746 |
|
} catch (ExecutionException success) { |
820 |
– |
} catch (CancellationException success) { |
821 |
– |
} catch (Exception ex) { |
822 |
– |
unexpectedException(); |
747 |
|
} finally { |
748 |
|
joinPool(e); |
749 |
|
} |
752 |
|
/** |
753 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
754 |
|
*/ |
755 |
< |
public void testInvokeAny5() { |
755 |
> |
public void testInvokeAny6() throws Throwable { |
756 |
|
ExecutorService e = new ForkJoinPool(1); |
757 |
|
try { |
758 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
760 |
|
l.add(new StringTask()); |
761 |
|
String result = e.invokeAny(l); |
762 |
|
assertSame(TEST_STRING, result); |
839 |
– |
} catch (ExecutionException success) { |
840 |
– |
} catch (CancellationException success) { |
841 |
– |
} catch (Exception ex) { |
842 |
– |
unexpectedException(); |
763 |
|
} finally { |
764 |
|
joinPool(e); |
765 |
|
} |
766 |
|
} |
767 |
|
|
768 |
|
/** |
769 |
< |
* invokeAll(null) throws NPE |
769 |
> |
* invokeAll(null) throws NullPointerException |
770 |
|
*/ |
771 |
< |
public void testInvokeAll1() { |
771 |
> |
public void testInvokeAll1() throws Throwable { |
772 |
|
ExecutorService e = new ForkJoinPool(1); |
773 |
|
try { |
774 |
|
e.invokeAll(null); |
775 |
+ |
shouldThrow(); |
776 |
|
} catch (NullPointerException success) { |
856 |
– |
} catch (Exception ex) { |
857 |
– |
unexpectedException(); |
777 |
|
} finally { |
778 |
|
joinPool(e); |
779 |
|
} |
782 |
|
/** |
783 |
|
* invokeAll(empty collection) returns empty collection |
784 |
|
*/ |
785 |
< |
public void testInvokeAll2() { |
785 |
> |
public void testInvokeAll2() throws InterruptedException { |
786 |
|
ExecutorService e = new ForkJoinPool(1); |
787 |
|
try { |
788 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
788 |
> |
List<Future<String>> r |
789 |
> |
= e.invokeAll(new ArrayList<Callable<String>>()); |
790 |
|
assertTrue(r.isEmpty()); |
871 |
– |
} catch (Exception ex) { |
872 |
– |
unexpectedException(); |
791 |
|
} finally { |
792 |
|
joinPool(e); |
793 |
|
} |
794 |
|
} |
795 |
|
|
796 |
|
/** |
797 |
< |
* invokeAll(c) throws NPE if c has null elements |
797 |
> |
* invokeAll(c) throws NullPointerException if c has null elements |
798 |
|
*/ |
799 |
< |
public void testInvokeAll3() { |
799 |
> |
public void testInvokeAll3() throws InterruptedException { |
800 |
|
ExecutorService e = new ForkJoinPool(1); |
801 |
|
try { |
802 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
803 |
|
l.add(new StringTask()); |
804 |
|
l.add(null); |
805 |
|
e.invokeAll(l); |
806 |
+ |
shouldThrow(); |
807 |
|
} catch (NullPointerException success) { |
889 |
– |
} catch (Exception ex) { |
890 |
– |
unexpectedException(); |
808 |
|
} finally { |
809 |
|
joinPool(e); |
810 |
|
} |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
< |
* get of returned element of invokeAll(c) throws exception on failed task |
814 |
> |
* get of returned element of invokeAll(c) throws |
815 |
> |
* ExecutionException on failed task |
816 |
|
*/ |
817 |
< |
public void testInvokeAll4() { |
817 |
> |
public void testInvokeAll4() throws Throwable { |
818 |
|
ExecutorService e = new ForkJoinPool(1); |
819 |
|
try { |
820 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
821 |
|
l.add(new NPETask()); |
822 |
|
List<Future<String>> result = e.invokeAll(l); |
823 |
|
assertEquals(1, result.size()); |
824 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
825 |
< |
it.next().get(); |
824 |
> |
for (Future<String> future : result) |
825 |
> |
future.get(); |
826 |
> |
shouldThrow(); |
827 |
|
} catch (ExecutionException success) { |
909 |
– |
} catch (CancellationException success) { |
910 |
– |
} catch (Exception ex) { |
911 |
– |
ex.printStackTrace(); |
912 |
– |
unexpectedException(); |
828 |
|
} finally { |
829 |
|
joinPool(e); |
830 |
|
} |
833 |
|
/** |
834 |
|
* invokeAll(c) returns results of all completed tasks in c |
835 |
|
*/ |
836 |
< |
public void testInvokeAll5() { |
836 |
> |
public void testInvokeAll5() throws Throwable { |
837 |
|
ExecutorService e = new ForkJoinPool(1); |
838 |
|
try { |
839 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
841 |
|
l.add(new StringTask()); |
842 |
|
List<Future<String>> result = e.invokeAll(l); |
843 |
|
assertEquals(2, result.size()); |
844 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
845 |
< |
assertSame(TEST_STRING, it.next().get()); |
931 |
< |
} catch (ExecutionException success) { |
932 |
< |
} catch (CancellationException success) { |
933 |
< |
} catch (Exception ex) { |
934 |
< |
ex.printStackTrace(); |
935 |
< |
unexpectedException(); |
844 |
> |
for (Future<String> future : result) |
845 |
> |
assertSame(TEST_STRING, future.get()); |
846 |
|
} finally { |
847 |
|
joinPool(e); |
848 |
|
} |
850 |
|
|
851 |
|
|
852 |
|
/** |
853 |
< |
* timed invokeAny(null) throws NPE |
853 |
> |
* timed invokeAny(null) throws NullPointerException |
854 |
|
*/ |
855 |
< |
public void testTimedInvokeAny1() { |
855 |
> |
public void testTimedInvokeAny1() throws Throwable { |
856 |
|
ExecutorService e = new ForkJoinPool(1); |
857 |
|
try { |
858 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
858 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
859 |
> |
shouldThrow(); |
860 |
|
} catch (NullPointerException success) { |
950 |
– |
} catch (Exception ex) { |
951 |
– |
ex.printStackTrace(); |
952 |
– |
unexpectedException(); |
861 |
|
} finally { |
862 |
|
joinPool(e); |
863 |
|
} |
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
< |
* timed invokeAny(null time unit) throws NPE |
867 |
> |
* timed invokeAny(null time unit) throws NullPointerException |
868 |
|
*/ |
869 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
869 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
870 |
|
ExecutorService e = new ForkJoinPool(1); |
871 |
|
try { |
872 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
873 |
|
l.add(new StringTask()); |
874 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
875 |
+ |
shouldThrow(); |
876 |
|
} catch (NullPointerException success) { |
968 |
– |
} catch (Exception ex) { |
969 |
– |
ex.printStackTrace(); |
970 |
– |
unexpectedException(); |
877 |
|
} finally { |
878 |
|
joinPool(e); |
879 |
|
} |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
< |
* timed invokeAny(empty collection) throws IAE |
883 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
884 |
|
*/ |
885 |
< |
public void testTimedInvokeAny2() { |
885 |
> |
public void testTimedInvokeAny2() throws Throwable { |
886 |
|
ExecutorService e = new ForkJoinPool(1); |
887 |
|
try { |
888 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
888 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
889 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
890 |
> |
shouldThrow(); |
891 |
|
} catch (IllegalArgumentException success) { |
984 |
– |
} catch (Exception ex) { |
985 |
– |
ex.printStackTrace(); |
986 |
– |
unexpectedException(); |
892 |
|
} finally { |
893 |
|
joinPool(e); |
894 |
|
} |
895 |
|
} |
896 |
|
|
897 |
|
/** |
898 |
< |
* timed invokeAny(c) throws NPE if c has null elements |
898 |
> |
* timed invokeAny(c) throws NullPointerException if c has null elements |
899 |
|
*/ |
900 |
< |
public void testTimedInvokeAny3() { |
900 |
> |
public void testTimedInvokeAny3() throws Throwable { |
901 |
|
ExecutorService e = new ForkJoinPool(1); |
902 |
|
try { |
903 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
904 |
|
l.add(new StringTask()); |
905 |
|
l.add(null); |
906 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
906 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
907 |
> |
shouldThrow(); |
908 |
|
} catch (NullPointerException success) { |
1003 |
– |
} catch (Exception ex) { |
1004 |
– |
ex.printStackTrace(); |
1005 |
– |
unexpectedException(); |
909 |
|
} finally { |
910 |
|
joinPool(e); |
911 |
|
} |
914 |
|
/** |
915 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
916 |
|
*/ |
917 |
< |
public void testTimedInvokeAny4() { |
917 |
> |
public void testTimedInvokeAny4() throws Throwable { |
918 |
|
ExecutorService e = new ForkJoinPool(1); |
919 |
|
try { |
920 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
921 |
|
l.add(new NPETask()); |
922 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
922 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
923 |
> |
shouldThrow(); |
924 |
|
} catch (ExecutionException success) { |
1021 |
– |
} catch (CancellationException success) { |
1022 |
– |
} catch (Exception ex) { |
1023 |
– |
ex.printStackTrace(); |
1024 |
– |
unexpectedException(); |
925 |
|
} finally { |
926 |
|
joinPool(e); |
927 |
|
} |
930 |
|
/** |
931 |
|
* timed invokeAny(c) returns result of some task in c |
932 |
|
*/ |
933 |
< |
public void testTimedInvokeAny5() { |
933 |
> |
public void testTimedInvokeAny5() throws Throwable { |
934 |
|
ExecutorService e = new ForkJoinPool(1); |
935 |
|
try { |
936 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
937 |
|
l.add(new StringTask()); |
938 |
|
l.add(new StringTask()); |
939 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
939 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
940 |
|
assertSame(TEST_STRING, result); |
1041 |
– |
} catch (ExecutionException success) { |
1042 |
– |
} catch (CancellationException success) { |
1043 |
– |
} catch (Exception ex) { |
1044 |
– |
ex.printStackTrace(); |
1045 |
– |
unexpectedException(); |
941 |
|
} finally { |
942 |
|
joinPool(e); |
943 |
|
} |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
< |
* timed invokeAll(null) throws NPE |
947 |
> |
* timed invokeAll(null) throws NullPointerException |
948 |
|
*/ |
949 |
< |
public void testTimedInvokeAll1() { |
949 |
> |
public void testTimedInvokeAll1() throws Throwable { |
950 |
|
ExecutorService e = new ForkJoinPool(1); |
951 |
|
try { |
952 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
952 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
953 |
> |
shouldThrow(); |
954 |
|
} catch (NullPointerException success) { |
1059 |
– |
} catch (Exception ex) { |
1060 |
– |
ex.printStackTrace(); |
1061 |
– |
unexpectedException(); |
955 |
|
} finally { |
956 |
|
joinPool(e); |
957 |
|
} |
958 |
|
} |
959 |
|
|
960 |
|
/** |
961 |
< |
* timed invokeAll(null time unit) throws NPE |
961 |
> |
* timed invokeAll(null time unit) throws NullPointerException |
962 |
|
*/ |
963 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
963 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
964 |
|
ExecutorService e = new ForkJoinPool(1); |
965 |
|
try { |
966 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
|
l.add(new StringTask()); |
968 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
969 |
+ |
shouldThrow(); |
970 |
|
} catch (NullPointerException success) { |
1077 |
– |
} catch (Exception ex) { |
1078 |
– |
ex.printStackTrace(); |
1079 |
– |
unexpectedException(); |
971 |
|
} finally { |
972 |
|
joinPool(e); |
973 |
|
} |
976 |
|
/** |
977 |
|
* timed invokeAll(empty collection) returns empty collection |
978 |
|
*/ |
979 |
< |
public void testTimedInvokeAll2() { |
979 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
980 |
|
ExecutorService e = new ForkJoinPool(1); |
981 |
|
try { |
982 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
982 |
> |
List<Future<String>> r |
983 |
> |
= e.invokeAll(new ArrayList<Callable<String>>(), |
984 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
985 |
|
assertTrue(r.isEmpty()); |
1093 |
– |
} catch (Exception ex) { |
1094 |
– |
ex.printStackTrace(); |
1095 |
– |
unexpectedException(); |
986 |
|
} finally { |
987 |
|
joinPool(e); |
988 |
|
} |
989 |
|
} |
990 |
|
|
991 |
|
/** |
992 |
< |
* timed invokeAll(c) throws NPE if c has null elements |
992 |
> |
* timed invokeAll(c) throws NullPointerException if c has null elements |
993 |
|
*/ |
994 |
< |
public void testTimedInvokeAll3() { |
994 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
995 |
|
ExecutorService e = new ForkJoinPool(1); |
996 |
|
try { |
997 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
998 |
|
l.add(new StringTask()); |
999 |
|
l.add(null); |
1000 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1000 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1001 |
> |
shouldThrow(); |
1002 |
|
} catch (NullPointerException success) { |
1112 |
– |
} catch (Exception ex) { |
1113 |
– |
ex.printStackTrace(); |
1114 |
– |
unexpectedException(); |
1003 |
|
} finally { |
1004 |
|
joinPool(e); |
1005 |
|
} |
1008 |
|
/** |
1009 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
1010 |
|
*/ |
1011 |
< |
public void testTimedInvokeAll4() { |
1011 |
> |
public void testTimedInvokeAll4() throws Throwable { |
1012 |
|
ExecutorService e = new ForkJoinPool(1); |
1013 |
|
try { |
1014 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1015 |
|
l.add(new NPETask()); |
1016 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1016 |
> |
List<Future<String>> result |
1017 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1018 |
|
assertEquals(1, result.size()); |
1019 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1020 |
< |
it.next().get(); |
1019 |
> |
for (Future<String> future : result) |
1020 |
> |
future.get(); |
1021 |
> |
shouldThrow(); |
1022 |
|
} catch (ExecutionException success) { |
1133 |
– |
} catch (CancellationException success) { |
1134 |
– |
} catch (Exception ex) { |
1135 |
– |
ex.printStackTrace(); |
1136 |
– |
unexpectedException(); |
1023 |
|
} finally { |
1024 |
|
joinPool(e); |
1025 |
|
} |
1028 |
|
/** |
1029 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
1030 |
|
*/ |
1031 |
< |
public void testTimedInvokeAll5() { |
1031 |
> |
public void testTimedInvokeAll5() throws Throwable { |
1032 |
|
ExecutorService e = new ForkJoinPool(1); |
1033 |
|
try { |
1034 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1035 |
|
l.add(new StringTask()); |
1036 |
|
l.add(new StringTask()); |
1037 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1037 |
> |
List<Future<String>> result |
1038 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1039 |
|
assertEquals(2, result.size()); |
1040 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1041 |
< |
assertSame(TEST_STRING, it.next().get()); |
1155 |
< |
} catch (ExecutionException success) { |
1156 |
< |
} catch (CancellationException success) { |
1157 |
< |
} catch (Exception ex) { |
1158 |
< |
ex.printStackTrace(); |
1159 |
< |
unexpectedException(); |
1040 |
> |
for (Future<String> future : result) |
1041 |
> |
assertSame(TEST_STRING, future.get()); |
1042 |
|
} finally { |
1043 |
|
joinPool(e); |
1044 |
|
} |