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.26 by jsr166, Thu Sep 16 00:52:49 2010 UTC vs.
Revision 1.37 by dl, Tue Feb 22 01:18:58 2011 UTC

# Line 23 | Line 23 | import java.util.concurrent.ForkJoinWork
23   import java.util.concurrent.RecursiveTask;
24   import java.util.concurrent.TimeUnit;
25   import java.util.concurrent.locks.ReentrantLock;
26 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
27   import java.security.AccessControlException;
28   import java.security.Policy;
29   import java.security.PrivilegedAction;
# Line 163 | Line 164 | public class ForkJoinPoolTest extends JS
164          try {
165              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
166                         p.getFactory());
166            assertTrue(p.isQuiescent());
167              assertFalse(p.getAsyncMode());
168              assertEquals(0, p.getActiveThreadCount());
169              assertEquals(0, p.getStealCount());
# Line 232 | Line 232 | public class ForkJoinPoolTest extends JS
232       * performs its defined action
233       */
234      public void testSetUncaughtExceptionHandler() throws InterruptedException {
235 <        MyHandler eh = new MyHandler();
235 >        final CountDownLatch uehInvoked = new CountDownLatch(1);
236 >        final Thread.UncaughtExceptionHandler eh =
237 >            new Thread.UncaughtExceptionHandler() {
238 >                public void uncaughtException(Thread t, Throwable e) {
239 >                    uehInvoked.countDown();
240 >                }};
241          ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
242                                            eh, false);
243          try {
244              assertSame(eh, p.getUncaughtExceptionHandler());
245 <            p.execute(new FailingTask());
246 <            Thread.sleep(MEDIUM_DELAY_MS);
242 <            assertTrue(eh.catches > 0);
245 >            p.execute(new FibTask(8));
246 >            assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
247          } finally {
248 <            p.shutdownNow();
248 >            p.shutdownNow(); // failure might have prevented processing task
249              joinPool(p);
250          }
251      }
# Line 254 | Line 258 | public class ForkJoinPoolTest extends JS
258      public void testisQuiescent() throws InterruptedException {
259          ForkJoinPool p = new ForkJoinPool(2);
260          try {
261 +            assertTrue(p.isQuiescent());
262              p.invoke(new FibTask(20));
263              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
264                         p.getFactory());
265 <            Thread.sleep(MEDIUM_DELAY_MS);
265 >            Thread.sleep(SMALL_DELAY_MS);
266              assertTrue(p.isQuiescent());
267              assertFalse(p.getAsyncMode());
268              assertEquals(0, p.getActiveThreadCount());
# Line 310 | Line 315 | public class ForkJoinPoolTest extends JS
315          try {
316              ReentrantLock lock = new ReentrantLock();
317              ManagedLocker locker = new ManagedLocker(lock);
318 <            ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
318 >            ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
319              p.execute(f);
320 <            assertEquals(832040, (int) f.get());
320 >            assertEquals(6765, (int) f.get());
321          } finally {
322              p.shutdownNow(); // don't wait out shutdown
323          }
# Line 322 | Line 327 | public class ForkJoinPoolTest extends JS
327       * pollSubmission returns unexecuted submitted task, if present
328       */
329      public void testPollSubmission() {
330 +        final CountDownLatch done = new CountDownLatch(1);
331          SubFJP p = new SubFJP();
332          try {
333 <            ForkJoinTask a = p.submit(new MediumRunnable());
334 <            ForkJoinTask b = p.submit(new MediumRunnable());
335 <            ForkJoinTask c = p.submit(new MediumRunnable());
333 >            ForkJoinTask a = p.submit(awaiter(done));
334 >            ForkJoinTask b = p.submit(awaiter(done));
335 >            ForkJoinTask c = p.submit(awaiter(done));
336              ForkJoinTask r = p.pollSubmission();
337              assertTrue(r == a || r == b || r == c);
338              assertFalse(r.isDone());
339          } finally {
340 +            done.countDown();
341              joinPool(p);
342          }
343      }
# Line 339 | Line 346 | public class ForkJoinPoolTest extends JS
346       * drainTasksTo transfers unexecuted submitted tasks, if present
347       */
348      public void testDrainTasksTo() {
349 +        final CountDownLatch done = new CountDownLatch(1);
350          SubFJP p = new SubFJP();
351          try {
352 <            ForkJoinTask a = p.submit(new MediumRunnable());
353 <            ForkJoinTask b = p.submit(new MediumRunnable());
354 <            ForkJoinTask c = p.submit(new MediumRunnable());
352 >            ForkJoinTask a = p.submit(awaiter(done));
353 >            ForkJoinTask b = p.submit(awaiter(done));
354 >            ForkJoinTask c = p.submit(awaiter(done));
355              ArrayList<ForkJoinTask> al = new ArrayList();
356              p.drainTasksTo(al);
357              assertTrue(al.size() > 0);
# Line 352 | Line 360 | public class ForkJoinPoolTest extends JS
360                  assertFalse(r.isDone());
361              }
362          } finally {
363 +            done.countDown();
364              joinPool(p);
365          }
366      }
# Line 365 | Line 374 | public class ForkJoinPoolTest extends JS
374      public void testExecuteRunnable() throws Throwable {
375          ExecutorService e = new ForkJoinPool(1);
376          try {
377 <            TrackedShortRunnable task = new TrackedShortRunnable();
378 <            assertFalse(task.done);
377 >            TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS);
378 >            assertFalse(task.isDone());
379              Future<?> future = e.submit(task);
380 <            future.get();
381 <            assertTrue(task.done);
380 >            assertNull(future.get());
381 >            assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
382 >            assertTrue(task.isDone());
383 >            assertTrue(future.isDone());
384 >            assertFalse(future.isCancelled());
385          } finally {
386              joinPool(e);
387          }
# Line 383 | Line 395 | public class ForkJoinPoolTest extends JS
395          ExecutorService e = new ForkJoinPool(1);
396          try {
397              Future<String> future = e.submit(new StringTask());
398 <            String result = future.get();
399 <            assertSame(TEST_STRING, result);
398 >            assertSame(TEST_STRING, future.get());
399 >            assertTrue(future.isDone());
400 >            assertFalse(future.isCancelled());
401          } finally {
402              joinPool(e);
403          }
# Line 397 | Line 410 | public class ForkJoinPoolTest extends JS
410          ExecutorService e = new ForkJoinPool(1);
411          try {
412              Future<?> future = e.submit(new NoOpRunnable());
413 <            future.get();
413 >            assertNull(future.get());
414              assertTrue(future.isDone());
415 +            assertFalse(future.isCancelled());
416          } finally {
417              joinPool(e);
418          }
# Line 411 | Line 425 | public class ForkJoinPoolTest extends JS
425          ExecutorService e = new ForkJoinPool(1);
426          try {
427              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
428 <            String result = future.get();
429 <            assertSame(TEST_STRING, result);
428 >            assertSame(TEST_STRING, future.get());
429 >            assertTrue(future.isDone());
430 >            assertFalse(future.isCancelled());
431          } finally {
432              joinPool(e);
433          }
434      }
435  
421
436      /**
437 <     * A submitted privileged action to completion
437 >     * A submitted privileged action runs to completion
438       */
439 <    public void testSubmitPrivilegedAction() throws Throwable {
440 <        Policy savedPolicy = null;
441 <        try {
442 <            savedPolicy = Policy.getPolicy();
429 <            AdjustablePolicy policy = new AdjustablePolicy();
430 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
431 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
432 <            Policy.setPolicy(policy);
433 <        } catch (AccessControlException ok) {
434 <            return;
435 <        }
436 <
437 <        try {
438 <            ExecutorService e = new ForkJoinPool(1);
439 <            try {
439 >    public void testSubmitPrivilegedAction() throws Exception {
440 >        Runnable r = new CheckedRunnable() {
441 >            public void realRun() throws Exception {
442 >                ExecutorService e = new ForkJoinPool(1);
443                  Future future = e.submit(Executors.callable(new PrivilegedAction() {
444                      public Object run() {
445                          return TEST_STRING;
446                      }}));
447  
448 <                Object result = future.get();
449 <                assertSame(TEST_STRING, result);
450 <            } finally {
451 <                joinPool(e);
452 <            }
450 <        } finally {
451 <            Policy.setPolicy(savedPolicy);
452 <        }
448 >                assertSame(TEST_STRING, future.get());
449 >            }};
450 >
451 >        runWithPermissions(r,
452 >                           new RuntimePermission("modifyThread"));
453      }
454  
455      /**
456 <     * A submitted a privileged exception action runs to completion
456 >     * A submitted privileged exception action runs to completion
457       */
458 <    public void testSubmitPrivilegedExceptionAction() throws Throwable {
459 <        Policy savedPolicy = null;
460 <        try {
461 <            savedPolicy = Policy.getPolicy();
462 <            AdjustablePolicy policy = new AdjustablePolicy();
463 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
464 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
465 <            Policy.setPolicy(policy);
466 <        } catch (AccessControlException ok) {
467 <            return;
468 <        }
469 <
470 <        try {
471 <            ExecutorService e = new ForkJoinPool(1);
472 <            try {
458 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
459 >        Runnable r = new CheckedRunnable() {
460 >            public void realRun() throws Exception {
461 >                ExecutorService e = new ForkJoinPool(1);
462                  Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
463                      public Object run() {
464                          return TEST_STRING;
465                      }}));
466  
467 <                Object result = future.get();
468 <                assertSame(TEST_STRING, result);
469 <            } finally {
470 <                joinPool(e);
482 <            }
483 <        } finally {
484 <            Policy.setPolicy(savedPolicy);
485 <        }
467 >                assertSame(TEST_STRING, future.get());
468 >            }};
469 >
470 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
471      }
472  
473      /**
474       * A submitted failed privileged exception action reports exception
475       */
476 <    public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
477 <        Policy savedPolicy = null;
478 <        try {
479 <            savedPolicy = Policy.getPolicy();
495 <            AdjustablePolicy policy = new AdjustablePolicy();
496 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
497 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
498 <            Policy.setPolicy(policy);
499 <        } catch (AccessControlException ok) {
500 <            return;
501 <        }
502 <
503 <        try {
504 <            ExecutorService e = new ForkJoinPool(1);
505 <            try {
476 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
477 >        Runnable r = new CheckedRunnable() {
478 >            public void realRun() throws Exception {
479 >                ExecutorService e = new ForkJoinPool(1);
480                  Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
481                      public Object run() throws Exception {
482                          throw new IndexOutOfBoundsException();
483                      }}));
484  
485 <                Object result = future.get();
486 <                shouldThrow();
487 <            } catch (ExecutionException success) {
488 <                assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
489 <            } finally {
490 <                joinPool(e);
491 <            }
492 <        } finally {
519 <            Policy.setPolicy(savedPolicy);
520 <        }
485 >                try {
486 >                    future.get();
487 >                    shouldThrow();
488 >                } catch (ExecutionException success) {
489 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
490 >                }}};
491 >
492 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
493      }
494  
495      /**
# Line 525 | Line 497 | public class ForkJoinPoolTest extends JS
497       */
498      public void testExecuteNullRunnable() {
499          ExecutorService e = new ForkJoinPool(1);
528        TrackedShortRunnable task = null;
500          try {
501 <            Future<?> future = e.submit(task);
501 >            Future<?> future = e.submit((Runnable) null);
502              shouldThrow();
503          } catch (NullPointerException success) {
504          } finally {
# Line 541 | Line 512 | public class ForkJoinPoolTest extends JS
512       */
513      public void testSubmitNullCallable() {
514          ExecutorService e = new ForkJoinPool(1);
544        StringTask t = null;
515          try {
516 <            Future<String> future = e.submit(t);
516 >            Future<String> future = e.submit((Callable) null);
517              shouldThrow();
518          } catch (NullPointerException success) {
519          } finally {
# Line 553 | Line 523 | public class ForkJoinPoolTest extends JS
523  
524  
525      /**
526 <     * Blocking on submit(callable) throws InterruptedException if
557 <     * caller interrupted.
526 >     * submit(callable).get() throws InterruptedException if interrupted
527       */
528      public void testInterruptedSubmit() throws InterruptedException {
529 <        final ForkJoinPool p = new ForkJoinPool(1);
530 <
531 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
532 <            public void realRun() throws Throwable {
533 <                p.submit(new CheckedCallable<Object>() {
534 <                    public Object realCall() throws Throwable {
535 <                        try {
536 <                            Thread.sleep(MEDIUM_DELAY_MS);
537 <                        } catch (InterruptedException ok) {
538 <                        }
539 <                        return null;
540 <                    }}).get();
541 <            }});
542 <
543 <        t.start();
544 <        Thread.sleep(SHORT_DELAY_MS);
545 <        t.interrupt();
546 <        t.join();
547 <        p.shutdownNow();
548 <        joinPool(p);
529 >        final CountDownLatch submitted    = new CountDownLatch(1);
530 >        final CountDownLatch quittingTime = new CountDownLatch(1);
531 >        final ExecutorService p = new ForkJoinPool(1);
532 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
533 >            public Void realCall() throws InterruptedException {
534 >                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
535 >                return null;
536 >            }};
537 >        try {
538 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
539 >                public void realRun() throws Exception {
540 >                    Future<Void> future = p.submit(awaiter);
541 >                    submitted.countDown();
542 >                    future.get();
543 >                }});
544 >            t.start();
545 >            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
546 >            t.interrupt();
547 >            t.join();
548 >        } finally {
549 >            quittingTime.countDown();
550 >            joinPool(p);
551 >        }
552      }
553  
554      /**
# Line 785 | Line 757 | public class ForkJoinPoolTest extends JS
757      public void testTimedInvokeAny1() throws Throwable {
758          ExecutorService e = new ForkJoinPool(1);
759          try {
760 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
760 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
761              shouldThrow();
762          } catch (NullPointerException success) {
763          } finally {
# Line 816 | Line 788 | public class ForkJoinPoolTest extends JS
788          ExecutorService e = new ForkJoinPool(1);
789          try {
790              e.invokeAny(new ArrayList<Callable<String>>(),
791 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
791 >                        MEDIUM_DELAY_MS, MILLISECONDS);
792              shouldThrow();
793          } catch (IllegalArgumentException success) {
794          } finally {
# Line 834 | Line 806 | public class ForkJoinPoolTest extends JS
806          l.add(latchAwaitingStringTask(latch));
807          l.add(null);
808          try {
809 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
809 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
810              shouldThrow();
811          } catch (NullPointerException success) {
812          } finally {
# Line 851 | Line 823 | public class ForkJoinPoolTest extends JS
823          List<Callable<String>> l = new ArrayList<Callable<String>>();
824          l.add(new NPETask());
825          try {
826 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
826 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
827              shouldThrow();
828          } catch (ExecutionException success) {
829              assertTrue(success.getCause() instanceof NullPointerException);
# Line 869 | Line 841 | public class ForkJoinPoolTest extends JS
841              List<Callable<String>> l = new ArrayList<Callable<String>>();
842              l.add(new StringTask());
843              l.add(new StringTask());
844 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
844 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
845              assertSame(TEST_STRING, result);
846          } finally {
847              joinPool(e);
# Line 882 | Line 854 | public class ForkJoinPoolTest extends JS
854      public void testTimedInvokeAll1() throws Throwable {
855          ExecutorService e = new ForkJoinPool(1);
856          try {
857 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
857 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
858              shouldThrow();
859          } catch (NullPointerException success) {
860          } finally {
# Line 914 | Line 886 | public class ForkJoinPoolTest extends JS
886          try {
887              List<Future<String>> r
888                  = e.invokeAll(new ArrayList<Callable<String>>(),
889 <                              MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
889 >                              MEDIUM_DELAY_MS, MILLISECONDS);
890              assertTrue(r.isEmpty());
891          } finally {
892              joinPool(e);
# Line 930 | Line 902 | public class ForkJoinPoolTest extends JS
902          l.add(new StringTask());
903          l.add(null);
904          try {
905 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
905 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
906              shouldThrow();
907          } catch (NullPointerException success) {
908          } finally {
# Line 946 | Line 918 | public class ForkJoinPoolTest extends JS
918          List<Callable<String>> l = new ArrayList<Callable<String>>();
919          l.add(new NPETask());
920          List<Future<String>> futures
921 <            = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
921 >            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
922          assertEquals(1, futures.size());
923          try {
924              futures.get(0).get();
# Line 968 | Line 940 | public class ForkJoinPoolTest extends JS
940              l.add(new StringTask());
941              l.add(new StringTask());
942              List<Future<String>> futures
943 <                = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
943 >                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
944              assertEquals(2, futures.size());
945              for (Future<String> future : futures)
946                  assertSame(TEST_STRING, future.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines