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.15 by dl, Tue Jan 20 20:20:56 2004 UTC vs.
Revision 1.40 by jsr166, Sun Oct 4 18:18:48 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(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(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(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(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 <     * A submitted privileged action to completion
107 >     * A submitted privileged action runs to completion
108       */
109 <    public void testSubmitPrivilegedAction() {
110 <        Policy savedPolicy = null;
111 <        try {
112 <            savedPolicy = Policy.getPolicy();
113 <            AdjustablePolicy policy = new AdjustablePolicy();
122 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
123 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
124 <            Policy.setPolicy(policy);
125 <        } catch(AccessControlException ok) {
126 <            return;
127 <        }
128 <        try {
129 <            ExecutorService e = new DirectExecutorService();
130 <            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) {
142 <            unexpectedException();
143 <        }
144 <        finally {
145 <            try {
146 <                Policy.setPolicy(savedPolicy);
147 <            } catch(AccessControlException ok) {
148 <                return;
149 <            }
150 <        }
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 <     * A submitted a privileged exception action runs to completion
128 >     * A submitted privileged exception action runs to completion
129       */
130 <    public void testSubmitPrivilegedExceptionAction() {
131 <        Policy savedPolicy = null;
132 <        try {
133 <            savedPolicy = Policy.getPolicy();
134 <            AdjustablePolicy policy = new AdjustablePolicy();
161 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
162 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
163 <            Policy.setPolicy(policy);
164 <        } catch(AccessControlException ok) {
165 <            return;
166 <        }
167 <
168 <        try {
169 <            ExecutorService e = new DirectExecutorService();
170 <            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) {
179 <            unexpectedException();
180 <        }
181 <        catch (InterruptedException ex) {
182 <            unexpectedException();
183 <        }
184 <        finally {
185 <            Policy.setPolicy(savedPolicy);
186 <        }
139 >                assertSame(TEST_STRING, future.get());
140 >            }};
141 >
142 >        runWithPermissions(r);
143      }
144  
145      /**
146       * A submitted failed privileged exception action reports exception
147       */
148 <    public void testSubmitFailedPrivilegedExceptionAction() {
149 <        Policy savedPolicy = null;
150 <        try {
151 <            savedPolicy = Policy.getPolicy();
152 <            AdjustablePolicy policy = new AdjustablePolicy();
197 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
198 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
199 <            Policy.setPolicy(policy);
200 <        } catch(AccessControlException ok) {
201 <            return;
202 <        }
203 <
204 <
205 <        try {
206 <            ExecutorService e = new DirectExecutorService();
207 <            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 <        }
220 <        finally {
221 <            Policy.setPolicy(savedPolicy);
222 <        }
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(null runnable) throws NPE
169       */
170      public void testExecuteNullRunnable() {
171 +        ExecutorService e = new DirectExecutorService();
172          try {
173 <            ExecutorService e = new DirectExecutorService();
231 <            TrackedShortRunnable task = null;
232 <            Future<?> future = e.submit(task);
173 >            e.submit((Runnable) null);
174              shouldThrow();
175 <        }
235 <        catch (NullPointerException success) {
236 <        }
237 <        catch (Exception ex) {
238 <            unexpectedException();
239 <        }
175 >        } catch (NullPointerException success) {}
176      }
177  
242
178      /**
179       * submit(null callable) throws NPE
180       */
181      public void testSubmitNullCallable() {
182 +        ExecutorService e = new DirectExecutorService();
183          try {
184 <            ExecutorService e = new DirectExecutorService();
249 <            StringTask t = null;
250 <            Future<String> future = e.submit(t);
251 <            shouldThrow();
252 <        }
253 <        catch (NullPointerException success) {
254 <        }
255 <        catch (Exception ex) {
256 <            unexpectedException();
257 <        }
258 <    }
259 <
260 <    /**
261 <     * submit(runnable) throws RejectedExecutionException if
262 <     * executor is saturated.
263 <     */
264 <    public void testExecute1() {
265 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
266 <        try {
267 <
268 <            for(int i = 0; i < 5; ++i){
269 <                p.submit(new MediumRunnable());
270 <            }
271 <            shouldThrow();
272 <        } catch(RejectedExecutionException success){}
273 <        joinPool(p);
274 <    }
275 <
276 <    /**
277 <     * submit(callable) throws RejectedExecutionException
278 <     * if executor is saturated.
279 <     */
280 <    public void testExecute2() {
281 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
282 <        try {
283 <            for(int i = 0; i < 5; ++i) {
284 <                p.submit(new SmallCallable());
285 <            }
184 >            e.submit((Callable) null);
185              shouldThrow();
186 <        } catch(RejectedExecutionException e){}
288 <        joinPool(p);
186 >        } catch (NullPointerException success) {}
187      }
188  
291
189      /**
190 <     *  Blocking on submit(callable) throws InterruptedException if
294 <     *  caller interrupted.
190 >     * submit(callable).get() throws InterruptedException if interrupted
191       */
192 <    public void testInterruptedSubmit() {
193 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
194 <        Thread t = new Thread(new Runnable() {
195 <                public void run() {
196 <                    try {
197 <                        p.submit(new Callable<Object>() {
198 <                                public Object call() {
199 <                                    try {
200 <                                        Thread.sleep(MEDIUM_DELAY_MS);
201 <                                        shouldThrow();
202 <                                    } catch(InterruptedException e){
203 <                                    }
204 <                                    return null;
205 <                                }
206 <                            }).get();
207 <                    } catch(InterruptedException success){
208 <                    } catch(Exception e) {
209 <                        unexpectedException();
314 <                    }
315 <
316 <                }
317 <            });
318 <        try {
192 >    public void testInterruptedSubmit() throws InterruptedException {
193 >        final CountDownLatch submitted    = new CountDownLatch(1);
194 >        final CountDownLatch quittingTime = new CountDownLatch(1);
195 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
196 >            public Void realCall() throws InterruptedException {
197 >                quittingTime.await();
198 >                return null;
199 >            }};
200 >        final ExecutorService p
201 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
202 >                                     new ArrayBlockingQueue<Runnable>(10));
203 >        try (PoolCleaner cleaner = cleaner(p)) {
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);
321 <            t.interrupt();
322 <        } catch(Exception e){
323 <            unexpectedException();
324 <        }
325 <        joinPool(p);
326 <    }
327 <
328 <    /**
329 <     *  get of submitted callable throws Exception if callable
330 <     *  interrupted
331 <     */
332 <    public void testSubmitIE() {
333 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
334 <
335 <        final Callable c = new Callable() {
336 <                public Object call() {
337 <                    try {
338 <                        p.submit(new SmallCallable()).get();
339 <                        shouldThrow();
340 <                    } catch(InterruptedException e){}
341 <                    catch(RejectedExecutionException e2){}
342 <                    catch(ExecutionException e3){}
343 <                    return Boolean.TRUE;
344 <                }
345 <            };
346 <
347 <
348 <
349 <        Thread t = new Thread(new Runnable() {
350 <                public void run() {
351 <                    try {
352 <                        c.call();
353 <                    } catch(Exception e){}
354 <                }
355 <          });
356 <        try {
357 <            t.start();
358 <            Thread.sleep(SHORT_DELAY_MS);
211 >            submitted.await();
212              t.interrupt();
213              t.join();
214 <        } catch(InterruptedException e){
362 <            unexpectedException();
214 >            quittingTime.countDown();
215          }
364
365        joinPool(p);
216      }
217  
218      /**
219 <     *  get of submit(callable) throws ExecutionException if callable
220 <     *  throws exception
219 >     * get of submit(callable) throws ExecutionException if callable
220 >     * throws exception
221       */
222 <    public void testSubmitEE() {
223 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
224 <
225 <        try {
222 >    public void testSubmitEE() throws InterruptedException {
223 >        final ThreadPoolExecutor p =
224 >            new ThreadPoolExecutor(1, 1,
225 >                                   60, TimeUnit.SECONDS,
226 >                                   new ArrayBlockingQueue<Runnable>(10));
227 >        try (PoolCleaner cleaner = cleaner(p)) {
228              Callable c = new Callable() {
229 <                    public Object call() {
230 <                        int i = 5/0;
379 <                        return Boolean.TRUE;
380 <                    }
381 <                };
382 <
383 <            for(int i =0; i < 5; i++){
229 >                public Object call() { throw new ArithmeticException(); }};
230 >            try {
231                  p.submit(c).get();
232 +                shouldThrow();
233 +            } catch (ExecutionException success) {
234 +                assertTrue(success.getCause() instanceof ArithmeticException);
235              }
386
387            shouldThrow();
388        }
389        catch(ExecutionException success){
390        } catch(Exception e) {
391            unexpectedException();
236          }
393        joinPool(p);
237      }
238  
239      /**
240       * invokeAny(null) throws NPE
241       */
242 <    public void testInvokeAny1() {
243 <        ExecutorService e = new DirectExecutorService();
244 <        try {
245 <            e.invokeAny(null);
246 <        } catch (NullPointerException success) {
247 <        } catch(Exception ex) {
248 <            unexpectedException();
406 <        } finally {
407 <            joinPool(e);
242 >    public void testInvokeAny1() throws Exception {
243 >        final ExecutorService e = new DirectExecutorService();
244 >        try (PoolCleaner cleaner = cleaner(e)) {
245 >            try {
246 >                e.invokeAny(null);
247 >                shouldThrow();
248 >            } catch (NullPointerException success) {}
249          }
250      }
251  
252      /**
253       * invokeAny(empty collection) throws IAE
254       */
255 <    public void testInvokeAny2() {
256 <        ExecutorService e = new DirectExecutorService();
257 <        try {
258 <            e.invokeAny(new ArrayList<Callable<String>>());
259 <        } catch (IllegalArgumentException success) {
260 <        } catch(Exception ex) {
261 <            unexpectedException();
421 <        } finally {
422 <            joinPool(e);
255 >    public void testInvokeAny2() throws Exception {
256 >        final ExecutorService e = new DirectExecutorService();
257 >        try (PoolCleaner cleaner = cleaner(e)) {
258 >            try {
259 >                e.invokeAny(new ArrayList<Callable<String>>());
260 >                shouldThrow();
261 >            } catch (IllegalArgumentException success) {}
262          }
263      }
264  
265      /**
266       * invokeAny(c) throws NPE if c has null elements
267       */
268 <    public void testInvokeAny3() {
269 <        ExecutorService e = new DirectExecutorService();
270 <        try {
271 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
272 <            l.add(new StringTask());
268 >    public void testInvokeAny3() throws Exception {
269 >        final ExecutorService e = new DirectExecutorService();
270 >        try (PoolCleaner cleaner = cleaner(e)) {
271 >            List<Callable<Long>> l = new ArrayList<Callable<Long>>();
272 >            l.add(new Callable<Long>() {
273 >                      public Long call() { throw new ArithmeticException(); }});
274              l.add(null);
275 <            e.invokeAny(l);
276 <        } catch (NullPointerException success) {
277 <        } catch(Exception ex) {
278 <            ex.printStackTrace();
439 <            unexpectedException();
440 <        } finally {
441 <            joinPool(e);
275 >            try {
276 >                e.invokeAny(l);
277 >                shouldThrow();
278 >            } catch (NullPointerException success) {}
279          }
280      }
281  
282      /**
283       * invokeAny(c) throws ExecutionException if no task in c completes
284       */
285 <    public void testInvokeAny4() {
286 <        ExecutorService e = new DirectExecutorService();
287 <        try {
288 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
285 >    public void testInvokeAny4() throws InterruptedException {
286 >        final ExecutorService e = new DirectExecutorService();
287 >        try (PoolCleaner cleaner = cleaner(e)) {
288 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
289              l.add(new NPETask());
290 <            e.invokeAny(l);
291 <        } catch(ExecutionException success) {
292 <        } catch(Exception ex) {
293 <            unexpectedException();
294 <        } finally {
295 <            joinPool(e);
290 >            try {
291 >                e.invokeAny(l);
292 >                shouldThrow();
293 >            } catch (ExecutionException success) {
294 >                assertTrue(success.getCause() instanceof NullPointerException);
295 >            }
296          }
297      }
298  
299      /**
300       * invokeAny(c) returns result of some task in c if at least one completes
301       */
302 <    public void testInvokeAny5() {
303 <        ExecutorService e = new DirectExecutorService();
304 <        try {
305 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
302 >    public void testInvokeAny5() throws Exception {
303 >        final ExecutorService e = new DirectExecutorService();
304 >        try (PoolCleaner cleaner = cleaner(e)) {
305 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
306              l.add(new StringTask());
307              l.add(new StringTask());
308              String result = e.invokeAny(l);
309              assertSame(TEST_STRING, result);
473        } catch (ExecutionException success) {
474        } catch(Exception ex) {
475            unexpectedException();
476        } finally {
477            joinPool(e);
310          }
311      }
312  
313      /**
314       * invokeAll(null) throws NPE
315       */
316 <    public void testInvokeAll1() {
317 <        ExecutorService e = new DirectExecutorService();
318 <        try {
319 <            e.invokeAll(null);
320 <        } catch (NullPointerException success) {
321 <        } catch(Exception ex) {
322 <            unexpectedException();
491 <        } finally {
492 <            joinPool(e);
316 >    public void testInvokeAll1() throws InterruptedException {
317 >        final ExecutorService e = new DirectExecutorService();
318 >        try (PoolCleaner cleaner = cleaner(e)) {
319 >            try {
320 >                e.invokeAll(null);
321 >                shouldThrow();
322 >            } catch (NullPointerException success) {}
323          }
324      }
325  
326      /**
327       * invokeAll(empty collection) returns empty collection
328       */
329 <    public void testInvokeAll2() {
330 <        ExecutorService e = new DirectExecutorService();
331 <        try {
329 >    public void testInvokeAll2() throws InterruptedException {
330 >        final ExecutorService e = new DirectExecutorService();
331 >        try (PoolCleaner cleaner = cleaner(e)) {
332              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
333              assertTrue(r.isEmpty());
504        } catch(Exception ex) {
505            unexpectedException();
506        } finally {
507            joinPool(e);
334          }
335      }
336  
337      /**
338       * invokeAll(c) throws NPE if c has null elements
339       */
340 <    public void testInvokeAll3() {
341 <        ExecutorService e = new DirectExecutorService();
342 <        try {
343 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
340 >    public void testInvokeAll3() throws InterruptedException {
341 >        final ExecutorService e = new DirectExecutorService();
342 >        try (PoolCleaner cleaner = cleaner(e)) {
343 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
344              l.add(new StringTask());
345              l.add(null);
346 <            e.invokeAll(l);
347 <        } catch (NullPointerException success) {
348 <        } catch(Exception ex) {
349 <            unexpectedException();
524 <        } finally {
525 <            joinPool(e);
346 >            try {
347 >                e.invokeAll(l);
348 >                shouldThrow();
349 >            } catch (NullPointerException success) {}
350          }
351      }
352  
353      /**
354       * get of returned element of invokeAll(c) throws exception on failed task
355       */
356 <    public void testInvokeAll4() {
357 <        ExecutorService e = new DirectExecutorService();
358 <        try {
359 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
356 >    public void testInvokeAll4() throws Exception {
357 >        final ExecutorService e = new DirectExecutorService();
358 >        try (PoolCleaner cleaner = cleaner(e)) {
359 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
360              l.add(new NPETask());
361 <            List<Future<String>> result = e.invokeAll(l);
362 <            assertEquals(1, result.size());
363 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
364 <                it.next().get();
365 <        } catch(ExecutionException success) {
366 <        } catch(Exception ex) {
367 <            unexpectedException();
368 <        } finally {
545 <            joinPool(e);
361 >            List<Future<String>> futures = e.invokeAll(l);
362 >            assertEquals(1, futures.size());
363 >            try {
364 >                futures.get(0).get();
365 >                shouldThrow();
366 >            } catch (ExecutionException success) {
367 >                assertTrue(success.getCause() instanceof NullPointerException);
368 >            }
369          }
370      }
371  
372      /**
373       * invokeAll(c) returns results of all completed tasks in c
374       */
375 <    public void testInvokeAll5() {
376 <        ExecutorService e = new DirectExecutorService();
377 <        try {
378 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
375 >    public void testInvokeAll5() throws Exception {
376 >        final ExecutorService e = new DirectExecutorService();
377 >        try (PoolCleaner cleaner = cleaner(e)) {
378 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
379              l.add(new StringTask());
380              l.add(new StringTask());
381 <            List<Future<String>> result = e.invokeAll(l);
382 <            assertEquals(2, result.size());
383 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
384 <                assertSame(TEST_STRING, it.next().get());
562 <        } catch (ExecutionException success) {
563 <        } catch(Exception ex) {
564 <            unexpectedException();
565 <        } finally {
566 <            joinPool(e);
381 >            List<Future<String>> futures = e.invokeAll(l);
382 >            assertEquals(2, futures.size());
383 >            for (Future<String> future : futures)
384 >                assertSame(TEST_STRING, future.get());
385          }
386      }
387  
570
388      /**
389       * timed invokeAny(null) throws NPE
390       */
391 <    public void testTimedInvokeAny1() {
392 <        ExecutorService e = new DirectExecutorService();
393 <        try {
394 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
395 <        } catch (NullPointerException success) {
396 <        } catch(Exception ex) {
397 <            unexpectedException();
581 <        } finally {
582 <            joinPool(e);
391 >    public void testTimedInvokeAny1() throws Exception {
392 >        final ExecutorService e = new DirectExecutorService();
393 >        try (PoolCleaner cleaner = cleaner(e)) {
394 >            try {
395 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
396 >                shouldThrow();
397 >            } catch (NullPointerException success) {}
398          }
399      }
400  
401      /**
402       * timed invokeAny(null time unit) throws NPE
403       */
404 <    public void testTimedInvokeAnyNullTimeUnit() {
405 <        ExecutorService e = new DirectExecutorService();
406 <        try {
407 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
404 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
405 >        final ExecutorService e = new DirectExecutorService();
406 >        try (PoolCleaner cleaner = cleaner(e)) {
407 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
408              l.add(new StringTask());
409 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
410 <        } catch (NullPointerException success) {
411 <        } catch(Exception ex) {
412 <            unexpectedException();
598 <        } finally {
599 <            joinPool(e);
409 >            try {
410 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
411 >                shouldThrow();
412 >            } catch (NullPointerException success) {}
413          }
414      }
415  
416      /**
417       * timed invokeAny(empty collection) throws IAE
418       */
419 <    public void testTimedInvokeAny2() {
420 <        ExecutorService e = new DirectExecutorService();
421 <        try {
422 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
423 <        } catch (IllegalArgumentException success) {
424 <        } catch(Exception ex) {
425 <            unexpectedException();
426 <        } finally {
614 <            joinPool(e);
419 >    public void testTimedInvokeAny2() throws Exception {
420 >        final ExecutorService e = new DirectExecutorService();
421 >        try (PoolCleaner cleaner = cleaner(e)) {
422 >            try {
423 >                e.invokeAny(new ArrayList<Callable<String>>(),
424 >                            MEDIUM_DELAY_MS, MILLISECONDS);
425 >                shouldThrow();
426 >            } catch (IllegalArgumentException success) {}
427          }
428      }
429  
430      /**
431       * timed invokeAny(c) throws NPE if c has null elements
432       */
433 <    public void testTimedInvokeAny3() {
434 <        ExecutorService e = new DirectExecutorService();
435 <        try {
436 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
437 <            l.add(new StringTask());
433 >    public void testTimedInvokeAny3() throws Exception {
434 >        final ExecutorService e = new DirectExecutorService();
435 >        try (PoolCleaner cleaner = cleaner(e)) {
436 >            List<Callable<Long>> l = new ArrayList<Callable<Long>>();
437 >            l.add(new Callable<Long>() {
438 >                      public Long call() { throw new ArithmeticException(); }});
439              l.add(null);
440 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
441 <        } catch (NullPointerException success) {
442 <        } catch(Exception ex) {
443 <            ex.printStackTrace();
631 <            unexpectedException();
632 <        } finally {
633 <            joinPool(e);
440 >            try {
441 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
442 >                shouldThrow();
443 >            } catch (NullPointerException success) {}
444          }
445      }
446  
447      /**
448       * timed invokeAny(c) throws ExecutionException if no task completes
449       */
450 <    public void testTimedInvokeAny4() {
451 <        ExecutorService e = new DirectExecutorService();
452 <        try {
453 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
450 >    public void testTimedInvokeAny4() throws Exception {
451 >        final ExecutorService e = new DirectExecutorService();
452 >        try (PoolCleaner cleaner = cleaner(e)) {
453 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
454              l.add(new NPETask());
455 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
456 <        } catch(ExecutionException success) {
457 <        } catch(Exception ex) {
458 <            unexpectedException();
459 <        } finally {
460 <            joinPool(e);
455 >            try {
456 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
457 >                shouldThrow();
458 >            } catch (ExecutionException success) {
459 >                assertTrue(success.getCause() instanceof NullPointerException);
460 >            }
461          }
462      }
463  
464      /**
465       * timed invokeAny(c) returns result of some task in c
466       */
467 <    public void testTimedInvokeAny5() {
468 <        ExecutorService e = new DirectExecutorService();
469 <        try {
470 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
467 >    public void testTimedInvokeAny5() throws Exception {
468 >        final ExecutorService e = new DirectExecutorService();
469 >        try (PoolCleaner cleaner = cleaner(e)) {
470 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
471              l.add(new StringTask());
472              l.add(new StringTask());
473 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
473 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
474              assertSame(TEST_STRING, result);
665        } catch (ExecutionException success) {
666        } catch(Exception ex) {
667            unexpectedException();
668        } finally {
669            joinPool(e);
475          }
476      }
477  
478      /**
479       * timed invokeAll(null) throws NPE
480       */
481 <    public void testTimedInvokeAll1() {
482 <        ExecutorService e = new DirectExecutorService();
483 <        try {
484 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
485 <        } catch (NullPointerException success) {
486 <        } catch(Exception ex) {
487 <            unexpectedException();
683 <        } finally {
684 <            joinPool(e);
481 >    public void testTimedInvokeAll1() throws InterruptedException {
482 >        final ExecutorService e = new DirectExecutorService();
483 >        try (PoolCleaner cleaner = cleaner(e)) {
484 >            try {
485 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
486 >                shouldThrow();
487 >            } catch (NullPointerException success) {}
488          }
489      }
490  
491      /**
492       * timed invokeAll(null time unit) throws NPE
493       */
494 <    public void testTimedInvokeAllNullTimeUnit() {
495 <        ExecutorService e = new DirectExecutorService();
496 <        try {
497 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
494 >    public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
495 >        final ExecutorService e = new DirectExecutorService();
496 >        try (PoolCleaner cleaner = cleaner(e)) {
497 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
498              l.add(new StringTask());
499 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
500 <        } catch (NullPointerException success) {
501 <        } catch(Exception ex) {
502 <            unexpectedException();
700 <        } finally {
701 <            joinPool(e);
499 >            try {
500 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
501 >                shouldThrow();
502 >            } catch (NullPointerException success) {}
503          }
504      }
505  
506      /**
507       * timed invokeAll(empty collection) returns empty collection
508       */
509 <    public void testTimedInvokeAll2() {
510 <        ExecutorService e = new DirectExecutorService();
511 <        try {
512 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
509 >    public void testTimedInvokeAll2() throws InterruptedException {
510 >        final ExecutorService e = new DirectExecutorService();
511 >        try (PoolCleaner cleaner = cleaner(e)) {
512 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
513              assertTrue(r.isEmpty());
713        } catch(Exception ex) {
714            unexpectedException();
715        } finally {
716            joinPool(e);
514          }
515      }
516  
517      /**
518       * timed invokeAll(c) throws NPE if c has null elements
519       */
520 <    public void testTimedInvokeAll3() {
521 <        ExecutorService e = new DirectExecutorService();
522 <        try {
523 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
520 >    public void testTimedInvokeAll3() throws InterruptedException {
521 >        final ExecutorService e = new DirectExecutorService();
522 >        try (PoolCleaner cleaner = cleaner(e)) {
523 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
524              l.add(new StringTask());
525              l.add(null);
526 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
527 <        } catch (NullPointerException success) {
528 <        } catch(Exception ex) {
529 <            unexpectedException();
733 <        } finally {
734 <            joinPool(e);
526 >            try {
527 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
528 >                shouldThrow();
529 >            } catch (NullPointerException success) {}
530          }
531      }
532  
533      /**
534       * get of returned element of invokeAll(c) throws exception on failed task
535       */
536 <    public void testTimedInvokeAll4() {
537 <        ExecutorService e = new DirectExecutorService();
538 <        try {
539 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
536 >    public void testTimedInvokeAll4() throws Exception {
537 >        final ExecutorService e = new DirectExecutorService();
538 >        try (PoolCleaner cleaner = cleaner(e)) {
539 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
540              l.add(new NPETask());
541 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
542 <            assertEquals(1, result.size());
543 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
544 <                it.next().get();
545 <        } catch(ExecutionException success) {
546 <        } catch(Exception ex) {
547 <            unexpectedException();
548 <        } finally {
549 <            joinPool(e);
541 >            List<Future<String>> futures =
542 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
543 >            assertEquals(1, futures.size());
544 >            try {
545 >                futures.get(0).get();
546 >                shouldThrow();
547 >            } catch (ExecutionException success) {
548 >                assertTrue(success.getCause() instanceof NullPointerException);
549 >            }
550          }
551      }
552  
553      /**
554       * timed invokeAll(c) returns results of all completed tasks in c
555       */
556 <    public void testTimedInvokeAll5() {
557 <        ExecutorService e = new DirectExecutorService();
558 <        try {
559 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
556 >    public void testTimedInvokeAll5() throws Exception {
557 >        final ExecutorService e = new DirectExecutorService();
558 >        try (PoolCleaner cleaner = cleaner(e)) {
559 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
560              l.add(new StringTask());
561              l.add(new StringTask());
562 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
563 <            assertEquals(2, result.size());
564 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
565 <                assertSame(TEST_STRING, it.next().get());
566 <        } catch (ExecutionException success) {
772 <        } catch(Exception ex) {
773 <            unexpectedException();
774 <        } finally {
775 <            joinPool(e);
562 >            List<Future<String>> futures =
563 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
564 >            assertEquals(2, futures.size());
565 >            for (Future<String> future : futures)
566 >                assertSame(TEST_STRING, future.get());
567          }
568      }
569  
570      /**
571       * timed invokeAll cancels tasks not completed by timeout
572       */
573 <    public void testTimedInvokeAll6() {
574 <        ExecutorService e = new DirectExecutorService();
575 <        try {
576 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
577 <            l.add(new StringTask());
578 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
579 <            l.add(new StringTask());
580 <            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
581 <            assertEquals(3, result.size());
582 <            Iterator<Future<String>> it = result.iterator();
583 <            Future<String> f1 = it.next();
584 <            Future<String> f2 = it.next();
585 <            Future<String> f3 = it.next();
586 <            assertTrue(f1.isDone());
587 <            assertFalse(f1.isCancelled());
588 <            assertTrue(f2.isDone());
589 <            assertTrue(f3.isDone());
590 <            assertTrue(f3.isCancelled());
591 <        } catch(Exception ex) {
592 <            unexpectedException();
593 <        } finally {
594 <            joinPool(e);
573 >    public void testTimedInvokeAll6() throws Exception {
574 >        final ExecutorService e = new DirectExecutorService();
575 >        try (PoolCleaner cleaner = cleaner(e)) {
576 >            for (long timeout = timeoutMillis();;) {
577 >                List<Callable<String>> tasks = new ArrayList<>();
578 >                tasks.add(new StringTask("0"));
579 >                tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout),
580 >                                             TEST_STRING));
581 >                tasks.add(new StringTask("2"));
582 >                long startTime = System.nanoTime();
583 >                List<Future<String>> futures =
584 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
585 >                assertEquals(tasks.size(), futures.size());
586 >                assertTrue(millisElapsedSince(startTime) >= timeout);
587 >                for (Future future : futures)
588 >                    assertTrue(future.isDone());
589 >                try {
590 >                    assertEquals("0", futures.get(0).get());
591 >                    assertEquals(TEST_STRING, futures.get(1).get());
592 >                } catch (CancellationException retryWithLongerTimeout) {
593 >                    // unusual delay before starting second task
594 >                    timeout *= 2;
595 >                    if (timeout >= LONG_DELAY_MS / 2)
596 >                        fail("expected exactly one task to be cancelled");
597 >                    continue;
598 >                }
599 >                assertTrue(futures.get(2).isCancelled());
600 >                break;
601 >            }
602          }
603      }
604  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines