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.2 by jsr166, Fri Jul 31 23:37:31 2009 UTC vs.
Revision 1.21 by jsr166, Wed Aug 25 00:07:03 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines