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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines