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.44 by dl, Tue Jan 10 13:48:27 2012 UTC vs.
Revision 1.52 by jsr166, Wed Sep 25 07:39:17 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 244 | Line 272 | public class ForkJoinPoolTest extends JS
272              assertSame(eh, p.getUncaughtExceptionHandler());
273              try {
274                  p.execute(new FibTask(8));
275 <                assertTrue(uehInvoked.await(10000, MILLISECONDS));
276 <            } catch(RejectedExecutionException ok) {
275 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
276 >            } catch (RejectedExecutionException ok) {
277              }
278          } finally {
279              p.shutdownNow(); // failure might have prevented processing task
# Line 391 | Line 419 | public class ForkJoinPoolTest extends JS
419          ExecutorService e = new ForkJoinPool(1);
420          try {
421              final AtomicBoolean done = new AtomicBoolean(false);
422 <            CheckedRunnable task = new CheckedRunnable() {
422 >            Future<?> future = e.submit(new CheckedRunnable() {
423                  public void realRun() {
424                      done.set(true);
425 <                }};
398 <            Future<?> future = e.submit(task);
425 >                }});
426              assertNull(future.get());
427              assertNull(future.get(0, MILLISECONDS));
428              assertTrue(done.get());
# Line 455 | Line 482 | public class ForkJoinPoolTest extends JS
482       * A submitted privileged action runs to completion
483       */
484      public void testSubmitPrivilegedAction() throws Exception {
485 +        final Callable callable = Executors.callable(new PrivilegedAction() {
486 +                public Object run() { return TEST_STRING; }});
487          Runnable r = new CheckedRunnable() {
488 <            public void realRun() throws Exception {
489 <                ExecutorService e = new ForkJoinPool(1);
490 <                Future future = e.submit(Executors.callable(new PrivilegedAction() {
491 <                    public Object run() {
463 <                        return TEST_STRING;
464 <                    }}));
465 <
488 >        public void realRun() throws Exception {
489 >            ExecutorService e = new ForkJoinPool(1);
490 >            try {
491 >                Future future = e.submit(callable);
492                  assertSame(TEST_STRING, future.get());
493 <            }};
493 >            } finally {
494 >                joinPool(e);
495 >            }
496 >        }};
497  
498 <        runWithPermissions(r,
470 <                           new RuntimePermission("modifyThread"));
498 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
499      }
500  
501      /**
502       * A submitted privileged exception action runs to completion
503       */
504      public void testSubmitPrivilegedExceptionAction() throws Exception {
505 +        final Callable callable =
506 +            Executors.callable(new PrivilegedExceptionAction() {
507 +                public Object run() { return TEST_STRING; }});
508          Runnable r = new CheckedRunnable() {
509 <            public void realRun() throws Exception {
510 <                ExecutorService e = new ForkJoinPool(1);
511 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
512 <                    public Object run() {
482 <                        return TEST_STRING;
483 <                    }}));
484 <
509 >        public void realRun() throws Exception {
510 >            ExecutorService e = new ForkJoinPool(1);
511 >            try {
512 >                Future future = e.submit(callable);
513                  assertSame(TEST_STRING, future.get());
514 <            }};
514 >            } finally {
515 >                joinPool(e);
516 >            }
517 >        }};
518  
519          runWithPermissions(r, new RuntimePermission("modifyThread"));
520      }
# Line 492 | Line 523 | public class ForkJoinPoolTest extends JS
523       * A submitted failed privileged exception action reports exception
524       */
525      public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
526 +        final Callable callable =
527 +            Executors.callable(new PrivilegedExceptionAction() {
528 +                public Object run() { throw new IndexOutOfBoundsException(); }});
529          Runnable r = new CheckedRunnable() {
530 <            public void realRun() throws Exception {
531 <                ExecutorService e = new ForkJoinPool(1);
532 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
533 <                    public Object run() throws Exception {
500 <                        throw new IndexOutOfBoundsException();
501 <                    }}));
502 <
530 >        public void realRun() throws Exception {
531 >            ExecutorService e = new ForkJoinPool(1);
532 >            try {
533 >                Future future = e.submit(callable);
534                  try {
535                      future.get();
536                      shouldThrow();
537                  } catch (ExecutionException success) {
538                      assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
539 <                }}};
539 >                }
540 >            } finally {
541 >                joinPool(e);
542 >            }
543 >        }};
544  
545          runWithPermissions(r, new RuntimePermission("modifyThread"));
546      }
# Line 575 | Line 610 | public class ForkJoinPoolTest extends JS
610          ForkJoinPool p = new ForkJoinPool(1);
611          try {
612              p.submit(new Callable() {
613 <                public Object call() {
614 <                    int i = 5/0;
580 <                    return Boolean.TRUE;
581 <                }}).get();
613 >                public Object call() { throw new ArithmeticException(); }})
614 >                .get();
615              shouldThrow();
616          } catch (ExecutionException success) {
617              assertTrue(success.getCause() instanceof ArithmeticException);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines