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.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.49 by jsr166, Sat Feb 16 20:50:29 2013 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
7   import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
10 < import java.util.concurrent.locks.*;
11 < import java.security.*;
8 > import java.util.ArrayList;
9 > import java.util.Collection;
10 > import java.util.List;
11 > import java.util.concurrent.Executors;
12 > import java.util.concurrent.ExecutorService;
13 > import java.util.concurrent.AbstractExecutorService;
14 > import java.util.concurrent.CountDownLatch;
15 > import java.util.concurrent.Callable;
16 > import java.util.concurrent.Future;
17 > import java.util.concurrent.ExecutionException;
18 > import java.util.concurrent.CancellationException;
19 > import java.util.concurrent.RejectedExecutionException;
20 > import java.util.concurrent.ForkJoinPool;
21 > import java.util.concurrent.ForkJoinTask;
22 > import java.util.concurrent.ForkJoinWorkerThread;
23 > import java.util.concurrent.RecursiveTask;
24 > import java.util.concurrent.TimeUnit;
25 > import java.util.concurrent.atomic.AtomicBoolean;
26 > import java.util.concurrent.locks.ReentrantLock;
27 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
28 > import java.security.AccessControlException;
29 > import java.security.Policy;
30 > import java.security.PrivilegedAction;
31 > import java.security.PrivilegedExceptionAction;
32  
33 < public class ForkJoinPoolTest extends JSR166TestCase{
33 > public class ForkJoinPoolTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run (suite());
35 >        junit.textui.TestRunner.run(suite());
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.
46       *
47       * 2. newTaskFor and adapters are tested in submit/invoke tests
48 <     *
48 >     *
49       * 3. We cannot portably test monitoring methods such as
50       * getStealCount() since they rely ultimately on random task
51       * stealing that may cause tasks not to be stolen/propagated
52       * across threads, especially on uniprocessors.
53 <     *
53 >     *
54       * 4. There are no independently testable ForkJoinWorkerThread
55       * methods, but they are covered here and in task tests.
56       */
# 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;
75 <        public ForkJoinWorkerThread newThread(ForkJoinPool p){
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);
79 <        }  
79 >        }
80      }
81  
82 <    static class SubFJP extends ForkJoinPool { // to expose protected
82 >    static class SubFJP extends ForkJoinPool { // to expose protected
83          SubFJP() { super(1); }
84          public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
85              return super.drainTasksTo(c);
# Line 83 | Line 104 | public class ForkJoinPoolTest extends JS
104      }
105  
106      // A simple recursive task for testing
107 <    static final class FibTask extends RecursiveTask<Integer> {
107 >    static final class FibTask extends RecursiveTask<Integer> {
108          final int number;
109          FibTask(int n) { number = n; }
110          public Integer compute() {
# Line 97 | Line 118 | public class ForkJoinPoolTest extends JS
118      }
119  
120      // A failing task for testing
121 <    static final class FailingTask extends ForkJoinTask<Void> {
121 >    static final class FailingTask extends ForkJoinTask<Void> {
122          public final Void getRawResult() { return null; }
123          protected final void setRawResult(Void mustBeNull) { }
124          protected final boolean exec() { throw new Error(); }
# Line 105 | Line 126 | public class ForkJoinPoolTest extends JS
126      }
127  
128      // Fib needlessly using locking to test ManagedBlockers
129 <    static final class LockingFibTask extends RecursiveTask<Integer> {
129 >    static final class LockingFibTask extends RecursiveTask<Integer> {
130          final int number;
131          final ManagedLocker locker;
132          final ReentrantLock lock;
133 <        LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
134 <            number = n;
133 >        LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
134 >            number = n;
135              this.locker = locker;
136              this.lock = lock;
137          }
# Line 119 | Line 140 | public class ForkJoinPoolTest extends JS
140              LockingFibTask f1 = null;
141              LockingFibTask f2 = null;
142              locker.block();
143 <            n = number;
143 >            n = number;
144              if (n > 1) {
145                  f1 = new LockingFibTask(n - 1, locker, lock);
146                  f2 = new LockingFibTask(n - 2, locker, lock);
# Line 134 | Line 155 | public class ForkJoinPoolTest extends JS
155          }
156      }
157  
158 <    /**
159 <     * Succesfully constructed pool reports default factory,
158 >    /**
159 >     * Successfully constructed pool reports default factory,
160       * parallelism and async mode policies, no active threads or
161       * tasks, and quiescent running state.
162       */
163      public void testDefaultInitialState() {
164 <        ForkJoinPool p = null;
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 160 | Line 179 | public class ForkJoinPoolTest extends JS
179          }
180      }
181  
182 <    /**
183 <     * Constructor throws if size argument is less than zero
182 >    /**
183 >     * Constructor throws if size argument is less than zero
184       */
185      public void testConstructor1() {
186          try {
187              new ForkJoinPool(-1);
188              shouldThrow();
189 <        }
171 <        catch (IllegalArgumentException success){}
189 >        } catch (IllegalArgumentException success) {}
190      }
191  
192 <    /**
193 <     * Constructor throws if factory argument is null
192 >    /**
193 >     * Constructor throws if factory argument is null
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  
202 <
203 <    /**
187 <     * getParallelism returns size set in constructor
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          }
212      }
213  
214 <    /**
214 >    /**
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);
222 >            assertEquals(1, p.getPoolSize());
223          } finally {
224              joinPool(p);
225          }
226      }
227  
228 <    /**
261 <     * setMaximumPoolSize with argument <= 0 throws exception
262 <     */
263 <    public void testSetMaximumPoolSize2() {
264 <        ForkJoinPool p = null;
265 <        try {
266 <            p = new ForkJoinPool(1);
267 <            p.setMaximumPoolSize(-2);
268 <            shouldThrow();
269 <        }catch (IllegalArgumentException success){
270 <        } finally {
271 <            joinPool(p);
272 <        }
273 <    }
274 <
275 <    /**
276 <     * setMaintainsParallelism changes policy reported by
277 <     * getMaintainsParallelism.
278 <     */
279 <    public void testSetMaintainsParallelism() {
280 <        ForkJoinPool p = null;
281 <        try {
282 <            p = new ForkJoinPool(1);
283 <            p.setMaintainsParallelism(false);
284 <            assertFalse(p.getMaintainsParallelism());
285 <        } finally {
286 <            joinPool(p);
287 <        }
288 <    }
289 <    
290 <    /**
291 <     * setAsyncMode changes policy reported by
292 <     * getAsyncMode.
293 <     */
294 <    public void testSetAsyncMode() {
295 <        ForkJoinPool p = null;
296 <        try {
297 <            p = new ForkJoinPool(1);
298 <            p.setAsyncMode(true);
299 <            assertTrue(p.getAsyncMode());
300 <        } finally {
301 <            joinPool(p);
302 <        }
303 <    }
304 <
305 <    /**
228 >    /**
229       * setUncaughtExceptionHandler changes handler for uncaught exceptions.
230       *
231       * Additionally tests: Overriding ForkJoinWorkerThread.onStart
232       * performs its defined action
233       */
234 <    public void testSetUncaughtExceptionHandler() {
235 <        ForkJoinPool p = null;
234 >    public void testSetUncaughtExceptionHandler() throws InterruptedException {
235 >        final CountDownLatch uehInvoked = new CountDownLatch(1);
236 >        final Thread.UncaughtExceptionHandler eh =
237 >            new Thread.UncaughtExceptionHandler() {
238 >                public void uncaughtException(Thread t, Throwable e) {
239 >                    uehInvoked.countDown();
240 >                }};
241 >        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
242 >                                          eh, false);
243          try {
244 <            p = new ForkJoinPool(1, new FailingThreadFactory());
245 <            MyHandler eh = new MyHandler();
246 <            p.setUncaughtExceptionHandler(eh);
247 <            assertEquals(eh, p.getUncaughtExceptionHandler());
248 <            p.execute(new FailingTask());
249 <            Thread.sleep(MEDIUM_DELAY_MS);
320 <            assertTrue(eh.catches > 0);
321 <        } catch(InterruptedException e){
322 <            unexpectedException();
244 >            assertSame(eh, p.getUncaughtExceptionHandler());
245 >            try {
246 >                p.execute(new FibTask(8));
247 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
248 >            } catch (RejectedExecutionException ok) {
249 >            }
250          } finally {
251 +            p.shutdownNow(); // failure might have prevented processing task
252              joinPool(p);
253          }
254      }
255  
256 <    /**
257 <     * setUncaughtExceptionHandler of null removes handler
256 >    /**
257 >     * After invoking a single task, isQuiescent eventually becomes
258 >     * true, at which time queues are empty, threads are not active,
259 >     * the task has completed successfully, and construction
260 >     * parameters continue to hold
261       */
262 <    public void testSetUncaughtExceptionHandler2() {
263 <        ForkJoinPool p = null;
262 >    public void testIsQuiescent() throws Exception {
263 >        ForkJoinPool p = new ForkJoinPool(2);
264          try {
265 <            p = new ForkJoinPool(1);
266 <            p.setUncaughtExceptionHandler(null);
267 <            assertNull(p.getUncaughtExceptionHandler());
268 <        } finally {
269 <            joinPool(p);
270 <        }
271 <    }
272 <
265 >            assertTrue(p.isQuiescent());
266 >            long startTime = System.nanoTime();
267 >            FibTask f = new FibTask(20);
268 >            p.invoke(f);
269 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
270 >                       p.getFactory());
271 >            while (! p.isQuiescent()) {
272 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
273 >                    throw new AssertionFailedError("timed out");
274 >                assertFalse(p.getAsyncMode());
275 >                assertFalse(p.isShutdown());
276 >                assertFalse(p.isTerminating());
277 >                assertFalse(p.isTerminated());
278 >                Thread.yield();
279 >            }
280  
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);
281              assertTrue(p.isQuiescent());
356            assertTrue(p.getMaintainsParallelism());
282              assertFalse(p.getAsyncMode());
283 <            assertTrue(p.getActiveThreadCount() == 0);
284 <            assertTrue(p.getQueuedTaskCount() == 0);
285 <            assertTrue(p.getQueuedSubmissionCount() == 0);
283 >            assertEquals(0, p.getActiveThreadCount());
284 >            assertEquals(0, p.getQueuedTaskCount());
285 >            assertEquals(0, p.getQueuedSubmissionCount());
286              assertFalse(p.hasQueuedSubmissions());
287              assertFalse(p.isShutdown());
288              assertFalse(p.isTerminating());
289              assertFalse(p.isTerminated());
290 <        } catch(InterruptedException e){
291 <            unexpectedException();
290 >            assertTrue(f.isDone());
291 >            assertEquals(6765, (int) f.get());
292          } finally {
293              joinPool(p);
294          }
# Line 372 | Line 297 | public class ForkJoinPoolTest extends JS
297      /**
298       * Completed submit(ForkJoinTask) returns result
299       */
300 <    public void testSubmitForkJoinTask() {
301 <        ForkJoinPool p = null;
300 >    public void testSubmitForkJoinTask() throws Throwable {
301 >        ForkJoinPool p = new ForkJoinPool(1);
302          try {
378            p = new ForkJoinPool(1);
303              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
304 <            int r = f.get();
381 <            assertTrue(r == 21);
382 <        } catch (ExecutionException ex) {
383 <            unexpectedException();
384 <        } catch (InterruptedException ex) {
385 <            unexpectedException();
304 >            assertEquals(21, (int) f.get());
305          } finally {
306              joinPool(p);
307          }
# Line 392 | Line 311 | public class ForkJoinPoolTest extends JS
311       * A task submitted after shutdown is rejected
312       */
313      public void testSubmitAfterShutdown() {
314 <        ForkJoinPool p = null;
314 >        ForkJoinPool p = new ForkJoinPool(1);
315          try {
397            p = new ForkJoinPool(1);
316              p.shutdown();
317              assertTrue(p.isShutdown());
318 <            ForkJoinTask<Integer> f = p.submit(new FibTask(8));
319 <            shouldThrow();
320 <        } catch (RejectedExecutionException success) {
318 >            try {
319 >                ForkJoinTask<Integer> f = p.submit(new FibTask(8));
320 >                shouldThrow();
321 >            } catch (RejectedExecutionException success) {}
322          } finally {
323              joinPool(p);
324          }
# Line 408 | Line 327 | public class ForkJoinPoolTest extends JS
327      /**
328       * Pool maintains parallelism when using ManagedBlocker
329       */
330 <    public void testBlockingForkJoinTask() {
331 <        ForkJoinPool p = null;
330 >    public void testBlockingForkJoinTask() throws Throwable {
331 >        ForkJoinPool p = new ForkJoinPool(4);
332          try {
414            p = new ForkJoinPool(4);
333              ReentrantLock lock = new ReentrantLock();
334              ManagedLocker locker = new ManagedLocker(lock);
335 <            ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
335 >            ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
336              p.execute(f);
337 <            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();
337 >            assertEquals(6765, (int) f.get());
338          } finally {
339 <            joinPool(p);
339 >            p.shutdownNow(); // don't wait out shutdown
340          }
341      }
342  
# Line 432 | Line 344 | public class ForkJoinPoolTest extends JS
344       * pollSubmission returns unexecuted submitted task, if present
345       */
346      public void testPollSubmission() {
347 <        SubFJP p = null;
347 >        final CountDownLatch done = new CountDownLatch(1);
348 >        SubFJP p = new SubFJP();
349          try {
350 <            p = new SubFJP();
351 <            ForkJoinTask a = p.submit(new MediumRunnable());
352 <            ForkJoinTask b = p.submit(new MediumRunnable());
440 <            ForkJoinTask c = p.submit(new MediumRunnable());
350 >            ForkJoinTask a = p.submit(awaiter(done));
351 >            ForkJoinTask b = p.submit(awaiter(done));
352 >            ForkJoinTask c = p.submit(awaiter(done));
353              ForkJoinTask r = p.pollSubmission();
354              assertTrue(r == a || r == b || r == c);
355              assertFalse(r.isDone());
356          } finally {
357 +            done.countDown();
358              joinPool(p);
359          }
360      }
# Line 450 | Line 363 | public class ForkJoinPoolTest extends JS
363       * drainTasksTo transfers unexecuted submitted tasks, if present
364       */
365      public void testDrainTasksTo() {
366 <        SubFJP p = null;
366 >        final CountDownLatch done = new CountDownLatch(1);
367 >        SubFJP p = new SubFJP();
368          try {
369 <            p = new SubFJP();
370 <            ForkJoinTask a = p.submit(new MediumRunnable());
371 <            ForkJoinTask b = p.submit(new MediumRunnable());
458 <            ForkJoinTask c = p.submit(new MediumRunnable());
369 >            ForkJoinTask a = p.submit(awaiter(done));
370 >            ForkJoinTask b = p.submit(awaiter(done));
371 >            ForkJoinTask c = p.submit(awaiter(done));
372              ArrayList<ForkJoinTask> al = new ArrayList();
373              p.drainTasksTo(al);
374              assertTrue(al.size() > 0);
# Line 464 | Line 377 | public class ForkJoinPoolTest extends JS
377                  assertFalse(r.isDone());
378              }
379          } finally {
380 +            done.countDown();
381              joinPool(p);
382          }
383      }
384  
471    
385      // FJ Versions of AbstractExecutorService tests
386  
387      /**
388       * execute(runnable) runs it to completion
389       */
390 <    public void testExecuteRunnable() {
390 >    public void testExecuteRunnable() throws Throwable {
391 >        ExecutorService e = new ForkJoinPool(1);
392          try {
393 <            ExecutorService e = new ForkJoinPool(1);
394 <            TrackedShortRunnable task = new TrackedShortRunnable();
395 <            assertFalse(task.done);
393 >            final AtomicBoolean done = new AtomicBoolean(false);
394 >            CheckedRunnable task = new CheckedRunnable() {
395 >                public void realRun() {
396 >                    done.set(true);
397 >                }};
398              Future<?> future = e.submit(task);
399 <            future.get();
400 <            assertTrue(task.done);
401 <        }
402 <        catch (ExecutionException ex) {
403 <            unexpectedException();
404 <        }
405 <        catch (InterruptedException ex) {
490 <            unexpectedException();
399 >            assertNull(future.get());
400 >            assertNull(future.get(0, MILLISECONDS));
401 >            assertTrue(done.get());
402 >            assertTrue(future.isDone());
403 >            assertFalse(future.isCancelled());
404 >        } finally {
405 >            joinPool(e);
406          }
407      }
408  
494
409      /**
410       * Completed submit(callable) returns result
411       */
412 <    public void testSubmitCallable() {
412 >    public void testSubmitCallable() throws Throwable {
413 >        ExecutorService e = new ForkJoinPool(1);
414          try {
500            ExecutorService e = new ForkJoinPool(1);
415              Future<String> future = e.submit(new StringTask());
416 <            String result = future.get();
417 <            assertSame(TEST_STRING, result);
418 <        }
419 <        catch (ExecutionException ex) {
420 <            unexpectedException();
507 <        }
508 <        catch (InterruptedException ex) {
509 <            unexpectedException();
416 >            assertSame(TEST_STRING, future.get());
417 >            assertTrue(future.isDone());
418 >            assertFalse(future.isCancelled());
419 >        } finally {
420 >            joinPool(e);
421          }
422      }
423  
424      /**
425       * Completed submit(runnable) returns successfully
426       */
427 <    public void testSubmitRunnable() {
427 >    public void testSubmitRunnable() throws Throwable {
428 >        ExecutorService e = new ForkJoinPool(1);
429          try {
518            ExecutorService e = new ForkJoinPool(1);
430              Future<?> future = e.submit(new NoOpRunnable());
431 <            future.get();
431 >            assertNull(future.get());
432              assertTrue(future.isDone());
433 <        }
434 <        catch (ExecutionException ex) {
435 <            unexpectedException();
525 <        }
526 <        catch (InterruptedException ex) {
527 <            unexpectedException();
433 >            assertFalse(future.isCancelled());
434 >        } finally {
435 >            joinPool(e);
436          }
437      }
438  
439      /**
440       * Completed submit(runnable, result) returns result
441       */
442 <    public void testSubmitRunnable2() {
442 >    public void testSubmitRunnable2() throws Throwable {
443 >        ExecutorService e = new ForkJoinPool(1);
444          try {
536            ExecutorService e = new ForkJoinPool(1);
445              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
446 <            String result = future.get();
447 <            assertSame(TEST_STRING, result);
448 <        }
449 <        catch (ExecutionException ex) {
450 <            unexpectedException();
543 <        }
544 <        catch (InterruptedException ex) {
545 <            unexpectedException();
446 >            assertSame(TEST_STRING, future.get());
447 >            assertTrue(future.isDone());
448 >            assertFalse(future.isCancelled());
449 >        } finally {
450 >            joinPool(e);
451          }
452      }
453  
549
454      /**
455 <     * A submitted privileged action to completion
455 >     * A submitted privileged action runs to completion
456       */
457 <    public void testSubmitPrivilegedAction() {
458 <        Policy savedPolicy = null;
459 <        try {
460 <            savedPolicy = Policy.getPolicy();
461 <            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 {
457 >    public void testSubmitPrivilegedAction() throws Exception {
458 >        final Callable callable = Executors.callable(new PrivilegedAction() {
459 >                public Object run() { return TEST_STRING; }});
460 >        Runnable r = new CheckedRunnable() {
461 >        public void realRun() throws Exception {
462              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 {
463              try {
464 <                Policy.setPolicy(savedPolicy);
465 <            } catch(AccessControlException ok) {
466 <                return;
464 >                Future future = e.submit(callable);
465 >                assertSame(TEST_STRING, future.get());
466 >            } finally {
467 >                joinPool(e);
468              }
469 <        }
469 >        }};
470 >
471 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
472      }
473  
474      /**
475 <     * A submitted a privileged exception action runs to completion
475 >     * A submitted privileged exception action runs to completion
476       */
477 <    public void testSubmitPrivilegedExceptionAction() {
478 <        Policy savedPolicy = null;
479 <        try {
480 <            savedPolicy = Policy.getPolicy();
481 <            AdjustablePolicy policy = new AdjustablePolicy();
482 <            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 {
477 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
478 >        final Callable callable =
479 >            Executors.callable(new PrivilegedExceptionAction() {
480 >                public Object run() { return TEST_STRING; }});
481 >        Runnable r = new CheckedRunnable() {
482 >        public void realRun() throws Exception {
483              ExecutorService e = new ForkJoinPool(1);
484 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
485 <                    public Object run() {
486 <                        return TEST_STRING;
487 <                    }}));
484 >            try {
485 >                Future future = e.submit(callable);
486 >                assertSame(TEST_STRING, future.get());
487 >            } finally {
488 >                joinPool(e);
489 >            }
490 >        }};
491  
492 <            Object result = future.get();
612 <            assertSame(TEST_STRING, result);
613 <        }
614 <        catch (ExecutionException ex) {
615 <            unexpectedException();
616 <        }
617 <        catch (InterruptedException ex) {
618 <            unexpectedException();
619 <        }
620 <        finally {
621 <            Policy.setPolicy(savedPolicy);
622 <        }
492 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
493      }
494  
495      /**
496       * A submitted failed privileged exception action reports exception
497       */
498 <    public void testSubmitFailedPrivilegedExceptionAction() {
499 <        Policy savedPolicy = null;
500 <        try {
501 <            savedPolicy = Policy.getPolicy();
502 <            AdjustablePolicy policy = new AdjustablePolicy();
503 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
634 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
635 <            Policy.setPolicy(policy);
636 <        } catch(AccessControlException ok) {
637 <            return;
638 <        }
639 <
640 <
641 <        try {
498 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
499 >        final Callable callable =
500 >            Executors.callable(new PrivilegedExceptionAction() {
501 >                public Object run() { throw new IndexOutOfBoundsException(); }});
502 >        Runnable r = new CheckedRunnable() {
503 >        public void realRun() throws Exception {
504              ExecutorService e = new ForkJoinPool(1);
505 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
506 <                    public Object run() throws Exception {
507 <                        throw new IndexOutOfBoundsException();
508 <                    }}));
505 >            try {
506 >                Future future = e.submit(callable);
507 >                try {
508 >                    future.get();
509 >                    shouldThrow();
510 >                } catch (ExecutionException success) {
511 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
512 >                }
513 >            } finally {
514 >                joinPool(e);
515 >            }
516 >        }};
517  
518 <            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 <        }
518 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
519      }
520  
521      /**
522 <     * execute(null runnable) throws NPE
522 >     * execute(null runnable) throws NullPointerException
523       */
524      public void testExecuteNullRunnable() {
525 +        ExecutorService e = new ForkJoinPool(1);
526          try {
527 <            ExecutorService e = new ForkJoinPool(1);
667 <            TrackedShortRunnable task = null;
668 <            Future<?> future = e.submit(task);
527 >            Future<?> future = e.submit((Runnable) null);
528              shouldThrow();
529 <        }
530 <        catch (NullPointerException success) {
531 <        }
673 <        catch (Exception ex) {
674 <            unexpectedException();
529 >        } catch (NullPointerException success) {
530 >        } finally {
531 >            joinPool(e);
532          }
533      }
534  
678
535      /**
536 <     * submit(null callable) throws NPE
536 >     * submit(null callable) throws NullPointerException
537       */
538      public void testSubmitNullCallable() {
539 +        ExecutorService e = new ForkJoinPool(1);
540          try {
541 <            ExecutorService e = new ForkJoinPool(1);
685 <            StringTask t = null;
686 <            Future<String> future = e.submit(t);
541 >            Future<String> future = e.submit((Callable) null);
542              shouldThrow();
543 <        }
544 <        catch (NullPointerException success) {
545 <        }
691 <        catch (Exception ex) {
692 <            unexpectedException();
543 >        } catch (NullPointerException success) {
544 >        } finally {
545 >            joinPool(e);
546          }
547      }
548  
696
549      /**
550 <     *  Blocking on submit(callable) throws InterruptedException if
699 <     *  caller interrupted.
550 >     * submit(callable).get() throws InterruptedException if interrupted
551       */
552 <    public void testInterruptedSubmit() {
553 <        final ForkJoinPool p = new ForkJoinPool(1);
554 <        Thread t = new Thread(new Runnable() {
555 <                public void run() {
556 <                    try {
557 <                        p.submit(new Callable<Object>() {
558 <                                public Object call() {
559 <                                    try {
560 <                                        Thread.sleep(MEDIUM_DELAY_MS);
561 <                                        shouldThrow();
562 <                                    } catch(InterruptedException e){
563 <                                    }
564 <                                    return null;
565 <                                }
566 <                            }).get();
567 <                    } catch(InterruptedException success){
717 <                    } catch(Exception e) {
718 <                        unexpectedException();
719 <                    }
720 <
721 <                }
722 <            });
723 <        try {
552 >    public void testInterruptedSubmit() throws InterruptedException {
553 >        final CountDownLatch submitted    = new CountDownLatch(1);
554 >        final CountDownLatch quittingTime = new CountDownLatch(1);
555 >        final ExecutorService p = new ForkJoinPool(1);
556 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
557 >            public Void realCall() throws InterruptedException {
558 >                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
559 >                return null;
560 >            }};
561 >        try {
562 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
563 >                public void realRun() throws Exception {
564 >                    Future<Void> future = p.submit(awaiter);
565 >                    submitted.countDown();
566 >                    future.get();
567 >                }});
568              t.start();
569 <            Thread.sleep(SHORT_DELAY_MS);
569 >            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
570              t.interrupt();
571 <        } catch(Exception e){
572 <            unexpectedException();
571 >            t.join();
572 >        } finally {
573 >            quittingTime.countDown();
574 >            joinPool(p);
575          }
730        joinPool(p);
576      }
577  
578      /**
579 <     *  get of submit(callable) throws ExecutionException if callable
580 <     *  throws exception
579 >     * get of submit(callable) throws ExecutionException if callable
580 >     * throws exception
581       */
582 <    public void testSubmitEE() {
582 >    public void testSubmitEE() throws Throwable {
583          ForkJoinPool p = new ForkJoinPool(1);
739
584          try {
585 <            Callable c = new Callable() {
586 <                    public Object call() {
587 <                        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 <
585 >            p.submit(new Callable() {
586 >                public Object call() { throw new ArithmeticException(); }})
587 >                .get();
588              shouldThrow();
589 +        } catch (ExecutionException success) {
590 +            assertTrue(success.getCause() instanceof ArithmeticException);
591 +        } finally {
592 +            joinPool(p);
593          }
754        catch(ExecutionException success){
755        } catch (CancellationException success) {
756        } catch(Exception e) {
757            unexpectedException();
758        }
759        joinPool(p);
594      }
595  
596      /**
597 <     * invokeAny(null) throws NPE
597 >     * invokeAny(null) throws NullPointerException
598       */
599 <    public void testInvokeAny1() {
599 >    public void testInvokeAny1() throws Throwable {
600          ExecutorService e = new ForkJoinPool(1);
601          try {
602              e.invokeAny(null);
603 +            shouldThrow();
604          } catch (NullPointerException success) {
770        } catch(Exception ex) {
771            unexpectedException();
605          } finally {
606              joinPool(e);
607          }
608      }
609  
610      /**
611 <     * invokeAny(empty collection) throws IAE
611 >     * invokeAny(empty collection) throws IllegalArgumentException
612       */
613 <    public void testInvokeAny2() {
613 >    public void testInvokeAny2() throws Throwable {
614          ExecutorService e = new ForkJoinPool(1);
615          try {
616              e.invokeAny(new ArrayList<Callable<String>>());
617 +            shouldThrow();
618          } catch (IllegalArgumentException success) {
785        } catch(Exception ex) {
786            unexpectedException();
619          } finally {
620              joinPool(e);
621          }
622      }
623  
624      /**
625 <     * invokeAny(c) throws NPE if c has null elements
625 >     * invokeAny(c) throws NullPointerException if c has a single null element
626       */
627 <    public void testInvokeAny3() {
627 >    public void testInvokeAny3() throws Throwable {
628          ExecutorService e = new ForkJoinPool(1);
629 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
630 +        l.add(null);
631          try {
798            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
799            l.add(new StringTask());
800            l.add(null);
632              e.invokeAny(l);
633 +            shouldThrow();
634          } catch (NullPointerException success) {
803        } catch(Exception ex) {
804            ex.printStackTrace();
805            unexpectedException();
635          } finally {
636              joinPool(e);
637          }
638      }
639  
640      /**
641 +     * invokeAny(c) throws NullPointerException if c has null elements
642 +     */
643 +    public void testInvokeAny4() throws Throwable {
644 +        CountDownLatch latch = new CountDownLatch(1);
645 +        ExecutorService e = new ForkJoinPool(1);
646 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
647 +        l.add(latchAwaitingStringTask(latch));
648 +        l.add(null);
649 +        try {
650 +            e.invokeAny(l);
651 +            shouldThrow();
652 +        } catch (NullPointerException success) {
653 +        } finally {
654 +            latch.countDown();
655 +            joinPool(e);
656 +        }
657 +    }
658 +
659 +    /**
660       * invokeAny(c) throws ExecutionException if no task in c completes
661       */
662 <    public void testInvokeAny4() {
662 >    public void testInvokeAny5() throws Throwable {
663          ExecutorService e = new ForkJoinPool(1);
664 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
665 +        l.add(new NPETask());
666          try {
817            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
818            l.add(new NPETask());
667              e.invokeAny(l);
668 <        } catch(ExecutionException success) {
669 <        } catch (CancellationException success) {
670 <        } catch(Exception ex) {
823 <            unexpectedException();
668 >            shouldThrow();
669 >        } catch (ExecutionException success) {
670 >            assertTrue(success.getCause() instanceof NullPointerException);
671          } finally {
672              joinPool(e);
673          }
# Line 829 | Line 676 | public class ForkJoinPoolTest extends JS
676      /**
677       * invokeAny(c) returns result of some task in c if at least one completes
678       */
679 <    public void testInvokeAny5() {
679 >    public void testInvokeAny6() throws Throwable {
680          ExecutorService e = new ForkJoinPool(1);
681          try {
682 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
682 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
683              l.add(new StringTask());
684              l.add(new StringTask());
685              String result = e.invokeAny(l);
686              assertSame(TEST_STRING, result);
840        } catch (ExecutionException success) {
841        } catch (CancellationException success) {
842        } catch(Exception ex) {
843            unexpectedException();
687          } finally {
688              joinPool(e);
689          }
690      }
691  
692      /**
693 <     * invokeAll(null) throws NPE
693 >     * invokeAll(null) throws NullPointerException
694       */
695 <    public void testInvokeAll1() {
695 >    public void testInvokeAll1() throws Throwable {
696          ExecutorService e = new ForkJoinPool(1);
697          try {
698              e.invokeAll(null);
699 +            shouldThrow();
700          } catch (NullPointerException success) {
857        } catch(Exception ex) {
858            unexpectedException();
701          } finally {
702              joinPool(e);
703          }
# Line 864 | Line 706 | public class ForkJoinPoolTest extends JS
706      /**
707       * invokeAll(empty collection) returns empty collection
708       */
709 <    public void testInvokeAll2() {
709 >    public void testInvokeAll2() throws InterruptedException {
710          ExecutorService e = new ForkJoinPool(1);
711          try {
712 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
712 >            List<Future<String>> r
713 >                = e.invokeAll(new ArrayList<Callable<String>>());
714              assertTrue(r.isEmpty());
872        } catch(Exception ex) {
873            unexpectedException();
715          } finally {
716              joinPool(e);
717          }
718      }
719  
720      /**
721 <     * invokeAll(c) throws NPE if c has null elements
721 >     * invokeAll(c) throws NullPointerException if c has null elements
722       */
723 <    public void testInvokeAll3() {
723 >    public void testInvokeAll3() throws InterruptedException {
724          ExecutorService e = new ForkJoinPool(1);
725 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
726 +        l.add(new StringTask());
727 +        l.add(null);
728          try {
885            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
886            l.add(new StringTask());
887            l.add(null);
729              e.invokeAll(l);
730 +            shouldThrow();
731          } catch (NullPointerException success) {
890        } catch(Exception ex) {
891            unexpectedException();
732          } finally {
733              joinPool(e);
734          }
735      }
736  
737      /**
738 <     * get of returned element of invokeAll(c) throws exception on failed task
738 >     * get of returned element of invokeAll(c) throws
739 >     * ExecutionException on failed task
740       */
741 <    public void testInvokeAll4() {
741 >    public void testInvokeAll4() throws Throwable {
742          ExecutorService e = new ForkJoinPool(1);
743 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
744 +        l.add(new NPETask());
745 +        List<Future<String>> futures = e.invokeAll(l);
746 +        assertEquals(1, futures.size());
747          try {
748 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
749 <            l.add(new NPETask());
750 <            List<Future<String>> result = e.invokeAll(l);
751 <            assertEquals(1, result.size());
907 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
908 <                it.next().get();
909 <        } catch(ExecutionException success) {
910 <        } catch (CancellationException success) {
911 <        } catch(Exception ex) {
912 <            ex.printStackTrace();
913 <            unexpectedException();
748 >            futures.get(0).get();
749 >            shouldThrow();
750 >        } catch (ExecutionException success) {
751 >            assertTrue(success.getCause() instanceof NullPointerException);
752          } finally {
753              joinPool(e);
754          }
# Line 919 | Line 757 | public class ForkJoinPoolTest extends JS
757      /**
758       * invokeAll(c) returns results of all completed tasks in c
759       */
760 <    public void testInvokeAll5() {
760 >    public void testInvokeAll5() throws Throwable {
761          ExecutorService e = new ForkJoinPool(1);
762          try {
763 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
763 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
764              l.add(new StringTask());
765              l.add(new StringTask());
766 <            List<Future<String>> result = e.invokeAll(l);
767 <            assertEquals(2, result.size());
768 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
769 <                assertSame(TEST_STRING, it.next().get());
932 <        } catch (ExecutionException success) {
933 <        } catch (CancellationException success) {
934 <        } catch(Exception ex) {
935 <            ex.printStackTrace();
936 <            unexpectedException();
766 >            List<Future<String>> futures = e.invokeAll(l);
767 >            assertEquals(2, futures.size());
768 >            for (Future<String> future : futures)
769 >                assertSame(TEST_STRING, future.get());
770          } finally {
771              joinPool(e);
772          }
773      }
774  
942
775      /**
776 <     * timed invokeAny(null) throws NPE
776 >     * timed invokeAny(null) throws NullPointerException
777       */
778 <    public void testTimedInvokeAny1() {
778 >    public void testTimedInvokeAny1() throws Throwable {
779          ExecutorService e = new ForkJoinPool(1);
780          try {
781 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
781 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
782 >            shouldThrow();
783          } catch (NullPointerException success) {
951        } catch(Exception ex) {
952            ex.printStackTrace();
953            unexpectedException();
784          } finally {
785              joinPool(e);
786          }
787      }
788  
789      /**
790 <     * timed invokeAny(null time unit) throws NPE
790 >     * timed invokeAny(null time unit) throws NullPointerException
791       */
792 <    public void testTimedInvokeAnyNullTimeUnit() {
792 >    public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
793          ExecutorService e = new ForkJoinPool(1);
794 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
795 +        l.add(new StringTask());
796          try {
965            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
966            l.add(new StringTask());
797              e.invokeAny(l, MEDIUM_DELAY_MS, null);
798 +            shouldThrow();
799          } catch (NullPointerException success) {
969        } catch(Exception ex) {
970            ex.printStackTrace();
971            unexpectedException();
800          } finally {
801              joinPool(e);
802          }
803      }
804  
805      /**
806 <     * timed invokeAny(empty collection) throws IAE
806 >     * timed invokeAny(empty collection) throws IllegalArgumentException
807       */
808 <    public void testTimedInvokeAny2() {
808 >    public void testTimedInvokeAny2() throws Throwable {
809          ExecutorService e = new ForkJoinPool(1);
810          try {
811 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
811 >            e.invokeAny(new ArrayList<Callable<String>>(),
812 >                        MEDIUM_DELAY_MS, MILLISECONDS);
813 >            shouldThrow();
814          } catch (IllegalArgumentException success) {
985        } catch(Exception ex) {
986            ex.printStackTrace();
987            unexpectedException();
815          } finally {
816              joinPool(e);
817          }
818      }
819  
820      /**
821 <     * timed invokeAny(c) throws NPE if c has null elements
821 >     * timed invokeAny(c) throws NullPointerException if c has null elements
822       */
823 <    public void testTimedInvokeAny3() {
823 >    public void testTimedInvokeAny3() throws Throwable {
824 >        CountDownLatch latch = new CountDownLatch(1);
825          ExecutorService e = new ForkJoinPool(1);
826 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
827 +        l.add(latchAwaitingStringTask(latch));
828 +        l.add(null);
829          try {
830 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
831 <            l.add(new StringTask());
1001 <            l.add(null);
1002 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
830 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
831 >            shouldThrow();
832          } catch (NullPointerException success) {
1004        } catch(Exception ex) {
1005            ex.printStackTrace();
1006            unexpectedException();
833          } finally {
834 +            latch.countDown();
835              joinPool(e);
836          }
837      }
# Line 1012 | Line 839 | public class ForkJoinPoolTest extends JS
839      /**
840       * timed invokeAny(c) throws ExecutionException if no task completes
841       */
842 <    public void testTimedInvokeAny4() {
842 >    public void testTimedInvokeAny4() throws Throwable {
843          ExecutorService e = new ForkJoinPool(1);
844 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
845 +        l.add(new NPETask());
846          try {
847 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
848 <            l.add(new NPETask());
849 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
850 <        } catch(ExecutionException success) {
1022 <        } catch (CancellationException success) {
1023 <        } catch(Exception ex) {
1024 <            ex.printStackTrace();
1025 <            unexpectedException();
847 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
848 >            shouldThrow();
849 >        } catch (ExecutionException success) {
850 >            assertTrue(success.getCause() instanceof NullPointerException);
851          } finally {
852              joinPool(e);
853          }
# Line 1031 | Line 856 | public class ForkJoinPoolTest extends JS
856      /**
857       * timed invokeAny(c) returns result of some task in c
858       */
859 <    public void testTimedInvokeAny5() {
859 >    public void testTimedInvokeAny5() throws Throwable {
860          ExecutorService e = new ForkJoinPool(1);
861          try {
862 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
862 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
863              l.add(new StringTask());
864              l.add(new StringTask());
865 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
865 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
866              assertSame(TEST_STRING, result);
1042        } catch (ExecutionException success) {
1043        } catch (CancellationException success) {
1044        } catch(Exception ex) {
1045            ex.printStackTrace();
1046            unexpectedException();
867          } finally {
868              joinPool(e);
869          }
870      }
871  
872      /**
873 <     * timed invokeAll(null) throws NPE
873 >     * timed invokeAll(null) throws NullPointerException
874       */
875 <    public void testTimedInvokeAll1() {
875 >    public void testTimedInvokeAll1() throws Throwable {
876          ExecutorService e = new ForkJoinPool(1);
877          try {
878 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
878 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
879 >            shouldThrow();
880          } catch (NullPointerException success) {
1060        } catch(Exception ex) {
1061            ex.printStackTrace();
1062            unexpectedException();
881          } finally {
882              joinPool(e);
883          }
884      }
885  
886      /**
887 <     * timed invokeAll(null time unit) throws NPE
887 >     * timed invokeAll(null time unit) throws NullPointerException
888       */
889 <    public void testTimedInvokeAllNullTimeUnit() {
889 >    public void testTimedInvokeAllNullTimeUnit() throws Throwable {
890          ExecutorService e = new ForkJoinPool(1);
891 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
892 +        l.add(new StringTask());
893          try {
1074            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1075            l.add(new StringTask());
894              e.invokeAll(l, MEDIUM_DELAY_MS, null);
895 +            shouldThrow();
896          } catch (NullPointerException success) {
1078        } catch(Exception ex) {
1079            ex.printStackTrace();
1080            unexpectedException();
897          } finally {
898              joinPool(e);
899          }
# Line 1086 | Line 902 | public class ForkJoinPoolTest extends JS
902      /**
903       * timed invokeAll(empty collection) returns empty collection
904       */
905 <    public void testTimedInvokeAll2() {
905 >    public void testTimedInvokeAll2() throws InterruptedException {
906          ExecutorService e = new ForkJoinPool(1);
907          try {
908 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
908 >            List<Future<String>> r
909 >                = e.invokeAll(new ArrayList<Callable<String>>(),
910 >                              MEDIUM_DELAY_MS, MILLISECONDS);
911              assertTrue(r.isEmpty());
1094        } catch(Exception ex) {
1095            ex.printStackTrace();
1096            unexpectedException();
912          } finally {
913              joinPool(e);
914          }
915      }
916  
917      /**
918 <     * timed invokeAll(c) throws NPE if c has null elements
918 >     * timed invokeAll(c) throws NullPointerException if c has null elements
919       */
920 <    public void testTimedInvokeAll3() {
920 >    public void testTimedInvokeAll3() throws InterruptedException {
921          ExecutorService e = new ForkJoinPool(1);
922 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
923 +        l.add(new StringTask());
924 +        l.add(null);
925          try {
926 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
927 <            l.add(new StringTask());
1110 <            l.add(null);
1111 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
926 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
927 >            shouldThrow();
928          } catch (NullPointerException success) {
1113        } catch(Exception ex) {
1114            ex.printStackTrace();
1115            unexpectedException();
929          } finally {
930              joinPool(e);
931          }
# Line 1121 | Line 934 | public class ForkJoinPoolTest extends JS
934      /**
935       * get of returned element of invokeAll(c) throws exception on failed task
936       */
937 <    public void testTimedInvokeAll4() {
937 >    public void testTimedInvokeAll4() throws Throwable {
938          ExecutorService e = new ForkJoinPool(1);
939 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
940 +        l.add(new NPETask());
941 +        List<Future<String>> futures
942 +            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
943 +        assertEquals(1, futures.size());
944          try {
945 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
946 <            l.add(new NPETask());
947 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
948 <            assertEquals(1, result.size());
1131 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1132 <                it.next().get();
1133 <        } catch(ExecutionException success) {
1134 <        } catch (CancellationException success) {
1135 <        } catch(Exception ex) {
1136 <            ex.printStackTrace();
1137 <            unexpectedException();
945 >            futures.get(0).get();
946 >            shouldThrow();
947 >        } catch (ExecutionException success) {
948 >            assertTrue(success.getCause() instanceof NullPointerException);
949          } finally {
950              joinPool(e);
951          }
# Line 1143 | Line 954 | public class ForkJoinPoolTest extends JS
954      /**
955       * timed invokeAll(c) returns results of all completed tasks in c
956       */
957 <    public void testTimedInvokeAll5() {
957 >    public void testTimedInvokeAll5() throws Throwable {
958          ExecutorService e = new ForkJoinPool(1);
959          try {
960 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
960 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
961              l.add(new StringTask());
962              l.add(new StringTask());
963 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
964 <            assertEquals(2, result.size());
965 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
966 <                assertSame(TEST_STRING, it.next().get());
967 <        } catch (ExecutionException success) {
1157 <        } catch (CancellationException success) {
1158 <        } catch(Exception ex) {
1159 <            ex.printStackTrace();
1160 <            unexpectedException();
963 >            List<Future<String>> futures
964 >                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
965 >            assertEquals(2, futures.size());
966 >            for (Future<String> future : futures)
967 >                assertSame(TEST_STRING, future.get());
968          } finally {
969              joinPool(e);
970          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines