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

Comparing jsr166/src/test/tck/AbstractExecutorServiceTest.java (file contents):
Revision 1.19 by jsr166, Fri Nov 20 05:25:10 2009 UTC vs.
Revision 1.23 by jsr166, Tue Jan 5 02:08:37 2010 UTC

# Line 10 | Line 10
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.math.BigInteger;
15   import java.security.*;
16  
# Line 80 | Line 81 | public class AbstractExecutorServiceTest
81  
82  
83      /**
84 <     * A submitted privileged action to completion
84 >     * A submitted privileged action runs to completion
85       */
86      public void testSubmitPrivilegedAction() throws Exception {
87 <        Policy savedPolicy = null;
88 <        try {
89 <            savedPolicy = Policy.getPolicy();
90 <            AdjustablePolicy policy = new AdjustablePolicy();
90 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
91 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
92 <            Policy.setPolicy(policy);
93 <        } catch (AccessControlException ok) {
94 <            return;
95 <        }
96 <        try {
97 <            ExecutorService e = new DirectExecutorService();
98 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
87 >        Runnable r = new CheckedRunnable() {
88 >            public void realRun() throws Exception {
89 >                ExecutorService e = new DirectExecutorService();
90 >                Future future = e.submit(Executors.callable(new PrivilegedAction() {
91                      public Object run() {
92                          return TEST_STRING;
93                      }}));
94  
95 <            Object result = future.get();
96 <            assertSame(TEST_STRING, result);
97 <        }
98 <        finally {
99 <            try {
100 <                Policy.setPolicy(savedPolicy);
101 <            } catch (AccessControlException ok) {
110 <                return;
111 <            }
112 <        }
95 >                assertSame(TEST_STRING, future.get());
96 >            }};
97 >
98 >        runWithPermissions(r,
99 >                           new RuntimePermission("getClassLoader"),
100 >                           new RuntimePermission("setContextClassLoader"),
101 >                           new RuntimePermission("modifyThread"));
102      }
103  
104      /**
105 <     * A submitted a privileged exception action runs to completion
105 >     * A submitted privileged exception action runs to completion
106       */
107      public void testSubmitPrivilegedExceptionAction() throws Exception {
108 <        Policy savedPolicy = null;
109 <        try {
110 <            savedPolicy = Policy.getPolicy();
111 <            AdjustablePolicy policy = new AdjustablePolicy();
123 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
124 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
125 <            Policy.setPolicy(policy);
126 <        } catch (AccessControlException ok) {
127 <            return;
128 <        }
129 <
130 <        try {
131 <            ExecutorService e = new DirectExecutorService();
132 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
108 >        Runnable r = new CheckedRunnable() {
109 >            public void realRun() throws Exception {
110 >                ExecutorService e = new DirectExecutorService();
111 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
112                      public Object run() {
113                          return TEST_STRING;
114                      }}));
115  
116 <            Object result = future.get();
117 <            assertSame(TEST_STRING, result);
118 <        }
119 <        finally {
141 <            Policy.setPolicy(savedPolicy);
142 <        }
116 >                assertSame(TEST_STRING, future.get());
117 >            }};
118 >
119 >        runWithPermissions(r);
120      }
121  
122      /**
123       * A submitted failed privileged exception action reports exception
124       */
125      public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
126 <        Policy savedPolicy = null;
127 <        try {
128 <            savedPolicy = Policy.getPolicy();
129 <            AdjustablePolicy policy = new AdjustablePolicy();
153 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
154 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
155 <            Policy.setPolicy(policy);
156 <        } catch (AccessControlException ok) {
157 <            return;
158 <        }
159 <
160 <        try {
161 <            ExecutorService e = new DirectExecutorService();
162 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
126 >        Runnable r = new CheckedRunnable() {
127 >            public void realRun() throws Exception {
128 >                ExecutorService e = new DirectExecutorService();
129 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
130                      public Object run() throws Exception {
131                          throw new IndexOutOfBoundsException();
132                      }}));
133  
134 <            future.get();
135 <            shouldThrow();
136 <        } catch (ExecutionException success) {
137 <            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
138 <        }
139 <        finally {
140 <            Policy.setPolicy(savedPolicy);
141 <        }
134 >                try {
135 >                    future.get();
136 >                    shouldThrow();
137 >                } catch (ExecutionException success) {
138 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
139 >                }}};
140 >
141 >        runWithPermissions(r);
142      }
143  
144      /**
# Line 344 | Line 311 | public class AbstractExecutorServiceTest
311       * invokeAny(c) throws NPE if c has null elements
312       */
313      public void testInvokeAny3() throws Exception {
347        final CountDownLatch latch = new CountDownLatch(1);
314          ExecutorService e = new DirectExecutorService();
315 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
316 +        l.add(new Callable<Integer>() {
317 +                  public Integer call() { return 5/0; }});
318 +        l.add(null);
319          try {
350            ArrayList<Callable<Integer>> l
351                = new ArrayList<Callable<Integer>>();
352            l.add(new Callable<Integer>() {
353                      public Integer call() { return 5/0; }});
354            l.add(null);
320              e.invokeAny(l);
321              shouldThrow();
322          } catch (NullPointerException success) {
323          } finally {
359            latch.countDown();
324              joinPool(e);
325          }
326      }
# Line 366 | Line 330 | public class AbstractExecutorServiceTest
330       */
331      public void testInvokeAny4() throws InterruptedException {
332          ExecutorService e = new DirectExecutorService();
333 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
334 +        l.add(new NPETask());
335          try {
370            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
371            l.add(new NPETask());
336              e.invokeAny(l);
337              shouldThrow();
338          } catch (ExecutionException success) {
# Line 384 | Line 348 | public class AbstractExecutorServiceTest
348      public void testInvokeAny5() throws Exception {
349          ExecutorService e = new DirectExecutorService();
350          try {
351 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
351 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
352              l.add(new StringTask());
353              l.add(new StringTask());
354              String result = e.invokeAny(l);
# Line 426 | Line 390 | public class AbstractExecutorServiceTest
390       */
391      public void testInvokeAll3() throws InterruptedException {
392          ExecutorService e = new DirectExecutorService();
393 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
394 +        l.add(new StringTask());
395 +        l.add(null);
396          try {
430            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
431            l.add(new StringTask());
432            l.add(null);
397              e.invokeAll(l);
398              shouldThrow();
399          } catch (NullPointerException success) {
# Line 444 | Line 408 | public class AbstractExecutorServiceTest
408      public void testInvokeAll4() throws Exception {
409          ExecutorService e = new DirectExecutorService();
410          try {
411 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
411 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
412              l.add(new NPETask());
413 <            List<Future<String>> result = e.invokeAll(l);
414 <            assertEquals(1, result.size());
415 <            for (Future<String> future : result) {
416 <                try {
417 <                    future.get();
418 <                    shouldThrow();
419 <                } catch (ExecutionException success) {
456 <                    Throwable cause = success.getCause();
457 <                    assertTrue(cause instanceof NullPointerException);
458 <                }
413 >            List<Future<String>> futures = e.invokeAll(l);
414 >            assertEquals(1, futures.size());
415 >            try {
416 >                futures.get(0).get();
417 >                shouldThrow();
418 >            } catch (ExecutionException success) {
419 >                assertTrue(success.getCause() instanceof NullPointerException);
420              }
421          } finally {
422              joinPool(e);
# Line 468 | Line 429 | public class AbstractExecutorServiceTest
429      public void testInvokeAll5() throws Exception {
430          ExecutorService e = new DirectExecutorService();
431          try {
432 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
432 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
433              l.add(new StringTask());
434              l.add(new StringTask());
435 <            List<Future<String>> result = e.invokeAll(l);
436 <            assertEquals(2, result.size());
437 <            for (Future<String> future : result)
435 >            List<Future<String>> futures = e.invokeAll(l);
436 >            assertEquals(2, futures.size());
437 >            for (Future<String> future : futures)
438                  assertSame(TEST_STRING, future.get());
439          } finally {
440              joinPool(e);
# Line 487 | Line 448 | public class AbstractExecutorServiceTest
448      public void testTimedInvokeAny1() throws Exception {
449          ExecutorService e = new DirectExecutorService();
450          try {
451 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
451 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
452              shouldThrow();
453          } catch (NullPointerException success) {
454          } finally {
# Line 500 | Line 461 | public class AbstractExecutorServiceTest
461       */
462      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
463          ExecutorService e = new DirectExecutorService();
464 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
465 +        l.add(new StringTask());
466          try {
504            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
505            l.add(new StringTask());
467              e.invokeAny(l, MEDIUM_DELAY_MS, null);
468              shouldThrow();
469          } catch (NullPointerException success) {
# Line 517 | Line 478 | public class AbstractExecutorServiceTest
478      public void testTimedInvokeAny2() throws Exception {
479          ExecutorService e = new DirectExecutorService();
480          try {
481 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
481 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
482              shouldThrow();
483          } catch (IllegalArgumentException success) {
484          } finally {
# Line 529 | Line 490 | public class AbstractExecutorServiceTest
490       * timed invokeAny(c) throws NPE if c has null elements
491       */
492      public void testTimedInvokeAny3() throws Exception {
532        final CountDownLatch latch = new CountDownLatch(1);
493          ExecutorService e = new DirectExecutorService();
494 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
495 +        l.add(new Callable<Integer>() {
496 +                  public Integer call() { return 5/0; }});
497 +        l.add(null);
498          try {
499 <            ArrayList<Callable<Integer>> l
536 <                = new ArrayList<Callable<Integer>>();
537 <            l.add(new Callable<Integer>() {
538 <                      public Integer call() { return 5/0; }});
539 <            l.add(null);
540 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
499 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
500              shouldThrow();
501          } catch (NullPointerException success) {
502          } finally {
544            latch.countDown();
503              joinPool(e);
504          }
505      }
# Line 551 | Line 509 | public class AbstractExecutorServiceTest
509       */
510      public void testTimedInvokeAny4() throws Exception {
511          ExecutorService e = new DirectExecutorService();
512 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
513 +        l.add(new NPETask());
514          try {
515 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
556 <            l.add(new NPETask());
557 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
515 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
516              shouldThrow();
517          } catch (ExecutionException success) {
518              assertTrue(success.getCause() instanceof NullPointerException);
# Line 569 | Line 527 | public class AbstractExecutorServiceTest
527      public void testTimedInvokeAny5() throws Exception {
528          ExecutorService e = new DirectExecutorService();
529          try {
530 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
530 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
531              l.add(new StringTask());
532              l.add(new StringTask());
533 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
533 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
534              assertSame(TEST_STRING, result);
535          } finally {
536              joinPool(e);
# Line 585 | Line 543 | public class AbstractExecutorServiceTest
543      public void testTimedInvokeAll1() throws InterruptedException {
544          ExecutorService e = new DirectExecutorService();
545          try {
546 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
546 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
547              shouldThrow();
548          } catch (NullPointerException success) {
549          } finally {
# Line 598 | Line 556 | public class AbstractExecutorServiceTest
556       */
557      public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
558          ExecutorService e = new DirectExecutorService();
559 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
560 +        l.add(new StringTask());
561          try {
602            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
603            l.add(new StringTask());
562              e.invokeAll(l, MEDIUM_DELAY_MS, null);
563              shouldThrow();
564          } catch (NullPointerException success) {
# Line 615 | Line 573 | public class AbstractExecutorServiceTest
573      public void testTimedInvokeAll2() throws InterruptedException {
574          ExecutorService e = new DirectExecutorService();
575          try {
576 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
576 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
577              assertTrue(r.isEmpty());
578          } finally {
579              joinPool(e);
# Line 627 | Line 585 | public class AbstractExecutorServiceTest
585       */
586      public void testTimedInvokeAll3() throws InterruptedException {
587          ExecutorService e = new DirectExecutorService();
588 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
589 +        l.add(new StringTask());
590 +        l.add(null);
591          try {
592 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
632 <            l.add(new StringTask());
633 <            l.add(null);
634 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
592 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
593              shouldThrow();
594          } catch (NullPointerException success) {
595          } finally {
# Line 645 | Line 603 | public class AbstractExecutorServiceTest
603      public void testTimedInvokeAll4() throws Exception {
604          ExecutorService e = new DirectExecutorService();
605          try {
606 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
606 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
607              l.add(new NPETask());
608 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
609 <            assertEquals(1, result.size());
610 <            for (Future<String> future : result) {
611 <                try {
612 <                    future.get();
613 <                } catch (ExecutionException success) {
614 <                    assertTrue(success.getCause() instanceof NullPointerException);
615 <                }
608 >            List<Future<String>> futures =
609 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
610 >            assertEquals(1, futures.size());
611 >            try {
612 >                futures.get(0).get();
613 >                shouldThrow();
614 >            } catch (ExecutionException success) {
615 >                assertTrue(success.getCause() instanceof NullPointerException);
616              }
617          } finally {
618              joinPool(e);
# Line 667 | Line 625 | public class AbstractExecutorServiceTest
625      public void testTimedInvokeAll5() throws Exception {
626          ExecutorService e = new DirectExecutorService();
627          try {
628 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
628 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
629              l.add(new StringTask());
630              l.add(new StringTask());
631 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
632 <            assertEquals(2, result.size());
633 <            for (Future<String> future : result)
631 >            List<Future<String>> futures =
632 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
633 >            assertEquals(2, futures.size());
634 >            for (Future<String> future : futures)
635                  assertSame(TEST_STRING, future.get());
636          } finally {
637              joinPool(e);
# Line 685 | Line 644 | public class AbstractExecutorServiceTest
644      public void testTimedInvokeAll6() throws InterruptedException {
645          ExecutorService e = new DirectExecutorService();
646          try {
647 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
647 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
648              l.add(new StringTask());
649              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
650              l.add(new StringTask());
651 <            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
652 <            assertEquals(3, result.size());
653 <            Iterator<Future<String>> it = result.iterator();
651 >            List<Future<String>> futures =
652 >                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
653 >            assertEquals(3, futures.size());
654 >            Iterator<Future<String>> it = futures.iterator();
655              Future<String> f1 = it.next();
656              Future<String> f2 = it.next();
657              Future<String> f3 = it.next();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines