ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinPoolTest.java (file contents):
Revision 1.6 by jsr166, Mon Aug 3 22:08:07 2009 UTC vs.
Revision 1.17 by jsr166, Tue Jan 5 02:08:37 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines