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.26 by jsr166, Sat Nov 1 16:02:23 2014 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 1490 | Line 1484 | public class ForkJoinPool8Test extends J
1484       */
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() {
# Line 1521 | Line 1515 | public class ForkJoinPool8Test extends J
1515                  assertFalse(p.isTerminated());
1516                  Thread.yield();
1517              }
1524            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1518              assertEquals(0, p.getQueuedTaskCount());
1519              assertFalse(p.getAsyncMode());
1527            assertEquals(0, p.getActiveThreadCount());
1528            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 {
1535 <            joinPool(p);
1528 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1529          }
1530      }
1531  
# Line 1541 | Line 1534 | public class ForkJoinPool8Test extends J
1534       * timeout elapsed
1535       */
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              final long startTime = System.nanoTime();
1549              ForkJoinTask a = new CheckedRecursiveAction() {
# Line 1563 | Line 1564 | public class ForkJoinPool8Test extends J
1564                  }};
1565              p.execute(a);
1566              assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1566            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1567              assertTrue(p.isQuiescent());
1568              assertTrue(a.isDone());
1569              assertEquals(0, p.getQueuedTaskCount());
1570              assertFalse(p.getAsyncMode());
1571            assertEquals(0, p.getActiveThreadCount());
1572            assertEquals(0, p.getQueuedTaskCount());
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 <        } finally {
1579 <            joinPool(p);
1579 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1580          }
1581      }
1582  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines