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.15 by dl, Sun Sep 15 22:35:46 2013 UTC vs.
Revision 1.40 by jsr166, Sun Jul 22 20:27:47 2018 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 >
9 > import java.util.HashSet;
10   import java.util.concurrent.CancellationException;
11 + import java.util.concurrent.CountedCompleter;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
12 import java.util.concurrent.ForkJoinWorkerThread;
15   import java.util.concurrent.RecursiveAction;
14 import java.util.concurrent.CountedCompleter;
15 import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.TimeUnit;
16   import java.util.concurrent.TimeoutException;
17 < import static java.util.concurrent.TimeUnit.SECONDS;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import java.util.Arrays;
21 < import java.util.HashSet;
17 >
18 > import junit.framework.Test;
19 > import junit.framework.TestSuite;
20  
21   public class ForkJoinPool8Test extends JSR166TestCase {
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25  
26      public static Test suite() {
# Line 85 | Line 83 | public class ForkJoinPool8Test extends J
83  
84              Thread.currentThread().interrupt();
85              try {
86 <                a.get(5L, SECONDS);
86 >                a.get(randomTimeout(), randomTimeUnit());
87                  shouldThrow();
88              } catch (InterruptedException success) {
89              } catch (Throwable fail) { threadUnexpectedException(fail); }
90          }
91  
92          try {
93 <            a.get(0L, SECONDS);
93 >            a.get(randomExpiredTimeout(), randomTimeUnit());
94              shouldThrow();
95          } catch (TimeoutException success) {
96          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 108 | Line 106 | public class ForkJoinPool8Test extends J
106          assertNull(a.join());
107          assertFalse(a.cancel(false));
108          assertFalse(a.cancel(true));
109 +
110 +        Object v1 = null, v2 = null;
111          try {
112 <            assertNull(a.get());
113 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
114 <        try {
115 <            assertNull(a.get(5L, SECONDS));
112 >            v1 = a.get();
113 >            v2 = a.get(randomTimeout(), randomTimeUnit());
114          } catch (Throwable fail) { threadUnexpectedException(fail); }
115 +        assertNull(v1);
116 +        assertNull(v2);
117      }
118  
119      void checkCancelled(ForkJoinTask a) {
# Line 137 | Line 137 | public class ForkJoinPool8Test extends J
137          } catch (Throwable fail) { threadUnexpectedException(fail); }
138  
139          try {
140 <            a.get(5L, SECONDS);
140 >            a.get(randomTimeout(), randomTimeUnit());
141              shouldThrow();
142          } catch (CancellationException success) {
143          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 168 | Line 168 | public class ForkJoinPool8Test extends J
168          } catch (Throwable fail) { threadUnexpectedException(fail); }
169  
170          try {
171 <            a.get(5L, SECONDS);
171 >            a.get(randomTimeout(), randomTimeUnit());
172              shouldThrow();
173          } catch (ExecutionException success) {
174              assertSame(t.getClass(), success.getCause().getClass());
# Line 180 | Line 180 | public class ForkJoinPool8Test extends J
180          public FJException(Throwable cause) { super(cause); }
181      }
182  
183 <    // A simple recursive action for testing
183 >    /** A simple recursive action for testing. */
184      final class FibAction extends CheckedRecursiveAction {
185          final int number;
186          int result;
# Line 198 | Line 198 | public class ForkJoinPool8Test extends J
198          }
199      }
200  
201 <    // A recursive action failing in base case
201 >    /** A recursive action failing in base case. */
202      static final class FailingFibAction extends RecursiveAction {
203          final int number;
204          int result;
# Line 270 | Line 270 | public class ForkJoinPool8Test extends J
270          RecursiveAction a = new CheckedRecursiveAction() {
271              protected void realCompute() {
272                  FibAction f = new FibAction(8);
273 <                final Thread myself = Thread.currentThread();
273 >                final Thread currentThread = Thread.currentThread();
274  
275                  // test join()
276                  assertSame(f, f.fork());
277 <                myself.interrupt();
278 <                assertTrue(myself.isInterrupted());
277 >                currentThread.interrupt();
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();
288 <                assertTrue(myself.isInterrupted());
286 >                currentThread.interrupt();
287                  try {
288                      f.join();
289                      shouldThrow();
# Line 297 | Line 295 | public class ForkJoinPool8Test extends J
295                  f = new FibAction(8);
296                  f.completeExceptionally(new FJException());
297                  assertSame(f, f.fork());
298 <                myself.interrupt();
301 <                assertTrue(myself.isInterrupted());
298 >                currentThread.interrupt();
299                  try {
300                      f.join();
301                      shouldThrow();
# Line 310 | Line 307 | public class ForkJoinPool8Test extends J
307                  // test quietlyJoin()
308                  f = new FibAction(8);
309                  assertSame(f, f.fork());
310 <                myself.interrupt();
314 <                assertTrue(myself.isInterrupted());
310 >                currentThread.interrupt();
311                  f.quietlyJoin();
312                  Thread.interrupted();
313                  assertEquals(21, f.result);
# Line 320 | Line 316 | public class ForkJoinPool8Test extends J
316                  f = new FibAction(8);
317                  f.cancel(true);
318                  assertSame(f, f.fork());
319 <                myself.interrupt();
324 <                assertTrue(myself.isInterrupted());
319 >                currentThread.interrupt();
320                  f.quietlyJoin();
321                  Thread.interrupted();
322                  checkCancelled(f);
# Line 329 | Line 324 | public class ForkJoinPool8Test extends J
324                  f = new FibAction(8);
325                  f.completeExceptionally(new FJException());
326                  assertSame(f, f.fork());
327 <                myself.interrupt();
333 <                assertTrue(myself.isInterrupted());
327 >                currentThread.interrupt();
328                  f.quietlyJoin();
329                  Thread.interrupted();
330                  checkCompletedAbnormally(f, f.getException());
# Line 363 | Line 357 | public class ForkJoinPool8Test extends J
357              protected void realCompute() throws Exception {
358                  FibAction f = new FibAction(8);
359                  assertSame(f, f.fork());
360 <                assertNull(f.get(5L, SECONDS));
360 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
361                  assertEquals(21, f.result);
362                  checkCompletedNormally(f);
363              }};
# Line 379 | Line 373 | public class ForkJoinPool8Test extends J
373                  FibAction f = new FibAction(8);
374                  assertSame(f, f.fork());
375                  try {
376 <                    f.get(5L, null);
376 >                    f.get(randomTimeout(), null);
377                      shouldThrow();
378                  } catch (NullPointerException success) {}
379              }};
# Line 479 | Line 473 | public class ForkJoinPool8Test extends J
473                  FailingFibAction f = new FailingFibAction(8);
474                  assertSame(f, f.fork());
475                  try {
476 <                    f.get(5L, TimeUnit.SECONDS);
476 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
477                      shouldThrow();
478                  } catch (ExecutionException success) {
479                      Throwable cause = success.getCause();
# Line 571 | Line 565 | public class ForkJoinPool8Test extends J
565                  assertTrue(f.cancel(true));
566                  assertSame(f, f.fork());
567                  try {
568 <                    f.get(5L, SECONDS);
568 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
569                      shouldThrow();
570                  } catch (CancellationException success) {
571                      checkCancelled(f);
# Line 912 | Line 906 | public class ForkJoinPool8Test extends J
906          }
907      }
908  
909 <    // Version of CCF with forced failure in left completions
909 >    /** Version of CCF with forced failure in left completions. */
910      abstract static class FailingCCF extends CountedCompleter {
911          int number;
912          int rnumber;
# Line 1047 | Line 1041 | public class ForkJoinPool8Test extends J
1041                  CCF f = new LCCF(null, 8);
1042                  assertSame(f, f.fork());
1043                  try {
1044 <                    f.get(5L, null);
1044 >                    f.get(randomTimeout(), null);
1045                      shouldThrow();
1046                  } catch (NullPointerException success) {}
1047              }};
# Line 1485 | Line 1479 | public class ForkJoinPool8Test extends J
1479      }
1480  
1481      /**
1482 <     * awaitQuiescent by a worker is equivalent in effect to
1482 >     * awaitQuiescence by a worker is equivalent in effect to
1483       * ForkJoinTask.helpQuiesce()
1484       */
1485 <    public void testAwaitQuiescent1() throws Exception {
1485 >    public void testAwaitQuiescence1() throws Exception {
1486          final ForkJoinPool p = new ForkJoinPool();
1487 <        try {
1487 >        try (PoolCleaner cleaner = cleaner(p)) {
1488              final long startTime = System.nanoTime();
1489              assertTrue(p.isQuiescent());
1490              ForkJoinTask a = new CheckedRecursiveAction() {
1491 <                    protected void realCompute() {
1492 <                        FibAction f = new FibAction(8);
1493 <                        assertSame(f, f.fork());
1494 <                        boolean t = ForkJoinTask.getPool().awaitQuiescence(MEDIUM_DELAY_MS, TimeUnit.SECONDS);
1495 <                        assertTrue(t);
1496 <                        while (!f.isDone()) {
1497 <                            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1498 <                                threadFail("timed out");
1499 <                            assertFalse(p.getAsyncMode());
1500 <                            assertFalse(p.isShutdown());
1501 <                            assertFalse(p.isTerminating());
1502 <                            assertFalse(p.isTerminated());
1503 <                            Thread.yield();
1510 <                        }
1511 <                        assertFalse(p.isQuiescent());
1512 <                        assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1513 <                        try {
1514 <                            assertEquals(21, f.result);
1515 <                        } catch (Throwable fail) {
1516 <                            threadFail(fail.getMessage());
1517 <                        }
1491 >                protected void realCompute() {
1492 >                    FibAction f = new FibAction(8);
1493 >                    assertSame(f, f.fork());
1494 >                    assertSame(p, ForkJoinTask.getPool());
1495 >                    boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
1496 >                    assertTrue(quiescent);
1497 >                    assertFalse(p.isQuiescent());
1498 >                    while (!f.isDone()) {
1499 >                        assertFalse(p.getAsyncMode());
1500 >                        assertFalse(p.isShutdown());
1501 >                        assertFalse(p.isTerminating());
1502 >                        assertFalse(p.isTerminated());
1503 >                        Thread.yield();
1504                      }
1505 <                };
1505 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1506 >                    assertFalse(p.isQuiescent());
1507 >                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1508 >                    assertEquals(21, f.result);
1509 >                }};
1510              p.execute(a);
1511              while (!a.isDone() || !p.isQuiescent()) {
1522                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1523                    throw new AssertionFailedError("timed out");
1512                  assertFalse(p.getAsyncMode());
1513                  assertFalse(p.isShutdown());
1514                  assertFalse(p.isTerminating());
# Line 1529 | Line 1517 | public class ForkJoinPool8Test extends J
1517              }
1518              assertEquals(0, p.getQueuedTaskCount());
1519              assertFalse(p.getAsyncMode());
1532            assertEquals(0, p.getActiveThreadCount());
1533            assertEquals(0, p.getQueuedTaskCount());
1520              assertEquals(0, p.getQueuedSubmissionCount());
1521              assertFalse(p.hasQueuedSubmissions());
1522 +            while (p.getActiveThreadCount() != 0
1523 +                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1524 +                Thread.yield();
1525              assertFalse(p.isShutdown());
1526              assertFalse(p.isTerminating());
1527              assertFalse(p.isTerminated());
1528 <        } finally {
1540 <            joinPool(p);
1528 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1529          }
1530      }
1531  
1532      /**
1533 <     * awaitQuiescent returns when pool isQuiescent() or the indicated
1534 <     * timeout elapse
1533 >     * awaitQuiescence returns when pool isQuiescent() or the indicated
1534 >     * timeout elapsed
1535       */
1536 <    public void testAwaitQuiescent2() throws Exception {
1536 >    public void testAwaitQuiescence2() throws Exception {
1537 >        /*
1538 >         * """It is possible to disable or limit the use of threads in the
1539 >         * common pool by setting the parallelism property to zero. However
1540 >         * doing so may cause unjoined tasks to never be executed."""
1541 >         */
1542 >        if ("0".equals(System.getProperty(
1543 >             "java.util.concurrent.ForkJoinPool.common.parallelism")))
1544 >            return;
1545          final ForkJoinPool p = new ForkJoinPool();
1546 <        try {
1546 >        try (PoolCleaner cleaner = cleaner(p)) {
1547              assertTrue(p.isQuiescent());
1548 <            for (;;) {
1549 <                final long startTime = System.nanoTime();
1550 <                ForkJoinTask a = new CheckedRecursiveAction() {
1551 <                        protected void realCompute() {
1552 <                            FibAction f = new FibAction(8);
1553 <                            assertSame(f, f.fork());
1554 <                            ForkJoinTask.helpQuiesce();
1555 <                            while (!f.isDone()) {
1556 <                                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1557 <                                    threadFail("timed out");
1558 <                                assertFalse(p.getAsyncMode());
1559 <                                assertFalse(p.isShutdown());
1560 <                                assertFalse(p.isTerminating());
1561 <                                assertFalse(p.isTerminated());
1562 <                                Thread.yield();
1563 <                            }
1564 <                            assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1565 <                            try {
1566 <                                assertEquals(21, f.result);
1567 <                            } catch (Throwable fail) { System.out.println("fail " + fail.getMessage()); }
1568 <                        }
1569 <                    };
1570 <                p.execute(a);
1571 <                if (a.isDone() || p.isQuiescent())
1572 <                    continue; // Already done so cannot test; retry
1573 <                while (!p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) {
1574 <                    if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1575 <                        threadFail("timed out");
1576 <                    assertFalse(p.getAsyncMode());
1577 <                    assertFalse(p.isShutdown());
1578 <                    assertFalse(p.isTerminating());
1579 <                    assertFalse(p.isTerminated());
1584 <                    Thread.yield();
1585 <                }
1586 <                assertTrue(p.isQuiescent());
1587 <                assertTrue(a.isDone());
1588 <                assertEquals(0, p.getQueuedTaskCount());
1589 <                assertFalse(p.getAsyncMode());
1590 <                assertEquals(0, p.getActiveThreadCount());
1591 <                assertEquals(0, p.getQueuedTaskCount());
1592 <                assertEquals(0, p.getQueuedSubmissionCount());
1593 <                assertFalse(p.hasQueuedSubmissions());
1594 <                assertFalse(p.isShutdown());
1595 <                assertFalse(p.isTerminating());
1596 <                assertFalse(p.isTerminated());
1597 <                break;
1598 <            }
1599 <        } finally {
1600 <            joinPool(p);
1548 >            final long startTime = System.nanoTime();
1549 >            ForkJoinTask a = new CheckedRecursiveAction() {
1550 >                protected void realCompute() {
1551 >                    FibAction f = new FibAction(8);
1552 >                    assertSame(f, f.fork());
1553 >                    while (!f.isDone()
1554 >                           && millisElapsedSince(startTime) < LONG_DELAY_MS) {
1555 >                        assertFalse(p.getAsyncMode());
1556 >                        assertFalse(p.isShutdown());
1557 >                        assertFalse(p.isTerminating());
1558 >                        assertFalse(p.isTerminated());
1559 >                        Thread.yield();
1560 >                    }
1561 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1562 >                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1563 >                    assertEquals(21, f.result);
1564 >                }};
1565 >            p.execute(a);
1566 >            assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1567 >            assertTrue(p.isQuiescent());
1568 >            assertTrue(a.isDone());
1569 >            assertEquals(0, p.getQueuedTaskCount());
1570 >            assertFalse(p.getAsyncMode());
1571 >            assertEquals(0, p.getQueuedSubmissionCount());
1572 >            assertFalse(p.hasQueuedSubmissions());
1573 >            while (p.getActiveThreadCount() != 0
1574 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
1575 >                Thread.yield();
1576 >            assertFalse(p.isShutdown());
1577 >            assertFalse(p.isTerminating());
1578 >            assertFalse(p.isTerminated());
1579 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1580          }
1581      }
1582  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines