ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinPoolTest.java (file contents):
Revision 1.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.16 by jsr166, Tue Dec 1 22:51:44 2009 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
12   import java.util.concurrent.locks.*;
13   import java.security.*;
14  
15 < public class ForkJoinPoolTest extends JSR166TestCase{
15 > public class ForkJoinPoolTest extends JSR166TestCase {
16      public static void main(String[] args) {
17          junit.textui.TestRunner.run (suite());
18      }
# Line 25 | Line 26 | public class ForkJoinPoolTest extends JS
26       * 1. shutdown and related methods are tested via super.joinPool.
27       *
28       * 2. newTaskFor and adapters are tested in submit/invoke tests
29 <     *
29 >     *
30       * 3. We cannot portably test monitoring methods such as
31       * getStealCount() since they rely ultimately on random task
32       * stealing that may cause tasks not to be stolen/propagated
33       * across threads, especially on uniprocessors.
34 <     *
34 >     *
35       * 4. There are no independently testable ForkJoinWorkerThread
36       * methods, but they are covered here and in task tests.
37       */
# Line 50 | Line 51 | public class ForkJoinPoolTest extends JS
51          protected void onStart() { throw new Error(); }
52      }
53  
54 <    static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
54 >    static class FailingThreadFactory
55 >            implements ForkJoinPool.ForkJoinWorkerThreadFactory {
56          int calls = 0;
57 <        public ForkJoinWorkerThread newThread(ForkJoinPool p){
57 >        public ForkJoinWorkerThread newThread(ForkJoinPool p) {
58              if (++calls > 1) return null;
59              return new FailingFJWSubclass(p);
60 <        }  
60 >        }
61      }
62  
63 <    static class SubFJP extends ForkJoinPool { // to expose protected
63 >    static class SubFJP extends ForkJoinPool { // to expose protected
64          SubFJP() { super(1); }
65          public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
66              return super.drainTasksTo(c);
# Line 83 | Line 85 | public class ForkJoinPoolTest extends JS
85      }
86  
87      // A simple recursive task for testing
88 <    static final class FibTask extends RecursiveTask<Integer> {
88 >    static final class FibTask extends RecursiveTask<Integer> {
89          final int number;
90          FibTask(int n) { number = n; }
91          public Integer compute() {
# Line 97 | Line 99 | public class ForkJoinPoolTest extends JS
99      }
100  
101      // A failing task for testing
102 <    static final class FailingTask extends ForkJoinTask<Void> {
102 >    static final class FailingTask extends ForkJoinTask<Void> {
103          public final Void getRawResult() { return null; }
104          protected final void setRawResult(Void mustBeNull) { }
105          protected final boolean exec() { throw new Error(); }
# Line 105 | Line 107 | public class ForkJoinPoolTest extends JS
107      }
108  
109      // Fib needlessly using locking to test ManagedBlockers
110 <    static final class LockingFibTask extends RecursiveTask<Integer> {
110 >    static final class LockingFibTask extends RecursiveTask<Integer> {
111          final int number;
112          final ManagedLocker locker;
113          final ReentrantLock lock;
114 <        LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
115 <            number = n;
114 >        LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
115 >            number = n;
116              this.locker = locker;
117              this.lock = lock;
118          }
# Line 119 | Line 121 | public class ForkJoinPoolTest extends JS
121              LockingFibTask f1 = null;
122              LockingFibTask f2 = null;
123              locker.block();
124 <            n = number;
124 >            n = number;
125              if (n > 1) {
126                  f1 = new LockingFibTask(n - 1, locker, lock);
127                  f2 = new LockingFibTask(n - 2, locker, lock);
# Line 134 | Line 136 | public class ForkJoinPoolTest extends JS
136          }
137      }
138  
139 <    /**
140 <     * Succesfully constructed pool reports default factory,
139 >    /**
140 >     * Successfully constructed pool reports default factory,
141       * parallelism and async mode policies, no active threads or
142       * tasks, and quiescent running state.
143       */
# Line 143 | Line 145 | public class ForkJoinPoolTest extends JS
145          ForkJoinPool p = null;
146          try {
147              p = new ForkJoinPool(1);
148 <            assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory);
148 >            assertTrue(p.getFactory() ==
149 >                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
150              assertTrue(p.isQuiescent());
151              assertTrue(p.getMaintainsParallelism());
152              assertFalse(p.getAsyncMode());
# Line 160 | Line 163 | public class ForkJoinPoolTest extends JS
163          }
164      }
165  
166 <    /**
167 <     * Constructor throws if size argument is less than zero
166 >    /**
167 >     * Constructor throws if size argument is less than zero
168       */
169      public void testConstructor1() {
170          try {
171              new ForkJoinPool(-1);
172              shouldThrow();
173 <        }
171 <        catch (IllegalArgumentException success){}
173 >        } catch (IllegalArgumentException success) {}
174      }
175  
176 <    /**
177 <     * Constructor throws if factory argument is null
176 >    /**
177 >     * Constructor throws if factory argument is null
178       */
179      public void testConstructor2() {
180          try {
181              new ForkJoinPool(1, null);
182              shouldThrow();
183 <        }
182 <        catch (NullPointerException success){}  
183 >        } catch (NullPointerException success) {}
184      }
185  
186  
187 <    /**
188 <     * getParallelism returns size set in constructor
187 >    /**
188 >     * getParallelism returns size set in constructor
189       */
190      public void testGetParallelism() {
191          ForkJoinPool p = null;
# Line 196 | Line 197 | public class ForkJoinPoolTest extends JS
197          }
198      }
199  
200 <    /**
200 >    /**
201       * setParallelism changes reported parallelism level.
202       */
203      public void testSetParallelism() {
# Line 211 | Line 212 | public class ForkJoinPoolTest extends JS
212          }
213      }
214  
215 <    /**
215 >    /**
216       * setParallelism with argument <= 0 throws exception
217       */
218      public void testSetParallelism2() {
# Line 221 | Line 222 | public class ForkJoinPoolTest extends JS
222              assertTrue(p.getParallelism() == 1);
223              p.setParallelism(-2);
224              shouldThrow();
225 <        }catch (IllegalArgumentException success){
225 >        } catch (IllegalArgumentException success) {
226          } finally {
227              joinPool(p);
228          }
229      }
230  
231 <    /**
231 >    /**
232       * getPoolSize returns number of started workers.
233       */
234      public void testGetPoolSize() {
# Line 243 | Line 244 | public class ForkJoinPoolTest extends JS
244          }
245      }
246  
247 <    /**
247 >    /**
248       * setMaximumPoolSize changes size reported by getMaximumPoolSize.
249       */
250      public void testSetMaximumPoolSize() {
# Line 257 | Line 258 | public class ForkJoinPoolTest extends JS
258          }
259      }
260  
261 <    /**
261 >    /**
262       * setMaximumPoolSize with argument <= 0 throws exception
263       */
264      public void testSetMaximumPoolSize2() {
# Line 266 | Line 267 | public class ForkJoinPoolTest extends JS
267              p = new ForkJoinPool(1);
268              p.setMaximumPoolSize(-2);
269              shouldThrow();
270 <        }catch (IllegalArgumentException success){
270 >        } catch (IllegalArgumentException success) {
271          } finally {
272              joinPool(p);
273          }
274      }
275  
276 <    /**
276 >    /**
277       * setMaintainsParallelism changes policy reported by
278       * getMaintainsParallelism.
279       */
# Line 286 | Line 287 | public class ForkJoinPoolTest extends JS
287              joinPool(p);
288          }
289      }
290 <    
291 <    /**
290 >
291 >    /**
292       * setAsyncMode changes policy reported by
293       * getAsyncMode.
294       */
# Line 302 | Line 303 | public class ForkJoinPoolTest extends JS
303          }
304      }
305  
306 <    /**
306 >    /**
307       * setUncaughtExceptionHandler changes handler for uncaught exceptions.
308       *
309       * Additionally tests: Overriding ForkJoinWorkerThread.onStart
310       * performs its defined action
311       */
312 <    public void testSetUncaughtExceptionHandler() {
312 >    public void testSetUncaughtExceptionHandler() throws InterruptedException {
313          ForkJoinPool p = null;
314          try {
315              p = new ForkJoinPool(1, new FailingThreadFactory());
# Line 318 | Line 319 | public class ForkJoinPoolTest extends JS
319              p.execute(new FailingTask());
320              Thread.sleep(MEDIUM_DELAY_MS);
321              assertTrue(eh.catches > 0);
321        } catch(InterruptedException e){
322            unexpectedException();
322          } finally {
323              joinPool(p);
324          }
325      }
326  
327 <    /**
327 >    /**
328       * setUncaughtExceptionHandler of null removes handler
329       */
330      public void testSetUncaughtExceptionHandler2() {
# Line 340 | Line 339 | public class ForkJoinPoolTest extends JS
339      }
340  
341  
342 <    /**
342 >    /**
343       * After invoking a single task, isQuiescent is true,
344       * queues are empty, threads are not active, and
345       * construction parameters continue to hold
346       */
347 <    public void testisQuiescent() {
347 >    public void testisQuiescent() throws InterruptedException {
348          ForkJoinPool p = null;
349          try {
350              p = new ForkJoinPool(2);
351              p.invoke(new FibTask(20));
352 <            assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory);
352 >            assertTrue(p.getFactory() ==
353 >                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
354              Thread.sleep(MEDIUM_DELAY_MS);
355              assertTrue(p.isQuiescent());
356              assertTrue(p.getMaintainsParallelism());
# Line 362 | Line 362 | public class ForkJoinPoolTest extends JS
362              assertFalse(p.isShutdown());
363              assertFalse(p.isTerminating());
364              assertFalse(p.isTerminated());
365        } catch(InterruptedException e){
366            unexpectedException();
365          } finally {
366              joinPool(p);
367          }
# Line 372 | Line 370 | public class ForkJoinPoolTest extends JS
370      /**
371       * Completed submit(ForkJoinTask) returns result
372       */
373 <    public void testSubmitForkJoinTask() {
373 >    public void testSubmitForkJoinTask() throws Throwable {
374          ForkJoinPool p = null;
375          try {
376              p = new ForkJoinPool(1);
377              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
378              int r = f.get();
379              assertTrue(r == 21);
382        } catch (ExecutionException ex) {
383            unexpectedException();
384        } catch (InterruptedException ex) {
385            unexpectedException();
380          } finally {
381              joinPool(p);
382          }
# Line 408 | Line 402 | public class ForkJoinPoolTest extends JS
402      /**
403       * Pool maintains parallelism when using ManagedBlocker
404       */
405 <    public void testBlockingForkJoinTask() {
405 >    public void testBlockingForkJoinTask() throws Throwable {
406          ForkJoinPool p = null;
407          try {
408              p = new ForkJoinPool(4);
# Line 418 | Line 412 | public class ForkJoinPoolTest extends JS
412              p.execute(f);
413              assertTrue(p.getPoolSize() >= 4);
414              int r = f.get();
415 <            assertTrue(r ==  832040);
422 <        } catch (ExecutionException ex) {
423 <            unexpectedException();
424 <        } catch (InterruptedException ex) {
425 <            unexpectedException();
415 >            assertTrue(r == 832040);
416          } finally {
417 <            joinPool(p);
417 >            p.shutdownNow(); // don't wait out shutdown
418          }
419      }
420  
# Line 468 | Line 458 | public class ForkJoinPoolTest extends JS
458          }
459      }
460  
461 <    
461 >
462      // FJ Versions of AbstractExecutorService tests
463  
464      /**
465       * execute(runnable) runs it to completion
466       */
467 <    public void testExecuteRunnable() {
468 <        try {
469 <            ExecutorService e = new ForkJoinPool(1);
470 <            TrackedShortRunnable task = new TrackedShortRunnable();
471 <            assertFalse(task.done);
472 <            Future<?> future = e.submit(task);
473 <            future.get();
484 <            assertTrue(task.done);
485 <        }
486 <        catch (ExecutionException ex) {
487 <            unexpectedException();
488 <        }
489 <        catch (InterruptedException ex) {
490 <            unexpectedException();
491 <        }
467 >    public void testExecuteRunnable() throws Throwable {
468 >        ExecutorService e = new ForkJoinPool(1);
469 >        TrackedShortRunnable task = new TrackedShortRunnable();
470 >        assertFalse(task.done);
471 >        Future<?> future = e.submit(task);
472 >        future.get();
473 >        assertTrue(task.done);
474      }
475  
476  
477      /**
478       * Completed submit(callable) returns result
479       */
480 <    public void testSubmitCallable() {
481 <        try {
482 <            ExecutorService e = new ForkJoinPool(1);
483 <            Future<String> future = e.submit(new StringTask());
484 <            String result = future.get();
503 <            assertSame(TEST_STRING, result);
504 <        }
505 <        catch (ExecutionException ex) {
506 <            unexpectedException();
507 <        }
508 <        catch (InterruptedException ex) {
509 <            unexpectedException();
510 <        }
480 >    public void testSubmitCallable() throws Throwable {
481 >        ExecutorService e = new ForkJoinPool(1);
482 >        Future<String> future = e.submit(new StringTask());
483 >        String result = future.get();
484 >        assertSame(TEST_STRING, result);
485      }
486  
487      /**
488       * Completed submit(runnable) returns successfully
489       */
490 <    public void testSubmitRunnable() {
491 <        try {
492 <            ExecutorService e = new ForkJoinPool(1);
493 <            Future<?> future = e.submit(new NoOpRunnable());
494 <            future.get();
521 <            assertTrue(future.isDone());
522 <        }
523 <        catch (ExecutionException ex) {
524 <            unexpectedException();
525 <        }
526 <        catch (InterruptedException ex) {
527 <            unexpectedException();
528 <        }
490 >    public void testSubmitRunnable() throws Throwable {
491 >        ExecutorService e = new ForkJoinPool(1);
492 >        Future<?> future = e.submit(new NoOpRunnable());
493 >        future.get();
494 >        assertTrue(future.isDone());
495      }
496  
497      /**
498       * Completed submit(runnable, result) returns result
499       */
500 <    public void testSubmitRunnable2() {
501 <        try {
502 <            ExecutorService e = new ForkJoinPool(1);
503 <            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
504 <            String result = future.get();
539 <            assertSame(TEST_STRING, result);
540 <        }
541 <        catch (ExecutionException ex) {
542 <            unexpectedException();
543 <        }
544 <        catch (InterruptedException ex) {
545 <            unexpectedException();
546 <        }
500 >    public void testSubmitRunnable2() throws Throwable {
501 >        ExecutorService e = new ForkJoinPool(1);
502 >        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
503 >        String result = future.get();
504 >        assertSame(TEST_STRING, result);
505      }
506  
507  
508      /**
509       * A submitted privileged action to completion
510       */
511 <    public void testSubmitPrivilegedAction() {
511 >    public void testSubmitPrivilegedAction() throws Throwable {
512          Policy savedPolicy = null;
513          try {
514              savedPolicy = Policy.getPolicy();
# Line 558 | Line 516 | public class ForkJoinPoolTest extends JS
516              policy.addPermission(new RuntimePermission("getContextClassLoader"));
517              policy.addPermission(new RuntimePermission("setContextClassLoader"));
518              Policy.setPolicy(policy);
519 <        } catch(AccessControlException ok) {
519 >        } catch (AccessControlException ok) {
520              return;
521          }
522          try {
# Line 571 | Line 529 | public class ForkJoinPoolTest extends JS
529              Object result = future.get();
530              assertSame(TEST_STRING, result);
531          }
574        catch (ExecutionException ex) {
575            unexpectedException();
576        }
577        catch (InterruptedException ex) {
578            unexpectedException();
579        }
532          finally {
533 <            try {
582 <                Policy.setPolicy(savedPolicy);
583 <            } catch(AccessControlException ok) {
584 <                return;
585 <            }
533 >            Policy.setPolicy(savedPolicy);
534          }
535      }
536  
537      /**
538       * A submitted a privileged exception action runs to completion
539       */
540 <    public void testSubmitPrivilegedExceptionAction() {
540 >    public void testSubmitPrivilegedExceptionAction() throws Throwable {
541          Policy savedPolicy = null;
542          try {
543              savedPolicy = Policy.getPolicy();
# Line 597 | Line 545 | public class ForkJoinPoolTest extends JS
545              policy.addPermission(new RuntimePermission("getContextClassLoader"));
546              policy.addPermission(new RuntimePermission("setContextClassLoader"));
547              Policy.setPolicy(policy);
548 <        } catch(AccessControlException ok) {
548 >        } catch (AccessControlException ok) {
549              return;
550          }
551  
# Line 611 | Line 559 | public class ForkJoinPoolTest extends JS
559              Object result = future.get();
560              assertSame(TEST_STRING, result);
561          }
614        catch (ExecutionException ex) {
615            unexpectedException();
616        }
617        catch (InterruptedException ex) {
618            unexpectedException();
619        }
562          finally {
563              Policy.setPolicy(savedPolicy);
564          }
# Line 625 | Line 567 | public class ForkJoinPoolTest extends JS
567      /**
568       * A submitted failed privileged exception action reports exception
569       */
570 <    public void testSubmitFailedPrivilegedExceptionAction() {
570 >    public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
571          Policy savedPolicy = null;
572          try {
573              savedPolicy = Policy.getPolicy();
# Line 633 | Line 575 | public class ForkJoinPoolTest extends JS
575              policy.addPermission(new RuntimePermission("getContextClassLoader"));
576              policy.addPermission(new RuntimePermission("setContextClassLoader"));
577              Policy.setPolicy(policy);
578 <        } catch(AccessControlException ok) {
578 >        } catch (AccessControlException ok) {
579              return;
580          }
581  
# Line 647 | Line 589 | public class ForkJoinPoolTest extends JS
589  
590              Object result = future.get();
591              shouldThrow();
592 <        }
593 <        catch (ExecutionException success) {
594 <        } catch (CancellationException success) {
653 <        } catch (InterruptedException ex) {
654 <            unexpectedException();
655 <        }
656 <        finally {
592 >        } catch (ExecutionException success) {
593 >            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
594 >        } finally {
595              Policy.setPolicy(savedPolicy);
596          }
597      }
598  
599      /**
600 <     * execute(null runnable) throws NPE
600 >     * execute(null runnable) throws NullPointerException
601       */
602      public void testExecuteNullRunnable() {
603          try {
# Line 667 | Line 605 | public class ForkJoinPoolTest extends JS
605              TrackedShortRunnable task = null;
606              Future<?> future = e.submit(task);
607              shouldThrow();
608 <        }
671 <        catch (NullPointerException success) {
672 <        }
673 <        catch (Exception ex) {
674 <            unexpectedException();
675 <        }
608 >        } catch (NullPointerException success) {}
609      }
610  
611  
612      /**
613 <     * submit(null callable) throws NPE
613 >     * submit(null callable) throws NullPointerException
614       */
615      public void testSubmitNullCallable() {
616          try {
# Line 685 | Line 618 | public class ForkJoinPoolTest extends JS
618              StringTask t = null;
619              Future<String> future = e.submit(t);
620              shouldThrow();
621 <        }
689 <        catch (NullPointerException success) {
690 <        }
691 <        catch (Exception ex) {
692 <            unexpectedException();
693 <        }
621 >        } catch (NullPointerException success) {}
622      }
623  
624  
625      /**
626 <     *  Blocking on submit(callable) throws InterruptedException if
627 <     *  caller interrupted.
626 >     * Blocking on submit(callable) throws InterruptedException if
627 >     * caller interrupted.
628       */
629 <    public void testInterruptedSubmit() {
629 >    public void testInterruptedSubmit() throws InterruptedException {
630          final ForkJoinPool p = new ForkJoinPool(1);
631 <        Thread t = new Thread(new Runnable() {
632 <                public void run() {
633 <                    try {
634 <                        p.submit(new Callable<Object>() {
635 <                                public Object call() {
636 <                                    try {
637 <                                        Thread.sleep(MEDIUM_DELAY_MS);
638 <                                        shouldThrow();
639 <                                    } catch(InterruptedException e){
640 <                                    }
641 <                                    return null;
642 <                                }
643 <                            }).get();
644 <                    } catch(InterruptedException success){
645 <                    } catch(Exception e) {
646 <                        unexpectedException();
647 <                    }
648 <
721 <                }
722 <            });
723 <        try {
724 <            t.start();
725 <            Thread.sleep(SHORT_DELAY_MS);
726 <            t.interrupt();
727 <        } catch(Exception e){
728 <            unexpectedException();
729 <        }
631 >
632 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
633 >            public void realRun() throws Throwable {
634 >                p.submit(new CheckedCallable<Object>() {
635 >                    public Object realCall() throws Throwable {
636 >                        try {
637 >                            Thread.sleep(MEDIUM_DELAY_MS);
638 >                        } catch (InterruptedException ok) {
639 >                        }
640 >                        return null;
641 >                    }}).get();
642 >            }});
643 >
644 >        t.start();
645 >        Thread.sleep(SHORT_DELAY_MS);
646 >        t.interrupt();
647 >        t.join();
648 >        p.shutdownNow();
649          joinPool(p);
650      }
651  
652      /**
653 <     *  get of submit(callable) throws ExecutionException if callable
654 <     *  throws exception
653 >     * get of submit(callable) throws ExecutionException if callable
654 >     * throws exception
655       */
656 <    public void testSubmitEE() {
656 >    public void testSubmitEE() throws Throwable {
657          ForkJoinPool p = new ForkJoinPool(1);
739
658          try {
659 <            Callable c = new Callable() {
660 <                    public Object call() {
661 <                        int i = 5/0;
662 <                        return Boolean.TRUE;
663 <                    }
746 <                };
747 <
748 <            for(int i =0; i < 5; i++){
749 <                p.submit(c).get();
750 <            }
751 <
659 >            p.submit(new Callable() {
660 >                public Object call() {
661 >                    int i = 5/0;
662 >                    return Boolean.TRUE;
663 >                }}).get();
664              shouldThrow();
665 +        } catch (ExecutionException success) {
666 +            assertTrue(success.getCause() instanceof ArithmeticException);
667          }
668 <        catch(ExecutionException success){
755 <        } catch (CancellationException success) {
756 <        } catch(Exception e) {
757 <            unexpectedException();
758 <        }
668 >
669          joinPool(p);
670      }
671  
672      /**
673 <     * invokeAny(null) throws NPE
673 >     * invokeAny(null) throws NullPointerException
674       */
675 <    public void testInvokeAny1() {
675 >    public void testInvokeAny1() throws Throwable {
676          ExecutorService e = new ForkJoinPool(1);
677          try {
678              e.invokeAny(null);
679 +            shouldThrow();
680          } catch (NullPointerException success) {
770        } catch(Exception ex) {
771            unexpectedException();
681          } finally {
682              joinPool(e);
683          }
684      }
685  
686      /**
687 <     * invokeAny(empty collection) throws IAE
687 >     * invokeAny(empty collection) throws IllegalArgumentException
688       */
689 <    public void testInvokeAny2() {
689 >    public void testInvokeAny2() throws Throwable {
690          ExecutorService e = new ForkJoinPool(1);
691          try {
692              e.invokeAny(new ArrayList<Callable<String>>());
693 +            shouldThrow();
694          } catch (IllegalArgumentException success) {
785        } catch(Exception ex) {
786            unexpectedException();
695          } finally {
696              joinPool(e);
697          }
698      }
699  
700      /**
701 <     * invokeAny(c) throws NPE if c has null elements
701 >     * invokeAny(c) throws NullPointerException if c has a single null element
702       */
703 <    public void testInvokeAny3() {
703 >    public void testInvokeAny3() throws Throwable {
704          ExecutorService e = new ForkJoinPool(1);
705 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
706 +        l.add(null);
707 +        try {
708 +            e.invokeAny(l);
709 +            shouldThrow();
710 +        } catch (NullPointerException success) {
711 +        } finally {
712 +            joinPool(e);
713 +        }
714 +    }
715 +
716 +    /**
717 +     * invokeAny(c) throws NullPointerException if c has null elements
718 +     */
719 +    public void testInvokeAny4() throws Throwable {
720 +        CountDownLatch latch = new CountDownLatch(1);
721 +        ExecutorService e = new ForkJoinPool(1);
722 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
723 +        l.add(latchAwaitingStringTask(latch));
724 +        l.add(null);
725          try {
798            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
799            l.add(new StringTask());
800            l.add(null);
726              e.invokeAny(l);
727 +            shouldThrow();
728          } catch (NullPointerException success) {
803        } catch(Exception ex) {
804            ex.printStackTrace();
805            unexpectedException();
729          } finally {
730 +            latch.countDown();
731              joinPool(e);
732          }
733      }
# Line 811 | Line 735 | public class ForkJoinPoolTest extends JS
735      /**
736       * invokeAny(c) throws ExecutionException if no task in c completes
737       */
738 <    public void testInvokeAny4() {
738 >    public void testInvokeAny5() throws Throwable {
739          ExecutorService e = new ForkJoinPool(1);
740 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
741 +        l.add(new NPETask());
742          try {
817            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
818            l.add(new NPETask());
743              e.invokeAny(l);
744 <        } catch(ExecutionException success) {
745 <        } catch (CancellationException success) {
746 <        } catch(Exception ex) {
823 <            unexpectedException();
744 >            shouldThrow();
745 >        } catch (ExecutionException success) {
746 >            assertTrue(success.getCause() instanceof NullPointerException);
747          } finally {
748              joinPool(e);
749          }
# Line 829 | Line 752 | public class ForkJoinPoolTest extends JS
752      /**
753       * invokeAny(c) returns result of some task in c if at least one completes
754       */
755 <    public void testInvokeAny5() {
755 >    public void testInvokeAny6() throws Throwable {
756          ExecutorService e = new ForkJoinPool(1);
757          try {
758 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
758 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
759              l.add(new StringTask());
760              l.add(new StringTask());
761              String result = e.invokeAny(l);
762              assertSame(TEST_STRING, result);
840        } catch (ExecutionException success) {
841        } catch (CancellationException success) {
842        } catch(Exception ex) {
843            unexpectedException();
763          } finally {
764              joinPool(e);
765          }
766      }
767  
768      /**
769 <     * invokeAll(null) throws NPE
769 >     * invokeAll(null) throws NullPointerException
770       */
771 <    public void testInvokeAll1() {
771 >    public void testInvokeAll1() throws Throwable {
772          ExecutorService e = new ForkJoinPool(1);
773          try {
774              e.invokeAll(null);
775 +            shouldThrow();
776          } catch (NullPointerException success) {
857        } catch(Exception ex) {
858            unexpectedException();
777          } finally {
778              joinPool(e);
779          }
# Line 864 | Line 782 | public class ForkJoinPoolTest extends JS
782      /**
783       * invokeAll(empty collection) returns empty collection
784       */
785 <    public void testInvokeAll2() {
785 >    public void testInvokeAll2() throws InterruptedException {
786          ExecutorService e = new ForkJoinPool(1);
787          try {
788 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
788 >            List<Future<String>> r
789 >                = e.invokeAll(new ArrayList<Callable<String>>());
790              assertTrue(r.isEmpty());
872        } catch(Exception ex) {
873            unexpectedException();
791          } finally {
792              joinPool(e);
793          }
794      }
795  
796      /**
797 <     * invokeAll(c) throws NPE if c has null elements
797 >     * invokeAll(c) throws NullPointerException if c has null elements
798       */
799 <    public void testInvokeAll3() {
799 >    public void testInvokeAll3() throws InterruptedException {
800          ExecutorService e = new ForkJoinPool(1);
801 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
802 +        l.add(new StringTask());
803 +        l.add(null);
804          try {
885            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
886            l.add(new StringTask());
887            l.add(null);
805              e.invokeAll(l);
806 +            shouldThrow();
807          } catch (NullPointerException success) {
890        } catch(Exception ex) {
891            unexpectedException();
808          } finally {
809              joinPool(e);
810          }
811      }
812  
813      /**
814 <     * get of returned element of invokeAll(c) throws exception on failed task
814 >     * get of returned element of invokeAll(c) throws
815 >     * ExecutionException on failed task
816       */
817 <    public void testInvokeAll4() {
817 >    public void testInvokeAll4() throws Throwable {
818          ExecutorService e = new ForkJoinPool(1);
819 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
820 +        l.add(new NPETask());
821 +        List<Future<String>> futures = e.invokeAll(l);
822 +        assertEquals(1, futures.size());
823          try {
824 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
825 <            l.add(new NPETask());
826 <            List<Future<String>> result = e.invokeAll(l);
827 <            assertEquals(1, result.size());
907 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
908 <                it.next().get();
909 <        } catch(ExecutionException success) {
910 <        } catch (CancellationException success) {
911 <        } catch(Exception ex) {
912 <            ex.printStackTrace();
913 <            unexpectedException();
824 >            futures.get(0).get();
825 >            shouldThrow();
826 >        } catch (ExecutionException success) {
827 >            assertTrue(success.getCause() instanceof NullPointerException);
828          } finally {
829              joinPool(e);
830          }
# Line 919 | Line 833 | public class ForkJoinPoolTest extends JS
833      /**
834       * invokeAll(c) returns results of all completed tasks in c
835       */
836 <    public void testInvokeAll5() {
836 >    public void testInvokeAll5() throws Throwable {
837          ExecutorService e = new ForkJoinPool(1);
838          try {
839 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
839 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
840              l.add(new StringTask());
841              l.add(new StringTask());
842 <            List<Future<String>> result = e.invokeAll(l);
843 <            assertEquals(2, result.size());
844 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
845 <                assertSame(TEST_STRING, it.next().get());
932 <        } catch (ExecutionException success) {
933 <        } catch (CancellationException success) {
934 <        } catch(Exception ex) {
935 <            ex.printStackTrace();
936 <            unexpectedException();
842 >            List<Future<String>> futures = e.invokeAll(l);
843 >            assertEquals(2, futures.size());
844 >            for (Future<String> future : futures)
845 >                assertSame(TEST_STRING, future.get());
846          } finally {
847              joinPool(e);
848          }
# Line 941 | Line 850 | public class ForkJoinPoolTest extends JS
850  
851  
852      /**
853 <     * timed invokeAny(null) throws NPE
853 >     * timed invokeAny(null) throws NullPointerException
854       */
855 <    public void testTimedInvokeAny1() {
855 >    public void testTimedInvokeAny1() throws Throwable {
856          ExecutorService e = new ForkJoinPool(1);
857          try {
858 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
858 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
859 >            shouldThrow();
860          } catch (NullPointerException success) {
951        } catch(Exception ex) {
952            ex.printStackTrace();
953            unexpectedException();
861          } finally {
862              joinPool(e);
863          }
864      }
865  
866      /**
867 <     * timed invokeAny(null time unit) throws NPE
867 >     * timed invokeAny(null time unit) throws NullPointerException
868       */
869 <    public void testTimedInvokeAnyNullTimeUnit() {
869 >    public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
870          ExecutorService e = new ForkJoinPool(1);
871 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
872 +        l.add(new StringTask());
873          try {
965            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
966            l.add(new StringTask());
874              e.invokeAny(l, MEDIUM_DELAY_MS, null);
875 +            shouldThrow();
876          } catch (NullPointerException success) {
969        } catch(Exception ex) {
970            ex.printStackTrace();
971            unexpectedException();
877          } finally {
878              joinPool(e);
879          }
880      }
881  
882      /**
883 <     * timed invokeAny(empty collection) throws IAE
883 >     * timed invokeAny(empty collection) throws IllegalArgumentException
884       */
885 <    public void testTimedInvokeAny2() {
885 >    public void testTimedInvokeAny2() throws Throwable {
886          ExecutorService e = new ForkJoinPool(1);
887          try {
888 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
888 >            e.invokeAny(new ArrayList<Callable<String>>(),
889 >                        MEDIUM_DELAY_MS, MILLISECONDS);
890 >            shouldThrow();
891          } catch (IllegalArgumentException success) {
985        } catch(Exception ex) {
986            ex.printStackTrace();
987            unexpectedException();
892          } finally {
893              joinPool(e);
894          }
895      }
896  
897      /**
898 <     * timed invokeAny(c) throws NPE if c has null elements
898 >     * timed invokeAny(c) throws NullPointerException if c has null elements
899       */
900 <    public void testTimedInvokeAny3() {
900 >    public void testTimedInvokeAny3() throws Throwable {
901 >        CountDownLatch latch = new CountDownLatch(1);
902          ExecutorService e = new ForkJoinPool(1);
903 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
904 +        l.add(latchAwaitingStringTask(latch));
905 +        l.add(null);
906          try {
907 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
908 <            l.add(new StringTask());
1001 <            l.add(null);
1002 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
907 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
908 >            shouldThrow();
909          } catch (NullPointerException success) {
1004        } catch(Exception ex) {
1005            ex.printStackTrace();
1006            unexpectedException();
910          } finally {
911 +            latch.countDown();
912              joinPool(e);
913          }
914      }
# Line 1012 | Line 916 | public class ForkJoinPoolTest extends JS
916      /**
917       * timed invokeAny(c) throws ExecutionException if no task completes
918       */
919 <    public void testTimedInvokeAny4() {
919 >    public void testTimedInvokeAny4() throws Throwable {
920          ExecutorService e = new ForkJoinPool(1);
921 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
922 +        l.add(new NPETask());
923          try {
924 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
925 <            l.add(new NPETask());
926 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
927 <        } catch(ExecutionException success) {
1022 <        } catch (CancellationException success) {
1023 <        } catch(Exception ex) {
1024 <            ex.printStackTrace();
1025 <            unexpectedException();
924 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
925 >            shouldThrow();
926 >        } catch (ExecutionException success) {
927 >            assertTrue(success.getCause() instanceof NullPointerException);
928          } finally {
929              joinPool(e);
930          }
# Line 1031 | Line 933 | public class ForkJoinPoolTest extends JS
933      /**
934       * timed invokeAny(c) returns result of some task in c
935       */
936 <    public void testTimedInvokeAny5() {
936 >    public void testTimedInvokeAny5() throws Throwable {
937          ExecutorService e = new ForkJoinPool(1);
938          try {
939 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
939 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
940              l.add(new StringTask());
941              l.add(new StringTask());
942 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
942 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
943              assertSame(TEST_STRING, result);
1042        } catch (ExecutionException success) {
1043        } catch (CancellationException success) {
1044        } catch(Exception ex) {
1045            ex.printStackTrace();
1046            unexpectedException();
944          } finally {
945              joinPool(e);
946          }
947      }
948  
949      /**
950 <     * timed invokeAll(null) throws NPE
950 >     * timed invokeAll(null) throws NullPointerException
951       */
952 <    public void testTimedInvokeAll1() {
952 >    public void testTimedInvokeAll1() throws Throwable {
953          ExecutorService e = new ForkJoinPool(1);
954          try {
955 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
955 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
956 >            shouldThrow();
957          } catch (NullPointerException success) {
1060        } catch(Exception ex) {
1061            ex.printStackTrace();
1062            unexpectedException();
958          } finally {
959              joinPool(e);
960          }
961      }
962  
963      /**
964 <     * timed invokeAll(null time unit) throws NPE
964 >     * timed invokeAll(null time unit) throws NullPointerException
965       */
966 <    public void testTimedInvokeAllNullTimeUnit() {
966 >    public void testTimedInvokeAllNullTimeUnit() throws Throwable {
967          ExecutorService e = new ForkJoinPool(1);
968 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
969 +        l.add(new StringTask());
970          try {
1074            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1075            l.add(new StringTask());
971              e.invokeAll(l, MEDIUM_DELAY_MS, null);
972 +            shouldThrow();
973          } catch (NullPointerException success) {
1078        } catch(Exception ex) {
1079            ex.printStackTrace();
1080            unexpectedException();
974          } finally {
975              joinPool(e);
976          }
# Line 1086 | Line 979 | public class ForkJoinPoolTest extends JS
979      /**
980       * timed invokeAll(empty collection) returns empty collection
981       */
982 <    public void testTimedInvokeAll2() {
982 >    public void testTimedInvokeAll2() throws InterruptedException {
983          ExecutorService e = new ForkJoinPool(1);
984          try {
985 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
985 >            List<Future<String>> r
986 >                = e.invokeAll(new ArrayList<Callable<String>>(),
987 >                              MEDIUM_DELAY_MS, MILLISECONDS);
988              assertTrue(r.isEmpty());
1094        } catch(Exception ex) {
1095            ex.printStackTrace();
1096            unexpectedException();
989          } finally {
990              joinPool(e);
991          }
992      }
993  
994      /**
995 <     * timed invokeAll(c) throws NPE if c has null elements
995 >     * timed invokeAll(c) throws NullPointerException if c has null elements
996       */
997 <    public void testTimedInvokeAll3() {
997 >    public void testTimedInvokeAll3() throws InterruptedException {
998          ExecutorService e = new ForkJoinPool(1);
999 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1000 +        l.add(new StringTask());
1001 +        l.add(null);
1002          try {
1003 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1004 <            l.add(new StringTask());
1110 <            l.add(null);
1111 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1003 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1004 >            shouldThrow();
1005          } catch (NullPointerException success) {
1113        } catch(Exception ex) {
1114            ex.printStackTrace();
1115            unexpectedException();
1006          } finally {
1007              joinPool(e);
1008          }
# Line 1121 | Line 1011 | public class ForkJoinPoolTest extends JS
1011      /**
1012       * get of returned element of invokeAll(c) throws exception on failed task
1013       */
1014 <    public void testTimedInvokeAll4() {
1014 >    public void testTimedInvokeAll4() throws Throwable {
1015          ExecutorService e = new ForkJoinPool(1);
1016 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
1017 +        l.add(new NPETask());
1018 +        List<Future<String>> futures
1019 +            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1020 +        assertEquals(1, futures.size());
1021          try {
1022 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1023 <            l.add(new NPETask());
1024 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1025 <            assertEquals(1, result.size());
1131 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1132 <                it.next().get();
1133 <        } catch(ExecutionException success) {
1134 <        } catch (CancellationException success) {
1135 <        } catch(Exception ex) {
1136 <            ex.printStackTrace();
1137 <            unexpectedException();
1022 >            futures.get(0).get();
1023 >            shouldThrow();
1024 >        } catch (ExecutionException success) {
1025 >            assertTrue(success.getCause() instanceof NullPointerException);
1026          } finally {
1027              joinPool(e);
1028          }
# Line 1143 | Line 1031 | public class ForkJoinPoolTest extends JS
1031      /**
1032       * timed invokeAll(c) returns results of all completed tasks in c
1033       */
1034 <    public void testTimedInvokeAll5() {
1034 >    public void testTimedInvokeAll5() throws Throwable {
1035          ExecutorService e = new ForkJoinPool(1);
1036          try {
1037 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1037 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1038              l.add(new StringTask());
1039              l.add(new StringTask());
1040 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1041 <            assertEquals(2, result.size());
1042 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1043 <                assertSame(TEST_STRING, it.next().get());
1044 <        } catch (ExecutionException success) {
1157 <        } catch (CancellationException success) {
1158 <        } catch(Exception ex) {
1159 <            ex.printStackTrace();
1160 <            unexpectedException();
1040 >            List<Future<String>> futures
1041 >                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1042 >            assertEquals(2, futures.size());
1043 >            for (Future<String> future : futures)
1044 >                assertSame(TEST_STRING, future.get());
1045          } finally {
1046              joinPool(e);
1047          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines