170 |
|
try { |
171 |
|
new ForkJoinPool(-1); |
172 |
|
shouldThrow(); |
173 |
< |
} |
174 |
< |
catch (IllegalArgumentException success) {} |
173 |
> |
} catch (IllegalArgumentException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
180 |
|
try { |
181 |
|
new ForkJoinPool(1, null); |
182 |
|
shouldThrow(); |
183 |
< |
} catch (NullPointerException success) { |
185 |
< |
} |
183 |
> |
} catch (NullPointerException success) {} |
184 |
|
} |
185 |
|
|
186 |
|
|
235 |
|
ForkJoinPool p = null; |
236 |
|
try { |
237 |
|
p = new ForkJoinPool(1); |
238 |
< |
assertTrue(p.getPoolSize() == 0); |
238 |
> |
assertTrue(p.getActiveThreadCount() == 0); |
239 |
|
Future<String> future = p.submit(new StringTask()); |
240 |
|
assertTrue(p.getPoolSize() == 1); |
241 |
|
|
320 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
321 |
|
assertTrue(eh.catches > 0); |
322 |
|
} finally { |
323 |
+ |
p.shutdownNow(); |
324 |
|
joinPool(p); |
325 |
|
} |
326 |
|
} |
413 |
|
p.execute(f); |
414 |
|
assertTrue(p.getPoolSize() >= 4); |
415 |
|
int r = f.get(); |
416 |
< |
assertTrue(r == 832040); |
416 |
> |
assertTrue(r == 832040); |
417 |
|
} finally { |
418 |
< |
joinPool(p); |
418 |
> |
p.shutdownNow(); // don't wait out shutdown |
419 |
|
} |
420 |
|
} |
421 |
|
|
505 |
|
assertSame(TEST_STRING, result); |
506 |
|
} |
507 |
|
|
509 |
– |
|
508 |
|
/** |
509 |
< |
* A submitted privileged action to completion |
509 |
> |
* A submitted privileged action runs to completion |
510 |
|
*/ |
511 |
|
public void testSubmitPrivilegedAction() throws Throwable { |
512 |
< |
Policy savedPolicy = null; |
513 |
< |
try { |
514 |
< |
savedPolicy = Policy.getPolicy(); |
515 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
518 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
519 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
520 |
< |
Policy.setPolicy(policy); |
521 |
< |
} catch (AccessControlException ok) { |
522 |
< |
return; |
523 |
< |
} |
524 |
< |
try { |
525 |
< |
ExecutorService e = new ForkJoinPool(1); |
526 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
512 |
> |
Runnable r = new CheckedRunnable() { |
513 |
> |
public void realRun() throws Exception { |
514 |
> |
ExecutorService e = new ForkJoinPool(1); |
515 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
516 |
|
public Object run() { |
517 |
|
return TEST_STRING; |
518 |
|
}})); |
519 |
|
|
520 |
< |
Object result = future.get(); |
521 |
< |
assertSame(TEST_STRING, result); |
522 |
< |
} |
523 |
< |
finally { |
524 |
< |
Policy.setPolicy(savedPolicy); |
536 |
< |
} |
520 |
> |
Object result = future.get(); |
521 |
> |
assertSame(TEST_STRING, result); |
522 |
> |
}}; |
523 |
> |
|
524 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
525 |
|
} |
526 |
|
|
527 |
|
/** |
528 |
< |
* A submitted a privileged exception action runs to completion |
528 |
> |
* A submitted privileged exception action runs to completion |
529 |
|
*/ |
530 |
|
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
531 |
< |
Policy savedPolicy = null; |
532 |
< |
try { |
533 |
< |
savedPolicy = Policy.getPolicy(); |
534 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
547 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
548 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
549 |
< |
Policy.setPolicy(policy); |
550 |
< |
} catch (AccessControlException ok) { |
551 |
< |
return; |
552 |
< |
} |
553 |
< |
|
554 |
< |
try { |
555 |
< |
ExecutorService e = new ForkJoinPool(1); |
556 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
531 |
> |
Runnable r = new CheckedRunnable() { |
532 |
> |
public void realRun() throws Exception { |
533 |
> |
ExecutorService e = new ForkJoinPool(1); |
534 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
535 |
|
public Object run() { |
536 |
|
return TEST_STRING; |
537 |
|
}})); |
538 |
|
|
539 |
< |
Object result = future.get(); |
540 |
< |
assertSame(TEST_STRING, result); |
541 |
< |
} |
542 |
< |
finally { |
543 |
< |
Policy.setPolicy(savedPolicy); |
566 |
< |
} |
539 |
> |
Object result = future.get(); |
540 |
> |
assertSame(TEST_STRING, result); |
541 |
> |
}}; |
542 |
> |
|
543 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
|
* A submitted failed privileged exception action reports exception |
548 |
|
*/ |
549 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
550 |
< |
Policy savedPolicy = null; |
551 |
< |
try { |
552 |
< |
savedPolicy = Policy.getPolicy(); |
553 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
577 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
578 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
579 |
< |
Policy.setPolicy(policy); |
580 |
< |
} catch (AccessControlException ok) { |
581 |
< |
return; |
582 |
< |
} |
583 |
< |
|
584 |
< |
|
585 |
< |
try { |
586 |
< |
ExecutorService e = new ForkJoinPool(1); |
587 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
550 |
> |
Runnable r = new CheckedRunnable() { |
551 |
> |
public void realRun() throws Exception { |
552 |
> |
ExecutorService e = new ForkJoinPool(1); |
553 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
554 |
|
public Object run() throws Exception { |
555 |
|
throw new IndexOutOfBoundsException(); |
556 |
|
}})); |
557 |
|
|
558 |
< |
Object result = future.get(); |
559 |
< |
shouldThrow(); |
560 |
< |
} catch (ExecutionException success) { |
561 |
< |
} finally { |
562 |
< |
Policy.setPolicy(savedPolicy); |
563 |
< |
} |
558 |
> |
try { |
559 |
> |
Object result = future.get(); |
560 |
> |
shouldThrow(); |
561 |
> |
} catch (ExecutionException success) { |
562 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
563 |
> |
}}}; |
564 |
> |
|
565 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
566 |
|
} |
567 |
|
|
568 |
|
/** |
574 |
|
TrackedShortRunnable task = null; |
575 |
|
Future<?> future = e.submit(task); |
576 |
|
shouldThrow(); |
577 |
< |
} catch (NullPointerException success) { |
610 |
< |
} |
577 |
> |
} catch (NullPointerException success) {} |
578 |
|
} |
579 |
|
|
580 |
|
|
587 |
|
StringTask t = null; |
588 |
|
Future<String> future = e.submit(t); |
589 |
|
shouldThrow(); |
590 |
< |
} catch (NullPointerException success) { |
624 |
< |
} |
590 |
> |
} catch (NullPointerException success) {} |
591 |
|
} |
592 |
|
|
593 |
|
|
599 |
|
final ForkJoinPool p = new ForkJoinPool(1); |
600 |
|
|
601 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
602 |
< |
void realRun() throws Throwable { |
602 |
> |
public void realRun() throws Throwable { |
603 |
|
p.submit(new CheckedCallable<Object>() { |
604 |
|
public Object realCall() throws Throwable { |
605 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
605 |
> |
try { |
606 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
607 |
> |
} catch (InterruptedException ok) { |
608 |
> |
} |
609 |
|
return null; |
610 |
|
}}).get(); |
611 |
|
}}); |
613 |
|
t.start(); |
614 |
|
Thread.sleep(SHORT_DELAY_MS); |
615 |
|
t.interrupt(); |
616 |
+ |
t.join(); |
617 |
+ |
p.shutdownNow(); |
618 |
|
joinPool(p); |
619 |
|
} |
620 |
|
|
624 |
|
*/ |
625 |
|
public void testSubmitEE() throws Throwable { |
626 |
|
ForkJoinPool p = new ForkJoinPool(1); |
656 |
– |
|
627 |
|
try { |
628 |
< |
Callable c = new Callable() { |
629 |
< |
public Object call() { |
630 |
< |
int i = 5/0; |
631 |
< |
return Boolean.TRUE; |
632 |
< |
} |
663 |
< |
}; |
664 |
< |
|
665 |
< |
for (int i = 0; i < 5; i++) { |
666 |
< |
p.submit(c).get(); |
667 |
< |
} |
628 |
> |
p.submit(new Callable() { |
629 |
> |
public Object call() { |
630 |
> |
int i = 5/0; |
631 |
> |
return Boolean.TRUE; |
632 |
> |
}}).get(); |
633 |
|
shouldThrow(); |
634 |
|
} catch (ExecutionException success) { |
635 |
+ |
assertTrue(success.getCause() instanceof ArithmeticException); |
636 |
|
} |
637 |
+ |
|
638 |
|
joinPool(p); |
639 |
|
} |
640 |
|
|
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* invokeAny(c) throws NullPointerException if c has null elements |
670 |
> |
* invokeAny(c) throws NullPointerException if c has a single null element |
671 |
|
*/ |
672 |
|
public void testInvokeAny3() throws Throwable { |
673 |
|
ExecutorService e = new ForkJoinPool(1); |
674 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
675 |
+ |
l.add(null); |
676 |
|
try { |
708 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
709 |
– |
l.add(new StringTask()); |
710 |
– |
l.add(null); |
677 |
|
e.invokeAny(l); |
678 |
|
shouldThrow(); |
679 |
|
} catch (NullPointerException success) { |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* invokeAny(c) throws ExecutionException if no task in c completes |
686 |
> |
* invokeAny(c) throws NullPointerException if c has null elements |
687 |
|
*/ |
688 |
|
public void testInvokeAny4() throws Throwable { |
689 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
690 |
|
ExecutorService e = new ForkJoinPool(1); |
691 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
692 |
+ |
l.add(latchAwaitingStringTask(latch)); |
693 |
+ |
l.add(null); |
694 |
+ |
try { |
695 |
+ |
e.invokeAny(l); |
696 |
+ |
shouldThrow(); |
697 |
+ |
} catch (NullPointerException success) { |
698 |
+ |
} finally { |
699 |
+ |
latch.countDown(); |
700 |
+ |
joinPool(e); |
701 |
+ |
} |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
/** |
705 |
+ |
* invokeAny(c) throws ExecutionException if no task in c completes |
706 |
+ |
*/ |
707 |
+ |
public void testInvokeAny5() throws Throwable { |
708 |
+ |
ExecutorService e = new ForkJoinPool(1); |
709 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
710 |
+ |
l.add(new NPETask()); |
711 |
|
try { |
725 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
726 |
– |
l.add(new NPETask()); |
712 |
|
e.invokeAny(l); |
713 |
|
shouldThrow(); |
714 |
|
} catch (ExecutionException success) { |
715 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
716 |
|
} finally { |
717 |
|
joinPool(e); |
718 |
|
} |
721 |
|
/** |
722 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
723 |
|
*/ |
724 |
< |
public void testInvokeAny5() throws Throwable { |
724 |
> |
public void testInvokeAny6() throws Throwable { |
725 |
|
ExecutorService e = new ForkJoinPool(1); |
726 |
|
try { |
727 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
727 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
728 |
|
l.add(new StringTask()); |
729 |
|
l.add(new StringTask()); |
730 |
|
String result = e.invokeAny(l); |
767 |
|
*/ |
768 |
|
public void testInvokeAll3() throws InterruptedException { |
769 |
|
ExecutorService e = new ForkJoinPool(1); |
770 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
771 |
+ |
l.add(new StringTask()); |
772 |
+ |
l.add(null); |
773 |
|
try { |
785 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
786 |
– |
l.add(new StringTask()); |
787 |
– |
l.add(null); |
774 |
|
e.invokeAll(l); |
775 |
|
shouldThrow(); |
776 |
|
} catch (NullPointerException success) { |
785 |
|
*/ |
786 |
|
public void testInvokeAll4() throws Throwable { |
787 |
|
ExecutorService e = new ForkJoinPool(1); |
788 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
789 |
+ |
l.add(new NPETask()); |
790 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
791 |
+ |
assertEquals(1, futures.size()); |
792 |
|
try { |
793 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
804 |
< |
l.add(new NPETask()); |
805 |
< |
List<Future<String>> result = e.invokeAll(l); |
806 |
< |
assertEquals(1, result.size()); |
807 |
< |
for (Future<String> future : result) |
808 |
< |
future.get(); |
793 |
> |
futures.get(0).get(); |
794 |
|
shouldThrow(); |
795 |
|
} catch (ExecutionException success) { |
796 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
797 |
|
} finally { |
798 |
|
joinPool(e); |
799 |
|
} |
805 |
|
public void testInvokeAll5() throws Throwable { |
806 |
|
ExecutorService e = new ForkJoinPool(1); |
807 |
|
try { |
808 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
808 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
809 |
|
l.add(new StringTask()); |
810 |
|
l.add(new StringTask()); |
811 |
< |
List<Future<String>> result = e.invokeAll(l); |
812 |
< |
assertEquals(2, result.size()); |
813 |
< |
for (Future<String> future : result) |
811 |
> |
List<Future<String>> futures = e.invokeAll(l); |
812 |
> |
assertEquals(2, futures.size()); |
813 |
> |
for (Future<String> future : futures) |
814 |
|
assertSame(TEST_STRING, future.get()); |
815 |
|
} finally { |
816 |
|
joinPool(e); |
837 |
|
*/ |
838 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
839 |
|
ExecutorService e = new ForkJoinPool(1); |
840 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
841 |
+ |
l.add(new StringTask()); |
842 |
|
try { |
855 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
856 |
– |
l.add(new StringTask()); |
843 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
844 |
|
shouldThrow(); |
845 |
|
} catch (NullPointerException success) { |
867 |
|
* timed invokeAny(c) throws NullPointerException if c has null elements |
868 |
|
*/ |
869 |
|
public void testTimedInvokeAny3() throws Throwable { |
870 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
871 |
|
ExecutorService e = new ForkJoinPool(1); |
872 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
873 |
+ |
l.add(latchAwaitingStringTask(latch)); |
874 |
+ |
l.add(null); |
875 |
|
try { |
886 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
887 |
– |
l.add(new StringTask()); |
888 |
– |
l.add(null); |
876 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
877 |
|
shouldThrow(); |
878 |
|
} catch (NullPointerException success) { |
879 |
|
} finally { |
880 |
+ |
latch.countDown(); |
881 |
|
joinPool(e); |
882 |
|
} |
883 |
|
} |
887 |
|
*/ |
888 |
|
public void testTimedInvokeAny4() throws Throwable { |
889 |
|
ExecutorService e = new ForkJoinPool(1); |
890 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
891 |
+ |
l.add(new NPETask()); |
892 |
|
try { |
903 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
904 |
– |
l.add(new NPETask()); |
893 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
894 |
|
shouldThrow(); |
895 |
|
} catch (ExecutionException success) { |
896 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
897 |
|
} finally { |
898 |
|
joinPool(e); |
899 |
|
} |
905 |
|
public void testTimedInvokeAny5() throws Throwable { |
906 |
|
ExecutorService e = new ForkJoinPool(1); |
907 |
|
try { |
908 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
908 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
909 |
|
l.add(new StringTask()); |
910 |
|
l.add(new StringTask()); |
911 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
934 |
|
*/ |
935 |
|
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
936 |
|
ExecutorService e = new ForkJoinPool(1); |
937 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
938 |
+ |
l.add(new StringTask()); |
939 |
|
try { |
949 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
950 |
– |
l.add(new StringTask()); |
940 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
941 |
|
shouldThrow(); |
942 |
|
} catch (NullPointerException success) { |
965 |
|
*/ |
966 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
967 |
|
ExecutorService e = new ForkJoinPool(1); |
968 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
969 |
+ |
l.add(new StringTask()); |
970 |
+ |
l.add(null); |
971 |
|
try { |
980 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
981 |
– |
l.add(new StringTask()); |
982 |
– |
l.add(null); |
972 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
973 |
|
shouldThrow(); |
974 |
|
} catch (NullPointerException success) { |
982 |
|
*/ |
983 |
|
public void testTimedInvokeAll4() throws Throwable { |
984 |
|
ExecutorService e = new ForkJoinPool(1); |
985 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
986 |
+ |
l.add(new NPETask()); |
987 |
+ |
List<Future<String>> futures |
988 |
+ |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
989 |
+ |
assertEquals(1, futures.size()); |
990 |
|
try { |
991 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
998 |
< |
l.add(new NPETask()); |
999 |
< |
List<Future<String>> result |
1000 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1001 |
< |
assertEquals(1, result.size()); |
1002 |
< |
for (Future<String> future : result) |
1003 |
< |
future.get(); |
991 |
> |
futures.get(0).get(); |
992 |
|
shouldThrow(); |
993 |
|
} catch (ExecutionException success) { |
994 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
995 |
|
} finally { |
996 |
|
joinPool(e); |
997 |
|
} |
1003 |
|
public void testTimedInvokeAll5() throws Throwable { |
1004 |
|
ExecutorService e = new ForkJoinPool(1); |
1005 |
|
try { |
1006 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1006 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1007 |
|
l.add(new StringTask()); |
1008 |
|
l.add(new StringTask()); |
1009 |
< |
List<Future<String>> result |
1009 |
> |
List<Future<String>> futures |
1010 |
|
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1011 |
< |
assertEquals(2, result.size()); |
1012 |
< |
for (Future<String> future : result) |
1011 |
> |
assertEquals(2, futures.size()); |
1012 |
> |
for (Future<String> future : futures) |
1013 |
|
assertSame(TEST_STRING, future.get()); |
1014 |
|
} finally { |
1015 |
|
joinPool(e); |