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.37 by dl, Tue Feb 22 01:18:58 2011 UTC vs.
Revision 1.48 by jsr166, Wed Feb 6 16:36:36 2013 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# Line 22 | Line 22 | import java.util.concurrent.ForkJoinTask
22   import java.util.concurrent.ForkJoinWorkerThread;
23   import java.util.concurrent.RecursiveTask;
24   import java.util.concurrent.TimeUnit;
25 + import java.util.concurrent.atomic.AtomicBoolean;
26   import java.util.concurrent.locks.ReentrantLock;
27   import static java.util.concurrent.TimeUnit.MILLISECONDS;
28   import java.security.AccessControlException;
# Line 198 | Line 199 | public class ForkJoinPoolTest extends JS
199          } catch (NullPointerException success) {}
200      }
201  
201
202      /**
203       * getParallelism returns size set in constructor
204       */
# Line 242 | Line 242 | public class ForkJoinPoolTest extends JS
242                                            eh, false);
243          try {
244              assertSame(eh, p.getUncaughtExceptionHandler());
245 <            p.execute(new FibTask(8));
246 <            assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
245 >            try {
246 >                p.execute(new FibTask(8));
247 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
248 >            } catch (RejectedExecutionException ok) {
249 >            }
250          } finally {
251              p.shutdownNow(); // failure might have prevented processing task
252              joinPool(p);
# Line 251 | Line 254 | public class ForkJoinPoolTest extends JS
254      }
255  
256      /**
257 <     * After invoking a single task, isQuiescent is true,
258 <     * queues are empty, threads are not active, and
259 <     * construction parameters continue to hold
257 >     * After invoking a single task, isQuiescent eventually becomes
258 >     * true, at which time queues are empty, threads are not active,
259 >     * the task has completed successfully, and construction
260 >     * parameters continue to hold
261       */
262 <    public void testisQuiescent() throws InterruptedException {
262 >    public void testIsQuiescent() throws Exception {
263          ForkJoinPool p = new ForkJoinPool(2);
264          try {
265              assertTrue(p.isQuiescent());
266 <            p.invoke(new FibTask(20));
266 >            long startTime = System.nanoTime();
267 >            FibTask f = new FibTask(20);
268 >            p.invoke(f);
269              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
270                         p.getFactory());
271 <            Thread.sleep(SMALL_DELAY_MS);
271 >            while (! p.isQuiescent()) {
272 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
273 >                    throw new AssertionFailedError("timed out");
274 >                assertFalse(p.getAsyncMode());
275 >                assertFalse(p.isShutdown());
276 >                assertFalse(p.isTerminating());
277 >                assertFalse(p.isTerminated());
278 >                Thread.yield();
279 >            }
280 >
281              assertTrue(p.isQuiescent());
282              assertFalse(p.getAsyncMode());
283              assertEquals(0, p.getActiveThreadCount());
# Line 272 | Line 287 | public class ForkJoinPoolTest extends JS
287              assertFalse(p.isShutdown());
288              assertFalse(p.isTerminating());
289              assertFalse(p.isTerminated());
290 +            assertTrue(f.isDone());
291 +            assertEquals(6765, (int) f.get());
292          } finally {
293              joinPool(p);
294          }
# Line 365 | Line 382 | public class ForkJoinPoolTest extends JS
382          }
383      }
384  
368
385      // FJ Versions of AbstractExecutorService tests
386  
387      /**
# Line 374 | Line 390 | public class ForkJoinPoolTest extends JS
390      public void testExecuteRunnable() throws Throwable {
391          ExecutorService e = new ForkJoinPool(1);
392          try {
393 <            TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS);
394 <            assertFalse(task.isDone());
393 >            final AtomicBoolean done = new AtomicBoolean(false);
394 >            CheckedRunnable task = new CheckedRunnable() {
395 >                public void realRun() {
396 >                    done.set(true);
397 >                }};
398              Future<?> future = e.submit(task);
399              assertNull(future.get());
400 <            assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
401 <            assertTrue(task.isDone());
400 >            assertNull(future.get(0, MILLISECONDS));
401 >            assertTrue(done.get());
402              assertTrue(future.isDone());
403              assertFalse(future.isCancelled());
404          } finally {
# Line 387 | Line 406 | public class ForkJoinPoolTest extends JS
406          }
407      }
408  
390
409      /**
410       * Completed submit(callable) returns result
411       */
# Line 437 | Line 455 | public class ForkJoinPoolTest extends JS
455       * A submitted privileged action runs to completion
456       */
457      public void testSubmitPrivilegedAction() throws Exception {
458 +        final Callable callable = Executors.callable(new PrivilegedAction() {
459 +                public Object run() { return TEST_STRING; }});
460          Runnable r = new CheckedRunnable() {
461 <            public void realRun() throws Exception {
462 <                ExecutorService e = new ForkJoinPool(1);
463 <                Future future = e.submit(Executors.callable(new PrivilegedAction() {
464 <                    public Object run() {
445 <                        return TEST_STRING;
446 <                    }}));
447 <
461 >        public void realRun() throws Exception {
462 >            ExecutorService e = new ForkJoinPool(1);
463 >            try {
464 >                Future future = e.submit(callable);
465                  assertSame(TEST_STRING, future.get());
466 <            }};
466 >            } finally {
467 >                joinPool(e);
468 >            }
469 >        }};
470  
471 <        runWithPermissions(r,
452 <                           new RuntimePermission("modifyThread"));
471 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
472      }
473  
474      /**
475       * A submitted privileged exception action runs to completion
476       */
477      public void testSubmitPrivilegedExceptionAction() throws Exception {
478 +        final Callable callable =
479 +            Executors.callable(new PrivilegedExceptionAction() {
480 +                public Object run() { return TEST_STRING; }});
481          Runnable r = new CheckedRunnable() {
482 <            public void realRun() throws Exception {
483 <                ExecutorService e = new ForkJoinPool(1);
484 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
485 <                    public Object run() {
464 <                        return TEST_STRING;
465 <                    }}));
466 <
482 >        public void realRun() throws Exception {
483 >            ExecutorService e = new ForkJoinPool(1);
484 >            try {
485 >                Future future = e.submit(callable);
486                  assertSame(TEST_STRING, future.get());
487 <            }};
487 >            } finally {
488 >                joinPool(e);
489 >            }
490 >        }};
491  
492          runWithPermissions(r, new RuntimePermission("modifyThread"));
493      }
# Line 474 | Line 496 | public class ForkJoinPoolTest extends JS
496       * A submitted failed privileged exception action reports exception
497       */
498      public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
499 +        final Callable callable =
500 +            Executors.callable(new PrivilegedExceptionAction() {
501 +                public Object run() { throw new IndexOutOfBoundsException(); }});
502          Runnable r = new CheckedRunnable() {
503 <            public void realRun() throws Exception {
504 <                ExecutorService e = new ForkJoinPool(1);
505 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
506 <                    public Object run() throws Exception {
482 <                        throw new IndexOutOfBoundsException();
483 <                    }}));
484 <
503 >        public void realRun() throws Exception {
504 >            ExecutorService e = new ForkJoinPool(1);
505 >            try {
506 >                Future future = e.submit(callable);
507                  try {
508                      future.get();
509                      shouldThrow();
510                  } catch (ExecutionException success) {
511                      assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
512 <                }}};
512 >                }
513 >            } finally {
514 >                joinPool(e);
515 >            }
516 >        }};
517  
518          runWithPermissions(r, new RuntimePermission("modifyThread"));
519      }
# Line 506 | Line 532 | public class ForkJoinPoolTest extends JS
532          }
533      }
534  
509
535      /**
536       * submit(null callable) throws NullPointerException
537       */
# Line 521 | Line 546 | public class ForkJoinPoolTest extends JS
546          }
547      }
548  
524
549      /**
550       * submit(callable).get() throws InterruptedException if interrupted
551       */
# Line 559 | Line 583 | public class ForkJoinPoolTest extends JS
583          ForkJoinPool p = new ForkJoinPool(1);
584          try {
585              p.submit(new Callable() {
586 <                public Object call() {
587 <                    int i = 5/0;
564 <                    return Boolean.TRUE;
565 <                }}).get();
586 >                public Object call() { throw new ArithmeticException(); }})
587 >                .get();
588              shouldThrow();
589          } catch (ExecutionException success) {
590              assertTrue(success.getCause() instanceof ArithmeticException);
# Line 750 | Line 772 | public class ForkJoinPoolTest extends JS
772          }
773      }
774  
753
775      /**
776       * timed invokeAny(null) throws NullPointerException
777       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines