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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines