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.5 by jsr166, Sat Aug 1 22:09:13 2009 UTC vs.
Revision 1.56 by jsr166, Fri Oct 2 21:52:36 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines