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.39 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.52 by jsr166, Wed Sep 25 07:39:17 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 106 | 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 134 | 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 198 | Line 200 | public class ForkJoinPoolTest extends JS
200          } catch (NullPointerException success) {}
201      }
202  
201
203      /**
204       * getParallelism returns size set in constructor
205       */
# 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 251 | Line 282 | public class ForkJoinPoolTest extends JS
282      }
283  
284      /**
285 <     * After invoking a single task, isQuiescent is true,
286 <     * queues are empty, threads are not active, and
287 <     * construction parameters continue to hold
285 >     * After invoking a single task, isQuiescent eventually becomes
286 >     * true, at which time queues are empty, threads are not active,
287 >     * the task has completed successfully, and construction
288 >     * parameters continue to hold
289       */
290 <    public void testisQuiescent() throws InterruptedException {
290 >    public void testIsQuiescent() throws Exception {
291          ForkJoinPool p = new ForkJoinPool(2);
292          try {
293              assertTrue(p.isQuiescent());
294 <            p.invoke(new FibTask(20));
294 >            long startTime = System.nanoTime();
295 >            FibTask f = new FibTask(20);
296 >            p.invoke(f);
297              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
298                         p.getFactory());
299 <            delay(SMALL_DELAY_MS);
299 >            while (! p.isQuiescent()) {
300 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
301 >                    throw new AssertionFailedError("timed out");
302 >                assertFalse(p.getAsyncMode());
303 >                assertFalse(p.isShutdown());
304 >                assertFalse(p.isTerminating());
305 >                assertFalse(p.isTerminated());
306 >                Thread.yield();
307 >            }
308 >
309              assertTrue(p.isQuiescent());
310              assertFalse(p.getAsyncMode());
311              assertEquals(0, p.getActiveThreadCount());
# Line 272 | Line 315 | public class ForkJoinPoolTest extends JS
315              assertFalse(p.isShutdown());
316              assertFalse(p.isTerminating());
317              assertFalse(p.isTerminated());
318 +            assertTrue(f.isDone());
319 +            assertEquals(6765, (int) f.get());
320          } finally {
321              joinPool(p);
322          }
# Line 365 | Line 410 | public class ForkJoinPoolTest extends JS
410          }
411      }
412  
368
413      // FJ Versions of AbstractExecutorService tests
414  
415      /**
# Line 374 | 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());
423 <            Future<?> future = e.submit(task);
421 >            final AtomicBoolean done = new AtomicBoolean(false);
422 >            Future<?> future = e.submit(new CheckedRunnable() {
423 >                public void realRun() {
424 >                    done.set(true);
425 >                }});
426              assertNull(future.get());
427 <            assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
428 <            assertTrue(task.isDone());
427 >            assertNull(future.get(0, MILLISECONDS));
428 >            assertTrue(done.get());
429              assertTrue(future.isDone());
430              assertFalse(future.isCancelled());
431          } finally {
# Line 387 | Line 433 | public class ForkJoinPoolTest extends JS
433          }
434      }
435  
390
436      /**
437       * Completed submit(callable) returns result
438       */
# Line 437 | 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() {
445 <                        return TEST_STRING;
446 <                    }}));
447 <
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,
452 <                           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() {
464 <                        return TEST_STRING;
465 <                    }}));
466 <
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 474 | 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 {
482 <                        throw new IndexOutOfBoundsException();
483 <                    }}));
484 <
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 506 | Line 559 | public class ForkJoinPoolTest extends JS
559          }
560      }
561  
509
562      /**
563       * submit(null callable) throws NullPointerException
564       */
# Line 521 | Line 573 | public class ForkJoinPoolTest extends JS
573          }
574      }
575  
524
576      /**
577       * submit(callable).get() throws InterruptedException if interrupted
578       */
# Line 559 | 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;
564 <                    return Boolean.TRUE;
565 <                }}).get();
613 >                public Object call() { throw new ArithmeticException(); }})
614 >                .get();
615              shouldThrow();
616          } catch (ExecutionException success) {
617              assertTrue(success.getCause() instanceof ArithmeticException);
# Line 750 | Line 799 | public class ForkJoinPoolTest extends JS
799          }
800      }
801  
753
802      /**
803       * timed invokeAny(null) throws NullPointerException
804       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines