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.30 by jsr166, Fri Sep 17 17:07:47 2010 UTC vs.
Revision 1.36 by jsr166, Mon Nov 29 07:42:58 2010 UTC

# Line 316 | Line 316 | public class ForkJoinPoolTest extends JS
316          try {
317              ReentrantLock lock = new ReentrantLock();
318              ManagedLocker locker = new ManagedLocker(lock);
319 <            ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
319 >            ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
320              p.execute(f);
321 <            assertEquals(832040, (int) f.get());
321 >            assertEquals(6765, (int) f.get());
322          } finally {
323              p.shutdownNow(); // don't wait out shutdown
324          }
# Line 328 | Line 328 | public class ForkJoinPoolTest extends JS
328       * pollSubmission returns unexecuted submitted task, if present
329       */
330      public void testPollSubmission() {
331 +        final CountDownLatch done = new CountDownLatch(1);
332          SubFJP p = new SubFJP();
333          try {
334 <            ForkJoinTask a = p.submit(new MediumRunnable());
335 <            ForkJoinTask b = p.submit(new MediumRunnable());
336 <            ForkJoinTask c = p.submit(new MediumRunnable());
334 >            ForkJoinTask a = p.submit(awaiter(done));
335 >            ForkJoinTask b = p.submit(awaiter(done));
336 >            ForkJoinTask c = p.submit(awaiter(done));
337              ForkJoinTask r = p.pollSubmission();
338              assertTrue(r == a || r == b || r == c);
339              assertFalse(r.isDone());
340          } finally {
341 +            done.countDown();
342              joinPool(p);
343          }
344      }
# Line 345 | Line 347 | public class ForkJoinPoolTest extends JS
347       * drainTasksTo transfers unexecuted submitted tasks, if present
348       */
349      public void testDrainTasksTo() {
350 +        final CountDownLatch done = new CountDownLatch(1);
351          SubFJP p = new SubFJP();
352          try {
353 <            ForkJoinTask a = p.submit(new MediumRunnable());
354 <            ForkJoinTask b = p.submit(new MediumRunnable());
355 <            ForkJoinTask c = p.submit(new MediumRunnable());
353 >            ForkJoinTask a = p.submit(awaiter(done));
354 >            ForkJoinTask b = p.submit(awaiter(done));
355 >            ForkJoinTask c = p.submit(awaiter(done));
356              ArrayList<ForkJoinTask> al = new ArrayList();
357              p.drainTasksTo(al);
358              assertTrue(al.size() > 0);
# Line 358 | Line 361 | public class ForkJoinPoolTest extends JS
361                  assertFalse(r.isDone());
362              }
363          } finally {
364 +            done.countDown();
365              joinPool(p);
366          }
367      }
# Line 371 | Line 375 | public class ForkJoinPoolTest extends JS
375      public void testExecuteRunnable() throws Throwable {
376          ExecutorService e = new ForkJoinPool(1);
377          try {
378 <            TrackedShortRunnable task = new TrackedShortRunnable();
379 <            assertFalse(task.done);
378 >            TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS);
379 >            assertFalse(task.isDone());
380              Future<?> future = e.submit(task);
381 <            future.get();
382 <            assertTrue(task.done);
381 >            assertNull(future.get());
382 >            assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
383 >            assertTrue(task.isDone());
384 >            assertTrue(future.isDone());
385 >            assertFalse(future.isCancelled());
386          } finally {
387              joinPool(e);
388          }
# Line 389 | Line 396 | public class ForkJoinPoolTest extends JS
396          ExecutorService e = new ForkJoinPool(1);
397          try {
398              Future<String> future = e.submit(new StringTask());
399 <            String result = future.get();
400 <            assertSame(TEST_STRING, result);
399 >            assertSame(TEST_STRING, future.get());
400 >            assertTrue(future.isDone());
401 >            assertFalse(future.isCancelled());
402          } finally {
403              joinPool(e);
404          }
# Line 403 | Line 411 | public class ForkJoinPoolTest extends JS
411          ExecutorService e = new ForkJoinPool(1);
412          try {
413              Future<?> future = e.submit(new NoOpRunnable());
414 <            future.get();
414 >            assertNull(future.get());
415              assertTrue(future.isDone());
416 +            assertFalse(future.isCancelled());
417          } finally {
418              joinPool(e);
419          }
# Line 417 | Line 426 | public class ForkJoinPoolTest extends JS
426          ExecutorService e = new ForkJoinPool(1);
427          try {
428              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
429 <            String result = future.get();
430 <            assertSame(TEST_STRING, result);
429 >            assertSame(TEST_STRING, future.get());
430 >            assertTrue(future.isDone());
431 >            assertFalse(future.isCancelled());
432          } finally {
433              joinPool(e);
434          }
435      }
436  
427
437      /**
438 <     * A submitted privileged action to completion
438 >     * A submitted privileged action runs to completion
439       */
440 <    public void testSubmitPrivilegedAction() throws Throwable {
441 <        Policy savedPolicy = null;
442 <        try {
443 <            savedPolicy = Policy.getPolicy();
435 <            AdjustablePolicy policy = new AdjustablePolicy();
436 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
437 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
438 <            Policy.setPolicy(policy);
439 <        } catch (AccessControlException ok) {
440 <            return;
441 <        }
442 <
443 <        try {
444 <            ExecutorService e = new ForkJoinPool(1);
445 <            try {
440 >    public void testSubmitPrivilegedAction() throws Exception {
441 >        Runnable r = new CheckedRunnable() {
442 >            public void realRun() throws Exception {
443 >                ExecutorService e = new ForkJoinPool(1);
444                  Future future = e.submit(Executors.callable(new PrivilegedAction() {
445                      public Object run() {
446                          return TEST_STRING;
447                      }}));
448  
449 <                Object result = future.get();
450 <                assertSame(TEST_STRING, result);
451 <            } finally {
452 <                joinPool(e);
453 <            }
456 <        } finally {
457 <            Policy.setPolicy(savedPolicy);
458 <        }
449 >                assertSame(TEST_STRING, future.get());
450 >            }};
451 >
452 >        runWithPermissions(r,
453 >                           new RuntimePermission("modifyThread"));
454      }
455  
456      /**
457 <     * A submitted a privileged exception action runs to completion
457 >     * A submitted privileged exception action runs to completion
458       */
459 <    public void testSubmitPrivilegedExceptionAction() throws Throwable {
460 <        Policy savedPolicy = null;
461 <        try {
462 <            savedPolicy = Policy.getPolicy();
468 <            AdjustablePolicy policy = new AdjustablePolicy();
469 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
470 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
471 <            Policy.setPolicy(policy);
472 <        } catch (AccessControlException ok) {
473 <            return;
474 <        }
475 <
476 <        try {
477 <            ExecutorService e = new ForkJoinPool(1);
478 <            try {
459 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
460 >        Runnable r = new CheckedRunnable() {
461 >            public void realRun() throws Exception {
462 >                ExecutorService e = new ForkJoinPool(1);
463                  Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
464                      public Object run() {
465                          return TEST_STRING;
466                      }}));
467  
468 <                Object result = future.get();
469 <                assertSame(TEST_STRING, result);
470 <            } finally {
471 <                joinPool(e);
488 <            }
489 <        } finally {
490 <            Policy.setPolicy(savedPolicy);
491 <        }
468 >                assertSame(TEST_STRING, future.get());
469 >            }};
470 >
471 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
472      }
473  
474      /**
475       * A submitted failed privileged exception action reports exception
476       */
477 <    public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
478 <        Policy savedPolicy = null;
479 <        try {
480 <            savedPolicy = Policy.getPolicy();
501 <            AdjustablePolicy policy = new AdjustablePolicy();
502 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
503 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
504 <            Policy.setPolicy(policy);
505 <        } catch (AccessControlException ok) {
506 <            return;
507 <        }
508 <
509 <        try {
510 <            ExecutorService e = new ForkJoinPool(1);
511 <            try {
477 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
478 >        Runnable r = new CheckedRunnable() {
479 >            public void realRun() throws Exception {
480 >                ExecutorService e = new ForkJoinPool(1);
481                  Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
482                      public Object run() throws Exception {
483                          throw new IndexOutOfBoundsException();
484                      }}));
485  
486 <                Object result = future.get();
487 <                shouldThrow();
488 <            } catch (ExecutionException success) {
489 <                assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
490 <            } finally {
491 <                joinPool(e);
492 <            }
493 <        } finally {
525 <            Policy.setPolicy(savedPolicy);
526 <        }
486 >                try {
487 >                    future.get();
488 >                    shouldThrow();
489 >                } catch (ExecutionException success) {
490 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
491 >                }}};
492 >
493 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
494      }
495  
496      /**
# Line 531 | Line 498 | public class ForkJoinPoolTest extends JS
498       */
499      public void testExecuteNullRunnable() {
500          ExecutorService e = new ForkJoinPool(1);
534        TrackedShortRunnable task = null;
501          try {
502 <            Future<?> future = e.submit(task);
502 >            Future<?> future = e.submit((Runnable) null);
503              shouldThrow();
504          } catch (NullPointerException success) {
505          } finally {
# Line 547 | Line 513 | public class ForkJoinPoolTest extends JS
513       */
514      public void testSubmitNullCallable() {
515          ExecutorService e = new ForkJoinPool(1);
550        StringTask t = null;
516          try {
517 <            Future<String> future = e.submit(t);
517 >            Future<String> future = e.submit((Callable) null);
518              shouldThrow();
519          } catch (NullPointerException success) {
520          } finally {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines