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.17 by jsr166, Mon Nov 16 04:57:09 2009 UTC vs.
Revision 1.28 by jsr166, Mon Oct 11 07:21:32 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{
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  
44      /**
45       * execute(runnable) runs it to completion
46       */
47 <    public void testExecuteRunnable() {
48 <        try {
49 <            ExecutorService e = new DirectExecutorService();
50 <            TrackedShortRunnable task = new TrackedShortRunnable();
51 <            assertFalse(task.done);
52 <            Future<?> future = e.submit(task);
53 <            future.get();
48 <            assertTrue(task.done);
49 <        }
50 <        catch (ExecutionException ex) {
51 <            unexpectedException();
52 <        }
53 <        catch (InterruptedException ex) {
54 <            unexpectedException();
55 <        }
47 >    public void testExecuteRunnable() throws Exception {
48 >        ExecutorService e = new DirectExecutorService();
49 >        TrackedShortRunnable task = new TrackedShortRunnable();
50 >        assertFalse(task.done);
51 >        Future<?> future = e.submit(task);
52 >        future.get();
53 >        assertTrue(task.done);
54      }
55  
56  
57      /**
58       * Completed submit(callable) returns result
59       */
60 <    public void testSubmitCallable() {
61 <        try {
62 <            ExecutorService e = new DirectExecutorService();
63 <            Future<String> future = e.submit(new StringTask());
64 <            String result = future.get();
67 <            assertSame(TEST_STRING, result);
68 <        }
69 <        catch (ExecutionException ex) {
70 <            unexpectedException();
71 <        }
72 <        catch (InterruptedException ex) {
73 <            unexpectedException();
74 <        }
60 >    public void testSubmitCallable() throws Exception {
61 >        ExecutorService e = new DirectExecutorService();
62 >        Future<String> future = e.submit(new StringTask());
63 >        String result = future.get();
64 >        assertSame(TEST_STRING, result);
65      }
66  
67      /**
68       * Completed submit(runnable) returns successfully
69       */
70 <    public void testSubmitRunnable() {
71 <        try {
72 <            ExecutorService e = new DirectExecutorService();
73 <            Future<?> future = e.submit(new NoOpRunnable());
74 <            future.get();
85 <            assertTrue(future.isDone());
86 <        }
87 <        catch (ExecutionException ex) {
88 <            unexpectedException();
89 <        }
90 <        catch (InterruptedException ex) {
91 <            unexpectedException();
92 <        }
70 >    public void testSubmitRunnable() throws Exception {
71 >        ExecutorService e = new DirectExecutorService();
72 >        Future<?> future = e.submit(new NoOpRunnable());
73 >        future.get();
74 >        assertTrue(future.isDone());
75      }
76  
77      /**
78       * Completed submit(runnable, result) returns result
79       */
80 <    public void testSubmitRunnable2() {
81 <        try {
82 <            ExecutorService e = new DirectExecutorService();
83 <            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
84 <            String result = future.get();
103 <            assertSame(TEST_STRING, result);
104 <        }
105 <        catch (ExecutionException ex) {
106 <            unexpectedException();
107 <        }
108 <        catch (InterruptedException ex) {
109 <            unexpectedException();
110 <        }
80 >    public void testSubmitRunnable2() throws Exception {
81 >        ExecutorService e = new DirectExecutorService();
82 >        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
83 >        String result = future.get();
84 >        assertSame(TEST_STRING, result);
85      }
86  
87  
88      /**
89 <     * A submitted privileged action to completion
89 >     * A submitted privileged action runs to completion
90       */
91 <    public void testSubmitPrivilegedAction() {
92 <        Policy savedPolicy = null;
93 <        try {
94 <            savedPolicy = Policy.getPolicy();
95 <            AdjustablePolicy policy = new AdjustablePolicy();
122 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
123 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
124 <            Policy.setPolicy(policy);
125 <        } catch (AccessControlException ok) {
126 <            return;
127 <        }
128 <        try {
129 <            ExecutorService e = new DirectExecutorService();
130 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
91 >    public void testSubmitPrivilegedAction() throws Exception {
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 <        catch (ExecutionException ex) {
104 <            unexpectedException();
105 <        }
106 <        catch (InterruptedException ex) {
142 <            unexpectedException();
143 <        }
144 <        finally {
145 <            try {
146 <                Policy.setPolicy(savedPolicy);
147 <            } catch (AccessControlException ok) {
148 <                return;
149 <            }
150 <        }
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() {
113 <        Policy savedPolicy = null;
114 <        try {
115 <            savedPolicy = Policy.getPolicy();
116 <            AdjustablePolicy policy = new AdjustablePolicy();
161 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
162 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
163 <            Policy.setPolicy(policy);
164 <        } catch (AccessControlException ok) {
165 <            return;
166 <        }
167 <
168 <        try {
169 <            ExecutorService e = new DirectExecutorService();
170 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
112 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
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 <        catch (ExecutionException ex) {
179 <            unexpectedException();
180 <        }
181 <        catch (InterruptedException ex) {
182 <            unexpectedException();
183 <        }
184 <        finally {
185 <            Policy.setPolicy(savedPolicy);
186 <        }
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() {
131 <        Policy savedPolicy = null;
132 <        try {
133 <            savedPolicy = Policy.getPolicy();
134 <            AdjustablePolicy policy = new AdjustablePolicy();
197 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
198 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
199 <            Policy.setPolicy(policy);
200 <        } catch (AccessControlException ok) {
201 <            return;
202 <        }
203 <
204 <
205 <        try {
206 <            ExecutorService e = new DirectExecutorService();
207 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
130 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
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 <            Object result = future.get();
140 <            shouldThrow();
141 <        }
142 <        catch (ExecutionException success) {
143 <        }
144 <        catch (InterruptedException ex) {
145 <            unexpectedException();
146 <        }
220 <        finally {
221 <            Policy.setPolicy(savedPolicy);
222 <        }
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 228 | Line 152 | public class AbstractExecutorServiceTest
152      public void testExecuteNullRunnable() {
153          try {
154              ExecutorService e = new DirectExecutorService();
155 <            TrackedShortRunnable task = null;
232 <            Future<?> future = e.submit(task);
155 >            e.submit((Runnable) null);
156              shouldThrow();
157 <        }
235 <        catch (NullPointerException success) {
236 <        }
237 <        catch (Exception ex) {
238 <            unexpectedException();
239 <        }
157 >        } catch (NullPointerException success) {}
158      }
159  
160  
# Line 246 | Line 164 | public class AbstractExecutorServiceTest
164      public void testSubmitNullCallable() {
165          try {
166              ExecutorService e = new DirectExecutorService();
167 <            StringTask t = null;
250 <            Future<String> future = e.submit(t);
167 >            e.submit((Callable) null);
168              shouldThrow();
169 <        }
253 <        catch (NullPointerException success) {
254 <        }
255 <        catch (Exception ex) {
256 <            unexpectedException();
257 <        }
169 >        } catch (NullPointerException success) {}
170      }
171  
172      /**
173 <     * submit(runnable) throws RejectedExecutionException if
262 <     * executor is saturated.
173 >     * submit(callable).get() throws InterruptedException if interrupted
174       */
175 <    public void testExecute1() {
176 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
177 <        try {
178 <
179 <            for (int i = 0; i < 5; ++i){
180 <                p.submit(new MediumRunnable());
181 <            }
182 <            shouldThrow();
183 <        } catch (RejectedExecutionException success){}
184 <        joinPool(p);
185 <    }
186 <
187 <    /**
188 <     * submit(callable) throws RejectedExecutionException
189 <     * if executor is saturated.
190 <     */
191 <    public void testExecute2() {
192 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
282 <        try {
283 <            for (int i = 0; i < 5; ++i) {
284 <                p.submit(new SmallCallable());
285 <            }
286 <            shouldThrow();
287 <        } catch (RejectedExecutionException e){}
288 <        joinPool(p);
289 <    }
290 <
291 <
292 <    /**
293 <     *  Blocking on submit(callable) throws InterruptedException if
294 <     *  caller interrupted.
295 <     */
296 <    public void testInterruptedSubmit() {
297 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
298 <        Thread t = new Thread(new Runnable() {
299 <                public void run() {
300 <                    try {
301 <                        p.submit(new Callable<Object>() {
302 <                                public Object call() {
303 <                                    try {
304 <                                        Thread.sleep(MEDIUM_DELAY_MS);
305 <                                        shouldThrow();
306 <                                    } catch (InterruptedException e){
307 <                                    }
308 <                                    return null;
309 <                                }
310 <                            }).get();
311 <                    } catch (InterruptedException success){
312 <                    } catch (Exception e) {
313 <                        unexpectedException();
314 <                    }
315 <
316 <                }
317 <            });
318 <        try {
319 <            t.start();
320 <            Thread.sleep(SHORT_DELAY_MS);
321 <            t.interrupt();
322 <        } catch (Exception e){
323 <            unexpectedException();
324 <        }
325 <        joinPool(p);
326 <    }
327 <
328 <    /**
329 <     *  get of submitted callable throws Exception if callable
330 <     *  interrupted
331 <     */
332 <    public void testSubmitIE() {
333 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
334 <
335 <        final Callable c = new Callable() {
336 <                public Object call() {
337 <                    try {
338 <                        p.submit(new SmallCallable()).get();
339 <                        shouldThrow();
340 <                    } catch (InterruptedException e){}
341 <                    catch (RejectedExecutionException e2){}
342 <                    catch (ExecutionException e3){}
343 <                    return Boolean.TRUE;
344 <                }
345 <            };
346 <
347 <
348 <
349 <        Thread t = new Thread(new Runnable() {
350 <                public void run() {
351 <                    try {
352 <                        c.call();
353 <                    } catch (Exception e){}
354 <                }
355 <          });
356 <        try {
175 >    public void testInterruptedSubmit() throws InterruptedException {
176 >        final CountDownLatch submitted    = new CountDownLatch(1);
177 >        final CountDownLatch quittingTime = new CountDownLatch(1);
178 >        final ExecutorService p
179 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
180 >                                     new ArrayBlockingQueue<Runnable>(10));
181 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
182 >            public Void realCall() throws InterruptedException {
183 >                quittingTime.await();
184 >                return null;
185 >            }};
186 >        try {
187 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
188 >                public void realRun() throws Exception {
189 >                    Future<Void> future = p.submit(awaiter);
190 >                    submitted.countDown();
191 >                    future.get();
192 >                }});
193              t.start();
194 <            Thread.sleep(SHORT_DELAY_MS);
194 >            submitted.await();
195              t.interrupt();
196              t.join();
197 <        } catch (InterruptedException e){
198 <            unexpectedException();
197 >        } finally {
198 >            quittingTime.countDown();
199 >            joinPool(p);
200          }
364
365        joinPool(p);
201      }
202  
203      /**
204 <     *  get of submit(callable) throws ExecutionException if callable
205 <     *  throws exception
204 >     * get of submit(callable) throws ExecutionException if callable
205 >     * throws exception
206       */
207 <    public void testSubmitEE() {
208 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
209 <
210 <        try {
211 <            Callable c = new Callable() {
377 <                    public Object call() {
378 <                        int i = 5/0;
379 <                        return Boolean.TRUE;
380 <                    }
381 <                };
207 >    public void testSubmitEE() throws InterruptedException {
208 >        ThreadPoolExecutor p =
209 >            new ThreadPoolExecutor(1, 1,
210 >                                   60, TimeUnit.SECONDS,
211 >                                   new ArrayBlockingQueue<Runnable>(10));
212  
213 <            for (int i =0; i < 5; i++){
214 <                p.submit(c).get();
385 <            }
213 >        Callable c = new Callable() {
214 >            public Object call() { return 5/0; }};
215  
216 +        try {
217 +            p.submit(c).get();
218              shouldThrow();
219 <        }
220 <        catch (ExecutionException success){
390 <        } catch (Exception e) {
391 <            unexpectedException();
219 >        } catch (ExecutionException success) {
220 >            assertTrue(success.getCause() instanceof ArithmeticException);
221          }
222          joinPool(p);
223      }
# Line 396 | Line 225 | public class AbstractExecutorServiceTest
225      /**
226       * invokeAny(null) throws NPE
227       */
228 <    public void testInvokeAny1() {
228 >    public void testInvokeAny1() throws Exception {
229          ExecutorService e = new DirectExecutorService();
230          try {
231              e.invokeAny(null);
232 +            shouldThrow();
233          } catch (NullPointerException success) {
404        } catch (Exception ex) {
405            unexpectedException();
234          } finally {
235              joinPool(e);
236          }
# Line 411 | Line 239 | public class AbstractExecutorServiceTest
239      /**
240       * invokeAny(empty collection) throws IAE
241       */
242 <    public void testInvokeAny2() {
242 >    public void testInvokeAny2() throws Exception {
243          ExecutorService e = new DirectExecutorService();
244          try {
245              e.invokeAny(new ArrayList<Callable<String>>());
246 +            shouldThrow();
247          } catch (IllegalArgumentException success) {
419        } catch (Exception ex) {
420            unexpectedException();
248          } finally {
249              joinPool(e);
250          }
# Line 426 | Line 253 | public class AbstractExecutorServiceTest
253      /**
254       * invokeAny(c) throws NPE if c has null elements
255       */
256 <    public void testInvokeAny3() {
256 >    public void testInvokeAny3() throws Exception {
257          ExecutorService e = new DirectExecutorService();
258 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
259 +        l.add(new Callable<Integer>() {
260 +                  public Integer call() { return 5/0; }});
261 +        l.add(null);
262          try {
432            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
433            l.add(new StringTask());
434            l.add(null);
263              e.invokeAny(l);
264 +            shouldThrow();
265          } catch (NullPointerException success) {
437        } catch (Exception ex) {
438            ex.printStackTrace();
439            unexpectedException();
266          } finally {
267              joinPool(e);
268          }
# Line 445 | Line 271 | public class AbstractExecutorServiceTest
271      /**
272       * invokeAny(c) throws ExecutionException if no task in c completes
273       */
274 <    public void testInvokeAny4() {
274 >    public void testInvokeAny4() throws InterruptedException {
275          ExecutorService e = new DirectExecutorService();
276 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
277 +        l.add(new NPETask());
278          try {
451            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
452            l.add(new NPETask());
279              e.invokeAny(l);
280 +            shouldThrow();
281          } catch (ExecutionException success) {
282 <        } catch (Exception ex) {
456 <            unexpectedException();
282 >            assertTrue(success.getCause() instanceof NullPointerException);
283          } finally {
284              joinPool(e);
285          }
# Line 462 | Line 288 | public class AbstractExecutorServiceTest
288      /**
289       * invokeAny(c) returns result of some task in c if at least one completes
290       */
291 <    public void testInvokeAny5() {
291 >    public void testInvokeAny5() throws Exception {
292          ExecutorService e = new DirectExecutorService();
293          try {
294 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
294 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
295              l.add(new StringTask());
296              l.add(new StringTask());
297              String result = e.invokeAny(l);
298              assertSame(TEST_STRING, result);
473        } catch (ExecutionException success) {
474        } catch (Exception ex) {
475            unexpectedException();
299          } finally {
300              joinPool(e);
301          }
# Line 481 | Line 304 | public class AbstractExecutorServiceTest
304      /**
305       * invokeAll(null) throws NPE
306       */
307 <    public void testInvokeAll1() {
307 >    public void testInvokeAll1() throws InterruptedException {
308          ExecutorService e = new DirectExecutorService();
309          try {
310              e.invokeAll(null);
311 +            shouldThrow();
312          } catch (NullPointerException success) {
489        } catch (Exception ex) {
490            unexpectedException();
313          } finally {
314              joinPool(e);
315          }
# Line 496 | Line 318 | public class AbstractExecutorServiceTest
318      /**
319       * invokeAll(empty collection) returns empty collection
320       */
321 <    public void testInvokeAll2() {
321 >    public void testInvokeAll2() throws InterruptedException {
322          ExecutorService e = new DirectExecutorService();
323          try {
324              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
325              assertTrue(r.isEmpty());
504        } catch (Exception ex) {
505            unexpectedException();
326          } finally {
327              joinPool(e);
328          }
# Line 511 | Line 331 | public class AbstractExecutorServiceTest
331      /**
332       * invokeAll(c) throws NPE if c has null elements
333       */
334 <    public void testInvokeAll3() {
334 >    public void testInvokeAll3() throws InterruptedException {
335          ExecutorService e = new DirectExecutorService();
336 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
337 +        l.add(new StringTask());
338 +        l.add(null);
339          try {
517            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
518            l.add(new StringTask());
519            l.add(null);
340              e.invokeAll(l);
341 +            shouldThrow();
342          } catch (NullPointerException success) {
522        } catch (Exception ex) {
523            unexpectedException();
343          } finally {
344              joinPool(e);
345          }
# Line 529 | Line 348 | public class AbstractExecutorServiceTest
348      /**
349       * get of returned element of invokeAll(c) throws exception on failed task
350       */
351 <    public void testInvokeAll4() {
351 >    public void testInvokeAll4() throws Exception {
352          ExecutorService e = new DirectExecutorService();
353          try {
354 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
354 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
355              l.add(new NPETask());
356 <            List<Future<String>> result = e.invokeAll(l);
357 <            assertEquals(1, result.size());
358 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
359 <                it.next().get();
360 <        } catch (ExecutionException success) {
361 <        } catch (Exception ex) {
362 <            unexpectedException();
356 >            List<Future<String>> futures = e.invokeAll(l);
357 >            assertEquals(1, futures.size());
358 >            try {
359 >                futures.get(0).get();
360 >                shouldThrow();
361 >            } catch (ExecutionException success) {
362 >                assertTrue(success.getCause() instanceof NullPointerException);
363 >            }
364          } finally {
365              joinPool(e);
366          }
# Line 549 | Line 369 | public class AbstractExecutorServiceTest
369      /**
370       * invokeAll(c) returns results of all completed tasks in c
371       */
372 <    public void testInvokeAll5() {
372 >    public void testInvokeAll5() throws Exception {
373          ExecutorService e = new DirectExecutorService();
374          try {
375 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
375 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
376              l.add(new StringTask());
377              l.add(new StringTask());
378 <            List<Future<String>> result = e.invokeAll(l);
379 <            assertEquals(2, result.size());
380 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
381 <                assertSame(TEST_STRING, it.next().get());
562 <        } catch (ExecutionException success) {
563 <        } catch (Exception ex) {
564 <            unexpectedException();
378 >            List<Future<String>> futures = e.invokeAll(l);
379 >            assertEquals(2, futures.size());
380 >            for (Future<String> future : futures)
381 >                assertSame(TEST_STRING, future.get());
382          } finally {
383              joinPool(e);
384          }
# Line 571 | Line 388 | public class AbstractExecutorServiceTest
388      /**
389       * timed invokeAny(null) throws NPE
390       */
391 <    public void testTimedInvokeAny1() {
391 >    public void testTimedInvokeAny1() throws Exception {
392          ExecutorService e = new DirectExecutorService();
393          try {
394 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
394 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
395 >            shouldThrow();
396          } catch (NullPointerException success) {
579        } catch (Exception ex) {
580            unexpectedException();
397          } finally {
398              joinPool(e);
399          }
# Line 586 | Line 402 | public class AbstractExecutorServiceTest
402      /**
403       * timed invokeAny(null time unit) throws NPE
404       */
405 <    public void testTimedInvokeAnyNullTimeUnit() {
405 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
406          ExecutorService e = new DirectExecutorService();
407 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
408 +        l.add(new StringTask());
409          try {
592            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
593            l.add(new StringTask());
410              e.invokeAny(l, MEDIUM_DELAY_MS, null);
411 +            shouldThrow();
412          } catch (NullPointerException success) {
596        } catch (Exception ex) {
597            unexpectedException();
413          } finally {
414              joinPool(e);
415          }
# Line 603 | Line 418 | public class AbstractExecutorServiceTest
418      /**
419       * timed invokeAny(empty collection) throws IAE
420       */
421 <    public void testTimedInvokeAny2() {
421 >    public void testTimedInvokeAny2() throws Exception {
422          ExecutorService e = new DirectExecutorService();
423          try {
424 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
424 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
425 >            shouldThrow();
426          } catch (IllegalArgumentException success) {
611        } catch (Exception ex) {
612            unexpectedException();
427          } finally {
428              joinPool(e);
429          }
# Line 618 | Line 432 | public class AbstractExecutorServiceTest
432      /**
433       * timed invokeAny(c) throws NPE if c has null elements
434       */
435 <    public void testTimedInvokeAny3() {
435 >    public void testTimedInvokeAny3() throws Exception {
436          ExecutorService e = new DirectExecutorService();
437 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
438 +        l.add(new Callable<Integer>() {
439 +                  public Integer call() { return 5/0; }});
440 +        l.add(null);
441          try {
442 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
443 <            l.add(new StringTask());
626 <            l.add(null);
627 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
442 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
443 >            shouldThrow();
444          } catch (NullPointerException success) {
629        } catch (Exception ex) {
630            ex.printStackTrace();
631            unexpectedException();
445          } finally {
446              joinPool(e);
447          }
# Line 637 | Line 450 | public class AbstractExecutorServiceTest
450      /**
451       * timed invokeAny(c) throws ExecutionException if no task completes
452       */
453 <    public void testTimedInvokeAny4() {
453 >    public void testTimedInvokeAny4() throws Exception {
454          ExecutorService e = new DirectExecutorService();
455 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
456 +        l.add(new NPETask());
457          try {
458 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
459 <            l.add(new NPETask());
645 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
458 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
459 >            shouldThrow();
460          } catch (ExecutionException success) {
461 <        } catch (Exception ex) {
648 <            unexpectedException();
461 >            assertTrue(success.getCause() instanceof NullPointerException);
462          } finally {
463              joinPool(e);
464          }
# Line 654 | Line 467 | public class AbstractExecutorServiceTest
467      /**
468       * timed invokeAny(c) returns result of some task in c
469       */
470 <    public void testTimedInvokeAny5() {
470 >    public void testTimedInvokeAny5() throws Exception {
471          ExecutorService e = new DirectExecutorService();
472          try {
473 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
473 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
474              l.add(new StringTask());
475              l.add(new StringTask());
476 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
476 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
477              assertSame(TEST_STRING, result);
665        } catch (ExecutionException success) {
666        } catch (Exception ex) {
667            unexpectedException();
478          } finally {
479              joinPool(e);
480          }
# Line 673 | Line 483 | public class AbstractExecutorServiceTest
483      /**
484       * timed invokeAll(null) throws NPE
485       */
486 <    public void testTimedInvokeAll1() {
486 >    public void testTimedInvokeAll1() throws InterruptedException {
487          ExecutorService e = new DirectExecutorService();
488          try {
489 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
489 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
490 >            shouldThrow();
491          } catch (NullPointerException success) {
681        } catch (Exception ex) {
682            unexpectedException();
492          } finally {
493              joinPool(e);
494          }
# Line 688 | Line 497 | public class AbstractExecutorServiceTest
497      /**
498       * timed invokeAll(null time unit) throws NPE
499       */
500 <    public void testTimedInvokeAllNullTimeUnit() {
500 >    public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
501          ExecutorService e = new DirectExecutorService();
502 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
503 +        l.add(new StringTask());
504          try {
694            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
695            l.add(new StringTask());
505              e.invokeAll(l, MEDIUM_DELAY_MS, null);
506 +            shouldThrow();
507          } catch (NullPointerException success) {
698        } catch (Exception ex) {
699            unexpectedException();
508          } finally {
509              joinPool(e);
510          }
# Line 705 | Line 513 | public class AbstractExecutorServiceTest
513      /**
514       * timed invokeAll(empty collection) returns empty collection
515       */
516 <    public void testTimedInvokeAll2() {
516 >    public void testTimedInvokeAll2() throws InterruptedException {
517          ExecutorService e = new DirectExecutorService();
518          try {
519 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
519 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
520              assertTrue(r.isEmpty());
713        } catch (Exception ex) {
714            unexpectedException();
521          } finally {
522              joinPool(e);
523          }
# Line 720 | Line 526 | public class AbstractExecutorServiceTest
526      /**
527       * timed invokeAll(c) throws NPE if c has null elements
528       */
529 <    public void testTimedInvokeAll3() {
529 >    public void testTimedInvokeAll3() throws InterruptedException {
530          ExecutorService e = new DirectExecutorService();
531 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
532 +        l.add(new StringTask());
533 +        l.add(null);
534          try {
535 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
536 <            l.add(new StringTask());
728 <            l.add(null);
729 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
535 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
536 >            shouldThrow();
537          } catch (NullPointerException success) {
731        } catch (Exception ex) {
732            unexpectedException();
538          } finally {
539              joinPool(e);
540          }
# Line 738 | Line 543 | public class AbstractExecutorServiceTest
543      /**
544       * get of returned element of invokeAll(c) throws exception on failed task
545       */
546 <    public void testTimedInvokeAll4() {
546 >    public void testTimedInvokeAll4() throws Exception {
547          ExecutorService e = new DirectExecutorService();
548          try {
549 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
549 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
550              l.add(new NPETask());
551 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
552 <            assertEquals(1, result.size());
553 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
554 <                it.next().get();
555 <        } catch (ExecutionException success) {
556 <        } catch (Exception ex) {
557 <            unexpectedException();
551 >            List<Future<String>> futures =
552 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
553 >            assertEquals(1, futures.size());
554 >            try {
555 >                futures.get(0).get();
556 >                shouldThrow();
557 >            } catch (ExecutionException success) {
558 >                assertTrue(success.getCause() instanceof NullPointerException);
559 >            }
560          } finally {
561              joinPool(e);
562          }
# Line 758 | Line 565 | public class AbstractExecutorServiceTest
565      /**
566       * timed invokeAll(c) returns results of all completed tasks in c
567       */
568 <    public void testTimedInvokeAll5() {
568 >    public void testTimedInvokeAll5() throws Exception {
569          ExecutorService e = new DirectExecutorService();
570          try {
571 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
571 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
572              l.add(new StringTask());
573              l.add(new StringTask());
574 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
575 <            assertEquals(2, result.size());
576 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
577 <                assertSame(TEST_STRING, it.next().get());
578 <        } catch (ExecutionException success) {
772 <        } catch (Exception ex) {
773 <            unexpectedException();
574 >            List<Future<String>> futures =
575 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
576 >            assertEquals(2, futures.size());
577 >            for (Future<String> future : futures)
578 >                assertSame(TEST_STRING, future.get());
579          } finally {
580              joinPool(e);
581          }
# Line 779 | Line 584 | public class AbstractExecutorServiceTest
584      /**
585       * timed invokeAll cancels tasks not completed by timeout
586       */
587 <    public void testTimedInvokeAll6() {
587 >    public void testTimedInvokeAll6() throws InterruptedException {
588          ExecutorService e = new DirectExecutorService();
589          try {
590 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
590 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
591              l.add(new StringTask());
592              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
593              l.add(new StringTask());
594 <            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
595 <            assertEquals(3, result.size());
596 <            Iterator<Future<String>> it = result.iterator();
594 >            List<Future<String>> futures =
595 >                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
596 >            assertEquals(3, futures.size());
597 >            Iterator<Future<String>> it = futures.iterator();
598              Future<String> f1 = it.next();
599              Future<String> f2 = it.next();
600              Future<String> f3 = it.next();
# Line 797 | Line 603 | public class AbstractExecutorServiceTest
603              assertTrue(f2.isDone());
604              assertTrue(f3.isDone());
605              assertTrue(f3.isCancelled());
800        } catch (Exception ex) {
801            unexpectedException();
606          } finally {
607              joinPool(e);
608          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines