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.22 by jsr166, Wed Sep 1 06:41:55 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 25 | Line 40 | public class ForkJoinPoolTest extends JS
40       * 1. shutdown and related methods are tested via super.joinPool.
41       *
42       * 2. newTaskFor and adapters are tested in submit/invoke tests
43 <     *
43 >     *
44       * 3. We cannot portably test monitoring methods such as
45       * getStealCount() since they rely ultimately on random task
46       * stealing that may cause tasks not to be stolen/propagated
47       * across threads, especially on uniprocessors.
48 <     *
48 >     *
49       * 4. There are no independently testable ForkJoinWorkerThread
50       * methods, but they are covered here and in task tests.
51       */
# 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;
70 <        public ForkJoinWorkerThread newThread(ForkJoinPool p){
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);
74 <        }  
74 >        }
75      }
76  
77 <    static class SubFJP extends ForkJoinPool { // to expose protected
77 >    static class SubFJP extends ForkJoinPool { // to expose protected
78          SubFJP() { super(1); }
79          public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
80              return super.drainTasksTo(c);
# Line 83 | Line 99 | public class ForkJoinPoolTest extends JS
99      }
100  
101      // A simple recursive task for testing
102 <    static final class FibTask extends RecursiveTask<Integer> {
102 >    static final class FibTask extends RecursiveTask<Integer> {
103          final int number;
104          FibTask(int n) { number = n; }
105          public Integer compute() {
# Line 97 | Line 113 | public class ForkJoinPoolTest extends JS
113      }
114  
115      // A failing task for testing
116 <    static final class FailingTask extends ForkJoinTask<Void> {
116 >    static final class FailingTask extends ForkJoinTask<Void> {
117          public final Void getRawResult() { return null; }
118          protected final void setRawResult(Void mustBeNull) { }
119          protected final boolean exec() { throw new Error(); }
# Line 105 | Line 121 | public class ForkJoinPoolTest extends JS
121      }
122  
123      // Fib needlessly using locking to test ManagedBlockers
124 <    static final class LockingFibTask extends RecursiveTask<Integer> {
124 >    static final class LockingFibTask extends RecursiveTask<Integer> {
125          final int number;
126          final ManagedLocker locker;
127          final ReentrantLock lock;
128 <        LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
129 <            number = n;
128 >        LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
129 >            number = n;
130              this.locker = locker;
131              this.lock = lock;
132          }
# Line 119 | Line 135 | public class ForkJoinPoolTest extends JS
135              LockingFibTask f1 = null;
136              LockingFibTask f2 = null;
137              locker.block();
138 <            n = number;
138 >            n = number;
139              if (n > 1) {
140                  f1 = new LockingFibTask(n - 1, locker, lock);
141                  f2 = new LockingFibTask(n - 2, locker, lock);
# Line 134 | Line 150 | public class ForkJoinPoolTest extends JS
150          }
151      }
152  
153 <    /**
154 <     * Succesfully constructed pool reports default factory,
153 >    /**
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 160 | Line 176 | public class ForkJoinPoolTest extends JS
176          }
177      }
178  
179 <    /**
180 <     * Constructor throws if size argument is less than zero
179 >    /**
180 >     * Constructor throws if size argument is less than zero
181       */
182      public void testConstructor1() {
183          try {
184              new ForkJoinPool(-1);
185              shouldThrow();
186 <        }
171 <        catch (IllegalArgumentException success){}
186 >        } catch (IllegalArgumentException success) {}
187      }
188  
189 <    /**
190 <     * Constructor throws if factory argument is null
189 >    /**
190 >     * Constructor throws if factory argument is null
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  
200 <    /**
201 <     * getParallelism returns size set in constructor
200 >    /**
201 >     * getParallelism returns size set in constructor
202       */
203      public void testGetParallelism() {
204          ForkJoinPool p = null;
# Line 196 | Line 210 | public class ForkJoinPoolTest extends JS
210          }
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 <    /**
213 >    /**
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 243 | Line 226 | public class ForkJoinPoolTest extends JS
226          }
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 <    /**
229 >    /**
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();
323        } finally {
324            joinPool(p);
325        }
326    }
327
328    /**
329     * setUncaughtExceptionHandler of null removes handler
330     */
331    public void testSetUncaughtExceptionHandler2() {
332        ForkJoinPool p = null;
333        try {
334            p = new ForkJoinPool(1);
335            p.setUncaughtExceptionHandler(null);
336            assertNull(p.getUncaughtExceptionHandler());
244          } finally {
245 +            p.shutdownNow();
246              joinPool(p);
247          }
248      }
249  
250 <
343 <    /**
250 >    /**
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 468 | Line 364 | public class ForkJoinPoolTest extends JS
364          }
365      }
366  
367 <    
367 >
368      // FJ Versions of AbstractExecutorService tests
369  
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 558 | Line 422 | public class ForkJoinPoolTest extends JS
422              policy.addPermission(new RuntimePermission("getContextClassLoader"));
423              policy.addPermission(new RuntimePermission("setContextClassLoader"));
424              Policy.setPolicy(policy);
425 <        } catch(AccessControlException ok) {
425 >        } catch (AccessControlException ok) {
426              return;
427          }
428          try {
# 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 597 | Line 451 | public class ForkJoinPoolTest extends JS
451              policy.addPermission(new RuntimePermission("getContextClassLoader"));
452              policy.addPermission(new RuntimePermission("setContextClassLoader"));
453              Policy.setPolicy(policy);
454 <        } catch(AccessControlException ok) {
454 >        } catch (AccessControlException ok) {
455              return;
456          }
457  
# 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 633 | Line 481 | public class ForkJoinPoolTest extends JS
481              policy.addPermission(new RuntimePermission("getContextClassLoader"));
482              policy.addPermission(new RuntimePermission("setContextClassLoader"));
483              Policy.setPolicy(policy);
484 <        } catch(AccessControlException ok) {
484 >        } catch (AccessControlException ok) {
485              return;
486          }
487  
# 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 +            assertTrue(success.getCause() instanceof ArithmeticException);
573          }
574 <        catch(ExecutionException success){
755 <        } catch (CancellationException success) {
756 <        } catch(Exception e) {
757 <            unexpectedException();
758 <        }
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) {
770        } catch(Exception ex) {
771            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) {
785        } catch(Exception ex) {
786            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 {
614 +            e.invokeAny(l);
615 +            shouldThrow();
616 +        } catch (NullPointerException success) {
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 {
798            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
799            l.add(new StringTask());
800            l.add(null);
632              e.invokeAny(l);
633 +            shouldThrow();
634          } catch (NullPointerException success) {
803        } catch(Exception ex) {
804            ex.printStackTrace();
805            unexpectedException();
635          } finally {
636 +            latch.countDown();
637              joinPool(e);
638          }
639      }
# Line 811 | Line 641 | public class ForkJoinPoolTest extends JS
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 {
817            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
818            l.add(new NPETask());
649              e.invokeAny(l);
650 <        } catch(ExecutionException success) {
651 <        } catch (CancellationException success) {
652 <        } catch(Exception ex) {
823 <            unexpectedException();
650 >            shouldThrow();
651 >        } catch (ExecutionException success) {
652 >            assertTrue(success.getCause() instanceof NullPointerException);
653          } finally {
654              joinPool(e);
655          }
# Line 829 | 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);
840        } catch (ExecutionException success) {
841        } catch (CancellationException success) {
842        } catch(Exception ex) {
843            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) {
857        } catch(Exception ex) {
858            unexpectedException();
683          } finally {
684              joinPool(e);
685          }
# Line 864 | 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());
872        } catch(Exception ex) {
873            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 {
885            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
886            l.add(new StringTask());
887            l.add(null);
711              e.invokeAll(l);
712 +            shouldThrow();
713          } catch (NullPointerException success) {
890        } catch(Exception ex) {
891            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());
732 <            List<Future<String>> result = e.invokeAll(l);
733 <            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();
730 >            futures.get(0).get();
731 >            shouldThrow();
732 >        } catch (ExecutionException success) {
733 >            assertTrue(success.getCause() instanceof NullPointerException);
734          } finally {
735              joinPool(e);
736          }
# Line 919 | 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());
932 <        } catch (ExecutionException success) {
933 <        } catch (CancellationException success) {
934 <        } catch(Exception ex) {
935 <            ex.printStackTrace();
936 <            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 941 | 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) {
951        } catch(Exception ex) {
952            ex.printStackTrace();
953            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 {
965            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
966            l.add(new StringTask());
780              e.invokeAny(l, MEDIUM_DELAY_MS, null);
781 +            shouldThrow();
782          } catch (NullPointerException success) {
969        } catch(Exception ex) {
970            ex.printStackTrace();
971            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) {
985        } catch(Exception ex) {
986            ex.printStackTrace();
987            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 {
999            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1000            l.add(new StringTask());
1001            l.add(null);
813              e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
814 +            shouldThrow();
815          } catch (NullPointerException success) {
1004        } catch(Exception ex) {
1005            ex.printStackTrace();
1006            unexpectedException();
816          } finally {
817 +            latch.countDown();
818              joinPool(e);
819          }
820      }
# Line 1012 | 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 {
1018            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1019            l.add(new NPETask());
830              e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
831 <        } catch(ExecutionException success) {
832 <        } catch (CancellationException success) {
833 <        } catch(Exception ex) {
1024 <            ex.printStackTrace();
1025 <            unexpectedException();
831 >            shouldThrow();
832 >        } catch (ExecutionException success) {
833 >            assertTrue(success.getCause() instanceof NullPointerException);
834          } finally {
835              joinPool(e);
836          }
# Line 1031 | 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);
1042        } catch (ExecutionException success) {
1043        } catch (CancellationException success) {
1044        } catch(Exception ex) {
1045            ex.printStackTrace();
1046            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) {
1060        } catch(Exception ex) {
1061            ex.printStackTrace();
1062            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 {
1074            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1075            l.add(new StringTask());
877              e.invokeAll(l, MEDIUM_DELAY_MS, null);
878 +            shouldThrow();
879          } catch (NullPointerException success) {
1078        } catch(Exception ex) {
1079            ex.printStackTrace();
1080            unexpectedException();
880          } finally {
881              joinPool(e);
882          }
# Line 1086 | 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());
1094        } catch(Exception ex) {
1095            ex.printStackTrace();
1096            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 {
1108            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1109            l.add(new StringTask());
1110            l.add(null);
909              e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
910 +            shouldThrow();
911          } catch (NullPointerException success) {
1113        } catch(Exception ex) {
1114            ex.printStackTrace();
1115            unexpectedException();
912          } finally {
913              joinPool(e);
914          }
# Line 1121 | 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());
930 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
931 <            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();
928 >            futures.get(0).get();
929 >            shouldThrow();
930 >        } catch (ExecutionException success) {
931 >            assertTrue(success.getCause() instanceof NullPointerException);
932          } finally {
933              joinPool(e);
934          }
# Line 1143 | 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) {
1157 <        } catch (CancellationException success) {
1158 <        } catch(Exception ex) {
1159 <            ex.printStackTrace();
1160 <            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