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.11 by dl, Sun Dec 28 21:56:18 2003 UTC vs.
Revision 1.34 by jsr166, Wed Sep 25 07:39:17 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines