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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines