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.32 by jsr166, Mon Oct 11 04:39:12 2010 UTC vs.
Revision 1.39 by dl, Fri May 6 11:22:07 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# Line 164 | Line 164 | public class ForkJoinPoolTest extends JS
164          try {
165              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
166                         p.getFactory());
167            assertTrue(p.isQuiescent());
167              assertFalse(p.getAsyncMode());
168              assertEquals(0, p.getActiveThreadCount());
169              assertEquals(0, p.getStealCount());
# Line 263 | Line 262 | public class ForkJoinPoolTest extends JS
262              p.invoke(new FibTask(20));
263              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
264                         p.getFactory());
265 <            Thread.sleep(SMALL_DELAY_MS);
265 >            delay(SMALL_DELAY_MS);
266              assertTrue(p.isQuiescent());
267              assertFalse(p.getAsyncMode());
268              assertEquals(0, p.getActiveThreadCount());
# Line 328 | 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 ShortRunnable());
334 <            ForkJoinTask b = p.submit(new ShortRunnable());
335 <            ForkJoinTask c = p.submit(new ShortRunnable());
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 345 | 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 ShortRunnable());
353 <            ForkJoinTask b = p.submit(new ShortRunnable());
354 <            ForkJoinTask c = p.submit(new ShortRunnable());
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 358 | Line 360 | public class ForkJoinPoolTest extends JS
360                  assertFalse(r.isDone());
361              }
362          } finally {
363 +            done.countDown();
364              joinPool(p);
365          }
366      }
# Line 374 | Line 377 | public class ForkJoinPoolTest extends JS
377              TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS);
378              assertFalse(task.isDone());
379              Future<?> future = e.submit(task);
380 <            future.get();
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 389 | 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 403 | 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 417 | 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  
427
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();
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 {
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 <            }
456 <        } finally {
457 <            Policy.setPolicy(savedPolicy);
458 <        }
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();
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 {
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);
488 <            }
489 <        } finally {
490 <            Policy.setPolicy(savedPolicy);
491 <        }
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();
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 {
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 {
525 <            Policy.setPolicy(savedPolicy);
526 <        }
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      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines