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.13 by jsr166, Thu Nov 26 15:42:15 2009 UTC vs.
Revision 1.18 by dl, Mon Feb 22 11:25:16 2010 UTC

# Line 235 | Line 235 | public class ForkJoinPoolTest extends JS
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  
# Line 412 | 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              p.shutdownNow(); // don't wait out shutdown
418          }
# Line 504 | Line 504 | public class ForkJoinPoolTest extends JS
504          assertSame(TEST_STRING, result);
505      }
506  
507
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();
516 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
517 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
518 <            Policy.setPolicy(policy);
519 <        } catch (AccessControlException ok) {
520 <            return;
521 <        }
522 <        try {
523 <            ExecutorService e = new ForkJoinPool(1);
524 <            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);
534 <        }
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();
545 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
546 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
547 <            Policy.setPolicy(policy);
548 <        } catch (AccessControlException ok) {
549 <            return;
550 <        }
551 <
552 <        try {
553 <            ExecutorService e = new ForkJoinPool(1);
554 <            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);
564 <        }
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();
575 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
576 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
577 <            Policy.setPolicy(policy);
578 <        } catch (AccessControlException ok) {
579 <            return;
580 <        }
581 <
582 <
583 <        try {
584 <            ExecutorService e = new ForkJoinPool(1);
585 <            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 <            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
561 <        } finally {
562 <            Policy.setPolicy(savedPolicy);
563 <        }
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 702 | Line 670 | public class ForkJoinPoolTest extends JS
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 {
706            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
707            l.add(null);
676              e.invokeAny(l);
677              shouldThrow();
678          } catch (NullPointerException success) {
# Line 717 | Line 685 | public class ForkJoinPoolTest extends JS
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 {
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);
694              e.invokeAny(l);
695              shouldThrow();
696          } catch (NullPointerException success) {
697          } finally {
698 +            latch.countDown();
699              joinPool(e);
700          }
701      }
# Line 741 | Line 705 | public class ForkJoinPoolTest extends JS
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 {
745            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
746            l.add(new NPETask());
711              e.invokeAny(l);
712              shouldThrow();
713          } catch (ExecutionException success) {
# Line 759 | Line 723 | public class ForkJoinPoolTest extends JS
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 802 | 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 {
806            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
807            l.add(new StringTask());
808            l.add(null);
773              e.invokeAll(l);
774              shouldThrow();
775          } catch (NullPointerException success) {
# Line 820 | 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>>();
825 <            l.add(new NPETask());
826 <            List<Future<String>> result = e.invokeAll(l);
827 <            assertEquals(1, result.size());
828 <            for (Future<String> future : result)
829 <                future.get();
792 >            futures.get(0).get();
793              shouldThrow();
794          } catch (ExecutionException success) {
795              assertTrue(success.getCause() instanceof NullPointerException);
# Line 841 | 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 873 | 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 {
877            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
878            l.add(new StringTask());
842              e.invokeAny(l, MEDIUM_DELAY_MS, null);
843              shouldThrow();
844          } catch (NullPointerException success) {
# Line 903 | 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 {
908            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
909            l.add(new StringTask());
910            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 921 | 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 {
925            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
926            l.add(new NPETask());
892              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
893              shouldThrow();
894          } catch (ExecutionException success) {
# Line 939 | 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 968 | 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 {
972            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
973            l.add(new StringTask());
939              e.invokeAll(l, MEDIUM_DELAY_MS, null);
940              shouldThrow();
941          } catch (NullPointerException success) {
# Line 999 | 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 {
1003            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1004            l.add(new StringTask());
1005            l.add(null);
971              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
972              shouldThrow();
973          } catch (NullPointerException success) {
# Line 1016 | 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>>();
1021 <            l.add(new NPETask());
1022 <            List<Future<String>> result
1023 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1024 <            assertEquals(1, result.size());
1025 <            for (Future<String> future : result)
1026 <                future.get();
990 >            futures.get(0).get();
991              shouldThrow();
992          } catch (ExecutionException success) {
993              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1038 | 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