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.4 by jsr166, Sat Aug 1 21:56:02 2009 UTC vs.
Revision 1.26 by jsr166, Thu Sep 16 00:52:49 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines