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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines