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.27 by jsr166, Sat Oct 9 19:30:34 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 28 | Line 29 | public class AbstractExecutorServiceTest
29      static class DirectExecutorService extends AbstractExecutorService {
30          public void execute(Runnable r) { r.run(); }
31          public void shutdown() { shutdown = true; }
32 <        public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
32 >        public List<Runnable> shutdownNow() {
33 >            shutdown = true;
34 >            return Collections.EMPTY_LIST;
35 >        }
36          public boolean isShutdown() { return shutdown; }
37          public boolean isTerminated() { return isShutdown(); }
38 <        public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
38 >        public boolean awaitTermination(long timeout, TimeUnit unit) {
39 >            return isShutdown();
40 >        }
41          private volatile boolean shutdown = false;
42      }
43  
# Line 80 | Line 86 | public class AbstractExecutorServiceTest
86  
87  
88      /**
89 <     * A submitted privileged action to completion
89 >     * A submitted privileged action runs to completion
90       */
91      public void testSubmitPrivilegedAction() throws Exception {
92 <        Policy savedPolicy = null;
93 <        try {
94 <            savedPolicy = Policy.getPolicy();
95 <            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() {
92 >        Runnable r = new CheckedRunnable() {
93 >            public void realRun() throws Exception {
94 >                ExecutorService e = new DirectExecutorService();
95 >                Future future = e.submit(Executors.callable(new PrivilegedAction() {
96                      public Object run() {
97                          return TEST_STRING;
98                      }}));
99  
100 <            Object result = future.get();
101 <            assertSame(TEST_STRING, result);
102 <        }
103 <        finally {
104 <            try {
105 <                Policy.setPolicy(savedPolicy);
106 <            } catch (AccessControlException ok) {
110 <                return;
111 <            }
112 <        }
100 >                assertSame(TEST_STRING, future.get());
101 >            }};
102 >
103 >        runWithPermissions(r,
104 >                           new RuntimePermission("getClassLoader"),
105 >                           new RuntimePermission("setContextClassLoader"),
106 >                           new RuntimePermission("modifyThread"));
107      }
108  
109      /**
110 <     * A submitted a privileged exception action runs to completion
110 >     * A submitted privileged exception action runs to completion
111       */
112      public void testSubmitPrivilegedExceptionAction() throws Exception {
113 <        Policy savedPolicy = null;
114 <        try {
115 <            savedPolicy = Policy.getPolicy();
116 <            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() {
113 >        Runnable r = new CheckedRunnable() {
114 >            public void realRun() throws Exception {
115 >                ExecutorService e = new DirectExecutorService();
116 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
117                      public Object run() {
118                          return TEST_STRING;
119                      }}));
120  
121 <            Object result = future.get();
122 <            assertSame(TEST_STRING, result);
123 <        }
124 <        finally {
141 <            Policy.setPolicy(savedPolicy);
142 <        }
121 >                assertSame(TEST_STRING, future.get());
122 >            }};
123 >
124 >        runWithPermissions(r);
125      }
126  
127      /**
128       * A submitted failed privileged exception action reports exception
129       */
130      public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
131 <        Policy savedPolicy = null;
132 <        try {
133 <            savedPolicy = Policy.getPolicy();
134 <            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() {
131 >        Runnable r = new CheckedRunnable() {
132 >            public void realRun() throws Exception {
133 >                ExecutorService e = new DirectExecutorService();
134 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
135                      public Object run() throws Exception {
136                          throw new IndexOutOfBoundsException();
137                      }}));
138  
139 <            future.get();
140 <            shouldThrow();
141 <        } catch (ExecutionException success) {
142 <            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
143 <        }
144 <        finally {
145 <            Policy.setPolicy(savedPolicy);
146 <        }
139 >                try {
140 >                    future.get();
141 >                    shouldThrow();
142 >                } catch (ExecutionException success) {
143 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
144 >                }}};
145 >
146 >        runWithPermissions(r);
147      }
148  
149      /**
# Line 245 | Line 217 | public class AbstractExecutorServiceTest
217  
218  
219      /**
220 <     *  Blocking on submit(callable) throws InterruptedException if
249 <     *  caller interrupted.
220 >     * submit(callable).get() throws InterruptedException if interrupted
221       */
222      public void testInterruptedSubmit() throws InterruptedException {
223 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
224 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
225 <            public void realRun() throws Exception {
226 <                p.submit(new CheckedCallable<Object>() {
227 <                             public Object realCall()
228 <                                 throws InterruptedException {
229 <                                 Thread.sleep(SMALL_DELAY_MS);
230 <                                 return null;
231 <                             }}).get();
232 <            }});
233 <
234 <        t.start();
235 <        Thread.sleep(SHORT_DELAY_MS);
236 <        t.interrupt();
237 <        joinPool(p);
223 >        final CountDownLatch submitted    = new CountDownLatch(1);
224 >        final CountDownLatch quittingTime = new CountDownLatch(1);
225 >        final ExecutorService p
226 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
227 >                                     new ArrayBlockingQueue<Runnable>(10));
228 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
229 >            public Void realCall() throws InterruptedException {
230 >                quittingTime.await();
231 >                return null;
232 >            }};
233 >        try {
234 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
235 >                public void realRun() throws Exception {
236 >                    Future<Void> future = p.submit(awaiter);
237 >                    submitted.countDown();
238 >                    future.get();
239 >                }});
240 >            t.start();
241 >            submitted.await();
242 >            t.interrupt();
243 >            t.join();
244 >        } finally {
245 >            quittingTime.countDown();
246 >            joinPool(p);
247 >        }
248      }
249  
250      /**
251 <     *  get of submitted callable throws InterruptedException if callable
252 <     *  interrupted
251 >     * get of submitted callable throws InterruptedException if callable
252 >     * interrupted
253       */
254      public void testSubmitIE() throws InterruptedException {
255          final ThreadPoolExecutor p =
# Line 289 | Line 270 | public class AbstractExecutorServiceTest
270      }
271  
272      /**
273 <     *  get of submit(callable) throws ExecutionException if callable
274 <     *  throws exception
273 >     * get of submit(callable) throws ExecutionException if callable
274 >     * throws exception
275       */
276      public void testSubmitEE() throws InterruptedException {
277          ThreadPoolExecutor p =
# Line 344 | Line 325 | public class AbstractExecutorServiceTest
325       * invokeAny(c) throws NPE if c has null elements
326       */
327      public void testInvokeAny3() throws Exception {
347        final CountDownLatch latch = new CountDownLatch(1);
328          ExecutorService e = new DirectExecutorService();
329 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
330 +        l.add(new Callable<Integer>() {
331 +                  public Integer call() { return 5/0; }});
332 +        l.add(null);
333          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);
334              e.invokeAny(l);
335              shouldThrow();
336          } catch (NullPointerException success) {
337          } finally {
359            latch.countDown();
338              joinPool(e);
339          }
340      }
# Line 366 | Line 344 | public class AbstractExecutorServiceTest
344       */
345      public void testInvokeAny4() throws InterruptedException {
346          ExecutorService e = new DirectExecutorService();
347 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
348 +        l.add(new NPETask());
349          try {
370            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
371            l.add(new NPETask());
350              e.invokeAny(l);
351              shouldThrow();
352          } catch (ExecutionException success) {
# Line 384 | Line 362 | public class AbstractExecutorServiceTest
362      public void testInvokeAny5() throws Exception {
363          ExecutorService e = new DirectExecutorService();
364          try {
365 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
365 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
366              l.add(new StringTask());
367              l.add(new StringTask());
368              String result = e.invokeAny(l);
# Line 426 | Line 404 | public class AbstractExecutorServiceTest
404       */
405      public void testInvokeAll3() throws InterruptedException {
406          ExecutorService e = new DirectExecutorService();
407 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
408 +        l.add(new StringTask());
409 +        l.add(null);
410          try {
430            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
431            l.add(new StringTask());
432            l.add(null);
411              e.invokeAll(l);
412              shouldThrow();
413          } catch (NullPointerException success) {
# Line 444 | Line 422 | public class AbstractExecutorServiceTest
422      public void testInvokeAll4() throws Exception {
423          ExecutorService e = new DirectExecutorService();
424          try {
425 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
425 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
426              l.add(new NPETask());
427 <            List<Future<String>> result = e.invokeAll(l);
428 <            assertEquals(1, result.size());
429 <            for (Future<String> future : result) {
430 <                try {
431 <                    future.get();
432 <                    shouldThrow();
433 <                } catch (ExecutionException success) {
456 <                    Throwable cause = success.getCause();
457 <                    assertTrue(cause instanceof NullPointerException);
458 <                }
427 >            List<Future<String>> futures = e.invokeAll(l);
428 >            assertEquals(1, futures.size());
429 >            try {
430 >                futures.get(0).get();
431 >                shouldThrow();
432 >            } catch (ExecutionException success) {
433 >                assertTrue(success.getCause() instanceof NullPointerException);
434              }
435          } finally {
436              joinPool(e);
# Line 468 | Line 443 | public class AbstractExecutorServiceTest
443      public void testInvokeAll5() throws Exception {
444          ExecutorService e = new DirectExecutorService();
445          try {
446 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
446 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
447              l.add(new StringTask());
448              l.add(new StringTask());
449 <            List<Future<String>> result = e.invokeAll(l);
450 <            assertEquals(2, result.size());
451 <            for (Future<String> future : result)
449 >            List<Future<String>> futures = e.invokeAll(l);
450 >            assertEquals(2, futures.size());
451 >            for (Future<String> future : futures)
452                  assertSame(TEST_STRING, future.get());
453          } finally {
454              joinPool(e);
# Line 487 | Line 462 | public class AbstractExecutorServiceTest
462      public void testTimedInvokeAny1() throws Exception {
463          ExecutorService e = new DirectExecutorService();
464          try {
465 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
465 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
466              shouldThrow();
467          } catch (NullPointerException success) {
468          } finally {
# Line 500 | Line 475 | public class AbstractExecutorServiceTest
475       */
476      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
477          ExecutorService e = new DirectExecutorService();
478 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
479 +        l.add(new StringTask());
480          try {
504            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
505            l.add(new StringTask());
481              e.invokeAny(l, MEDIUM_DELAY_MS, null);
482              shouldThrow();
483          } catch (NullPointerException success) {
# Line 517 | Line 492 | public class AbstractExecutorServiceTest
492      public void testTimedInvokeAny2() throws Exception {
493          ExecutorService e = new DirectExecutorService();
494          try {
495 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
495 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
496              shouldThrow();
497          } catch (IllegalArgumentException success) {
498          } finally {
# Line 529 | Line 504 | public class AbstractExecutorServiceTest
504       * timed invokeAny(c) throws NPE if c has null elements
505       */
506      public void testTimedInvokeAny3() throws Exception {
532        final CountDownLatch latch = new CountDownLatch(1);
507          ExecutorService e = new DirectExecutorService();
508 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
509 +        l.add(new Callable<Integer>() {
510 +                  public Integer call() { return 5/0; }});
511 +        l.add(null);
512          try {
513 <            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);
513 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
514              shouldThrow();
515          } catch (NullPointerException success) {
516          } finally {
544            latch.countDown();
517              joinPool(e);
518          }
519      }
# Line 551 | Line 523 | public class AbstractExecutorServiceTest
523       */
524      public void testTimedInvokeAny4() throws Exception {
525          ExecutorService e = new DirectExecutorService();
526 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
527 +        l.add(new NPETask());
528          try {
529 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
556 <            l.add(new NPETask());
557 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
529 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
530              shouldThrow();
531          } catch (ExecutionException success) {
532              assertTrue(success.getCause() instanceof NullPointerException);
# Line 569 | Line 541 | public class AbstractExecutorServiceTest
541      public void testTimedInvokeAny5() throws Exception {
542          ExecutorService e = new DirectExecutorService();
543          try {
544 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
544 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
545              l.add(new StringTask());
546              l.add(new StringTask());
547 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
547 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
548              assertSame(TEST_STRING, result);
549          } finally {
550              joinPool(e);
# Line 585 | Line 557 | public class AbstractExecutorServiceTest
557      public void testTimedInvokeAll1() throws InterruptedException {
558          ExecutorService e = new DirectExecutorService();
559          try {
560 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
560 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
561              shouldThrow();
562          } catch (NullPointerException success) {
563          } finally {
# Line 598 | Line 570 | public class AbstractExecutorServiceTest
570       */
571      public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
572          ExecutorService e = new DirectExecutorService();
573 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
574 +        l.add(new StringTask());
575          try {
602            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
603            l.add(new StringTask());
576              e.invokeAll(l, MEDIUM_DELAY_MS, null);
577              shouldThrow();
578          } catch (NullPointerException success) {
# Line 615 | Line 587 | public class AbstractExecutorServiceTest
587      public void testTimedInvokeAll2() throws InterruptedException {
588          ExecutorService e = new DirectExecutorService();
589          try {
590 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
590 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
591              assertTrue(r.isEmpty());
592          } finally {
593              joinPool(e);
# Line 627 | Line 599 | public class AbstractExecutorServiceTest
599       */
600      public void testTimedInvokeAll3() throws InterruptedException {
601          ExecutorService e = new DirectExecutorService();
602 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
603 +        l.add(new StringTask());
604 +        l.add(null);
605          try {
606 <            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);
606 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
607              shouldThrow();
608          } catch (NullPointerException success) {
609          } finally {
# Line 645 | Line 617 | public class AbstractExecutorServiceTest
617      public void testTimedInvokeAll4() throws Exception {
618          ExecutorService e = new DirectExecutorService();
619          try {
620 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
620 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
621              l.add(new NPETask());
622 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
623 <            assertEquals(1, result.size());
624 <            for (Future<String> future : result) {
625 <                try {
626 <                    future.get();
627 <                } catch (ExecutionException success) {
628 <                    assertTrue(success.getCause() instanceof NullPointerException);
629 <                }
622 >            List<Future<String>> futures =
623 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
624 >            assertEquals(1, futures.size());
625 >            try {
626 >                futures.get(0).get();
627 >                shouldThrow();
628 >            } catch (ExecutionException success) {
629 >                assertTrue(success.getCause() instanceof NullPointerException);
630              }
631          } finally {
632              joinPool(e);
# Line 667 | Line 639 | public class AbstractExecutorServiceTest
639      public void testTimedInvokeAll5() throws Exception {
640          ExecutorService e = new DirectExecutorService();
641          try {
642 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
642 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
643              l.add(new StringTask());
644              l.add(new StringTask());
645 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
646 <            assertEquals(2, result.size());
647 <            for (Future<String> future : result)
645 >            List<Future<String>> futures =
646 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
647 >            assertEquals(2, futures.size());
648 >            for (Future<String> future : futures)
649                  assertSame(TEST_STRING, future.get());
650          } finally {
651              joinPool(e);
# Line 685 | Line 658 | public class AbstractExecutorServiceTest
658      public void testTimedInvokeAll6() throws InterruptedException {
659          ExecutorService e = new DirectExecutorService();
660          try {
661 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
661 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
662              l.add(new StringTask());
663              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
664              l.add(new StringTask());
665 <            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
666 <            assertEquals(3, result.size());
667 <            Iterator<Future<String>> it = result.iterator();
665 >            List<Future<String>> futures =
666 >                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
667 >            assertEquals(3, futures.size());
668 >            Iterator<Future<String>> it = futures.iterator();
669              Future<String> f1 = it.next();
670              Future<String> f2 = it.next();
671              Future<String> f3 = it.next();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines