ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines