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.41 by jsr166, Fri May 27 19:42:42 2011 UTC vs.
Revision 1.50 by jsr166, Mon May 20 16:46:23 2013 UTC

# 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 static java.util.concurrent.TimeUnit.NANOSECONDS;
29   import java.security.AccessControlException;
30   import java.security.Policy;
31   import java.security.PrivilegedAction;
# Line 38 | Line 40 | public class ForkJoinPoolTest extends JS
40          return new TestSuite(ForkJoinPoolTest.class);
41      }
42  
43 <    /**
43 >    /*
44       * Testing coverage notes:
45       *
46       * 1. shutdown and related methods are tested via super.joinPool.
# Line 225 | Line 227 | public class ForkJoinPoolTest extends JS
227      }
228  
229      /**
230 +     * awaitTermination on a non-shutdown pool times out
231 +     */
232 +    public void testAwaitTermination_timesOut() throws InterruptedException {
233 +        ForkJoinPool p = new ForkJoinPool(1);
234 +        assertFalse(p.isTerminated());
235 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
236 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
237 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
238 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
239 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
240 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
241 +        long timeoutNanos = 999999L;
242 +        long startTime = System.nanoTime();
243 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
244 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
245 +        assertFalse(p.isTerminated());
246 +        startTime = System.nanoTime();
247 +        long timeoutMillis = timeoutMillis();
248 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
249 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
250 +        assertFalse(p.isTerminated());
251 +        p.shutdown();
252 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
253 +        assertTrue(p.isTerminated());
254 +    }
255 +
256 +    /**
257       * setUncaughtExceptionHandler changes handler for uncaught exceptions.
258       *
259       * Additionally tests: Overriding ForkJoinWorkerThread.onStart
# Line 241 | Line 270 | public class ForkJoinPoolTest extends JS
270                                            eh, false);
271          try {
272              assertSame(eh, p.getUncaughtExceptionHandler());
273 <            p.execute(new FibTask(8));
274 <            assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
273 >            try {
274 >                p.execute(new FibTask(8));
275 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
276 >            } catch (RejectedExecutionException ok) {
277 >            }
278          } finally {
279              p.shutdownNow(); // failure might have prevented processing task
280              joinPool(p);
# Line 255 | Line 287 | public class ForkJoinPoolTest extends JS
287       * the task has completed successfully, and construction
288       * parameters continue to hold
289       */
290 <    public void testisQuiescent() throws Exception {
290 >    public void testIsQuiescent() throws Exception {
291          ForkJoinPool p = new ForkJoinPool(2);
292          try {
293              assertTrue(p.isQuiescent());
# Line 386 | Line 418 | public class ForkJoinPoolTest extends JS
418      public void testExecuteRunnable() throws Throwable {
419          ExecutorService e = new ForkJoinPool(1);
420          try {
421 <            TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS);
422 <            assertFalse(task.isDone());
421 >            final AtomicBoolean done = new AtomicBoolean(false);
422 >            CheckedRunnable task = new CheckedRunnable() {
423 >                public void realRun() {
424 >                    done.set(true);
425 >                }};
426              Future<?> future = e.submit(task);
427              assertNull(future.get());
428 <            assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
429 <            assertTrue(task.isDone());
428 >            assertNull(future.get(0, MILLISECONDS));
429 >            assertTrue(done.get());
430              assertTrue(future.isDone());
431              assertFalse(future.isCancelled());
432          } finally {
# Line 448 | Line 483 | public class ForkJoinPoolTest extends JS
483       * A submitted privileged action runs to completion
484       */
485      public void testSubmitPrivilegedAction() throws Exception {
486 +        final Callable callable = Executors.callable(new PrivilegedAction() {
487 +                public Object run() { return TEST_STRING; }});
488          Runnable r = new CheckedRunnable() {
489 <            public void realRun() throws Exception {
490 <                ExecutorService e = new ForkJoinPool(1);
491 <                Future future = e.submit(Executors.callable(new PrivilegedAction() {
492 <                    public Object run() {
456 <                        return TEST_STRING;
457 <                    }}));
458 <
489 >        public void realRun() throws Exception {
490 >            ExecutorService e = new ForkJoinPool(1);
491 >            try {
492 >                Future future = e.submit(callable);
493                  assertSame(TEST_STRING, future.get());
494 <            }};
494 >            } finally {
495 >                joinPool(e);
496 >            }
497 >        }};
498  
499 <        runWithPermissions(r,
463 <                           new RuntimePermission("modifyThread"));
499 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
500      }
501  
502      /**
503       * A submitted privileged exception action runs to completion
504       */
505      public void testSubmitPrivilegedExceptionAction() throws Exception {
506 +        final Callable callable =
507 +            Executors.callable(new PrivilegedExceptionAction() {
508 +                public Object run() { return TEST_STRING; }});
509          Runnable r = new CheckedRunnable() {
510 <            public void realRun() throws Exception {
511 <                ExecutorService e = new ForkJoinPool(1);
512 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
513 <                    public Object run() {
475 <                        return TEST_STRING;
476 <                    }}));
477 <
510 >        public void realRun() throws Exception {
511 >            ExecutorService e = new ForkJoinPool(1);
512 >            try {
513 >                Future future = e.submit(callable);
514                  assertSame(TEST_STRING, future.get());
515 <            }};
515 >            } finally {
516 >                joinPool(e);
517 >            }
518 >        }};
519  
520          runWithPermissions(r, new RuntimePermission("modifyThread"));
521      }
# Line 485 | Line 524 | public class ForkJoinPoolTest extends JS
524       * A submitted failed privileged exception action reports exception
525       */
526      public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
527 +        final Callable callable =
528 +            Executors.callable(new PrivilegedExceptionAction() {
529 +                public Object run() { throw new IndexOutOfBoundsException(); }});
530          Runnable r = new CheckedRunnable() {
531 <            public void realRun() throws Exception {
532 <                ExecutorService e = new ForkJoinPool(1);
533 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
534 <                    public Object run() throws Exception {
493 <                        throw new IndexOutOfBoundsException();
494 <                    }}));
495 <
531 >        public void realRun() throws Exception {
532 >            ExecutorService e = new ForkJoinPool(1);
533 >            try {
534 >                Future future = e.submit(callable);
535                  try {
536                      future.get();
537                      shouldThrow();
538                  } catch (ExecutionException success) {
539                      assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
540 <                }}};
540 >                }
541 >            } finally {
542 >                joinPool(e);
543 >            }
544 >        }};
545  
546          runWithPermissions(r, new RuntimePermission("modifyThread"));
547      }
# Line 568 | Line 611 | public class ForkJoinPoolTest extends JS
611          ForkJoinPool p = new ForkJoinPool(1);
612          try {
613              p.submit(new Callable() {
614 <                public Object call() {
615 <                    int i = 5/0;
573 <                    return Boolean.TRUE;
574 <                }}).get();
614 >                public Object call() { throw new ArithmeticException(); }})
615 >                .get();
616              shouldThrow();
617          } catch (ExecutionException success) {
618              assertTrue(success.getCause() instanceof ArithmeticException);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines