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.10 by dl, Sat Dec 27 19:26:42 2003 UTC vs.
Revision 1.20 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 2 | Line 2
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/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9  
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());
20      }
# Line 21 | Line 22 | public class AbstractExecutorServiceTest
22          return new TestSuite(AbstractExecutorServiceTest.class);
23      }
24  
25 <    /**
25 >    /**
26       * A no-frills implementation of AbstractExecutorService, designed
27       * to test the submit methods only.
28       */
# Line 36 | Line 37 | public class AbstractExecutorServiceTest
37      }
38  
39      /**
40 <     * execute of runnable runs it to completion
40 >     * execute(runnable) runs it to completion
41       */
42 <    public void testExecuteRunnable() {
43 <        try {
44 <            ExecutorService e = new DirectExecutorService();
45 <            TrackedShortRunnable task = new TrackedShortRunnable();
46 <            assertFalse(task.done);
47 <            Future<?> future = e.submit(task);
48 <            future.get();
48 <            assertTrue(task.done);
49 <        }
50 <        catch (ExecutionException ex) {
51 <            unexpectedException();
52 <        }
53 <        catch (InterruptedException ex) {
54 <            unexpectedException();
55 <        }
42 >    public void testExecuteRunnable() throws Exception {
43 >        ExecutorService e = new DirectExecutorService();
44 >        TrackedShortRunnable task = new TrackedShortRunnable();
45 >        assertFalse(task.done);
46 >        Future<?> future = e.submit(task);
47 >        future.get();
48 >        assertTrue(task.done);
49      }
50  
51  
52      /**
53 <     * completed submit of callable returns result
53 >     * Completed submit(callable) returns result
54       */
55 <    public void testSubmitCallable() {
56 <        try {
57 <            ExecutorService e = new DirectExecutorService();
58 <            Future<String> future = e.submit(new StringTask());
59 <            String result = future.get();
67 <            assertSame(TEST_STRING, result);
68 <        }
69 <        catch (ExecutionException ex) {
70 <            unexpectedException();
71 <        }
72 <        catch (InterruptedException ex) {
73 <            unexpectedException();
74 <        }
55 >    public void testSubmitCallable() throws Exception {
56 >        ExecutorService e = new DirectExecutorService();
57 >        Future<String> future = e.submit(new StringTask());
58 >        String result = future.get();
59 >        assertSame(TEST_STRING, result);
60      }
61  
62      /**
63 <     * completed submit of runnable returns successfully
63 >     * Completed submit(runnable) returns successfully
64       */
65 <    public void testSubmitRunnable() {
66 <        try {
67 <            ExecutorService e = new DirectExecutorService();
68 <            Future<?> future = e.submit(new NoOpRunnable());
69 <            future.get();
85 <            assertTrue(future.isDone());
86 <        }
87 <        catch (ExecutionException ex) {
88 <            unexpectedException();
89 <        }
90 <        catch (InterruptedException ex) {
91 <            unexpectedException();
92 <        }
65 >    public void testSubmitRunnable() throws Exception {
66 >        ExecutorService e = new DirectExecutorService();
67 >        Future<?> future = e.submit(new NoOpRunnable());
68 >        future.get();
69 >        assertTrue(future.isDone());
70      }
71  
72      /**
73 <     * completed submit of (runnable, result) returns result
73 >     * Completed submit(runnable, result) returns result
74       */
75 <    public void testSubmitRunnable2() {
76 <        try {
77 <            ExecutorService e = new DirectExecutorService();
78 <            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
79 <            String result = future.get();
103 <            assertSame(TEST_STRING, result);
104 <        }
105 <        catch (ExecutionException ex) {
106 <            unexpectedException();
107 <        }
108 <        catch (InterruptedException ex) {
109 <            unexpectedException();
110 <        }
75 >    public void testSubmitRunnable2() throws Exception {
76 >        ExecutorService e = new DirectExecutorService();
77 >        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
78 >        String result = future.get();
79 >        assertSame(TEST_STRING, result);
80      }
81  
82  
83      /**
84 <     * submit of a privileged action runs it to completion
84 >     * A submitted privileged action to completion
85       */
86 <    public void testSubmitPrivilegedAction() {
87 <        Policy savedPolicy = Policy.getPolicy();
88 <        AdjustablePolicy policy = new AdjustablePolicy();
89 <        policy.addPermission(new RuntimePermission("getContextClassLoader"));
90 <        policy.addPermission(new RuntimePermission("setContextClassLoader"));
91 <        Policy.setPolicy(policy);
86 >    public void testSubmitPrivilegedAction() throws Exception {
87 >        Policy savedPolicy = null;
88 >        try {
89 >            savedPolicy = Policy.getPolicy();
90 >            AdjustablePolicy policy = new AdjustablePolicy();
91 >            policy.addPermission(new RuntimePermission("getContextClassLoader"));
92 >            policy.addPermission(new RuntimePermission("setContextClassLoader"));
93 >            Policy.setPolicy(policy);
94 >        } catch (AccessControlException ok) {
95 >            return;
96 >        }
97          try {
98              ExecutorService e = new DirectExecutorService();
99              Future future = e.submit(Executors.callable(new PrivilegedAction() {
# Line 130 | Line 104 | public class AbstractExecutorServiceTest
104              Object result = future.get();
105              assertSame(TEST_STRING, result);
106          }
133        catch (ExecutionException ex) {
134            unexpectedException();
135        }
136        catch (InterruptedException ex) {
137            unexpectedException();
138        }
107          finally {
108 <            Policy.setPolicy(savedPolicy);
108 >            try {
109 >                Policy.setPolicy(savedPolicy);
110 >            } catch (AccessControlException ok) {
111 >                return;
112 >            }
113          }
114      }
115  
116      /**
117 <     * submit of a privileged exception action runs it to completion
117 >     * A submitted a privileged exception action runs to completion
118       */
119 <    public void testSubmitPrivilegedExceptionAction() {
120 <        Policy savedPolicy = Policy.getPolicy();
121 <        AdjustablePolicy policy = new AdjustablePolicy();
122 <        policy.addPermission(new RuntimePermission("getContextClassLoader"));
123 <        policy.addPermission(new RuntimePermission("setContextClassLoader"));
124 <        Policy.setPolicy(policy);
119 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
120 >        Policy savedPolicy = null;
121 >        try {
122 >            savedPolicy = Policy.getPolicy();
123 >            AdjustablePolicy policy = new AdjustablePolicy();
124 >            policy.addPermission(new RuntimePermission("getContextClassLoader"));
125 >            policy.addPermission(new RuntimePermission("setContextClassLoader"));
126 >            Policy.setPolicy(policy);
127 >        } catch (AccessControlException ok) {
128 >            return;
129 >        }
130 >
131          try {
132              ExecutorService e = new DirectExecutorService();
133              Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
# Line 160 | Line 138 | public class AbstractExecutorServiceTest
138              Object result = future.get();
139              assertSame(TEST_STRING, result);
140          }
163        catch (ExecutionException ex) {
164            unexpectedException();
165        }
166        catch (InterruptedException ex) {
167            unexpectedException();
168        }
141          finally {
142              Policy.setPolicy(savedPolicy);
143          }
144      }
145  
146      /**
147 <     * submit of a failed privileged exception action reports exception
147 >     * A submitted failed privileged exception action reports exception
148       */
149 <    public void testSubmitFailedPrivilegedExceptionAction() {
150 <        Policy savedPolicy = Policy.getPolicy();
151 <        AdjustablePolicy policy = new AdjustablePolicy();
152 <        policy.addPermission(new RuntimePermission("getContextClassLoader"));
153 <        policy.addPermission(new RuntimePermission("setContextClassLoader"));
154 <        Policy.setPolicy(policy);
149 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
150 >        Policy savedPolicy = null;
151 >        try {
152 >            savedPolicy = Policy.getPolicy();
153 >            AdjustablePolicy policy = new AdjustablePolicy();
154 >            policy.addPermission(new RuntimePermission("getContextClassLoader"));
155 >            policy.addPermission(new RuntimePermission("setContextClassLoader"));
156 >            Policy.setPolicy(policy);
157 >        } catch (AccessControlException ok) {
158 >            return;
159 >        }
160 >
161          try {
162              ExecutorService e = new DirectExecutorService();
163              Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
# Line 187 | Line 165 | public class AbstractExecutorServiceTest
165                          throw new IndexOutOfBoundsException();
166                      }}));
167  
168 <            Object result = future.get();
168 >            future.get();
169              shouldThrow();
170 <        }
171 <        catch (ExecutionException success) {
194 <        }
195 <        catch (InterruptedException ex) {
196 <            unexpectedException();
170 >        } catch (ExecutionException success) {
171 >            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
172          }
173          finally {
174              Policy.setPolicy(savedPolicy);
# Line 201 | Line 176 | public class AbstractExecutorServiceTest
176      }
177  
178      /**
179 <     * execute with a null runnable throws NPE
179 >     * execute(null runnable) throws NPE
180       */
181      public void testExecuteNullRunnable() {
182          try {
183              ExecutorService e = new DirectExecutorService();
184 <            TrackedShortRunnable task = null;
210 <            Future<?> future = e.submit(task);
184 >            e.submit((Runnable) null);
185              shouldThrow();
186 <        }
213 <        catch (NullPointerException success) {
214 <        }
215 <        catch (Exception ex) {
216 <            unexpectedException();
217 <        }
186 >        } catch (NullPointerException success) {}
187      }
188  
189  
190      /**
191 <     * submit of a null callable throws NPE
191 >     * submit(null callable) throws NPE
192       */
193      public void testSubmitNullCallable() {
194          try {
195              ExecutorService e = new DirectExecutorService();
196 <            StringTask t = null;
228 <            Future<String> future = e.submit(t);
196 >            e.submit((Callable) null);
197              shouldThrow();
198 <        }
231 <        catch (NullPointerException success) {
232 <        }
233 <        catch (Exception ex) {
234 <            unexpectedException();
235 <        }
198 >        } catch (NullPointerException success) {}
199      }
200  
201      /**
202 <     * submit of Runnable throws RejectedExecutionException if
203 <     * saturated.
202 >     * submit(runnable) throws RejectedExecutionException if
203 >     * executor is saturated.
204       */
205      public void testExecute1() {
206 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
206 >        ThreadPoolExecutor p =
207 >            new ThreadPoolExecutor(1, 1,
208 >                                   60, TimeUnit.SECONDS,
209 >                                   new ArrayBlockingQueue<Runnable>(1));
210          try {
211 <
246 <            for(int i = 0; i < 5; ++i){
211 >            for (int i = 0; i < 2; ++i)
212                  p.submit(new MediumRunnable());
213 +            for (int i = 0; i < 2; ++i) {
214 +                try {
215 +                    p.submit(new MediumRunnable());
216 +                    shouldThrow();
217 +                } catch (RejectedExecutionException success) {}
218              }
219 <            shouldThrow();
220 <        } catch(RejectedExecutionException success){}
221 <        joinPool(p);
219 >        } finally {
220 >            joinPool(p);
221 >        }
222      }
223  
224      /**
225 <     * Completed submit of Callable throws RejectedExecutionException
226 <     *  if saturated.
225 >     * submit(callable) throws RejectedExecutionException
226 >     * if executor is saturated.
227       */
228      public void testExecute2() {
229 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
229 >        ThreadPoolExecutor p =
230 >            new ThreadPoolExecutor(1, 1,
231 >                                   60, TimeUnit.SECONDS,
232 >                                   new ArrayBlockingQueue<Runnable>(1));
233          try {
234 <            for(int i = 0; i < 5; ++i) {
235 <                p.submit(new SmallCallable());
234 >            for (int i = 0; i < 2; ++i)
235 >                p.submit(new MediumRunnable());
236 >            for (int i = 0; i < 2; ++i) {
237 >                try {
238 >                    p.submit(new SmallCallable());
239 >                    shouldThrow();
240 >                } catch (RejectedExecutionException success) {}
241              }
242 <            shouldThrow();
243 <        } catch(RejectedExecutionException e){}
244 <        joinPool(p);
242 >        } finally {
243 >            joinPool(p);
244 >        }
245      }
246  
247  
248      /**
249 <     *  blocking on submit of Callable throws InterruptedException if
249 >     *  Blocking on submit(callable) throws InterruptedException if
250       *  caller interrupted.
251       */
252 <    public void testInterruptedSubmit() {
253 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
254 <        Thread t = new Thread(new Runnable() {
255 <                public void run() {
256 <                    try {
257 <                        p.submit(new Callable<Object>() {
258 <                                public Object call() {
259 <                                    try {
260 <                                        Thread.sleep(MEDIUM_DELAY_MS);
261 <                                        shouldThrow();
262 <                                    } catch(InterruptedException e){
263 <                                    }
264 <                                    return null;
265 <                                }
266 <                            }).get();
289 <                    } catch(InterruptedException success){
290 <                    } catch(Exception e) {
291 <                        unexpectedException();
292 <                    }
293 <
294 <                }
295 <            });
296 <        try {
297 <            t.start();
298 <            Thread.sleep(SHORT_DELAY_MS);
299 <            t.interrupt();
300 <        } catch(Exception e){
301 <            unexpectedException();
302 <        }
252 >    public void testInterruptedSubmit() throws InterruptedException {
253 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
254 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
255 >            public void realRun() throws Exception {
256 >                p.submit(new CheckedCallable<Object>() {
257 >                             public Object realCall()
258 >                                 throws InterruptedException {
259 >                                 Thread.sleep(SMALL_DELAY_MS);
260 >                                 return null;
261 >                             }}).get();
262 >            }});
263 >
264 >        t.start();
265 >        Thread.sleep(SHORT_DELAY_MS);
266 >        t.interrupt();
267          joinPool(p);
268      }
269  
270      /**
271 <     *  get of submit of Callable throws Exception if callable
271 >     *  get of submitted callable throws InterruptedException if callable
272       *  interrupted
273       */
274 <    public void testSubmitIE() {
275 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
276 <
277 <        final Callable c = new Callable() {
278 <                public Object call() {
279 <                    try {
280 <                        p.submit(new SmallCallable()).get();
281 <                        shouldThrow();
282 <                    } catch(InterruptedException e){}
283 <                    catch(RejectedExecutionException e2){}
284 <                    catch(ExecutionException e3){}
285 <                    return Boolean.TRUE;
286 <                }
287 <            };
288 <
325 <
326 <
327 <        Thread t = new Thread(new Runnable() {
328 <                public void run() {
329 <                    try {
330 <                        c.call();
331 <                    } catch(Exception e){}
332 <                }
333 <          });
334 <        try {
335 <            t.start();
336 <            Thread.sleep(SHORT_DELAY_MS);
337 <            t.interrupt();
338 <            t.join();
339 <        } catch(InterruptedException e){
340 <            unexpectedException();
341 <        }
342 <
274 >    public void testSubmitIE() throws InterruptedException {
275 >        final ThreadPoolExecutor p =
276 >            new ThreadPoolExecutor(1, 1,
277 >                                   60, TimeUnit.SECONDS,
278 >                                   new ArrayBlockingQueue<Runnable>(10));
279 >
280 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
281 >            public void realRun() throws Exception {
282 >                p.submit(new SmallCallable()).get();
283 >            }});
284 >
285 >        t.start();
286 >        Thread.sleep(SHORT_DELAY_MS);
287 >        t.interrupt();
288 >        t.join();
289          joinPool(p);
290      }
291  
292      /**
293 <     *  completed submit of Callable throws ExecutionException if
294 <     *  callable throws exception
293 >     *  get of submit(callable) throws ExecutionException if callable
294 >     *  throws exception
295       */
296 <    public void testSubmitEE() {
297 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
298 <
299 <        try {
300 <            Callable c = new Callable() {
355 <                    public Object call() {
356 <                        int i = 5/0;
357 <                        return Boolean.TRUE;
358 <                    }
359 <                };
296 >    public void testSubmitEE() throws InterruptedException {
297 >        ThreadPoolExecutor p =
298 >            new ThreadPoolExecutor(1, 1,
299 >                                   60, TimeUnit.SECONDS,
300 >                                   new ArrayBlockingQueue<Runnable>(10));
301  
302 <            for(int i =0; i < 5; i++){
303 <                p.submit(c).get();
363 <            }
302 >        Callable c = new Callable() {
303 >            public Object call() { return 5/0; }};
304  
305 +        try {
306 +            p.submit(c).get();
307              shouldThrow();
308 <        }
309 <        catch(ExecutionException success){
368 <        } catch(Exception e) {
369 <            unexpectedException();
308 >        } catch (ExecutionException success) {
309 >            assertTrue(success.getCause() instanceof ArithmeticException);
310          }
311          joinPool(p);
312      }
# Line 374 | Line 314 | public class AbstractExecutorServiceTest
314      /**
315       * invokeAny(null) throws NPE
316       */
317 <    public void testInvokeAny1() {
317 >    public void testInvokeAny1()
318 >        throws InterruptedException, ExecutionException {
319          ExecutorService e = new DirectExecutorService();
320          try {
321              e.invokeAny(null);
322 +            shouldThrow();
323          } catch (NullPointerException success) {
382        } catch(Exception ex) {
383            unexpectedException();
324          } finally {
325              joinPool(e);
326          }
# Line 389 | Line 329 | public class AbstractExecutorServiceTest
329      /**
330       * invokeAny(empty collection) throws IAE
331       */
332 <    public void testInvokeAny2() {
332 >    public void testInvokeAny2()
333 >        throws InterruptedException, ExecutionException {
334          ExecutorService e = new DirectExecutorService();
335          try {
336              e.invokeAny(new ArrayList<Callable<String>>());
337 +            shouldThrow();
338          } catch (IllegalArgumentException success) {
397        } catch(Exception ex) {
398            unexpectedException();
339          } finally {
340              joinPool(e);
341          }
# Line 404 | Line 344 | public class AbstractExecutorServiceTest
344      /**
345       * invokeAny(c) throws NPE if c has null elements
346       */
347 <    public void testInvokeAny3() {
347 >    public void testInvokeAny3() throws Exception {
348 >        final CountDownLatch latch = new CountDownLatch(1);
349          ExecutorService e = new DirectExecutorService();
350          try {
351 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
352 <            l.add(new StringTask());
351 >            ArrayList<Callable<Integer>> l
352 >                = new ArrayList<Callable<Integer>>();
353 >            l.add(new Callable<Integer>() {
354 >                      public Integer call() { return 5/0; }});
355              l.add(null);
356              e.invokeAny(l);
357 +            shouldThrow();
358          } catch (NullPointerException success) {
415        } catch(Exception ex) {
416            ex.printStackTrace();
417            unexpectedException();
359          } finally {
360 +            latch.countDown();
361              joinPool(e);
362          }
363      }
364  
365      /**
366 <     * invokeAny(c) throws ExecutionException if no task completes
366 >     * invokeAny(c) throws ExecutionException if no task in c completes
367       */
368 <    public void testInvokeAny4() {
368 >    public void testInvokeAny4() throws InterruptedException {
369          ExecutorService e = new DirectExecutorService();
370          try {
371              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
372              l.add(new NPETask());
373              e.invokeAny(l);
374 <        } catch(ExecutionException success) {
375 <        } catch(Exception ex) {
376 <            unexpectedException();
374 >            shouldThrow();
375 >        } catch (ExecutionException success) {
376 >            assertTrue(success.getCause() instanceof NullPointerException);
377          } finally {
378              joinPool(e);
379          }
380      }
381  
382      /**
383 <     * invokeAny(c) returns result of some task
383 >     * invokeAny(c) returns result of some task in c if at least one completes
384       */
385 <    public void testInvokeAny5() {
385 >    public void testInvokeAny5() throws Exception {
386          ExecutorService e = new DirectExecutorService();
387          try {
388              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
# Line 448 | Line 390 | public class AbstractExecutorServiceTest
390              l.add(new StringTask());
391              String result = e.invokeAny(l);
392              assertSame(TEST_STRING, result);
451        } catch (ExecutionException success) {
452        } catch(Exception ex) {
453            unexpectedException();
393          } finally {
394              joinPool(e);
395          }
# Line 459 | Line 398 | public class AbstractExecutorServiceTest
398      /**
399       * invokeAll(null) throws NPE
400       */
401 <    public void testInvokeAll1() {
401 >    public void testInvokeAll1() throws InterruptedException {
402          ExecutorService e = new DirectExecutorService();
403          try {
404              e.invokeAll(null);
405 +            shouldThrow();
406          } catch (NullPointerException success) {
467        } catch(Exception ex) {
468            unexpectedException();
407          } finally {
408              joinPool(e);
409          }
# Line 474 | Line 412 | public class AbstractExecutorServiceTest
412      /**
413       * invokeAll(empty collection) returns empty collection
414       */
415 <    public void testInvokeAll2() {
415 >    public void testInvokeAll2() throws InterruptedException {
416          ExecutorService e = new DirectExecutorService();
417          try {
418              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
419              assertTrue(r.isEmpty());
482        } catch(Exception ex) {
483            unexpectedException();
420          } finally {
421              joinPool(e);
422          }
# Line 489 | Line 425 | public class AbstractExecutorServiceTest
425      /**
426       * invokeAll(c) throws NPE if c has null elements
427       */
428 <    public void testInvokeAll3() {
428 >    public void testInvokeAll3() throws InterruptedException {
429          ExecutorService e = new DirectExecutorService();
430          try {
431              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
432              l.add(new StringTask());
433              l.add(null);
434              e.invokeAll(l);
435 +            shouldThrow();
436          } catch (NullPointerException success) {
500        } catch(Exception ex) {
501            unexpectedException();
437          } finally {
438              joinPool(e);
439          }
440      }
441  
442      /**
443 <     * get of element of invokeAll(c) throws exception on failed task
443 >     * get of returned element of invokeAll(c) throws exception on failed task
444       */
445 <    public void testInvokeAll4() {
445 >    public void testInvokeAll4() throws Exception {
446          ExecutorService e = new DirectExecutorService();
447          try {
448              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
449              l.add(new NPETask());
450              List<Future<String>> result = e.invokeAll(l);
451              assertEquals(1, result.size());
452 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
453 <                it.next().get();
454 <        } catch(ExecutionException success) {
455 <        } catch(Exception ex) {
456 <            unexpectedException();
452 >            for (Future<String> future : result) {
453 >                try {
454 >                    future.get();
455 >                    shouldThrow();
456 >                } catch (ExecutionException success) {
457 >                    Throwable cause = success.getCause();
458 >                    assertTrue(cause instanceof NullPointerException);
459 >                }
460 >            }
461          } finally {
462              joinPool(e);
463          }
464      }
465  
466      /**
467 <     * invokeAll(c) returns results of all completed tasks
467 >     * invokeAll(c) returns results of all completed tasks in c
468       */
469 <    public void testInvokeAll5() {
469 >    public void testInvokeAll5() throws Exception {
470          ExecutorService e = new DirectExecutorService();
471          try {
472              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
# Line 535 | Line 474 | public class AbstractExecutorServiceTest
474              l.add(new StringTask());
475              List<Future<String>> result = e.invokeAll(l);
476              assertEquals(2, result.size());
477 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
478 <                assertSame(TEST_STRING, it.next().get());
540 <        } catch (ExecutionException success) {
541 <        } catch(Exception ex) {
542 <            unexpectedException();
477 >            for (Future<String> future : result)
478 >                assertSame(TEST_STRING, future.get());
479          } finally {
480              joinPool(e);
481          }
# Line 549 | Line 485 | public class AbstractExecutorServiceTest
485      /**
486       * timed invokeAny(null) throws NPE
487       */
488 <    public void testTimedInvokeAny1() {
488 >    public void testTimedInvokeAny1() throws Exception {
489          ExecutorService e = new DirectExecutorService();
490          try {
491 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
491 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
492 >            shouldThrow();
493          } catch (NullPointerException success) {
557        } catch(Exception ex) {
558            unexpectedException();
494          } finally {
495              joinPool(e);
496          }
497      }
498  
499      /**
500 <     * timed invokeAny(,,null) throws NPE
500 >     * timed invokeAny(null time unit) throws NPE
501       */
502 <    public void testTimedInvokeAnyNullTimeUnit() {
502 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
503          ExecutorService e = new DirectExecutorService();
504          try {
505              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
506              l.add(new StringTask());
507              e.invokeAny(l, MEDIUM_DELAY_MS, null);
508 +            shouldThrow();
509          } catch (NullPointerException success) {
574        } catch(Exception ex) {
575            unexpectedException();
510          } finally {
511              joinPool(e);
512          }
# Line 581 | Line 515 | public class AbstractExecutorServiceTest
515      /**
516       * timed invokeAny(empty collection) throws IAE
517       */
518 <    public void testTimedInvokeAny2() {
518 >    public void testTimedInvokeAny2() throws Exception {
519          ExecutorService e = new DirectExecutorService();
520          try {
521 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
521 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
522 >            shouldThrow();
523          } catch (IllegalArgumentException success) {
589        } catch(Exception ex) {
590            unexpectedException();
524          } finally {
525              joinPool(e);
526          }
# Line 596 | Line 529 | public class AbstractExecutorServiceTest
529      /**
530       * timed invokeAny(c) throws NPE if c has null elements
531       */
532 <    public void testTimedInvokeAny3() {
532 >    public void testTimedInvokeAny3() throws Exception {
533 >        final CountDownLatch latch = new CountDownLatch(1);
534          ExecutorService e = new DirectExecutorService();
535          try {
536 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
537 <            l.add(new StringTask());
536 >            ArrayList<Callable<Integer>> l
537 >                = new ArrayList<Callable<Integer>>();
538 >            l.add(new Callable<Integer>() {
539 >                      public Integer call() { return 5/0; }});
540              l.add(null);
541 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
541 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
542 >            shouldThrow();
543          } catch (NullPointerException success) {
607        } catch(Exception ex) {
608            ex.printStackTrace();
609            unexpectedException();
544          } finally {
545 +            latch.countDown();
546              joinPool(e);
547          }
548      }
# Line 615 | Line 550 | public class AbstractExecutorServiceTest
550      /**
551       * timed invokeAny(c) throws ExecutionException if no task completes
552       */
553 <    public void testTimedInvokeAny4() {
553 >    public void testTimedInvokeAny4() throws Exception {
554          ExecutorService e = new DirectExecutorService();
555          try {
556              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
557              l.add(new NPETask());
558 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
559 <        } catch(ExecutionException success) {
560 <        } catch(Exception ex) {
561 <            unexpectedException();
558 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
559 >            shouldThrow();
560 >        } catch (ExecutionException success) {
561 >            assertTrue(success.getCause() instanceof NullPointerException);
562          } finally {
563              joinPool(e);
564          }
565      }
566  
567      /**
568 <     * timed invokeAny(c) returns result of some task
568 >     * timed invokeAny(c) returns result of some task in c
569       */
570 <    public void testTimedInvokeAny5() {
570 >    public void testTimedInvokeAny5() throws Exception {
571          ExecutorService e = new DirectExecutorService();
572          try {
573              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
574              l.add(new StringTask());
575              l.add(new StringTask());
576 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
576 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
577              assertSame(TEST_STRING, result);
643        } catch (ExecutionException success) {
644        } catch(Exception ex) {
645            unexpectedException();
578          } finally {
579              joinPool(e);
580          }
# Line 651 | Line 583 | public class AbstractExecutorServiceTest
583      /**
584       * timed invokeAll(null) throws NPE
585       */
586 <    public void testTimedInvokeAll1() {
586 >    public void testTimedInvokeAll1() throws InterruptedException {
587          ExecutorService e = new DirectExecutorService();
588          try {
589 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
589 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
590 >            shouldThrow();
591          } catch (NullPointerException success) {
659        } catch(Exception ex) {
660            unexpectedException();
592          } finally {
593              joinPool(e);
594          }
595      }
596  
597      /**
598 <     * timed invokeAll(,,null) throws NPE
598 >     * timed invokeAll(null time unit) throws NPE
599       */
600 <    public void testTimedInvokeAllNullTimeUnit() {
600 >    public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
601          ExecutorService e = new DirectExecutorService();
602          try {
603              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
604              l.add(new StringTask());
605              e.invokeAll(l, MEDIUM_DELAY_MS, null);
606 +            shouldThrow();
607          } catch (NullPointerException success) {
676        } catch(Exception ex) {
677            unexpectedException();
608          } finally {
609              joinPool(e);
610          }
# Line 683 | Line 613 | public class AbstractExecutorServiceTest
613      /**
614       * timed invokeAll(empty collection) returns empty collection
615       */
616 <    public void testTimedInvokeAll2() {
616 >    public void testTimedInvokeAll2() throws InterruptedException {
617          ExecutorService e = new DirectExecutorService();
618          try {
619 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
619 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
620              assertTrue(r.isEmpty());
691        } catch(Exception ex) {
692            unexpectedException();
621          } finally {
622              joinPool(e);
623          }
# Line 698 | Line 626 | public class AbstractExecutorServiceTest
626      /**
627       * timed invokeAll(c) throws NPE if c has null elements
628       */
629 <    public void testTimedInvokeAll3() {
629 >    public void testTimedInvokeAll3() throws InterruptedException {
630          ExecutorService e = new DirectExecutorService();
631          try {
632              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
633              l.add(new StringTask());
634              l.add(null);
635 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
635 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
636 >            shouldThrow();
637          } catch (NullPointerException success) {
709        } catch(Exception ex) {
710            unexpectedException();
638          } finally {
639              joinPool(e);
640          }
641      }
642  
643      /**
644 <     * get of element of invokeAll(c) throws exception on failed task
644 >     * get of returned element of invokeAll(c) throws exception on failed task
645       */
646 <    public void testTimedInvokeAll4() {
646 >    public void testTimedInvokeAll4() throws Exception {
647          ExecutorService e = new DirectExecutorService();
648          try {
649              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
650              l.add(new NPETask());
651 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
651 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
652              assertEquals(1, result.size());
653 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
654 <                it.next().get();
655 <        } catch(ExecutionException success) {
656 <        } catch(Exception ex) {
657 <            unexpectedException();
653 >            for (Future<String> future : result) {
654 >                try {
655 >                    future.get();
656 >                } catch (ExecutionException success) {
657 >                    assertTrue(success.getCause() instanceof NullPointerException);
658 >                }
659 >            }
660          } finally {
661              joinPool(e);
662          }
663      }
664  
665      /**
666 <     * timed invokeAll(c) returns results of all completed tasks
666 >     * timed invokeAll(c) returns results of all completed tasks in c
667       */
668 <    public void testTimedInvokeAll5() {
668 >    public void testTimedInvokeAll5() throws Exception {
669          ExecutorService e = new DirectExecutorService();
670          try {
671              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
672              l.add(new StringTask());
673              l.add(new StringTask());
674 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
674 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
675              assertEquals(2, result.size());
676 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
677 <                assertSame(TEST_STRING, it.next().get());
749 <        } catch (ExecutionException success) {
750 <        } catch(Exception ex) {
751 <            unexpectedException();
676 >            for (Future<String> future : result)
677 >                assertSame(TEST_STRING, future.get());
678          } finally {
679              joinPool(e);
680          }
681      }
682  
683      /**
684 <     * timed invokeAll(c) cancels tasks not completed by timeout
684 >     * timed invokeAll cancels tasks not completed by timeout
685       */
686 <    public void testTimedInvokeAll6() {
686 >    public void testTimedInvokeAll6() throws InterruptedException {
687          ExecutorService e = new DirectExecutorService();
688          try {
689              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
690              l.add(new StringTask());
691 <            l.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
692 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
693 <            assertEquals(2, result.size());
694 <            Iterator<Future<String>> it = result.iterator();
691 >            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
692 >            l.add(new StringTask());
693 >            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
694 >            assertEquals(3, result.size());
695 >            Iterator<Future<String>> it = result.iterator();
696              Future<String> f1 = it.next();
697              Future<String> f2 = it.next();
698 +            Future<String> f3 = it.next();
699              assertTrue(f1.isDone());
700              assertFalse(f1.isCancelled());
701              assertTrue(f2.isDone());
702 <            //            assertTrue(f2.isCancelled());
703 <        } catch(Exception ex) {
776 <            unexpectedException();
702 >            assertTrue(f3.isDone());
703 >            assertTrue(f3.isCancelled());
704          } finally {
705              joinPool(e);
706          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines