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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines