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

Comparing jsr166/src/test/tck/ForkJoinPoolTest.java (file contents):
Revision 1.4 by jsr166, Sat Aug 1 21:56:02 2009 UTC vs.
Revision 1.64 by jsr166, Mon Oct 5 22:54:45 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
9  
10 < import junit.framework.*;
11 < import java.util.*;
12 < import java.util.concurrent.*;
13 < import java.util.concurrent.locks.*;
14 < import java.security.*;
10 > import java.security.PrivilegedAction;
11 > import java.security.PrivilegedExceptionAction;
12 > import java.util.ArrayList;
13 > import java.util.Collection;
14 > import java.util.List;
15 > import java.util.concurrent.Callable;
16 > import java.util.concurrent.CountDownLatch;
17 > import java.util.concurrent.ExecutionException;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.ForkJoinPool;
21 > import java.util.concurrent.ForkJoinTask;
22 > import java.util.concurrent.ForkJoinWorkerThread;
23 > import java.util.concurrent.Future;
24 > import java.util.concurrent.RecursiveTask;
25 > import java.util.concurrent.RejectedExecutionException;
26 > import java.util.concurrent.atomic.AtomicBoolean;
27 > import java.util.concurrent.locks.ReentrantLock;
28 >
29 > import junit.framework.AssertionFailedError;
30 > import junit.framework.Test;
31 > import junit.framework.TestSuite;
32  
33   public class ForkJoinPoolTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run (suite());
35 >        main(suite(), args);
36      }
37 +
38      public static Test suite() {
39          return new TestSuite(ForkJoinPoolTest.class);
40      }
41  
42 <    /**
42 >    /*
43       * Testing coverage notes:
44       *
45       * 1. shutdown and related methods are tested via super.joinPool.
# Line 38 | Line 58 | public class ForkJoinPoolTest extends JS
58      // Some classes to test extension and factory methods
59  
60      static class MyHandler implements Thread.UncaughtExceptionHandler {
61 <        int catches = 0;
61 >        volatile int catches = 0;
62          public void uncaughtException(Thread t, Throwable e) {
63              ++catches;
64          }
# Line 47 | Line 67 | public class ForkJoinPoolTest extends JS
67      // to test handlers
68      static class FailingFJWSubclass extends ForkJoinWorkerThread {
69          public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
70 <        protected void onStart() { throw new Error(); }
70 >        protected void onStart() { super.onStart(); throw new Error(); }
71      }
72  
73 <    static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
74 <        int calls = 0;
73 >    static class FailingThreadFactory
74 >            implements ForkJoinPool.ForkJoinWorkerThreadFactory {
75 >        volatile int calls = 0;
76          public ForkJoinWorkerThread newThread(ForkJoinPool p) {
77              if (++calls > 1) return null;
78              return new FailingFJWSubclass(p);
# Line 86 | Line 107 | public class ForkJoinPoolTest extends JS
107      static final class FibTask extends RecursiveTask<Integer> {
108          final int number;
109          FibTask(int n) { number = n; }
110 <        public Integer compute() {
110 >        protected Integer compute() {
111              int n = number;
112              if (n <= 1)
113                  return n;
# Line 114 | Line 135 | public class ForkJoinPoolTest extends JS
135              this.locker = locker;
136              this.lock = lock;
137          }
138 <        public Integer compute() {
138 >        protected Integer compute() {
139              int n;
140              LockingFibTask f1 = null;
141              LockingFibTask f2 = null;
# Line 135 | Line 156 | public class ForkJoinPoolTest extends JS
156      }
157  
158      /**
159 <     * Succesfully constructed pool reports default factory,
159 >     * Successfully constructed pool reports default factory,
160       * parallelism and async mode policies, no active threads or
161       * tasks, and quiescent running state.
162       */
163      public void testDefaultInitialState() {
164 <        ForkJoinPool p = null;
165 <        try {
166 <            p = new ForkJoinPool(1);
167 <            assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory);
147 <            assertTrue(p.isQuiescent());
148 <            assertTrue(p.getMaintainsParallelism());
164 >        ForkJoinPool p = new ForkJoinPool(1);
165 >        try (PoolCleaner cleaner = cleaner(p)) {
166 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
167 >                       p.getFactory());
168              assertFalse(p.getAsyncMode());
169 <            assertTrue(p.getActiveThreadCount() == 0);
170 <            assertTrue(p.getStealCount() == 0);
171 <            assertTrue(p.getQueuedTaskCount() == 0);
172 <            assertTrue(p.getQueuedSubmissionCount() == 0);
169 >            assertEquals(0, p.getActiveThreadCount());
170 >            assertEquals(0, p.getStealCount());
171 >            assertEquals(0, p.getQueuedTaskCount());
172 >            assertEquals(0, p.getQueuedSubmissionCount());
173              assertFalse(p.hasQueuedSubmissions());
174              assertFalse(p.isShutdown());
175              assertFalse(p.isTerminating());
176              assertFalse(p.isTerminated());
158        } finally {
159            joinPool(p);
177          }
178      }
179  
# Line 167 | Line 184 | public class ForkJoinPoolTest extends JS
184          try {
185              new ForkJoinPool(-1);
186              shouldThrow();
187 <        }
171 <        catch (IllegalArgumentException success) {}
187 >        } catch (IllegalArgumentException success) {}
188      }
189  
190      /**
# Line 176 | Line 192 | public class ForkJoinPoolTest extends JS
192       */
193      public void testConstructor2() {
194          try {
195 <            new ForkJoinPool(1, null);
195 >            new ForkJoinPool(1, null, null, false);
196              shouldThrow();
197 <        }
182 <        catch (NullPointerException success) {}
197 >        } catch (NullPointerException success) {}
198      }
199  
185
200      /**
201       * getParallelism returns size set in constructor
202       */
203      public void testGetParallelism() {
204 <        ForkJoinPool p = null;
205 <        try {
206 <            p = new ForkJoinPool(1);
193 <            assertTrue(p.getParallelism() == 1);
194 <        } finally {
195 <            joinPool(p);
196 <        }
197 <    }
198 <
199 <    /**
200 <     * setParallelism changes reported parallelism level.
201 <     */
202 <    public void testSetParallelism() {
203 <        ForkJoinPool p = null;
204 <        try {
205 <            p = new ForkJoinPool(1);
206 <            assertTrue(p.getParallelism() == 1);
207 <            p.setParallelism(2);
208 <            assertTrue(p.getParallelism() == 2);
209 <        } finally {
210 <            joinPool(p);
211 <        }
212 <    }
213 <
214 <    /**
215 <     * setParallelism with argument <= 0 throws exception
216 <     */
217 <    public void testSetParallelism2() {
218 <        ForkJoinPool p = null;
219 <        try {
220 <            p = new ForkJoinPool(1);
221 <            assertTrue(p.getParallelism() == 1);
222 <            p.setParallelism(-2);
223 <            shouldThrow();
224 <        } catch (IllegalArgumentException success) {
225 <        } finally {
226 <            joinPool(p);
204 >        ForkJoinPool p = new ForkJoinPool(1);
205 >        try (PoolCleaner cleaner = cleaner(p)) {
206 >            assertEquals(1, p.getParallelism());
207          }
208      }
209  
# Line 231 | Line 211 | public class ForkJoinPoolTest extends JS
211       * getPoolSize returns number of started workers.
212       */
213      public void testGetPoolSize() {
214 <        ForkJoinPool p = null;
215 <        try {
216 <            p = new ForkJoinPool(1);
237 <            assertTrue(p.getPoolSize() == 0);
214 >        ForkJoinPool p = new ForkJoinPool(1);
215 >        try (PoolCleaner cleaner = cleaner(p)) {
216 >            assertEquals(0, p.getActiveThreadCount());
217              Future<String> future = p.submit(new StringTask());
218 <            assertTrue(p.getPoolSize() == 1);
240 <
241 <        } finally {
242 <            joinPool(p);
218 >            assertEquals(1, p.getPoolSize());
219          }
220      }
221  
222      /**
223 <     * setMaximumPoolSize changes size reported by getMaximumPoolSize.
223 >     * awaitTermination on a non-shutdown pool times out
224       */
225 <    public void testSetMaximumPoolSize() {
226 <        ForkJoinPool p = null;
227 <        try {
228 <            p = new ForkJoinPool(1);
229 <            p.setMaximumPoolSize(2);
230 <            assertTrue(p.getMaximumPoolSize() == 2);
231 <        } finally {
232 <            joinPool(p);
233 <        }
234 <    }
235 <
236 <    /**
237 <     * setMaximumPoolSize with argument <= 0 throws exception
238 <     */
239 <    public void testSetMaximumPoolSize2() {
240 <        ForkJoinPool p = null;
241 <        try {
242 <            p = new ForkJoinPool(1);
243 <            p.setMaximumPoolSize(-2);
244 <            shouldThrow();
245 <        } catch (IllegalArgumentException success) {
246 <        } finally {
247 <            joinPool(p);
272 <        }
273 <    }
274 <
275 <    /**
276 <     * setMaintainsParallelism changes policy reported by
277 <     * getMaintainsParallelism.
278 <     */
279 <    public void testSetMaintainsParallelism() {
280 <        ForkJoinPool p = null;
281 <        try {
282 <            p = new ForkJoinPool(1);
283 <            p.setMaintainsParallelism(false);
284 <            assertFalse(p.getMaintainsParallelism());
285 <        } finally {
286 <            joinPool(p);
287 <        }
288 <    }
289 <
290 <    /**
291 <     * setAsyncMode changes policy reported by
292 <     * getAsyncMode.
293 <     */
294 <    public void testSetAsyncMode() {
295 <        ForkJoinPool p = null;
296 <        try {
297 <            p = new ForkJoinPool(1);
298 <            p.setAsyncMode(true);
299 <            assertTrue(p.getAsyncMode());
300 <        } finally {
301 <            joinPool(p);
225 >    public void testAwaitTermination_timesOut() throws InterruptedException {
226 >        ForkJoinPool p = new ForkJoinPool(1);
227 >        try (PoolCleaner cleaner = cleaner(p)) {
228 >            assertFalse(p.isTerminated());
229 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
230 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
231 >            assertFalse(p.awaitTermination(-1L, NANOSECONDS));
232 >            assertFalse(p.awaitTermination(-1L, MILLISECONDS));
233 >            assertFalse(p.awaitTermination(0L, NANOSECONDS));
234 >            assertFalse(p.awaitTermination(0L, MILLISECONDS));
235 >            long timeoutNanos = 999999L;
236 >            long startTime = System.nanoTime();
237 >            assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
238 >            assertTrue(System.nanoTime() - startTime >= timeoutNanos);
239 >            assertFalse(p.isTerminated());
240 >            startTime = System.nanoTime();
241 >            long timeoutMillis = timeoutMillis();
242 >            assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
243 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
244 >            assertFalse(p.isTerminated());
245 >            p.shutdown();
246 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
247 >            assertTrue(p.isTerminated());
248          }
249      }
250  
# Line 308 | Line 254 | public class ForkJoinPoolTest extends JS
254       * Additionally tests: Overriding ForkJoinWorkerThread.onStart
255       * performs its defined action
256       */
257 <    public void testSetUncaughtExceptionHandler() {
258 <        ForkJoinPool p = null;
257 >    public void testSetUncaughtExceptionHandler() throws InterruptedException {
258 >        final CountDownLatch uehInvoked = new CountDownLatch(1);
259 >        final Thread.UncaughtExceptionHandler eh =
260 >            new Thread.UncaughtExceptionHandler() {
261 >                public void uncaughtException(Thread t, Throwable e) {
262 >                    uehInvoked.countDown();
263 >                }};
264 >        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
265 >                                          eh, false);
266          try {
267 <            p = new ForkJoinPool(1, new FailingThreadFactory());
268 <            MyHandler eh = new MyHandler();
269 <            p.setUncaughtExceptionHandler(eh);
270 <            assertEquals(eh, p.getUncaughtExceptionHandler());
271 <            p.execute(new FailingTask());
272 <            Thread.sleep(MEDIUM_DELAY_MS);
320 <            assertTrue(eh.catches > 0);
321 <        } catch (InterruptedException e) {
322 <            unexpectedException();
267 >            assertSame(eh, p.getUncaughtExceptionHandler());
268 >            try {
269 >                p.execute(new FibTask(8));
270 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
271 >            } catch (RejectedExecutionException ok) {
272 >            }
273          } finally {
274 +            p.shutdownNow(); // failure might have prevented processing task
275              joinPool(p);
276          }
277      }
278  
279      /**
280 <     * setUncaughtExceptionHandler of null removes handler
280 >     * After invoking a single task, isQuiescent eventually becomes
281 >     * true, at which time queues are empty, threads are not active,
282 >     * the task has completed successfully, and construction
283 >     * parameters continue to hold
284       */
285 <    public void testSetUncaughtExceptionHandler2() {
286 <        ForkJoinPool p = null;
287 <        try {
288 <            p = new ForkJoinPool(1);
289 <            p.setUncaughtExceptionHandler(null);
290 <            assertNull(p.getUncaughtExceptionHandler());
291 <        } finally {
292 <            joinPool(p);
293 <        }
294 <    }
295 <
285 >    public void testIsQuiescent() throws Exception {
286 >        ForkJoinPool p = new ForkJoinPool(2);
287 >        try (PoolCleaner cleaner = cleaner(p)) {
288 >            assertTrue(p.isQuiescent());
289 >            long startTime = System.nanoTime();
290 >            FibTask f = new FibTask(20);
291 >            p.invoke(f);
292 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
293 >                       p.getFactory());
294 >            while (! p.isQuiescent()) {
295 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
296 >                    throw new AssertionFailedError("timed out");
297 >                assertFalse(p.getAsyncMode());
298 >                assertFalse(p.isShutdown());
299 >                assertFalse(p.isTerminating());
300 >                assertFalse(p.isTerminated());
301 >                Thread.yield();
302 >            }
303  
343    /**
344     * After invoking a single task, isQuiescent is true,
345     * queues are empty, threads are not active, and
346     * construction parameters continue to hold
347     */
348    public void testisQuiescent() {
349        ForkJoinPool p = null;
350        try {
351            p = new ForkJoinPool(2);
352            p.invoke(new FibTask(20));
353            assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory);
354            Thread.sleep(MEDIUM_DELAY_MS);
304              assertTrue(p.isQuiescent());
356            assertTrue(p.getMaintainsParallelism());
305              assertFalse(p.getAsyncMode());
306 <            assertTrue(p.getActiveThreadCount() == 0);
307 <            assertTrue(p.getQueuedTaskCount() == 0);
308 <            assertTrue(p.getQueuedSubmissionCount() == 0);
306 >            assertEquals(0, p.getActiveThreadCount());
307 >            assertEquals(0, p.getQueuedTaskCount());
308 >            assertEquals(0, p.getQueuedSubmissionCount());
309              assertFalse(p.hasQueuedSubmissions());
310              assertFalse(p.isShutdown());
311              assertFalse(p.isTerminating());
312              assertFalse(p.isTerminated());
313 <        } catch (InterruptedException e) {
314 <            unexpectedException();
367 <        } finally {
368 <            joinPool(p);
313 >            assertTrue(f.isDone());
314 >            assertEquals(6765, (int) f.get());
315          }
316      }
317  
318      /**
319       * Completed submit(ForkJoinTask) returns result
320       */
321 <    public void testSubmitForkJoinTask() {
322 <        ForkJoinPool p = null;
323 <        try {
378 <            p = new ForkJoinPool(1);
321 >    public void testSubmitForkJoinTask() throws Throwable {
322 >        ForkJoinPool p = new ForkJoinPool(1);
323 >        try (PoolCleaner cleaner = cleaner(p)) {
324              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
325 <            int r = f.get();
381 <            assertTrue(r == 21);
382 <        } catch (ExecutionException ex) {
383 <            unexpectedException();
384 <        } catch (InterruptedException ex) {
385 <            unexpectedException();
386 <        } finally {
387 <            joinPool(p);
325 >            assertEquals(21, (int) f.get());
326          }
327      }
328  
# Line 392 | Line 330 | public class ForkJoinPoolTest extends JS
330       * A task submitted after shutdown is rejected
331       */
332      public void testSubmitAfterShutdown() {
333 <        ForkJoinPool p = null;
334 <        try {
397 <            p = new ForkJoinPool(1);
333 >        ForkJoinPool p = new ForkJoinPool(1);
334 >        try (PoolCleaner cleaner = cleaner(p)) {
335              p.shutdown();
336              assertTrue(p.isShutdown());
337 <            ForkJoinTask<Integer> f = p.submit(new FibTask(8));
338 <            shouldThrow();
339 <        } catch (RejectedExecutionException success) {
340 <        } finally {
404 <            joinPool(p);
337 >            try {
338 >                ForkJoinTask<Integer> f = p.submit(new FibTask(8));
339 >                shouldThrow();
340 >            } catch (RejectedExecutionException success) {}
341          }
342      }
343  
344      /**
345       * Pool maintains parallelism when using ManagedBlocker
346       */
347 <    public void testBlockingForkJoinTask() {
348 <        ForkJoinPool p = null;
347 >    public void testBlockingForkJoinTask() throws Throwable {
348 >        ForkJoinPool p = new ForkJoinPool(4);
349          try {
414            p = new ForkJoinPool(4);
350              ReentrantLock lock = new ReentrantLock();
351              ManagedLocker locker = new ManagedLocker(lock);
352 <            ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
352 >            ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
353              p.execute(f);
354 <            assertTrue(p.getPoolSize() >= 4);
420 <            int r = f.get();
421 <            assertTrue(r ==  832040);
422 <        } catch (ExecutionException ex) {
423 <            unexpectedException();
424 <        } catch (InterruptedException ex) {
425 <            unexpectedException();
354 >            assertEquals(6765, (int) f.get());
355          } finally {
356 <            joinPool(p);
356 >            p.shutdownNow(); // don't wait out shutdown
357          }
358      }
359  
# Line 432 | Line 361 | public class ForkJoinPoolTest extends JS
361       * pollSubmission returns unexecuted submitted task, if present
362       */
363      public void testPollSubmission() {
364 <        SubFJP p = null;
365 <        try {
366 <            p = new SubFJP();
367 <            ForkJoinTask a = p.submit(new MediumRunnable());
368 <            ForkJoinTask b = p.submit(new MediumRunnable());
369 <            ForkJoinTask c = p.submit(new MediumRunnable());
364 >        final CountDownLatch done = new CountDownLatch(1);
365 >        SubFJP p = new SubFJP();
366 >        try (PoolCleaner cleaner = cleaner(p)) {
367 >            ForkJoinTask a = p.submit(awaiter(done));
368 >            ForkJoinTask b = p.submit(awaiter(done));
369 >            ForkJoinTask c = p.submit(awaiter(done));
370              ForkJoinTask r = p.pollSubmission();
371              assertTrue(r == a || r == b || r == c);
372              assertFalse(r.isDone());
373 <        } finally {
445 <            joinPool(p);
373 >            done.countDown();
374          }
375      }
376  
# Line 450 | Line 378 | public class ForkJoinPoolTest extends JS
378       * drainTasksTo transfers unexecuted submitted tasks, if present
379       */
380      public void testDrainTasksTo() {
381 <        SubFJP p = null;
382 <        try {
383 <            p = new SubFJP();
384 <            ForkJoinTask a = p.submit(new MediumRunnable());
385 <            ForkJoinTask b = p.submit(new MediumRunnable());
386 <            ForkJoinTask c = p.submit(new MediumRunnable());
381 >        final CountDownLatch done = new CountDownLatch(1);
382 >        SubFJP p = new SubFJP();
383 >        try (PoolCleaner cleaner = cleaner(p)) {
384 >            ForkJoinTask a = p.submit(awaiter(done));
385 >            ForkJoinTask b = p.submit(awaiter(done));
386 >            ForkJoinTask c = p.submit(awaiter(done));
387              ArrayList<ForkJoinTask> al = new ArrayList();
388              p.drainTasksTo(al);
389              assertTrue(al.size() > 0);
# Line 463 | Line 391 | public class ForkJoinPoolTest extends JS
391                  assertTrue(r == a || r == b || r == c);
392                  assertFalse(r.isDone());
393              }
394 <        } finally {
467 <            joinPool(p);
394 >            done.countDown();
395          }
396      }
397  
471
398      // FJ Versions of AbstractExecutorService tests
399  
400      /**
401       * execute(runnable) runs it to completion
402       */
403 <    public void testExecuteRunnable() {
404 <        try {
405 <            ExecutorService e = new ForkJoinPool(1);
406 <            TrackedShortRunnable task = new TrackedShortRunnable();
407 <            assertFalse(task.done);
408 <            Future<?> future = e.submit(task);
409 <            future.get();
410 <            assertTrue(task.done);
411 <        }
412 <        catch (ExecutionException ex) {
413 <            unexpectedException();
414 <        }
415 <        catch (InterruptedException ex) {
490 <            unexpectedException();
403 >    public void testExecuteRunnable() throws Throwable {
404 >        ExecutorService e = new ForkJoinPool(1);
405 >        try (PoolCleaner cleaner = cleaner(e)) {
406 >            final AtomicBoolean done = new AtomicBoolean(false);
407 >            Future<?> future = e.submit(new CheckedRunnable() {
408 >                public void realRun() {
409 >                    done.set(true);
410 >                }});
411 >            assertNull(future.get());
412 >            assertNull(future.get(0, MILLISECONDS));
413 >            assertTrue(done.get());
414 >            assertTrue(future.isDone());
415 >            assertFalse(future.isCancelled());
416          }
417      }
418  
494
419      /**
420       * Completed submit(callable) returns result
421       */
422 <    public void testSubmitCallable() {
423 <        try {
424 <            ExecutorService e = new ForkJoinPool(1);
422 >    public void testSubmitCallable() throws Throwable {
423 >        ExecutorService e = new ForkJoinPool(1);
424 >        try (PoolCleaner cleaner = cleaner(e)) {
425              Future<String> future = e.submit(new StringTask());
426 <            String result = future.get();
427 <            assertSame(TEST_STRING, result);
428 <        }
505 <        catch (ExecutionException ex) {
506 <            unexpectedException();
507 <        }
508 <        catch (InterruptedException ex) {
509 <            unexpectedException();
426 >            assertSame(TEST_STRING, future.get());
427 >            assertTrue(future.isDone());
428 >            assertFalse(future.isCancelled());
429          }
430      }
431  
432      /**
433       * Completed submit(runnable) returns successfully
434       */
435 <    public void testSubmitRunnable() {
436 <        try {
437 <            ExecutorService e = new ForkJoinPool(1);
435 >    public void testSubmitRunnable() throws Throwable {
436 >        ExecutorService e = new ForkJoinPool(1);
437 >        try (PoolCleaner cleaner = cleaner(e)) {
438              Future<?> future = e.submit(new NoOpRunnable());
439 <            future.get();
439 >            assertNull(future.get());
440              assertTrue(future.isDone());
441 <        }
523 <        catch (ExecutionException ex) {
524 <            unexpectedException();
525 <        }
526 <        catch (InterruptedException ex) {
527 <            unexpectedException();
441 >            assertFalse(future.isCancelled());
442          }
443      }
444  
445      /**
446       * Completed submit(runnable, result) returns result
447       */
448 <    public void testSubmitRunnable2() {
449 <        try {
450 <            ExecutorService e = new ForkJoinPool(1);
448 >    public void testSubmitRunnable2() throws Throwable {
449 >        ExecutorService e = new ForkJoinPool(1);
450 >        try (PoolCleaner cleaner = cleaner(e)) {
451              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
452 <            String result = future.get();
453 <            assertSame(TEST_STRING, result);
454 <        }
541 <        catch (ExecutionException ex) {
542 <            unexpectedException();
543 <        }
544 <        catch (InterruptedException ex) {
545 <            unexpectedException();
452 >            assertSame(TEST_STRING, future.get());
453 >            assertTrue(future.isDone());
454 >            assertFalse(future.isCancelled());
455          }
456      }
457  
549
458      /**
459 <     * A submitted privileged action to completion
459 >     * A submitted privileged action runs to completion
460       */
461 <    public void testSubmitPrivilegedAction() {
462 <        Policy savedPolicy = null;
463 <        try {
464 <            savedPolicy = Policy.getPolicy();
465 <            AdjustablePolicy policy = new AdjustablePolicy();
558 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
559 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
560 <            Policy.setPolicy(policy);
561 <        } catch (AccessControlException ok) {
562 <            return;
563 <        }
564 <        try {
461 >    public void testSubmitPrivilegedAction() throws Exception {
462 >        final Callable callable = Executors.callable(new PrivilegedAction() {
463 >                public Object run() { return TEST_STRING; }});
464 >        Runnable r = new CheckedRunnable() {
465 >        public void realRun() throws Exception {
466              ExecutorService e = new ForkJoinPool(1);
467 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
468 <                    public Object run() {
469 <                        return TEST_STRING;
569 <                    }}));
570 <
571 <            Object result = future.get();
572 <            assertSame(TEST_STRING, result);
573 <        }
574 <        catch (ExecutionException ex) {
575 <            unexpectedException();
576 <        }
577 <        catch (InterruptedException ex) {
578 <            unexpectedException();
579 <        }
580 <        finally {
581 <            try {
582 <                Policy.setPolicy(savedPolicy);
583 <            } catch (AccessControlException ok) {
584 <                return;
467 >            try (PoolCleaner cleaner = cleaner(e)) {
468 >                Future future = e.submit(callable);
469 >                assertSame(TEST_STRING, future.get());
470              }
471 <        }
471 >        }};
472 >
473 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
474      }
475  
476      /**
477 <     * A submitted a privileged exception action runs to completion
477 >     * A submitted privileged exception action runs to completion
478       */
479 <    public void testSubmitPrivilegedExceptionAction() {
480 <        Policy savedPolicy = null;
481 <        try {
482 <            savedPolicy = Policy.getPolicy();
483 <            AdjustablePolicy policy = new AdjustablePolicy();
484 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
598 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
599 <            Policy.setPolicy(policy);
600 <        } catch (AccessControlException ok) {
601 <            return;
602 <        }
603 <
604 <        try {
479 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
480 >        final Callable callable =
481 >            Executors.callable(new PrivilegedExceptionAction() {
482 >                public Object run() { return TEST_STRING; }});
483 >        Runnable r = new CheckedRunnable() {
484 >        public void realRun() throws Exception {
485              ExecutorService e = new ForkJoinPool(1);
486 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
487 <                    public Object run() {
488 <                        return TEST_STRING;
489 <                    }}));
486 >            try (PoolCleaner cleaner = cleaner(e)) {
487 >                Future future = e.submit(callable);
488 >                assertSame(TEST_STRING, future.get());
489 >            }
490 >        }};
491  
492 <            Object result = future.get();
612 <            assertSame(TEST_STRING, result);
613 <        }
614 <        catch (ExecutionException ex) {
615 <            unexpectedException();
616 <        }
617 <        catch (InterruptedException ex) {
618 <            unexpectedException();
619 <        }
620 <        finally {
621 <            Policy.setPolicy(savedPolicy);
622 <        }
492 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
493      }
494  
495      /**
496       * A submitted failed privileged exception action reports exception
497       */
498 <    public void testSubmitFailedPrivilegedExceptionAction() {
499 <        Policy savedPolicy = null;
500 <        try {
501 <            savedPolicy = Policy.getPolicy();
502 <            AdjustablePolicy policy = new AdjustablePolicy();
503 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
634 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
635 <            Policy.setPolicy(policy);
636 <        } catch (AccessControlException ok) {
637 <            return;
638 <        }
639 <
640 <
641 <        try {
498 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
499 >        final Callable callable =
500 >            Executors.callable(new PrivilegedExceptionAction() {
501 >                public Object run() { throw new IndexOutOfBoundsException(); }});
502 >        Runnable r = new CheckedRunnable() {
503 >        public void realRun() throws Exception {
504              ExecutorService e = new ForkJoinPool(1);
505 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
506 <                    public Object run() throws Exception {
507 <                        throw new IndexOutOfBoundsException();
508 <                    }}));
505 >            try (PoolCleaner cleaner = cleaner(e)) {
506 >                Future future = e.submit(callable);
507 >                try {
508 >                    future.get();
509 >                    shouldThrow();
510 >                } catch (ExecutionException success) {
511 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
512 >                }
513 >            }
514 >        }};
515  
516 <            Object result = future.get();
649 <            shouldThrow();
650 <        }
651 <        catch (ExecutionException success) {
652 <        } catch (CancellationException success) {
653 <        } catch (InterruptedException ex) {
654 <            unexpectedException();
655 <        }
656 <        finally {
657 <            Policy.setPolicy(savedPolicy);
658 <        }
516 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
517      }
518  
519      /**
520 <     * execute(null runnable) throws NPE
520 >     * execute(null runnable) throws NullPointerException
521       */
522      public void testExecuteNullRunnable() {
523 <        try {
524 <            ExecutorService e = new ForkJoinPool(1);
525 <            TrackedShortRunnable task = null;
526 <            Future<?> future = e.submit(task);
527 <            shouldThrow();
528 <        }
671 <        catch (NullPointerException success) {
672 <        }
673 <        catch (Exception ex) {
674 <            unexpectedException();
523 >        ExecutorService e = new ForkJoinPool(1);
524 >        try (PoolCleaner cleaner = cleaner(e)) {
525 >            try {
526 >                Future<?> future = e.submit((Runnable) null);
527 >                shouldThrow();
528 >            } catch (NullPointerException success) {}
529          }
530      }
531  
678
532      /**
533 <     * submit(null callable) throws NPE
533 >     * submit(null callable) throws NullPointerException
534       */
535      public void testSubmitNullCallable() {
536 <        try {
537 <            ExecutorService e = new ForkJoinPool(1);
538 <            StringTask t = null;
539 <            Future<String> future = e.submit(t);
540 <            shouldThrow();
541 <        }
689 <        catch (NullPointerException success) {
690 <        }
691 <        catch (Exception ex) {
692 <            unexpectedException();
536 >        ExecutorService e = new ForkJoinPool(1);
537 >        try (PoolCleaner cleaner = cleaner(e)) {
538 >            try {
539 >                Future<String> future = e.submit((Callable) null);
540 >                shouldThrow();
541 >            } catch (NullPointerException success) {}
542          }
543      }
544  
696
545      /**
546 <     * Blocking on submit(callable) throws InterruptedException if
547 <     * caller interrupted.
548 <     */
549 <    public void testInterruptedSubmit() {
550 <        final ForkJoinPool p = new ForkJoinPool(1);
551 <        Thread t = new Thread(new Runnable() {
552 <                public void run() {
553 <                    try {
554 <                        p.submit(new Callable<Object>() {
555 <                                public Object call() {
556 <                                    try {
557 <                                        Thread.sleep(MEDIUM_DELAY_MS);
558 <                                        shouldThrow();
559 <                                    } catch (InterruptedException e) {
560 <                                    }
561 <                                    return null;
562 <                                }
563 <                            }).get();
716 <                    } catch (InterruptedException success) {
717 <                    } catch (Exception e) {
718 <                        unexpectedException();
719 <                    }
720 <
721 <                }
722 <            });
723 <        try {
546 >     * submit(callable).get() throws InterruptedException if interrupted
547 >     */
548 >    public void testInterruptedSubmit() throws InterruptedException {
549 >        final CountDownLatch submitted    = new CountDownLatch(1);
550 >        final CountDownLatch quittingTime = new CountDownLatch(1);
551 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
552 >            public Void realCall() throws InterruptedException {
553 >                assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
554 >                return null;
555 >            }};
556 >        final ExecutorService p = new ForkJoinPool(1);
557 >        try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
558 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
559 >                public void realRun() throws Exception {
560 >                    Future<Void> future = p.submit(awaiter);
561 >                    submitted.countDown();
562 >                    future.get();
563 >                }});
564              t.start();
565 <            Thread.sleep(SHORT_DELAY_MS);
565 >            await(submitted);
566              t.interrupt();
567 <        } catch (Exception e) {
728 <            unexpectedException();
567 >            awaitTermination(t);
568          }
730        joinPool(p);
569      }
570  
571      /**
572       * get of submit(callable) throws ExecutionException if callable
573       * throws exception
574       */
575 <    public void testSubmitEE() {
575 >    public void testSubmitEE() throws Throwable {
576          ForkJoinPool p = new ForkJoinPool(1);
577 <
578 <        try {
579 <            Callable c = new Callable() {
580 <                    public Object call() {
581 <                        int i = 5/0;
582 <                        return Boolean.TRUE;
583 <                    }
584 <                };
747 <
748 <            for (int i = 0; i < 5; i++) {
749 <                p.submit(c).get();
577 >        try (PoolCleaner cleaner = cleaner(p)) {
578 >            try {
579 >                p.submit(new Callable() {
580 >                        public Object call() { throw new ArithmeticException(); }})
581 >                    .get();
582 >                shouldThrow();
583 >            } catch (ExecutionException success) {
584 >                assertTrue(success.getCause() instanceof ArithmeticException);
585              }
586 +        }
587 +    }
588  
589 <            shouldThrow();
590 <        } catch (ExecutionException success) {
591 <        } catch (CancellationException success) {
592 <        } catch (Exception e) {
593 <            unexpectedException();
589 >    /**
590 >     * invokeAny(null) throws NullPointerException
591 >     */
592 >    public void testInvokeAny1() throws Throwable {
593 >        ExecutorService e = new ForkJoinPool(1);
594 >        try (PoolCleaner cleaner = cleaner(e)) {
595 >            try {
596 >                e.invokeAny(null);
597 >                shouldThrow();
598 >            } catch (NullPointerException success) {}
599          }
758        joinPool(p);
600      }
601  
602      /**
603 <     * invokeAny(null) throws NPE
603 >     * invokeAny(empty collection) throws IllegalArgumentException
604       */
605 <    public void testInvokeAny1() {
605 >    public void testInvokeAny2() throws Throwable {
606          ExecutorService e = new ForkJoinPool(1);
607 <        try {
608 <            e.invokeAny(null);
609 <        } catch (NullPointerException success) {
610 <        } catch (Exception ex) {
611 <            unexpectedException();
771 <        } finally {
772 <            joinPool(e);
607 >        try (PoolCleaner cleaner = cleaner(e)) {
608 >            try {
609 >                e.invokeAny(new ArrayList<Callable<String>>());
610 >                shouldThrow();
611 >            } catch (IllegalArgumentException success) {}
612          }
613      }
614  
615      /**
616 <     * invokeAny(empty collection) throws IAE
616 >     * invokeAny(c) throws NullPointerException if c has a single null element
617       */
618 <    public void testInvokeAny2() {
618 >    public void testInvokeAny3() throws Throwable {
619          ExecutorService e = new ForkJoinPool(1);
620 <        try {
621 <            e.invokeAny(new ArrayList<Callable<String>>());
622 <        } catch (IllegalArgumentException success) {
623 <        } catch (Exception ex) {
624 <            unexpectedException();
625 <        } finally {
626 <            joinPool(e);
620 >        try (PoolCleaner cleaner = cleaner(e)) {
621 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
622 >            l.add(null);
623 >            try {
624 >                e.invokeAny(l);
625 >                shouldThrow();
626 >            } catch (NullPointerException success) {}
627          }
628      }
629  
630      /**
631 <     * invokeAny(c) throws NPE if c has null elements
631 >     * invokeAny(c) throws NullPointerException if c has null elements
632       */
633 <    public void testInvokeAny3() {
633 >    public void testInvokeAny4() throws Throwable {
634 >        CountDownLatch latch = new CountDownLatch(1);
635          ExecutorService e = new ForkJoinPool(1);
636 <        try {
637 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
638 <            l.add(new StringTask());
636 >        try (PoolCleaner cleaner = cleaner(e)) {
637 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
638 >            l.add(latchAwaitingStringTask(latch));
639              l.add(null);
640 <            e.invokeAny(l);
641 <        } catch (NullPointerException success) {
642 <        } catch (Exception ex) {
643 <            ex.printStackTrace();
644 <            unexpectedException();
805 <        } finally {
806 <            joinPool(e);
640 >            try {
641 >                e.invokeAny(l);
642 >                shouldThrow();
643 >            } catch (NullPointerException success) {}
644 >            latch.countDown();
645          }
646      }
647  
648      /**
649       * invokeAny(c) throws ExecutionException if no task in c completes
650       */
651 <    public void testInvokeAny4() {
651 >    public void testInvokeAny5() throws Throwable {
652          ExecutorService e = new ForkJoinPool(1);
653 <        try {
654 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
653 >        try (PoolCleaner cleaner = cleaner(e)) {
654 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
655              l.add(new NPETask());
656 <            e.invokeAny(l);
657 <        } catch (ExecutionException success) {
658 <        } catch (CancellationException success) {
659 <        } catch (Exception ex) {
660 <            unexpectedException();
661 <        } finally {
824 <            joinPool(e);
656 >            try {
657 >                e.invokeAny(l);
658 >                shouldThrow();
659 >            } catch (ExecutionException success) {
660 >                assertTrue(success.getCause() instanceof NullPointerException);
661 >            }
662          }
663      }
664  
665      /**
666       * invokeAny(c) returns result of some task in c if at least one completes
667       */
668 <    public void testInvokeAny5() {
668 >    public void testInvokeAny6() throws Throwable {
669          ExecutorService e = new ForkJoinPool(1);
670 <        try {
671 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
670 >        try (PoolCleaner cleaner = cleaner(e)) {
671 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
672              l.add(new StringTask());
673              l.add(new StringTask());
674              String result = e.invokeAny(l);
675              assertSame(TEST_STRING, result);
839        } catch (ExecutionException success) {
840        } catch (CancellationException success) {
841        } catch (Exception ex) {
842            unexpectedException();
843        } finally {
844            joinPool(e);
676          }
677      }
678  
679      /**
680 <     * invokeAll(null) throws NPE
680 >     * invokeAll(null) throws NullPointerException
681       */
682 <    public void testInvokeAll1() {
682 >    public void testInvokeAll1() throws Throwable {
683          ExecutorService e = new ForkJoinPool(1);
684 <        try {
685 <            e.invokeAll(null);
686 <        } catch (NullPointerException success) {
687 <        } catch (Exception ex) {
688 <            unexpectedException();
858 <        } finally {
859 <            joinPool(e);
684 >        try (PoolCleaner cleaner = cleaner(e)) {
685 >            try {
686 >                e.invokeAll(null);
687 >                shouldThrow();
688 >            } catch (NullPointerException success) {}
689          }
690      }
691  
692      /**
693       * invokeAll(empty collection) returns empty collection
694       */
695 <    public void testInvokeAll2() {
695 >    public void testInvokeAll2() throws InterruptedException {
696          ExecutorService e = new ForkJoinPool(1);
697 <        try {
698 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
697 >        try (PoolCleaner cleaner = cleaner(e)) {
698 >            List<Future<String>> r
699 >                = e.invokeAll(new ArrayList<Callable<String>>());
700              assertTrue(r.isEmpty());
871        } catch (Exception ex) {
872            unexpectedException();
873        } finally {
874            joinPool(e);
701          }
702      }
703  
704      /**
705 <     * invokeAll(c) throws NPE if c has null elements
705 >     * invokeAll(c) throws NullPointerException if c has null elements
706       */
707 <    public void testInvokeAll3() {
707 >    public void testInvokeAll3() throws InterruptedException {
708          ExecutorService e = new ForkJoinPool(1);
709 <        try {
710 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
709 >        try (PoolCleaner cleaner = cleaner(e)) {
710 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
711              l.add(new StringTask());
712              l.add(null);
713 <            e.invokeAll(l);
714 <        } catch (NullPointerException success) {
715 <        } catch (Exception ex) {
716 <            unexpectedException();
891 <        } finally {
892 <            joinPool(e);
713 >            try {
714 >                e.invokeAll(l);
715 >                shouldThrow();
716 >            } catch (NullPointerException success) {}
717          }
718      }
719  
720      /**
721 <     * get of returned element of invokeAll(c) throws exception on failed task
721 >     * get of returned element of invokeAll(c) throws
722 >     * ExecutionException on failed task
723       */
724 <    public void testInvokeAll4() {
724 >    public void testInvokeAll4() throws Throwable {
725          ExecutorService e = new ForkJoinPool(1);
726 <        try {
727 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
726 >        try (PoolCleaner cleaner = cleaner(e)) {
727 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
728              l.add(new NPETask());
729 <            List<Future<String>> result = e.invokeAll(l);
730 <            assertEquals(1, result.size());
731 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
732 <                it.next().get();
733 <        } catch (ExecutionException success) {
734 <        } catch (CancellationException success) {
735 <        } catch (Exception ex) {
736 <            ex.printStackTrace();
912 <            unexpectedException();
913 <        } finally {
914 <            joinPool(e);
729 >            List<Future<String>> futures = e.invokeAll(l);
730 >            assertEquals(1, futures.size());
731 >            try {
732 >                futures.get(0).get();
733 >                shouldThrow();
734 >            } catch (ExecutionException success) {
735 >                assertTrue(success.getCause() instanceof NullPointerException);
736 >            }
737          }
738      }
739  
740      /**
741       * invokeAll(c) returns results of all completed tasks in c
742       */
743 <    public void testInvokeAll5() {
743 >    public void testInvokeAll5() throws Throwable {
744          ExecutorService e = new ForkJoinPool(1);
745 <        try {
746 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
745 >        try (PoolCleaner cleaner = cleaner(e)) {
746 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
747              l.add(new StringTask());
748              l.add(new StringTask());
749 <            List<Future<String>> result = e.invokeAll(l);
750 <            assertEquals(2, result.size());
751 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
752 <                assertSame(TEST_STRING, it.next().get());
931 <        } catch (ExecutionException success) {
932 <        } catch (CancellationException success) {
933 <        } catch (Exception ex) {
934 <            ex.printStackTrace();
935 <            unexpectedException();
936 <        } finally {
937 <            joinPool(e);
749 >            List<Future<String>> futures = e.invokeAll(l);
750 >            assertEquals(2, futures.size());
751 >            for (Future<String> future : futures)
752 >                assertSame(TEST_STRING, future.get());
753          }
754      }
755  
941
756      /**
757 <     * timed invokeAny(null) throws NPE
757 >     * timed invokeAny(null) throws NullPointerException
758       */
759 <    public void testTimedInvokeAny1() {
759 >    public void testTimedInvokeAny1() throws Throwable {
760          ExecutorService e = new ForkJoinPool(1);
761 <        try {
762 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
763 <        } catch (NullPointerException success) {
764 <        } catch (Exception ex) {
765 <            ex.printStackTrace();
952 <            unexpectedException();
953 <        } finally {
954 <            joinPool(e);
761 >        try (PoolCleaner cleaner = cleaner(e)) {
762 >            try {
763 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
764 >                shouldThrow();
765 >            } catch (NullPointerException success) {}
766          }
767      }
768  
769      /**
770 <     * timed invokeAny(null time unit) throws NPE
770 >     * timed invokeAny(null time unit) throws NullPointerException
771       */
772 <    public void testTimedInvokeAnyNullTimeUnit() {
772 >    public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
773          ExecutorService e = new ForkJoinPool(1);
774 <        try {
775 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
774 >        try (PoolCleaner cleaner = cleaner(e)) {
775 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
776              l.add(new StringTask());
777 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
778 <        } catch (NullPointerException success) {
779 <        } catch (Exception ex) {
780 <            ex.printStackTrace();
970 <            unexpectedException();
971 <        } finally {
972 <            joinPool(e);
777 >            try {
778 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
779 >                shouldThrow();
780 >            } catch (NullPointerException success) {}
781          }
782      }
783  
784      /**
785 <     * timed invokeAny(empty collection) throws IAE
785 >     * timed invokeAny(empty collection) throws IllegalArgumentException
786       */
787 <    public void testTimedInvokeAny2() {
787 >    public void testTimedInvokeAny2() throws Throwable {
788          ExecutorService e = new ForkJoinPool(1);
789 <        try {
790 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
791 <        } catch (IllegalArgumentException success) {
792 <        } catch (Exception ex) {
793 <            ex.printStackTrace();
794 <            unexpectedException();
987 <        } finally {
988 <            joinPool(e);
789 >        try (PoolCleaner cleaner = cleaner(e)) {
790 >            try {
791 >                e.invokeAny(new ArrayList<Callable<String>>(),
792 >                            MEDIUM_DELAY_MS, MILLISECONDS);
793 >                shouldThrow();
794 >            } catch (IllegalArgumentException success) {}
795          }
796      }
797  
798      /**
799 <     * timed invokeAny(c) throws NPE if c has null elements
799 >     * timed invokeAny(c) throws NullPointerException if c has null elements
800       */
801 <    public void testTimedInvokeAny3() {
801 >    public void testTimedInvokeAny3() throws Throwable {
802 >        CountDownLatch latch = new CountDownLatch(1);
803          ExecutorService e = new ForkJoinPool(1);
804 <        try {
805 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
806 <            l.add(new StringTask());
804 >        try (PoolCleaner cleaner = cleaner(e)) {
805 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
806 >            l.add(latchAwaitingStringTask(latch));
807              l.add(null);
808 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
809 <        } catch (NullPointerException success) {
810 <        } catch (Exception ex) {
811 <            ex.printStackTrace();
812 <            unexpectedException();
1006 <        } finally {
1007 <            joinPool(e);
808 >            try {
809 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
810 >                shouldThrow();
811 >            } catch (NullPointerException success) {}
812 >            latch.countDown();
813          }
814      }
815  
816      /**
817       * timed invokeAny(c) throws ExecutionException if no task completes
818       */
819 <    public void testTimedInvokeAny4() {
819 >    public void testTimedInvokeAny4() throws Throwable {
820          ExecutorService e = new ForkJoinPool(1);
821 <        try {
822 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
821 >        try (PoolCleaner cleaner = cleaner(e)) {
822 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
823              l.add(new NPETask());
824 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
825 <        } catch (ExecutionException success) {
826 <        } catch (CancellationException success) {
827 <        } catch (Exception ex) {
828 <            ex.printStackTrace();
829 <            unexpectedException();
1025 <        } finally {
1026 <            joinPool(e);
824 >            try {
825 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
826 >                shouldThrow();
827 >            } catch (ExecutionException success) {
828 >                assertTrue(success.getCause() instanceof NullPointerException);
829 >            }
830          }
831      }
832  
833      /**
834       * timed invokeAny(c) returns result of some task in c
835       */
836 <    public void testTimedInvokeAny5() {
836 >    public void testTimedInvokeAny5() throws Throwable {
837          ExecutorService e = new ForkJoinPool(1);
838 <        try {
839 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
838 >        try (PoolCleaner cleaner = cleaner(e)) {
839 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
840              l.add(new StringTask());
841              l.add(new StringTask());
842 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
842 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
843              assertSame(TEST_STRING, result);
1041        } catch (ExecutionException success) {
1042        } catch (CancellationException success) {
1043        } catch (Exception ex) {
1044            ex.printStackTrace();
1045            unexpectedException();
1046        } finally {
1047            joinPool(e);
844          }
845      }
846  
847      /**
848 <     * timed invokeAll(null) throws NPE
848 >     * timed invokeAll(null) throws NullPointerException
849       */
850 <    public void testTimedInvokeAll1() {
850 >    public void testTimedInvokeAll1() throws Throwable {
851          ExecutorService e = new ForkJoinPool(1);
852 <        try {
853 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
854 <        } catch (NullPointerException success) {
855 <        } catch (Exception ex) {
856 <            ex.printStackTrace();
1061 <            unexpectedException();
1062 <        } finally {
1063 <            joinPool(e);
852 >        try (PoolCleaner cleaner = cleaner(e)) {
853 >            try {
854 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
855 >                shouldThrow();
856 >            } catch (NullPointerException success) {}
857          }
858      }
859  
860      /**
861 <     * timed invokeAll(null time unit) throws NPE
861 >     * timed invokeAll(null time unit) throws NullPointerException
862       */
863 <    public void testTimedInvokeAllNullTimeUnit() {
863 >    public void testTimedInvokeAllNullTimeUnit() throws Throwable {
864          ExecutorService e = new ForkJoinPool(1);
865 <        try {
866 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
865 >        try (PoolCleaner cleaner = cleaner(e)) {
866 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
867              l.add(new StringTask());
868 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
869 <        } catch (NullPointerException success) {
870 <        } catch (Exception ex) {
871 <            ex.printStackTrace();
1079 <            unexpectedException();
1080 <        } finally {
1081 <            joinPool(e);
868 >            try {
869 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
870 >                shouldThrow();
871 >            } catch (NullPointerException success) {}
872          }
873      }
874  
875      /**
876       * timed invokeAll(empty collection) returns empty collection
877       */
878 <    public void testTimedInvokeAll2() {
878 >    public void testTimedInvokeAll2() throws InterruptedException {
879          ExecutorService e = new ForkJoinPool(1);
880 <        try {
881 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
880 >        try (PoolCleaner cleaner = cleaner(e)) {
881 >            List<Future<String>> r
882 >                = e.invokeAll(new ArrayList<Callable<String>>(),
883 >                              MEDIUM_DELAY_MS, MILLISECONDS);
884              assertTrue(r.isEmpty());
1093        } catch (Exception ex) {
1094            ex.printStackTrace();
1095            unexpectedException();
1096        } finally {
1097            joinPool(e);
885          }
886      }
887  
888      /**
889 <     * timed invokeAll(c) throws NPE if c has null elements
889 >     * timed invokeAll(c) throws NullPointerException if c has null elements
890       */
891 <    public void testTimedInvokeAll3() {
891 >    public void testTimedInvokeAll3() throws InterruptedException {
892          ExecutorService e = new ForkJoinPool(1);
893 <        try {
894 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
893 >        try (PoolCleaner cleaner = cleaner(e)) {
894 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
895              l.add(new StringTask());
896              l.add(null);
897 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
898 <        } catch (NullPointerException success) {
899 <        } catch (Exception ex) {
900 <            ex.printStackTrace();
1114 <            unexpectedException();
1115 <        } finally {
1116 <            joinPool(e);
897 >            try {
898 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
899 >                shouldThrow();
900 >            } catch (NullPointerException success) {}
901          }
902      }
903  
904      /**
905       * get of returned element of invokeAll(c) throws exception on failed task
906       */
907 <    public void testTimedInvokeAll4() {
907 >    public void testTimedInvokeAll4() throws Throwable {
908          ExecutorService e = new ForkJoinPool(1);
909 <        try {
910 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
909 >        try (PoolCleaner cleaner = cleaner(e)) {
910 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
911              l.add(new NPETask());
912 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
913 <            assertEquals(1, result.size());
914 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
915 <                it.next().get();
916 <        } catch (ExecutionException success) {
917 <        } catch (CancellationException success) {
918 <        } catch (Exception ex) {
919 <            ex.printStackTrace();
920 <            unexpectedException();
1137 <        } finally {
1138 <            joinPool(e);
912 >            List<Future<String>> futures
913 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
914 >            assertEquals(1, futures.size());
915 >            try {
916 >                futures.get(0).get();
917 >                shouldThrow();
918 >            } catch (ExecutionException success) {
919 >                assertTrue(success.getCause() instanceof NullPointerException);
920 >            }
921          }
922      }
923  
924      /**
925       * timed invokeAll(c) returns results of all completed tasks in c
926       */
927 <    public void testTimedInvokeAll5() {
928 <        ExecutorService e = new ForkJoinPool(1);
929 <        try {
930 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
927 >    public void testTimedInvokeAll5() throws Throwable {
928 >        ForkJoinPool e = new ForkJoinPool(1);
929 >        try (PoolCleaner cleaner = cleaner(e)) {
930 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
931              l.add(new StringTask());
932              l.add(new StringTask());
933 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
934 <            assertEquals(2, result.size());
935 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
936 <                assertSame(TEST_STRING, it.next().get());
937 <        } catch (ExecutionException success) {
1156 <        } catch (CancellationException success) {
1157 <        } catch (Exception ex) {
1158 <            ex.printStackTrace();
1159 <            unexpectedException();
1160 <        } finally {
1161 <            joinPool(e);
933 >            List<Future<String>> futures
934 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
935 >            assertEquals(2, futures.size());
936 >            for (Future<String> future : futures)
937 >                assertSame(TEST_STRING, future.get());
938          }
939      }
940  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines