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.11 by jsr166, Sat Nov 21 20:02:13 2009 UTC vs.
Revision 1.19 by dl, Sun Feb 28 13:35:22 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 320 | Line 320 | public class ForkJoinPoolTest extends JS
320              Thread.sleep(MEDIUM_DELAY_MS);
321              assertTrue(eh.catches > 0);
322          } finally {
323 +            p.shutdownNow();
324              joinPool(p);
325          }
326      }
# Line 412 | Line 413 | public class ForkJoinPoolTest extends JS
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              p.shutdownNow(); // don't wait out shutdown
419          }
# Line 504 | Line 505 | public class ForkJoinPoolTest extends JS
505          assertSame(TEST_STRING, result);
506      }
507  
507
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();
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() {
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);
534 <        }
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();
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() {
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);
564 <        }
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();
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() {
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      /**
# Line 629 | Line 599 | public class ForkJoinPoolTest extends JS
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                          try {
# Line 661 | Line 631 | public class ForkJoinPoolTest extends JS
631                      return Boolean.TRUE;
632                  }}).get();
633              shouldThrow();
634 <        } catch (ExecutionException success) {}
634 >        } catch (ExecutionException success) {
635 >            assertTrue(success.getCause() instanceof ArithmeticException);
636 >        }
637  
638          joinPool(p);
639      }
# Line 699 | Line 671 | public class ForkJoinPoolTest extends JS
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 {
703            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
704            l.add(null);
677              e.invokeAny(l);
678              shouldThrow();
679          } catch (NullPointerException success) {
# Line 714 | Line 686 | public class ForkJoinPoolTest extends JS
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 {
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);
695              e.invokeAny(l);
696              shouldThrow();
697          } catch (NullPointerException success) {
698          } finally {
699 +            latch.countDown();
700              joinPool(e);
701          }
702      }
# Line 738 | Line 706 | public class ForkJoinPoolTest extends JS
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 {
742            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
743            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          }
# Line 755 | Line 724 | public class ForkJoinPoolTest extends JS
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);
# Line 798 | Line 767 | public class ForkJoinPoolTest extends JS
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 {
802            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
803            l.add(new StringTask());
804            l.add(null);
774              e.invokeAll(l);
775              shouldThrow();
776          } catch (NullPointerException success) {
# Line 816 | Line 785 | public class ForkJoinPoolTest extends JS
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>>();
821 <            l.add(new NPETask());
822 <            List<Future<String>> result = e.invokeAll(l);
823 <            assertEquals(1, result.size());
824 <            for (Future<String> future : result)
825 <                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          }
# Line 836 | Line 805 | public class ForkJoinPoolTest extends JS
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);
# Line 868 | Line 837 | public class ForkJoinPoolTest extends JS
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 {
872            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
873            l.add(new StringTask());
843              e.invokeAny(l, MEDIUM_DELAY_MS, null);
844              shouldThrow();
845          } catch (NullPointerException success) {
# Line 898 | Line 867 | public class ForkJoinPoolTest extends JS
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 {
903            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
904            l.add(new StringTask());
905            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      }
# Line 916 | Line 887 | public class ForkJoinPoolTest extends JS
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 {
920            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
921            l.add(new NPETask());
893              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
894              shouldThrow();
895          } catch (ExecutionException success) {
# Line 934 | Line 905 | public class ForkJoinPoolTest extends JS
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);
# Line 963 | Line 934 | public class ForkJoinPoolTest extends JS
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 {
967            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
968            l.add(new StringTask());
940              e.invokeAll(l, MEDIUM_DELAY_MS, null);
941              shouldThrow();
942          } catch (NullPointerException success) {
# Line 994 | Line 965 | public class ForkJoinPoolTest extends JS
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 {
998            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
999            l.add(new StringTask());
1000            l.add(null);
972              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
973              shouldThrow();
974          } catch (NullPointerException success) {
# Line 1011 | Line 982 | public class ForkJoinPoolTest extends JS
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>>();
1016 <            l.add(new NPETask());
1017 <            List<Future<String>> result
1018 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1019 <            assertEquals(1, result.size());
1020 <            for (Future<String> future : result)
1021 <                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          }
# Line 1032 | Line 1003 | public class ForkJoinPoolTest extends JS
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines