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.8 by dl, Tue Dec 23 19:40:24 2003 UTC vs.
Revision 1.32 by jsr166, Sun May 29 07:01:17 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
8
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 < import java.math.BigInteger;
12 > import java.util.concurrent.atomic.AtomicBoolean;
13 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.security.*;
15  
16 < public class AbstractExecutorServiceTest extends JSR166TestCase{
16 > public class AbstractExecutorServiceTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21          return new TestSuite(AbstractExecutorServiceTest.class);
22      }
23  
24 <    /**
24 >    /**
25       * A no-frills implementation of AbstractExecutorService, designed
26       * to test the submit methods only.
27       */
28      static class DirectExecutorService extends AbstractExecutorService {
29          public void execute(Runnable r) { r.run(); }
30          public void shutdown() { shutdown = true; }
31 <        public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
31 >        public List<Runnable> shutdownNow() {
32 >            shutdown = true;
33 >            return Collections.EMPTY_LIST;
34 >        }
35          public boolean isShutdown() { return shutdown; }
36          public boolean isTerminated() { return isShutdown(); }
37 <        public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
37 >        public boolean awaitTermination(long timeout, TimeUnit unit) {
38 >            return isShutdown();
39 >        }
40          private volatile boolean shutdown = false;
41      }
42  
43      /**
44 <     * execute of runnable runs it to completion
44 >     * execute(runnable) runs it to completion
45       */
46 <    public void testExecuteRunnable() {
47 <        try {
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 <        catch (ExecutionException ex) {
56 <            unexpectedException();
57 <        }
58 <        catch (InterruptedException ex) {
53 <            unexpectedException();
54 <        }
46 >    public void testExecuteRunnable() throws Exception {
47 >        ExecutorService e = new DirectExecutorService();
48 >        final AtomicBoolean done = new AtomicBoolean(false);
49 >        CheckedRunnable task = new CheckedRunnable() {
50 >            public void realRun() {
51 >                done.set(true);
52 >            }};
53 >        Future<?> future = e.submit(task);
54 >        assertNull(future.get());
55 >        assertNull(future.get(0, MILLISECONDS));
56 >        assertTrue(done.get());
57 >        assertTrue(future.isDone());
58 >        assertFalse(future.isCancelled());
59      }
60  
57
61      /**
62 <     * completed submit of callable returns result
62 >     * Completed submit(callable) returns result
63       */
64 <    public void testSubmitCallable() {
65 <        try {
66 <            ExecutorService e = new DirectExecutorService();
67 <            Future<String> future = e.submit(new StringTask());
68 <            String result = future.get();
66 <            assertSame(TEST_STRING, result);
67 <        }
68 <        catch (ExecutionException ex) {
69 <            unexpectedException();
70 <        }
71 <        catch (InterruptedException ex) {
72 <            unexpectedException();
73 <        }
64 >    public void testSubmitCallable() throws Exception {
65 >        ExecutorService e = new DirectExecutorService();
66 >        Future<String> future = e.submit(new StringTask());
67 >        String result = future.get();
68 >        assertSame(TEST_STRING, result);
69      }
70  
71      /**
72 <     * completed submit of runnable returns successfully
72 >     * Completed submit(runnable) returns successfully
73       */
74 <    public void testSubmitRunnable() {
75 <        try {
76 <            ExecutorService e = new DirectExecutorService();
77 <            Future<?> future = e.submit(new NoOpRunnable());
78 <            future.get();
84 <            assertTrue(future.isDone());
85 <        }
86 <        catch (ExecutionException ex) {
87 <            unexpectedException();
88 <        }
89 <        catch (InterruptedException ex) {
90 <            unexpectedException();
91 <        }
74 >    public void testSubmitRunnable() throws Exception {
75 >        ExecutorService e = new DirectExecutorService();
76 >        Future<?> future = e.submit(new NoOpRunnable());
77 >        future.get();
78 >        assertTrue(future.isDone());
79      }
80  
81      /**
82 <     * completed submit of (runnable, result) returns result
82 >     * Completed submit(runnable, result) returns result
83       */
84 <    public void testSubmitRunnable2() {
85 <        try {
86 <            ExecutorService e = new DirectExecutorService();
87 <            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
88 <            String result = future.get();
102 <            assertSame(TEST_STRING, result);
103 <        }
104 <        catch (ExecutionException ex) {
105 <            unexpectedException();
106 <        }
107 <        catch (InterruptedException ex) {
108 <            unexpectedException();
109 <        }
84 >    public void testSubmitRunnable2() throws Exception {
85 >        ExecutorService e = new DirectExecutorService();
86 >        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
87 >        String result = future.get();
88 >        assertSame(TEST_STRING, result);
89      }
90  
112
91      /**
92 <     * submit of a privileged action runs it to completion
92 >     * A submitted privileged action runs to completion
93       */
94 <    public void testSubmitPrivilegedAction() {
95 <        Policy savedPolicy = Policy.getPolicy();
96 <        AdjustablePolicy policy = new AdjustablePolicy();
97 <        policy.addPermission(new RuntimePermission("getContextClassLoader"));
98 <        policy.addPermission(new RuntimePermission("setContextClassLoader"));
121 <        Policy.setPolicy(policy);
122 <        try {
123 <            ExecutorService e = new DirectExecutorService();
124 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
94 >    public void testSubmitPrivilegedAction() throws Exception {
95 >        Runnable r = new CheckedRunnable() {
96 >            public void realRun() throws Exception {
97 >                ExecutorService e = new DirectExecutorService();
98 >                Future future = e.submit(Executors.callable(new PrivilegedAction() {
99                      public Object run() {
100                          return TEST_STRING;
101                      }}));
102  
103 <            Object result = future.get();
104 <            assertSame(TEST_STRING, result);
105 <        }
106 <        catch (ExecutionException ex) {
107 <            unexpectedException();
108 <        }
109 <        catch (InterruptedException ex) {
136 <            unexpectedException();
137 <        }
138 <        finally {
139 <            Policy.setPolicy(savedPolicy);
140 <        }
103 >                assertSame(TEST_STRING, future.get());
104 >            }};
105 >
106 >        runWithPermissions(r,
107 >                           new RuntimePermission("getClassLoader"),
108 >                           new RuntimePermission("setContextClassLoader"),
109 >                           new RuntimePermission("modifyThread"));
110      }
111  
112      /**
113 <     * submit of a privileged exception action runs it to completion
113 >     * A submitted privileged exception action runs to completion
114       */
115 <    public void testSubmitPrivilegedExceptionAction() {
116 <        Policy savedPolicy = Policy.getPolicy();
117 <        AdjustablePolicy policy = new AdjustablePolicy();
118 <        policy.addPermission(new RuntimePermission("getContextClassLoader"));
119 <        policy.addPermission(new RuntimePermission("setContextClassLoader"));
151 <        Policy.setPolicy(policy);
152 <        try {
153 <            ExecutorService e = new DirectExecutorService();
154 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
115 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
116 >        Runnable r = new CheckedRunnable() {
117 >            public void realRun() throws Exception {
118 >                ExecutorService e = new DirectExecutorService();
119 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
120                      public Object run() {
121                          return TEST_STRING;
122                      }}));
123  
124 <            Object result = future.get();
125 <            assertSame(TEST_STRING, result);
126 <        }
127 <        catch (ExecutionException ex) {
163 <            unexpectedException();
164 <        }
165 <        catch (InterruptedException ex) {
166 <            unexpectedException();
167 <        }
168 <        finally {
169 <            Policy.setPolicy(savedPolicy);
170 <        }
124 >                assertSame(TEST_STRING, future.get());
125 >            }};
126 >
127 >        runWithPermissions(r);
128      }
129  
130      /**
131 <     * submit of a failed privileged exception action reports exception
131 >     * A submitted failed privileged exception action reports exception
132       */
133 <    public void testSubmitFailedPrivilegedExceptionAction() {
134 <        Policy savedPolicy = Policy.getPolicy();
135 <        AdjustablePolicy policy = new AdjustablePolicy();
136 <        policy.addPermission(new RuntimePermission("getContextClassLoader"));
137 <        policy.addPermission(new RuntimePermission("setContextClassLoader"));
181 <        Policy.setPolicy(policy);
182 <        try {
183 <            ExecutorService e = new DirectExecutorService();
184 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
133 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
134 >        Runnable r = new CheckedRunnable() {
135 >            public void realRun() throws Exception {
136 >                ExecutorService e = new DirectExecutorService();
137 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
138                      public Object run() throws Exception {
139                          throw new IndexOutOfBoundsException();
140                      }}));
141  
142 <            Object result = future.get();
143 <            shouldThrow();
144 <        }
145 <        catch (ExecutionException success) {
146 <        }
147 <        catch (InterruptedException ex) {
148 <            unexpectedException();
149 <        }
197 <        finally {
198 <            Policy.setPolicy(savedPolicy);
199 <        }
142 >                try {
143 >                    future.get();
144 >                    shouldThrow();
145 >                } catch (ExecutionException success) {
146 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
147 >                }}};
148 >
149 >        runWithPermissions(r);
150      }
151  
152      /**
153 <     * execute with a null runnable throws NPE
153 >     * execute(null runnable) throws NPE
154       */
155      public void testExecuteNullRunnable() {
156          try {
157              ExecutorService e = new DirectExecutorService();
158 <            TrackedShortRunnable task = null;
209 <            Future<?> future = e.submit(task);
158 >            e.submit((Runnable) null);
159              shouldThrow();
160 <        }
212 <        catch (NullPointerException success) {
213 <        }
214 <        catch (Exception ex) {
215 <            unexpectedException();
216 <        }
160 >        } catch (NullPointerException success) {}
161      }
162  
219
163      /**
164 <     * submit of a null callable throws NPE
164 >     * submit(null callable) throws NPE
165       */
166      public void testSubmitNullCallable() {
167          try {
168              ExecutorService e = new DirectExecutorService();
169 <            StringTask t = null;
227 <            Future<String> future = e.submit(t);
228 <            shouldThrow();
229 <        }
230 <        catch (NullPointerException success) {
231 <        }
232 <        catch (Exception ex) {
233 <            unexpectedException();
234 <        }
235 <    }
236 <
237 <    /**
238 <     * submit of Runnable throws RejectedExecutionException if
239 <     * saturated.
240 <     */
241 <    public void testExecute1() {
242 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
243 <        try {
244 <
245 <            for(int i = 0; i < 5; ++i){
246 <                p.submit(new MediumRunnable());
247 <            }
248 <            shouldThrow();
249 <        } catch(RejectedExecutionException success){}
250 <        joinPool(p);
251 <    }
252 <
253 <    /**
254 <     * Completed submit of Callable throws RejectedExecutionException
255 <     *  if saturated.
256 <     */
257 <    public void testExecute2() {
258 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
259 <        try {
260 <            for(int i = 0; i < 5; ++i) {
261 <                p.submit(new SmallCallable());
262 <            }
169 >            e.submit((Callable) null);
170              shouldThrow();
171 <        } catch(RejectedExecutionException e){}
265 <        joinPool(p);
171 >        } catch (NullPointerException success) {}
172      }
173  
268
174      /**
175 <     *  blocking on submit of Callable throws InterruptedException if
271 <     *  caller interrupted.
175 >     * submit(callable).get() throws InterruptedException if interrupted
176       */
177 <    public void testInterruptedSubmit() {
178 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
179 <        Thread t = new Thread(new Runnable() {
180 <                public void run() {
181 <                    try {
182 <                        p.submit(new Callable<Object>() {
183 <                                public Object call() {
184 <                                    try {
185 <                                        Thread.sleep(MEDIUM_DELAY_MS);
186 <                                        shouldThrow();
187 <                                    } catch(InterruptedException e){
188 <                                    }
189 <                                    return null;
190 <                                }
191 <                            }).get();
192 <                    } catch(InterruptedException success){
193 <                    } catch(Exception e) {
194 <                        unexpectedException();
291 <                    }
292 <
293 <                }
294 <            });
295 <        try {
177 >    public void testInterruptedSubmit() throws InterruptedException {
178 >        final CountDownLatch submitted    = new CountDownLatch(1);
179 >        final CountDownLatch quittingTime = new CountDownLatch(1);
180 >        final ExecutorService p
181 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
182 >                                     new ArrayBlockingQueue<Runnable>(10));
183 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
184 >            public Void realCall() throws InterruptedException {
185 >                quittingTime.await();
186 >                return null;
187 >            }};
188 >        try {
189 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
190 >                public void realRun() throws Exception {
191 >                    Future<Void> future = p.submit(awaiter);
192 >                    submitted.countDown();
193 >                    future.get();
194 >                }});
195              t.start();
196 <            Thread.sleep(SHORT_DELAY_MS);
298 <            t.interrupt();
299 <        } catch(Exception e){
300 <            unexpectedException();
301 <        }
302 <        joinPool(p);
303 <    }
304 <
305 <    /**
306 <     *  get of submit of Callable throws Exception if callable
307 <     *  interrupted
308 <     */
309 <    public void testSubmitIE() {
310 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
311 <
312 <        final Callable c = new Callable() {
313 <                public Object call() {
314 <                    try {
315 <                        p.submit(new SmallCallable()).get();
316 <                        shouldThrow();
317 <                    } catch(InterruptedException e){}
318 <                    catch(RejectedExecutionException e2){}
319 <                    catch(ExecutionException e3){}
320 <                    return Boolean.TRUE;
321 <                }
322 <            };
323 <
324 <
325 <
326 <        Thread t = new Thread(new Runnable() {
327 <                public void run() {
328 <                    try {
329 <                        c.call();
330 <                    } catch(Exception e){}
331 <                }
332 <          });
333 <        try {
334 <            t.start();
335 <            Thread.sleep(SHORT_DELAY_MS);
196 >            submitted.await();
197              t.interrupt();
198              t.join();
199 <        } catch(InterruptedException e){
200 <            unexpectedException();
199 >        } finally {
200 >            quittingTime.countDown();
201 >            joinPool(p);
202          }
341
342        joinPool(p);
203      }
204  
205      /**
206 <     *  completed submit of Callable throws ExecutionException if
207 <     *  callable throws exception
206 >     * get of submit(callable) throws ExecutionException if callable
207 >     * throws exception
208       */
209 <    public void testSubmitEE() {
210 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
211 <
212 <        try {
213 <            Callable c = new Callable() {
354 <                    public Object call() {
355 <                        int i = 5/0;
356 <                        return Boolean.TRUE;
357 <                    }
358 <                };
209 >    public void testSubmitEE() throws InterruptedException {
210 >        ThreadPoolExecutor p =
211 >            new ThreadPoolExecutor(1, 1,
212 >                                   60, TimeUnit.SECONDS,
213 >                                   new ArrayBlockingQueue<Runnable>(10));
214  
215 <            for(int i =0; i < 5; i++){
216 <                p.submit(c).get();
362 <            }
215 >        Callable c = new Callable() {
216 >            public Object call() { return 5/0; }};
217  
218 +        try {
219 +            p.submit(c).get();
220              shouldThrow();
221 <        }
222 <        catch(ExecutionException success){
367 <        } catch(Exception e) {
368 <            unexpectedException();
221 >        } catch (ExecutionException success) {
222 >            assertTrue(success.getCause() instanceof ArithmeticException);
223          }
224          joinPool(p);
225      }
# Line 373 | Line 227 | public class AbstractExecutorServiceTest
227      /**
228       * invokeAny(null) throws NPE
229       */
230 <    public void testInvokeAny1() {
230 >    public void testInvokeAny1() throws Exception {
231          ExecutorService e = new DirectExecutorService();
232          try {
233              e.invokeAny(null);
234 +            shouldThrow();
235          } catch (NullPointerException success) {
381        } catch(Exception ex) {
382            unexpectedException();
236          } finally {
237              joinPool(e);
238          }
# Line 388 | Line 241 | public class AbstractExecutorServiceTest
241      /**
242       * invokeAny(empty collection) throws IAE
243       */
244 <    public void testInvokeAny2() {
244 >    public void testInvokeAny2() throws Exception {
245          ExecutorService e = new DirectExecutorService();
246          try {
247              e.invokeAny(new ArrayList<Callable<String>>());
248 +            shouldThrow();
249          } catch (IllegalArgumentException success) {
396        } catch(Exception ex) {
397            unexpectedException();
250          } finally {
251              joinPool(e);
252          }
# Line 403 | Line 255 | public class AbstractExecutorServiceTest
255      /**
256       * invokeAny(c) throws NPE if c has null elements
257       */
258 <    public void testInvokeAny3() {
258 >    public void testInvokeAny3() throws Exception {
259          ExecutorService e = new DirectExecutorService();
260 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
261 +        l.add(new Callable<Integer>() {
262 +                  public Integer call() { return 5/0; }});
263 +        l.add(null);
264          try {
409            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
410            l.add(new StringTask());
411            l.add(null);
265              e.invokeAny(l);
266 +            shouldThrow();
267          } catch (NullPointerException success) {
414        } catch(Exception ex) {
415            ex.printStackTrace();
416            unexpectedException();
268          } finally {
269              joinPool(e);
270          }
271      }
272  
273      /**
274 <     * invokeAny(c) throws ExecutionException if no task completes
274 >     * invokeAny(c) throws ExecutionException if no task in c completes
275       */
276 <    public void testInvokeAny4() {
276 >    public void testInvokeAny4() throws InterruptedException {
277          ExecutorService e = new DirectExecutorService();
278 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
279 +        l.add(new NPETask());
280          try {
428            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
429            l.add(new NPETask());
281              e.invokeAny(l);
282 <        } catch(ExecutionException success) {
283 <        } catch(Exception ex) {
284 <            unexpectedException();
282 >            shouldThrow();
283 >        } catch (ExecutionException success) {
284 >            assertTrue(success.getCause() instanceof NullPointerException);
285          } finally {
286              joinPool(e);
287          }
288      }
289  
290      /**
291 <     * invokeAny(c) returns result of some task
291 >     * invokeAny(c) returns result of some task in c if at least one completes
292       */
293 <    public void testInvokeAny5() {
293 >    public void testInvokeAny5() throws Exception {
294          ExecutorService e = new DirectExecutorService();
295          try {
296 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
296 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
297              l.add(new StringTask());
298              l.add(new StringTask());
299              String result = e.invokeAny(l);
300              assertSame(TEST_STRING, result);
450        } catch (ExecutionException success) {
451        } catch(Exception ex) {
452            unexpectedException();
301          } finally {
302              joinPool(e);
303          }
# Line 458 | Line 306 | public class AbstractExecutorServiceTest
306      /**
307       * invokeAll(null) throws NPE
308       */
309 <    public void testInvokeAll1() {
309 >    public void testInvokeAll1() throws InterruptedException {
310          ExecutorService e = new DirectExecutorService();
311          try {
312              e.invokeAll(null);
313 +            shouldThrow();
314          } catch (NullPointerException success) {
466        } catch(Exception ex) {
467            unexpectedException();
315          } finally {
316              joinPool(e);
317          }
# Line 473 | Line 320 | public class AbstractExecutorServiceTest
320      /**
321       * invokeAll(empty collection) returns empty collection
322       */
323 <    public void testInvokeAll2() {
323 >    public void testInvokeAll2() throws InterruptedException {
324          ExecutorService e = new DirectExecutorService();
325          try {
326              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
327              assertTrue(r.isEmpty());
481        } catch(Exception ex) {
482            unexpectedException();
328          } finally {
329              joinPool(e);
330          }
# Line 488 | Line 333 | public class AbstractExecutorServiceTest
333      /**
334       * invokeAll(c) throws NPE if c has null elements
335       */
336 <    public void testInvokeAll3() {
336 >    public void testInvokeAll3() throws InterruptedException {
337          ExecutorService e = new DirectExecutorService();
338 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
339 +        l.add(new StringTask());
340 +        l.add(null);
341          try {
494            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
495            l.add(new StringTask());
496            l.add(null);
342              e.invokeAll(l);
343 +            shouldThrow();
344          } catch (NullPointerException success) {
499        } catch(Exception ex) {
500            unexpectedException();
345          } finally {
346              joinPool(e);
347          }
348      }
349  
350      /**
351 <     * get of element of invokeAll(c) throws exception on failed task
351 >     * get of returned element of invokeAll(c) throws exception on failed task
352       */
353 <    public void testInvokeAll4() {
353 >    public void testInvokeAll4() throws Exception {
354          ExecutorService e = new DirectExecutorService();
355          try {
356 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
356 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
357              l.add(new NPETask());
358 <            List<Future<String>> result = e.invokeAll(l);
359 <            assertEquals(1, result.size());
360 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
361 <                it.next().get();
362 <        } catch(ExecutionException success) {
363 <        } catch(Exception ex) {
364 <            unexpectedException();
358 >            List<Future<String>> futures = e.invokeAll(l);
359 >            assertEquals(1, futures.size());
360 >            try {
361 >                futures.get(0).get();
362 >                shouldThrow();
363 >            } catch (ExecutionException success) {
364 >                assertTrue(success.getCause() instanceof NullPointerException);
365 >            }
366          } finally {
367              joinPool(e);
368          }
369      }
370  
371      /**
372 <     * invokeAll(c) returns results of all completed tasks
372 >     * invokeAll(c) returns results of all completed tasks in c
373       */
374 <    public void testInvokeAll5() {
374 >    public void testInvokeAll5() throws Exception {
375          ExecutorService e = new DirectExecutorService();
376          try {
377 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
377 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
378              l.add(new StringTask());
379              l.add(new StringTask());
380 <            List<Future<String>> result = e.invokeAll(l);
381 <            assertEquals(2, result.size());
382 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
383 <                assertSame(TEST_STRING, it.next().get());
539 <        } catch (ExecutionException success) {
540 <        } catch(Exception ex) {
541 <            unexpectedException();
380 >            List<Future<String>> futures = e.invokeAll(l);
381 >            assertEquals(2, futures.size());
382 >            for (Future<String> future : futures)
383 >                assertSame(TEST_STRING, future.get());
384          } finally {
385              joinPool(e);
386          }
387      }
388  
547
389      /**
390       * timed invokeAny(null) throws NPE
391       */
392 <    public void testTimedInvokeAny1() {
392 >    public void testTimedInvokeAny1() throws Exception {
393          ExecutorService e = new DirectExecutorService();
394          try {
395 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
395 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
396 >            shouldThrow();
397          } catch (NullPointerException success) {
556        } catch(Exception ex) {
557            unexpectedException();
398          } finally {
399              joinPool(e);
400          }
401      }
402  
403      /**
404 <     * timed invokeAny(,,null) throws NPE
404 >     * timed invokeAny(null time unit) throws NPE
405       */
406 <    public void testTimedInvokeAnyNullTimeUnit() {
406 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
407          ExecutorService e = new DirectExecutorService();
408 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
409 +        l.add(new StringTask());
410          try {
569            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
570            l.add(new StringTask());
411              e.invokeAny(l, MEDIUM_DELAY_MS, null);
412 +            shouldThrow();
413          } catch (NullPointerException success) {
573        } catch(Exception ex) {
574            unexpectedException();
414          } finally {
415              joinPool(e);
416          }
# Line 580 | Line 419 | public class AbstractExecutorServiceTest
419      /**
420       * timed invokeAny(empty collection) throws IAE
421       */
422 <    public void testTimedInvokeAny2() {
422 >    public void testTimedInvokeAny2() throws Exception {
423          ExecutorService e = new DirectExecutorService();
424          try {
425 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
425 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
426 >            shouldThrow();
427          } catch (IllegalArgumentException success) {
588        } catch(Exception ex) {
589            unexpectedException();
428          } finally {
429              joinPool(e);
430          }
# Line 595 | Line 433 | public class AbstractExecutorServiceTest
433      /**
434       * timed invokeAny(c) throws NPE if c has null elements
435       */
436 <    public void testTimedInvokeAny3() {
436 >    public void testTimedInvokeAny3() throws Exception {
437          ExecutorService e = new DirectExecutorService();
438 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
439 +        l.add(new Callable<Integer>() {
440 +                  public Integer call() { return 5/0; }});
441 +        l.add(null);
442          try {
443 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
444 <            l.add(new StringTask());
603 <            l.add(null);
604 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
443 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
444 >            shouldThrow();
445          } catch (NullPointerException success) {
606        } catch(Exception ex) {
607            ex.printStackTrace();
608            unexpectedException();
446          } finally {
447              joinPool(e);
448          }
# Line 614 | Line 451 | public class AbstractExecutorServiceTest
451      /**
452       * timed invokeAny(c) throws ExecutionException if no task completes
453       */
454 <    public void testTimedInvokeAny4() {
454 >    public void testTimedInvokeAny4() throws Exception {
455          ExecutorService e = new DirectExecutorService();
456 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
457 +        l.add(new NPETask());
458          try {
459 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
460 <            l.add(new NPETask());
461 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
462 <        } catch(ExecutionException success) {
624 <        } catch(Exception ex) {
625 <            unexpectedException();
459 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
460 >            shouldThrow();
461 >        } catch (ExecutionException success) {
462 >            assertTrue(success.getCause() instanceof NullPointerException);
463          } finally {
464              joinPool(e);
465          }
466      }
467  
468      /**
469 <     * timed invokeAny(c) returns result of some task
469 >     * timed invokeAny(c) returns result of some task in c
470       */
471 <    public void testTimedInvokeAny5() {
471 >    public void testTimedInvokeAny5() throws Exception {
472          ExecutorService e = new DirectExecutorService();
473          try {
474 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
474 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
475              l.add(new StringTask());
476              l.add(new StringTask());
477 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
477 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
478              assertSame(TEST_STRING, result);
642        } catch (ExecutionException success) {
643        } catch(Exception ex) {
644            unexpectedException();
479          } finally {
480              joinPool(e);
481          }
# Line 650 | Line 484 | public class AbstractExecutorServiceTest
484      /**
485       * timed invokeAll(null) throws NPE
486       */
487 <    public void testTimedInvokeAll1() {
487 >    public void testTimedInvokeAll1() throws InterruptedException {
488          ExecutorService e = new DirectExecutorService();
489          try {
490 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
490 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
491 >            shouldThrow();
492          } catch (NullPointerException success) {
658        } catch(Exception ex) {
659            unexpectedException();
493          } finally {
494              joinPool(e);
495          }
496      }
497  
498      /**
499 <     * timed invokeAll(,,null) throws NPE
499 >     * timed invokeAll(null time unit) throws NPE
500       */
501 <    public void testTimedInvokeAllNullTimeUnit() {
501 >    public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
502          ExecutorService e = new DirectExecutorService();
503 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
504 +        l.add(new StringTask());
505          try {
671            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
672            l.add(new StringTask());
506              e.invokeAll(l, MEDIUM_DELAY_MS, null);
507 +            shouldThrow();
508          } catch (NullPointerException success) {
675        } catch(Exception ex) {
676            unexpectedException();
509          } finally {
510              joinPool(e);
511          }
# Line 682 | Line 514 | public class AbstractExecutorServiceTest
514      /**
515       * timed invokeAll(empty collection) returns empty collection
516       */
517 <    public void testTimedInvokeAll2() {
517 >    public void testTimedInvokeAll2() throws InterruptedException {
518          ExecutorService e = new DirectExecutorService();
519          try {
520 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
520 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
521              assertTrue(r.isEmpty());
690        } catch(Exception ex) {
691            unexpectedException();
522          } finally {
523              joinPool(e);
524          }
# Line 697 | Line 527 | public class AbstractExecutorServiceTest
527      /**
528       * timed invokeAll(c) throws NPE if c has null elements
529       */
530 <    public void testTimedInvokeAll3() {
530 >    public void testTimedInvokeAll3() throws InterruptedException {
531          ExecutorService e = new DirectExecutorService();
532 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
533 +        l.add(new StringTask());
534 +        l.add(null);
535          try {
536 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
537 <            l.add(new StringTask());
705 <            l.add(null);
706 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
536 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
537 >            shouldThrow();
538          } catch (NullPointerException success) {
708        } catch(Exception ex) {
709            unexpectedException();
539          } finally {
540              joinPool(e);
541          }
542      }
543  
544      /**
545 <     * get of element of invokeAll(c) throws exception on failed task
545 >     * get of returned element of invokeAll(c) throws exception on failed task
546       */
547 <    public void testTimedInvokeAll4() {
547 >    public void testTimedInvokeAll4() throws Exception {
548          ExecutorService e = new DirectExecutorService();
549          try {
550 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
550 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
551              l.add(new NPETask());
552 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
553 <            assertEquals(1, result.size());
554 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
555 <                it.next().get();
556 <        } catch(ExecutionException success) {
557 <        } catch(Exception ex) {
558 <            unexpectedException();
552 >            List<Future<String>> futures =
553 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
554 >            assertEquals(1, futures.size());
555 >            try {
556 >                futures.get(0).get();
557 >                shouldThrow();
558 >            } catch (ExecutionException success) {
559 >                assertTrue(success.getCause() instanceof NullPointerException);
560 >            }
561          } finally {
562              joinPool(e);
563          }
564      }
565  
566      /**
567 <     * timed invokeAll(c) returns results of all completed tasks
567 >     * timed invokeAll(c) returns results of all completed tasks in c
568       */
569 <    public void testTimedInvokeAll5() {
569 >    public void testTimedInvokeAll5() throws Exception {
570          ExecutorService e = new DirectExecutorService();
571          try {
572 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
572 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
573              l.add(new StringTask());
574              l.add(new StringTask());
575 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
576 <            assertEquals(2, result.size());
577 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
578 <                assertSame(TEST_STRING, it.next().get());
579 <        } catch (ExecutionException success) {
749 <        } catch(Exception ex) {
750 <            unexpectedException();
575 >            List<Future<String>> futures =
576 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
577 >            assertEquals(2, futures.size());
578 >            for (Future<String> future : futures)
579 >                assertSame(TEST_STRING, future.get());
580          } finally {
581              joinPool(e);
582          }
583      }
584  
585      /**
586 <     * timed invokeAll(c) cancels tasks not completed by timeout
586 >     * timed invokeAll cancels tasks not completed by timeout
587       */
588 <    public void testTimedInvokeAll6() {
589 <        ExecutorService e = Executors.newCachedThreadPool();
588 >    public void testTimedInvokeAll6() throws InterruptedException {
589 >        ExecutorService e = new DirectExecutorService();
590          try {
591 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
591 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
592 >            l.add(new StringTask());
593 >            l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING));
594              l.add(new StringTask());
595 <            l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING));
596 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
597 <            assertEquals(2, result.size());
598 <            Iterator<Future<String>> it = result.iterator();
599 <            Future<String> f1 = it.next();
600 <            Future<String> f2 = it.next();
601 <            assertTrue(f1.isDone());
602 <            assertFalse(f1.isCancelled());
772 <            assertTrue(f2.isDone());
773 <            assertTrue(f2.isCancelled());
774 <        } catch(Exception ex) {
775 <            unexpectedException();
595 >            List<Future<String>> futures =
596 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
597 >            assertEquals(l.size(), futures.size());
598 >            for (Future future : futures)
599 >                assertTrue(future.isDone());
600 >            assertFalse(futures.get(0).isCancelled());
601 >            assertFalse(futures.get(1).isCancelled());
602 >            assertTrue(futures.get(2).isCancelled());
603          } finally {
604              joinPool(e);
605          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines