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.34 by jsr166, Thu Nov 18 19:14:34 2010 UTC vs.
Revision 1.41 by jsr166, Fri May 27 19:42:42 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 199 | Line 198 | public class ForkJoinPoolTest extends JS
198          } catch (NullPointerException success) {}
199      }
200  
202
201      /**
202       * getParallelism returns size set in constructor
203       */
# Line 252 | Line 250 | public class ForkJoinPoolTest extends JS
250      }
251  
252      /**
253 <     * After invoking a single task, isQuiescent is true,
254 <     * queues are empty, threads are not active, and
255 <     * construction parameters continue to hold
253 >     * After invoking a single task, isQuiescent eventually becomes
254 >     * true, at which time queues are empty, threads are not active,
255 >     * the task has completed successfully, and construction
256 >     * parameters continue to hold
257       */
258 <    public void testisQuiescent() throws InterruptedException {
258 >    public void testisQuiescent() throws Exception {
259          ForkJoinPool p = new ForkJoinPool(2);
260          try {
261              assertTrue(p.isQuiescent());
262 <            p.invoke(new FibTask(20));
262 >            long startTime = System.nanoTime();
263 >            FibTask f = new FibTask(20);
264 >            p.invoke(f);
265              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
266                         p.getFactory());
267 <            Thread.sleep(SMALL_DELAY_MS);
267 >            while (! p.isQuiescent()) {
268 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
269 >                    throw new AssertionFailedError("timed out");
270 >                assertFalse(p.getAsyncMode());
271 >                assertFalse(p.isShutdown());
272 >                assertFalse(p.isTerminating());
273 >                assertFalse(p.isTerminated());
274 >                Thread.yield();
275 >            }
276 >
277              assertTrue(p.isQuiescent());
278              assertFalse(p.getAsyncMode());
279              assertEquals(0, p.getActiveThreadCount());
# Line 273 | Line 283 | public class ForkJoinPoolTest extends JS
283              assertFalse(p.isShutdown());
284              assertFalse(p.isTerminating());
285              assertFalse(p.isTerminated());
286 +            assertTrue(f.isDone());
287 +            assertEquals(6765, (int) f.get());
288          } finally {
289              joinPool(p);
290          }
# Line 328 | Line 340 | public class ForkJoinPoolTest extends JS
340       * pollSubmission returns unexecuted submitted task, if present
341       */
342      public void testPollSubmission() {
343 +        final CountDownLatch done = new CountDownLatch(1);
344          SubFJP p = new SubFJP();
345          try {
346 <            ForkJoinTask a = p.submit(new ShortRunnable());
347 <            ForkJoinTask b = p.submit(new ShortRunnable());
348 <            ForkJoinTask c = p.submit(new ShortRunnable());
346 >            ForkJoinTask a = p.submit(awaiter(done));
347 >            ForkJoinTask b = p.submit(awaiter(done));
348 >            ForkJoinTask c = p.submit(awaiter(done));
349              ForkJoinTask r = p.pollSubmission();
350              assertTrue(r == a || r == b || r == c);
351              assertFalse(r.isDone());
352          } finally {
353 +            done.countDown();
354              joinPool(p);
355          }
356      }
# Line 345 | Line 359 | public class ForkJoinPoolTest extends JS
359       * drainTasksTo transfers unexecuted submitted tasks, if present
360       */
361      public void testDrainTasksTo() {
362 +        final CountDownLatch done = new CountDownLatch(1);
363          SubFJP p = new SubFJP();
364          try {
365 <            ForkJoinTask a = p.submit(new ShortRunnable());
366 <            ForkJoinTask b = p.submit(new ShortRunnable());
367 <            ForkJoinTask c = p.submit(new ShortRunnable());
365 >            ForkJoinTask a = p.submit(awaiter(done));
366 >            ForkJoinTask b = p.submit(awaiter(done));
367 >            ForkJoinTask c = p.submit(awaiter(done));
368              ArrayList<ForkJoinTask> al = new ArrayList();
369              p.drainTasksTo(al);
370              assertTrue(al.size() > 0);
# Line 358 | Line 373 | public class ForkJoinPoolTest extends JS
373                  assertFalse(r.isDone());
374              }
375          } finally {
376 +            done.countDown();
377              joinPool(p);
378          }
379      }
380  
365
381      // FJ Versions of AbstractExecutorService tests
382  
383      /**
# Line 375 | Line 390 | public class ForkJoinPoolTest extends JS
390              assertFalse(task.isDone());
391              Future<?> future = e.submit(task);
392              assertNull(future.get());
393 +            assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
394              assertTrue(task.isDone());
395 +            assertTrue(future.isDone());
396              assertFalse(future.isCancelled());
397          } finally {
398              joinPool(e);
399          }
400      }
401  
385
402      /**
403       * Completed submit(callable) returns result
404       */
# Line 428 | Line 444 | public class ForkJoinPoolTest extends JS
444          }
445      }
446  
431
447      /**
448       * A submitted privileged action runs to completion
449       */
450 <    public void testSubmitPrivilegedAction() throws Throwable {
451 <        Policy savedPolicy = null;
452 <        try {
453 <            savedPolicy = Policy.getPolicy();
439 <            AdjustablePolicy policy = new AdjustablePolicy();
440 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
441 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
442 <            Policy.setPolicy(policy);
443 <        } catch (AccessControlException ok) {
444 <            return;
445 <        }
446 <
447 <        try {
448 <            ExecutorService e = new ForkJoinPool(1);
449 <            try {
450 >    public void testSubmitPrivilegedAction() throws Exception {
451 >        Runnable r = new CheckedRunnable() {
452 >            public void realRun() throws Exception {
453 >                ExecutorService e = new ForkJoinPool(1);
454                  Future future = e.submit(Executors.callable(new PrivilegedAction() {
455                      public Object run() {
456                          return TEST_STRING;
457                      }}));
458  
459 <                Object result = future.get();
460 <                assertSame(TEST_STRING, result);
461 <            } finally {
462 <                joinPool(e);
463 <            }
460 <        } finally {
461 <            Policy.setPolicy(savedPolicy);
462 <        }
459 >                assertSame(TEST_STRING, future.get());
460 >            }};
461 >
462 >        runWithPermissions(r,
463 >                           new RuntimePermission("modifyThread"));
464      }
465  
466      /**
467       * A submitted privileged exception action runs to completion
468       */
469 <    public void testSubmitPrivilegedExceptionAction() throws Throwable {
470 <        Policy savedPolicy = null;
471 <        try {
472 <            savedPolicy = Policy.getPolicy();
472 <            AdjustablePolicy policy = new AdjustablePolicy();
473 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
474 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
475 <            Policy.setPolicy(policy);
476 <        } catch (AccessControlException ok) {
477 <            return;
478 <        }
479 <
480 <        try {
481 <            ExecutorService e = new ForkJoinPool(1);
482 <            try {
469 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
470 >        Runnable r = new CheckedRunnable() {
471 >            public void realRun() throws Exception {
472 >                ExecutorService e = new ForkJoinPool(1);
473                  Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
474                      public Object run() {
475                          return TEST_STRING;
476                      }}));
477  
478 <                Object result = future.get();
479 <                assertSame(TEST_STRING, result);
480 <            } finally {
481 <                joinPool(e);
492 <            }
493 <        } finally {
494 <            Policy.setPolicy(savedPolicy);
495 <        }
478 >                assertSame(TEST_STRING, future.get());
479 >            }};
480 >
481 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
482      }
483  
484      /**
485       * A submitted failed privileged exception action reports exception
486       */
487 <    public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
488 <        Policy savedPolicy = null;
489 <        try {
490 <            savedPolicy = Policy.getPolicy();
505 <            AdjustablePolicy policy = new AdjustablePolicy();
506 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
507 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
508 <            Policy.setPolicy(policy);
509 <        } catch (AccessControlException ok) {
510 <            return;
511 <        }
512 <
513 <        try {
514 <            ExecutorService e = new ForkJoinPool(1);
515 <            try {
487 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
488 >        Runnable r = new CheckedRunnable() {
489 >            public void realRun() throws Exception {
490 >                ExecutorService e = new ForkJoinPool(1);
491                  Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
492                      public Object run() throws Exception {
493                          throw new IndexOutOfBoundsException();
494                      }}));
495  
496 <                Object result = future.get();
497 <                shouldThrow();
498 <            } catch (ExecutionException success) {
499 <                assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
500 <            } finally {
501 <                joinPool(e);
502 <            }
503 <        } finally {
529 <            Policy.setPolicy(savedPolicy);
530 <        }
496 >                try {
497 >                    future.get();
498 >                    shouldThrow();
499 >                } catch (ExecutionException success) {
500 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
501 >                }}};
502 >
503 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
504      }
505  
506      /**
# Line 544 | Line 517 | public class ForkJoinPoolTest extends JS
517          }
518      }
519  
547
520      /**
521       * submit(null callable) throws NullPointerException
522       */
# Line 559 | Line 531 | public class ForkJoinPoolTest extends JS
531          }
532      }
533  
562
534      /**
535       * submit(callable).get() throws InterruptedException if interrupted
536       */
# Line 788 | Line 759 | public class ForkJoinPoolTest extends JS
759          }
760      }
761  
791
762      /**
763       * timed invokeAny(null) throws NullPointerException
764       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines