ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinPool8Test.java (file contents):
Revision 1.24 by jsr166, Tue Sep 24 15:49:55 2013 UTC vs.
Revision 1.33 by jsr166, Tue Aug 16 23:02:57 2016 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.HashSet;
11   import java.util.concurrent.CancellationException;
12 + import java.util.concurrent.CountedCompleter;
13   import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   import java.util.concurrent.ForkJoinTask;
12 import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
14 import java.util.concurrent.CountedCompleter;
15 import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.TimeoutException;
18 < import static java.util.concurrent.TimeUnit.SECONDS;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import java.util.Arrays;
21 < import java.util.HashSet;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class ForkJoinPool8Test extends JSR166TestCase {
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run(suite());
24 >        main(suite(), args);
25      }
26  
27      public static Test suite() {
# Line 270 | Line 269 | public class ForkJoinPool8Test extends J
269          RecursiveAction a = new CheckedRecursiveAction() {
270              protected void realCompute() {
271                  FibAction f = new FibAction(8);
272 <                final Thread myself = Thread.currentThread();
272 >                final Thread currentThread = Thread.currentThread();
273  
274                  // test join()
275                  assertSame(f, f.fork());
276 <                myself.interrupt();
277 <                assertTrue(myself.isInterrupted());
276 >                currentThread.interrupt();
277 >                assertTrue(currentThread.isInterrupted());
278                  assertNull(f.join());
279                  Thread.interrupted();
280                  assertEquals(21, f.result);
# Line 284 | Line 283 | public class ForkJoinPool8Test extends J
283                  f = new FibAction(8);
284                  f.cancel(true);
285                  assertSame(f, f.fork());
286 <                myself.interrupt();
287 <                assertTrue(myself.isInterrupted());
286 >                currentThread.interrupt();
287 >                assertTrue(currentThread.isInterrupted());
288                  try {
289                      f.join();
290                      shouldThrow();
# Line 297 | Line 296 | public class ForkJoinPool8Test extends J
296                  f = new FibAction(8);
297                  f.completeExceptionally(new FJException());
298                  assertSame(f, f.fork());
299 <                myself.interrupt();
300 <                assertTrue(myself.isInterrupted());
299 >                currentThread.interrupt();
300 >                assertTrue(currentThread.isInterrupted());
301                  try {
302                      f.join();
303                      shouldThrow();
# Line 310 | Line 309 | public class ForkJoinPool8Test extends J
309                  // test quietlyJoin()
310                  f = new FibAction(8);
311                  assertSame(f, f.fork());
312 <                myself.interrupt();
313 <                assertTrue(myself.isInterrupted());
312 >                currentThread.interrupt();
313 >                assertTrue(currentThread.isInterrupted());
314                  f.quietlyJoin();
315                  Thread.interrupted();
316                  assertEquals(21, f.result);
# Line 320 | Line 319 | public class ForkJoinPool8Test extends J
319                  f = new FibAction(8);
320                  f.cancel(true);
321                  assertSame(f, f.fork());
322 <                myself.interrupt();
323 <                assertTrue(myself.isInterrupted());
322 >                currentThread.interrupt();
323 >                assertTrue(currentThread.isInterrupted());
324                  f.quietlyJoin();
325                  Thread.interrupted();
326                  checkCancelled(f);
# Line 329 | Line 328 | public class ForkJoinPool8Test extends J
328                  f = new FibAction(8);
329                  f.completeExceptionally(new FJException());
330                  assertSame(f, f.fork());
331 <                myself.interrupt();
332 <                assertTrue(myself.isInterrupted());
331 >                currentThread.interrupt();
332 >                assertTrue(currentThread.isInterrupted());
333                  f.quietlyJoin();
334                  Thread.interrupted();
335                  checkCompletedAbnormally(f, f.getException());
# Line 479 | Line 478 | public class ForkJoinPool8Test extends J
478                  FailingFibAction f = new FailingFibAction(8);
479                  assertSame(f, f.fork());
480                  try {
481 <                    f.get(5L, TimeUnit.SECONDS);
481 >                    f.get(5L, SECONDS);
482                      shouldThrow();
483                  } catch (ExecutionException success) {
484                      Throwable cause = success.getCause();
# Line 1490 | Line 1489 | public class ForkJoinPool8Test extends J
1489       */
1490      public void testAwaitQuiescence1() throws Exception {
1491          final ForkJoinPool p = new ForkJoinPool();
1492 <        try {
1492 >        try (PoolCleaner cleaner = cleaner(p)) {
1493              final long startTime = System.nanoTime();
1494              assertTrue(p.isQuiescent());
1495              ForkJoinTask a = new CheckedRecursiveAction() {
# Line 1521 | Line 1520 | public class ForkJoinPool8Test extends J
1520                  assertFalse(p.isTerminated());
1521                  Thread.yield();
1522              }
1524            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1523              assertEquals(0, p.getQueuedTaskCount());
1524              assertFalse(p.getAsyncMode());
1527            assertEquals(0, p.getActiveThreadCount());
1528            assertEquals(0, p.getQueuedTaskCount());
1525              assertEquals(0, p.getQueuedSubmissionCount());
1526              assertFalse(p.hasQueuedSubmissions());
1527 +            while (p.getActiveThreadCount() != 0
1528 +                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1529 +                Thread.yield();
1530              assertFalse(p.isShutdown());
1531              assertFalse(p.isTerminating());
1532              assertFalse(p.isTerminated());
1533 <        } finally {
1535 <            joinPool(p);
1533 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1534          }
1535      }
1536  
# Line 1541 | Line 1539 | public class ForkJoinPool8Test extends J
1539       * timeout elapsed
1540       */
1541      public void testAwaitQuiescence2() throws Exception {
1542 +        /**
1543 +         * """It is possible to disable or limit the use of threads in the
1544 +         * common pool by setting the parallelism property to zero. However
1545 +         * doing so may cause unjoined tasks to never be executed."""
1546 +         */
1547 +        if ("0".equals(System.getProperty(
1548 +             "java.util.concurrent.ForkJoinPool.common.parallelism")))
1549 +            return;
1550          final ForkJoinPool p = new ForkJoinPool();
1551 <        try {
1551 >        try (PoolCleaner cleaner = cleaner(p)) {
1552              assertTrue(p.isQuiescent());
1553 <            for (;;) {
1554 <                final long startTime = System.nanoTime();
1555 <                ForkJoinTask a = new CheckedRecursiveAction() {
1556 <                    protected void realCompute() {
1557 <                        FibAction f = new FibAction(8);
1558 <                        assertSame(f, f.fork());
1559 <                        ForkJoinTask.helpQuiesce();
1560 <                        while (!f.isDone()) {
1561 <                            assertFalse(p.getAsyncMode());
1562 <                            assertFalse(p.isShutdown());
1563 <                            assertFalse(p.isTerminating());
1564 <                            assertFalse(p.isTerminated());
1565 <                            Thread.yield();
1566 <                        }
1567 <                        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1568 <                        assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1569 <                        assertEquals(21, f.result);
1570 <                    }};
1571 <                p.execute(a);
1572 <                if (a.isDone() || p.isQuiescent())
1573 <                    continue; // Already done so cannot test; retry
1574 <                assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1575 <                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1576 <                assertTrue(p.isQuiescent());
1577 <                assertTrue(a.isDone());
1578 <                assertEquals(0, p.getQueuedTaskCount());
1579 <                assertFalse(p.getAsyncMode());
1580 <                assertEquals(0, p.getActiveThreadCount());
1581 <                assertEquals(0, p.getQueuedTaskCount());
1582 <                assertEquals(0, p.getQueuedSubmissionCount());
1583 <                assertFalse(p.hasQueuedSubmissions());
1584 <                assertFalse(p.isShutdown());
1579 <                assertFalse(p.isTerminating());
1580 <                assertFalse(p.isTerminated());
1581 <                break;
1582 <            }
1583 <        } finally {
1584 <            joinPool(p);
1553 >            final long startTime = System.nanoTime();
1554 >            ForkJoinTask a = new CheckedRecursiveAction() {
1555 >                protected void realCompute() {
1556 >                    FibAction f = new FibAction(8);
1557 >                    assertSame(f, f.fork());
1558 >                    while (!f.isDone()
1559 >                           && millisElapsedSince(startTime) < LONG_DELAY_MS) {
1560 >                        assertFalse(p.getAsyncMode());
1561 >                        assertFalse(p.isShutdown());
1562 >                        assertFalse(p.isTerminating());
1563 >                        assertFalse(p.isTerminated());
1564 >                        Thread.yield();
1565 >                    }
1566 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1567 >                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1568 >                    assertEquals(21, f.result);
1569 >                }};
1570 >            p.execute(a);
1571 >            assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1572 >            assertTrue(p.isQuiescent());
1573 >            assertTrue(a.isDone());
1574 >            assertEquals(0, p.getQueuedTaskCount());
1575 >            assertFalse(p.getAsyncMode());
1576 >            assertEquals(0, p.getQueuedSubmissionCount());
1577 >            assertFalse(p.hasQueuedSubmissions());
1578 >            while (p.getActiveThreadCount() != 0
1579 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1580 >                Thread.yield();
1581 >            assertFalse(p.isShutdown());
1582 >            assertFalse(p.isTerminating());
1583 >            assertFalse(p.isTerminated());
1584 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1585          }
1586      }
1587  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines