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.2 by tim, Wed Dec 10 02:24:00 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      }
20      public static Test suite() {
21 <        return new TestSuite(ExecutorsTest.class);
21 >        return new TestSuite(AbstractExecutorServiceTest.class);
22      }
23  
24 <    /**
24 >    /**
25       * A no-frills implementation of AbstractExecutorService, designed
26 <     * to test the submit/invoke methods only.
26 >     * to test the submit methods only.
27       */
28      static class DirectExecutorService extends AbstractExecutorService {
29          public void execute(Runnable r) { r.run(); }
# Line 34 | Line 35 | public class AbstractExecutorServiceTest
35          private volatile boolean shutdown = false;
36      }
37  
38 <    private static final String TEST_STRING = "a test string";
39 <
40 <    private static class StringTask implements Callable<String> {
41 <        public String call() { return TEST_STRING; }
38 >    /**
39 >     * execute(runnable) runs it to completion
40 >     */
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 <     * execute of runnable runs it to completion
52 >     * Completed submit(callable) returns result
53       */
54 <    public void testExecuteRunnable() {
55 <        try {
56 <            ExecutorService e = new DirectExecutorService();
57 <            TrackedShortRunnable task = new TrackedShortRunnable();
58 <            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) {
59 <            unexpectedException();
60 <        }
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 <     * invoke of a runnable runs it to completion
62 >     * Completed submit(runnable) returns successfully
63       */
64 <    public void testInvokeRunnable() {
65 <        try {
66 <            ExecutorService e = new DirectExecutorService();
67 <            TrackedShortRunnable task = new TrackedShortRunnable();
68 <            assertFalse(task.done);
71 <            e.invoke(task);
72 <            assertTrue(task.done);
73 <        }
74 <        catch (ExecutionException ex) {
75 <            unexpectedException();
76 <        }
77 <        catch (InterruptedException ex) {
78 <            unexpectedException();
79 <        }
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 <     * execute of a callable runs it to completion
72 >     * Completed submit(runnable, result) returns result
73       */
74 <    public void testExecuteCallable() {
75 <        try {
76 <            ExecutorService e = new DirectExecutorService();
77 <            Future<String> future = e.submit(new StringTask());
78 <            String result = future.get();
90 <            assertSame(TEST_STRING, result);
91 <        }
92 <        catch (ExecutionException ex) {
93 <            unexpectedException();
94 <        }
95 <        catch (InterruptedException ex) {
96 <            unexpectedException();
97 <        }
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 <     * execute of a privileged action runs it to completion
83 >     * A submitted privileged action to completion
84       */
85 <    public void testExecutePrivilegedAction() {
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(new PrivilegedAction() {
98 >            Future future = e.submit(Executors.callable(new PrivilegedAction() {
99                      public Object run() {
100                          return TEST_STRING;
101 <                    }});
101 >                    }}));
102  
103              Object result = future.get();
104              assertSame(TEST_STRING, result);
105          }
120        catch (ExecutionException ex) {
121            unexpectedException();
122        }
123        catch (InterruptedException ex) {
124            unexpectedException();
125        }
106          finally {
107 <            Policy.setPolicy(savedPolicy);
107 >            try {
108 >                Policy.setPolicy(savedPolicy);
109 >            } catch (AccessControlException ok) {
110 >                return;
111 >            }
112          }
113      }
114  
115      /**
116 <     * execute of a privileged exception action runs it to completion
116 >     * A submitted a privileged exception action runs to completion
117       */
118 <    public void testExecutePrivilegedExceptionAction() {
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(new PrivilegedExceptionAction() {
132 >            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
133                      public Object run() {
134                          return TEST_STRING;
135 <                    }});
135 >                    }}));
136  
137              Object result = future.get();
138              assertSame(TEST_STRING, result);
139          }
150        catch (ExecutionException ex) {
151            unexpectedException();
152        }
153        catch (InterruptedException ex) {
154            unexpectedException();
155        }
140          finally {
141              Policy.setPolicy(savedPolicy);
142          }
143      }
144  
145      /**
146 <     * execute of a failed privileged exception action reports exception
146 >     * A submitted failed privileged exception action reports exception
147       */
148 <    public void testExecuteFailedPrivilegedExceptionAction() {
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(new PrivilegedExceptionAction() {
162 >            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
163                      public Object run() throws Exception {
164                          throw new IndexOutOfBoundsException();
165 <                    }});
165 >                    }}));
166  
167 <            Object result = future.get();
167 >            future.get();
168              shouldThrow();
169 <        }
170 <        catch (ExecutionException success) {
181 <        }
182 <        catch (InterruptedException ex) {
183 <            unexpectedException();
169 >        } catch (ExecutionException success) {
170 >            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
171          }
172          finally {
173              Policy.setPolicy(savedPolicy);
# Line 188 | Line 175 | public class AbstractExecutorServiceTest
175      }
176  
177      /**
178 <     * invoke of a collable runs it to completion
178 >     * execute(null runnable) throws NPE
179       */
180 <    public void testInvokeCallable() {
180 >    public void testExecuteNullRunnable() {
181          try {
182              ExecutorService e = new DirectExecutorService();
183 <            String result = e.invoke(new StringTask());
184 <
185 <            assertSame(TEST_STRING, result);
199 <        }
200 <        catch (ExecutionException ex) {
201 <            unexpectedException();
202 <        }
203 <        catch (InterruptedException ex) {
204 <            unexpectedException();
205 <        }
183 >            e.submit((Runnable) null);
184 >            shouldThrow();
185 >        } catch (NullPointerException success) {}
186      }
187  
188 +
189      /**
190 <     * execute with a null runnable throws NPE
190 >     * submit(null callable) throws NPE
191       */
192 <    public void testExecuteNullRunnable() {
192 >    public void testSubmitNullCallable() {
193          try {
194              ExecutorService e = new DirectExecutorService();
195 <            TrackedShortRunnable task = null;
215 <            Future<?> future = e.submit(task);
195 >            e.submit((Callable) null);
196              shouldThrow();
197 +        } catch (NullPointerException success) {}
198 +    }
199 +
200 +    /**
201 +     * submit(runnable) throws RejectedExecutionException if
202 +     * executor is saturated.
203 +     */
204 +    public void testExecute1() {
205 +        ThreadPoolExecutor p =
206 +            new ThreadPoolExecutor(1, 1,
207 +                                   60, TimeUnit.SECONDS,
208 +                                   new ArrayBlockingQueue<Runnable>(1));
209 +        try {
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 +        } finally {
219 +            joinPool(p);
220          }
221 <        catch (NullPointerException success) {
222 <        }
223 <        catch (Exception ex) {
224 <            unexpectedException();
221 >    }
222 >
223 >    /**
224 >     * submit(callable) throws RejectedExecutionException
225 >     * if executor is saturated.
226 >     */
227 >    public void testExecute2() {
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 < 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 >        } finally {
242 >            joinPool(p);
243          }
244      }
245  
246 +
247 +    /**
248 +     *  Blocking on submit(callable) throws InterruptedException if
249 +     *  caller interrupted.
250 +     */
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 submitted callable throws InterruptedException if callable
271 +     *  interrupted
272 +     */
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 <     * invoke of a null runnable throws NPE
292 >     *  get of submit(callable) throws ExecutionException if callable
293 >     *  throws exception
294       */
295 <    public void testInvokeNullRunnable() {
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 >        Callable c = new Callable() {
302 >            public Object call() { return 5/0; }};
303 >
304          try {
305 <            ExecutorService e = new DirectExecutorService();
231 <            TrackedShortRunnable task = null;
232 <            e.invoke(task);
305 >            p.submit(c).get();
306              shouldThrow();
307 +        } catch (ExecutionException success) {
308 +            assertTrue(success.getCause() instanceof ArithmeticException);
309          }
310 <        catch (NullPointerException success) {
236 <        }
237 <        catch (Exception ex) {
238 <            unexpectedException();
239 <        }
310 >        joinPool(p);
311      }
312  
313      /**
314 <     * execute of a null callable throws NPE
314 >     * invokeAny(null) throws NPE
315       */
316 <    public void testExecuteNullCallable() {
316 >    public void testInvokeAny1()
317 >        throws InterruptedException, ExecutionException {
318 >        ExecutorService e = new DirectExecutorService();
319          try {
320 <            ExecutorService e = new DirectExecutorService();
248 <            StringTask t = null;
249 <            Future<String> future = e.submit(t);
320 >            e.invokeAny(null);
321              shouldThrow();
322 <        }
323 <        catch (NullPointerException success) {
324 <        }
254 <        catch (Exception ex) {
255 <            unexpectedException();
322 >        } catch (NullPointerException success) {
323 >        } finally {
324 >            joinPool(e);
325          }
326      }
327  
328      /**
329 <     * invoke of a null callable throws NPE
329 >     * invokeAny(empty collection) throws IAE
330       */
331 <    public void testInvokeNullCallable() {
331 >    public void testInvokeAny2()
332 >        throws InterruptedException, ExecutionException {
333 >        ExecutorService e = new DirectExecutorService();
334          try {
335 <            ExecutorService e = new DirectExecutorService();
265 <            StringTask t = null;
266 <            String result = e.invoke(t);
335 >            e.invokeAny(new ArrayList<Callable<String>>());
336              shouldThrow();
337 <        }
338 <        catch (NullPointerException success) {
339 <        }
271 <        catch (Exception ex) {
272 <            unexpectedException();
337 >        } catch (IllegalArgumentException success) {
338 >        } finally {
339 >            joinPool(e);
340          }
341      }
342  
343      /**
344 <     *  execute(Executor, Runnable) throws RejectedExecutionException
278 <     *  if saturated.
344 >     * invokeAny(c) throws NPE if c has null elements
345       */
346 <    public void testExecute1() {
347 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
346 >    public void testInvokeAny3() throws Exception {
347 >        final CountDownLatch latch = new CountDownLatch(1);
348 >        ExecutorService e = new DirectExecutorService();
349          try {
350 <
351 <            for(int i = 0; i < 5; ++i){
352 <                p.submit(new MediumRunnable());
353 <            }
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(RejectedExecutionException success){}
358 <        joinPool(p);
357 >        } catch (NullPointerException success) {
358 >        } finally {
359 >            latch.countDown();
360 >            joinPool(e);
361 >        }
362      }
363  
364      /**
365 <     *  execute(Executor, Callable)throws RejectedExecutionException
294 <     *  if saturated.
365 >     * invokeAny(c) throws ExecutionException if no task in c completes
366       */
367 <    public void testExecute2() {
368 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
367 >    public void testInvokeAny4() throws InterruptedException {
368 >        ExecutorService e = new DirectExecutorService();
369          try {
370 <            for(int i = 0; i < 5; ++i) {
371 <                p.submit(new SmallCallable());
372 <            }
370 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
371 >            l.add(new NPETask());
372 >            e.invokeAny(l);
373              shouldThrow();
374 <        } catch(RejectedExecutionException e){}
375 <        joinPool(p);
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 in c if at least one completes
383 +     */
384 +    public void testInvokeAny5() throws Exception {
385 +        ExecutorService e = new DirectExecutorService();
386 +        try {
387 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
388 +            l.add(new StringTask());
389 +            l.add(new StringTask());
390 +            String result = e.invokeAny(l);
391 +            assertSame(TEST_STRING, result);
392 +        } finally {
393 +            joinPool(e);
394 +        }
395 +    }
396  
397      /**
398 <     *  invoke(Executor, Runnable) throws InterruptedException if
310 <     *  caller interrupted.
398 >     * invokeAll(null) throws NPE
399       */
400 <    public void testInterruptedInvoke() {
401 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
402 <        Thread t = new Thread(new Runnable() {
403 <                public void run() {
404 <                    try {
405 <                        p.invoke(new Runnable() {
406 <                                public void run() {
407 <                                    try {
408 <                                        Thread.sleep(MEDIUM_DELAY_MS);
409 <                                        shouldThrow();
322 <                                    } catch(InterruptedException e){
323 <                                    }
324 <                                }
325 <                            });
326 <                    } catch(InterruptedException success){
327 <                    } catch(Exception e) {
328 <                        unexpectedException();
329 <                    }
400 >    public void testInvokeAll1() throws InterruptedException {
401 >        ExecutorService e = new DirectExecutorService();
402 >        try {
403 >            e.invokeAll(null);
404 >            shouldThrow();
405 >        } catch (NullPointerException success) {
406 >        } finally {
407 >            joinPool(e);
408 >        }
409 >    }
410  
411 <                }
412 <            });
411 >    /**
412 >     * invokeAll(empty collection) returns empty collection
413 >     */
414 >    public void testInvokeAll2() throws InterruptedException {
415 >        ExecutorService e = new DirectExecutorService();
416          try {
417 <            t.start();
418 <            Thread.sleep(SHORT_DELAY_MS);
419 <            t.interrupt();
420 <        } catch(Exception e){
338 <            unexpectedException();
417 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
418 >            assertTrue(r.isEmpty());
419 >        } finally {
420 >            joinPool(e);
421          }
340        joinPool(p);
422      }
423  
424      /**
425 <     *  invoke(Executor, Runnable) throws ExecutionException if
345 <     *  runnable throws exception.
425 >     * invokeAll(c) throws NPE if c has null elements
426       */
427 <    public void testInvoke3() {
428 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
427 >    public void testInvokeAll3() throws InterruptedException {
428 >        ExecutorService e = new DirectExecutorService();
429          try {
430 <            Runnable r = new Runnable() {
431 <                    public void run() {
432 <                        int i = 5/0;
433 <                    }
434 <                };
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) {
436 >        } finally {
437 >            joinPool(e);
438 >        }
439 >    }
440  
441 <            for(int i =0; i < 5; i++){
442 <                p.invoke(r);
441 >    /**
442 >     * get of returned element of invokeAll(c) throws exception on failed task
443 >     */
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 (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 <            shouldThrow();
466 <        } catch(ExecutionException success){
467 <        } catch(Exception e){
468 <            unexpectedException();
465 >    /**
466 >     * invokeAll(c) returns results of all completed tasks in c
467 >     */
468 >    public void testInvokeAll5() throws Exception {
469 >        ExecutorService e = new DirectExecutorService();
470 >        try {
471 >            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
472 >            l.add(new StringTask());
473 >            l.add(new StringTask());
474 >            List<Future<String>> result = e.invokeAll(l);
475 >            assertEquals(2, result.size());
476 >            for (Future<String> future : result)
477 >                assertSame(TEST_STRING, future.get());
478 >        } finally {
479 >            joinPool(e);
480          }
365        joinPool(p);
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 <     *  invoke(Executor, Callable) throws InterruptedException if
372 <     *  callable throws exception
499 >     * timed invokeAny(null time unit) throws NPE
500       */
501 <    public void testInvoke5() {
502 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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 <        final Callable c = new Callable() {
515 <                public Object call() {
516 <                    try {
517 <                        p.invoke(new SmallCallable());
518 <                        shouldThrow();
519 <                    } catch(InterruptedException e){}
520 <                    catch(RejectedExecutionException e2){}
521 <                    catch(ExecutionException e3){}
522 <                    return Boolean.TRUE;
523 <                }
524 <            };
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 +            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 <        Thread t = new Thread(new Runnable() {
583 <                public void run() {
584 <                    try {
585 <                        c.call();
586 <                    } catch(Exception e){}
396 <                }
397 <          });
582 >    /**
583 >     * timed invokeAll(null) throws NPE
584 >     */
585 >    public void testTimedInvokeAll1() throws InterruptedException {
586 >        ExecutorService e = new DirectExecutorService();
587          try {
588 <            t.start();
589 <            Thread.sleep(SHORT_DELAY_MS);
590 <            t.interrupt();
591 <            t.join();
592 <        } catch(InterruptedException e){
404 <            unexpectedException();
588 >            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
589 >            shouldThrow();
590 >        } catch (NullPointerException success) {
591 >        } finally {
592 >            joinPool(e);
593          }
594 +    }
595  
596 <        joinPool(p);
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 <     *  invoke(Executor, Callable) will throw ExecutionException
412 <     *  if callable throws exception
613 >     * timed invokeAll(empty collection) returns empty collection
614       */
615 <    public void testInvoke6() {
616 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
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 <            Callable c = new Callable() {
632 <                    public Object call() {
633 <                        int i = 5/0;
634 <                        return Boolean.TRUE;
635 <                    }
636 <                };
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 <            for(int i =0; i < 5; i++){
643 <                p.invoke(c);
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 <            shouldThrow();
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 <        catch(ExecutionException success){
681 <        } catch(Exception e) {
682 <            unexpectedException();
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          }
435        joinPool(p);
706      }
707  
708 < }
708 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines