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.1 by tim, Wed Dec 10 01:51:12 2003 UTC vs.
Revision 1.30 by jsr166, Tue Mar 15 19:47:06 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines