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.43 by jsr166, Sun May 29 13:45:35 2011 UTC vs.
Revision 1.51 by jsr166, Fri Jul 26 16:36:21 2013 UTC

# Line 25 | Line 25 | 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 39 | 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 107 | Line 108 | public class ForkJoinPoolTest extends JS
108      static final class FibTask extends RecursiveTask<Integer> {
109          final int number;
110          FibTask(int n) { number = n; }
111 <        public Integer compute() {
111 >        protected Integer compute() {
112              int n = number;
113              if (n <= 1)
114                  return n;
# Line 135 | Line 136 | public class ForkJoinPoolTest extends JS
136              this.locker = locker;
137              this.lock = lock;
138          }
139 <        public Integer compute() {
139 >        protected Integer compute() {
140              int n;
141              LockingFibTask f1 = null;
142              LockingFibTask f2 = null;
# Line 226 | 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 242 | 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 452 | 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() {
460 <                        return TEST_STRING;
461 <                    }}));
462 <
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,
467 <                           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() {
479 <                        return TEST_STRING;
480 <                    }}));
481 <
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 489 | 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 {
497 <                        throw new IndexOutOfBoundsException();
498 <                    }}));
499 <
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 572 | 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;
577 <                    return Boolean.TRUE;
578 <                }}).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