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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines