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.25 by jsr166, Thu Sep 16 02:54:10 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  
17   public class AbstractExecutorServiceTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(AbstractExecutorServiceTest.class);
# 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 245 | Line 212 | public class AbstractExecutorServiceTest
212  
213  
214      /**
215 <     *  Blocking on submit(callable) throws InterruptedException if
249 <     *  caller interrupted.
215 >     * submit(callable).get() throws InterruptedException if interrupted
216       */
217      public void testInterruptedSubmit() throws InterruptedException {
218 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
219 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
220 <            public void realRun() throws Exception {
221 <                p.submit(new CheckedCallable<Object>() {
222 <                             public Object realCall()
223 <                                 throws InterruptedException {
224 <                                 Thread.sleep(SMALL_DELAY_MS);
225 <                                 return null;
226 <                             }}).get();
227 <            }});
228 <
229 <        t.start();
230 <        Thread.sleep(SHORT_DELAY_MS);
231 <        t.interrupt();
232 <        joinPool(p);
218 >        final CountDownLatch submitted    = new CountDownLatch(1);
219 >        final CountDownLatch quittingTime = new CountDownLatch(1);
220 >        final ExecutorService p
221 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
222 >                                     new ArrayBlockingQueue<Runnable>(10));
223 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
224 >            public Void realCall() throws InterruptedException {
225 >                quittingTime.await();
226 >                return null;
227 >            }};
228 >        try {
229 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
230 >                public void realRun() throws Exception {
231 >                    Future<Void> future = p.submit(awaiter);
232 >                    submitted.countDown();
233 >                    future.get();
234 >                }});
235 >            t.start();
236 >            submitted.await();
237 >            t.interrupt();
238 >            t.join();
239 >        } finally {
240 >            quittingTime.countDown();
241 >            joinPool(p);
242 >        }
243      }
244  
245      /**
# Line 344 | Line 320 | public class AbstractExecutorServiceTest
320       * invokeAny(c) throws NPE if c has null elements
321       */
322      public void testInvokeAny3() throws Exception {
347        final CountDownLatch latch = new CountDownLatch(1);
323          ExecutorService e = new DirectExecutorService();
324 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
325 +        l.add(new Callable<Integer>() {
326 +                  public Integer call() { return 5/0; }});
327 +        l.add(null);
328          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);
329              e.invokeAny(l);
330              shouldThrow();
331          } catch (NullPointerException success) {
332          } finally {
359            latch.countDown();
333              joinPool(e);
334          }
335      }
# Line 366 | Line 339 | public class AbstractExecutorServiceTest
339       */
340      public void testInvokeAny4() throws InterruptedException {
341          ExecutorService e = new DirectExecutorService();
342 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
343 +        l.add(new NPETask());
344          try {
370            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
371            l.add(new NPETask());
345              e.invokeAny(l);
346              shouldThrow();
347          } catch (ExecutionException success) {
# Line 384 | Line 357 | public class AbstractExecutorServiceTest
357      public void testInvokeAny5() throws Exception {
358          ExecutorService e = new DirectExecutorService();
359          try {
360 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
360 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
361              l.add(new StringTask());
362              l.add(new StringTask());
363              String result = e.invokeAny(l);
# Line 426 | Line 399 | public class AbstractExecutorServiceTest
399       */
400      public void testInvokeAll3() throws InterruptedException {
401          ExecutorService e = new DirectExecutorService();
402 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
403 +        l.add(new StringTask());
404 +        l.add(null);
405          try {
430            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
431            l.add(new StringTask());
432            l.add(null);
406              e.invokeAll(l);
407              shouldThrow();
408          } catch (NullPointerException success) {
# Line 444 | Line 417 | public class AbstractExecutorServiceTest
417      public void testInvokeAll4() throws Exception {
418          ExecutorService e = new DirectExecutorService();
419          try {
420 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
420 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
421              l.add(new NPETask());
422 <            List<Future<String>> result = e.invokeAll(l);
423 <            assertEquals(1, result.size());
424 <            for (Future<String> future : result) {
425 <                try {
426 <                    future.get();
427 <                    shouldThrow();
428 <                } catch (ExecutionException success) {
456 <                    Throwable cause = success.getCause();
457 <                    assertTrue(cause instanceof NullPointerException);
458 <                }
422 >            List<Future<String>> futures = e.invokeAll(l);
423 >            assertEquals(1, futures.size());
424 >            try {
425 >                futures.get(0).get();
426 >                shouldThrow();
427 >            } catch (ExecutionException success) {
428 >                assertTrue(success.getCause() instanceof NullPointerException);
429              }
430          } finally {
431              joinPool(e);
# Line 468 | Line 438 | public class AbstractExecutorServiceTest
438      public void testInvokeAll5() throws Exception {
439          ExecutorService e = new DirectExecutorService();
440          try {
441 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
441 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
442              l.add(new StringTask());
443              l.add(new StringTask());
444 <            List<Future<String>> result = e.invokeAll(l);
445 <            assertEquals(2, result.size());
446 <            for (Future<String> future : result)
444 >            List<Future<String>> futures = e.invokeAll(l);
445 >            assertEquals(2, futures.size());
446 >            for (Future<String> future : futures)
447                  assertSame(TEST_STRING, future.get());
448          } finally {
449              joinPool(e);
# Line 487 | Line 457 | public class AbstractExecutorServiceTest
457      public void testTimedInvokeAny1() throws Exception {
458          ExecutorService e = new DirectExecutorService();
459          try {
460 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
460 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
461              shouldThrow();
462          } catch (NullPointerException success) {
463          } finally {
# Line 500 | Line 470 | public class AbstractExecutorServiceTest
470       */
471      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
472          ExecutorService e = new DirectExecutorService();
473 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
474 +        l.add(new StringTask());
475          try {
504            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
505            l.add(new StringTask());
476              e.invokeAny(l, MEDIUM_DELAY_MS, null);
477              shouldThrow();
478          } catch (NullPointerException success) {
# Line 517 | Line 487 | public class AbstractExecutorServiceTest
487      public void testTimedInvokeAny2() throws Exception {
488          ExecutorService e = new DirectExecutorService();
489          try {
490 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
490 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
491              shouldThrow();
492          } catch (IllegalArgumentException success) {
493          } finally {
# Line 529 | Line 499 | public class AbstractExecutorServiceTest
499       * timed invokeAny(c) throws NPE if c has null elements
500       */
501      public void testTimedInvokeAny3() throws Exception {
532        final CountDownLatch latch = new CountDownLatch(1);
502          ExecutorService e = new DirectExecutorService();
503 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
504 +        l.add(new Callable<Integer>() {
505 +                  public Integer call() { return 5/0; }});
506 +        l.add(null);
507          try {
508 <            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);
508 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
509              shouldThrow();
510          } catch (NullPointerException success) {
511          } finally {
544            latch.countDown();
512              joinPool(e);
513          }
514      }
# Line 551 | Line 518 | public class AbstractExecutorServiceTest
518       */
519      public void testTimedInvokeAny4() throws Exception {
520          ExecutorService e = new DirectExecutorService();
521 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
522 +        l.add(new NPETask());
523          try {
524 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
556 <            l.add(new NPETask());
557 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
524 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
525              shouldThrow();
526          } catch (ExecutionException success) {
527              assertTrue(success.getCause() instanceof NullPointerException);
# Line 569 | Line 536 | public class AbstractExecutorServiceTest
536      public void testTimedInvokeAny5() throws Exception {
537          ExecutorService e = new DirectExecutorService();
538          try {
539 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
539 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
540              l.add(new StringTask());
541              l.add(new StringTask());
542 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
542 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
543              assertSame(TEST_STRING, result);
544          } finally {
545              joinPool(e);
# Line 585 | Line 552 | public class AbstractExecutorServiceTest
552      public void testTimedInvokeAll1() throws InterruptedException {
553          ExecutorService e = new DirectExecutorService();
554          try {
555 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
555 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
556              shouldThrow();
557          } catch (NullPointerException success) {
558          } finally {
# Line 598 | Line 565 | public class AbstractExecutorServiceTest
565       */
566      public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
567          ExecutorService e = new DirectExecutorService();
568 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
569 +        l.add(new StringTask());
570          try {
602            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
603            l.add(new StringTask());
571              e.invokeAll(l, MEDIUM_DELAY_MS, null);
572              shouldThrow();
573          } catch (NullPointerException success) {
# Line 615 | Line 582 | public class AbstractExecutorServiceTest
582      public void testTimedInvokeAll2() throws InterruptedException {
583          ExecutorService e = new DirectExecutorService();
584          try {
585 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
585 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
586              assertTrue(r.isEmpty());
587          } finally {
588              joinPool(e);
# Line 627 | Line 594 | public class AbstractExecutorServiceTest
594       */
595      public void testTimedInvokeAll3() throws InterruptedException {
596          ExecutorService e = new DirectExecutorService();
597 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
598 +        l.add(new StringTask());
599 +        l.add(null);
600          try {
601 <            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);
601 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
602              shouldThrow();
603          } catch (NullPointerException success) {
604          } finally {
# Line 645 | Line 612 | public class AbstractExecutorServiceTest
612      public void testTimedInvokeAll4() throws Exception {
613          ExecutorService e = new DirectExecutorService();
614          try {
615 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
615 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
616              l.add(new NPETask());
617 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
618 <            assertEquals(1, result.size());
619 <            for (Future<String> future : result) {
620 <                try {
621 <                    future.get();
622 <                } catch (ExecutionException success) {
623 <                    assertTrue(success.getCause() instanceof NullPointerException);
624 <                }
617 >            List<Future<String>> futures =
618 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
619 >            assertEquals(1, futures.size());
620 >            try {
621 >                futures.get(0).get();
622 >                shouldThrow();
623 >            } catch (ExecutionException success) {
624 >                assertTrue(success.getCause() instanceof NullPointerException);
625              }
626          } finally {
627              joinPool(e);
# Line 667 | Line 634 | public class AbstractExecutorServiceTest
634      public void testTimedInvokeAll5() throws Exception {
635          ExecutorService e = new DirectExecutorService();
636          try {
637 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
637 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
638              l.add(new StringTask());
639              l.add(new StringTask());
640 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
641 <            assertEquals(2, result.size());
642 <            for (Future<String> future : result)
640 >            List<Future<String>> futures =
641 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
642 >            assertEquals(2, futures.size());
643 >            for (Future<String> future : futures)
644                  assertSame(TEST_STRING, future.get());
645          } finally {
646              joinPool(e);
# Line 685 | Line 653 | public class AbstractExecutorServiceTest
653      public void testTimedInvokeAll6() throws InterruptedException {
654          ExecutorService e = new DirectExecutorService();
655          try {
656 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
656 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
657              l.add(new StringTask());
658              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
659              l.add(new StringTask());
660 <            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
661 <            assertEquals(3, result.size());
662 <            Iterator<Future<String>> it = result.iterator();
660 >            List<Future<String>> futures =
661 >                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
662 >            assertEquals(3, futures.size());
663 >            Iterator<Future<String>> it = futures.iterator();
664              Future<String> f1 = it.next();
665              Future<String> f2 = it.next();
666              Future<String> f3 = it.next();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines