--- jsr166/src/test/tck/ForkJoinPool8Test.java 2013/07/14 16:36:37 1.10 +++ jsr166/src/test/tck/ForkJoinPool8Test.java 2017/10/21 06:54:53 1.37 @@ -4,25 +4,23 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.HashSet; import java.util.concurrent.CancellationException; +import java.util.concurrent.CountedCompleter; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; -import java.util.concurrent.CountedCompleter; -import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.SECONDS; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.Arrays; -import java.util.HashSet; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinPool8Test extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -85,14 +83,14 @@ public class ForkJoinPool8Test extends J Thread.currentThread().interrupt(); try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } } try { - a.get(0L, SECONDS); + a.get(randomExpiredTimeout(), randomTimeUnit()); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -110,10 +108,8 @@ public class ForkJoinPool8Test extends J assertFalse(a.cancel(true)); try { assertNull(a.get()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertNull(a.get(5L, SECONDS)); - } catch (Throwable fail) { threadUnexpectedException(fail); } + assertNull(a.get(randomTimeout(), randomTimeUnit())); + } catch (Exception fail) { threadUnexpectedException(fail); } } void checkCancelled(ForkJoinTask a) { @@ -137,7 +133,7 @@ public class ForkJoinPool8Test extends J } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -168,7 +164,7 @@ public class ForkJoinPool8Test extends J } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { assertSame(t.getClass(), success.getCause().getClass()); @@ -180,7 +176,7 @@ public class ForkJoinPool8Test extends J public FJException(Throwable cause) { super(cause); } } - // A simple recursive action for testing + /** A simple recursive action for testing. */ final class FibAction extends CheckedRecursiveAction { final int number; int result; @@ -198,7 +194,7 @@ public class ForkJoinPool8Test extends J } } - // A recursive action failing in base case + /** A recursive action failing in base case. */ static final class FailingFibAction extends RecursiveAction { final int number; int result; @@ -270,12 +266,11 @@ public class ForkJoinPool8Test extends J RecursiveAction a = new CheckedRecursiveAction() { protected void realCompute() { FibAction f = new FibAction(8); - final Thread myself = Thread.currentThread(); + final Thread currentThread = Thread.currentThread(); // test join() assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); assertNull(f.join()); Thread.interrupted(); assertEquals(21, f.result); @@ -284,8 +279,7 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -297,8 +291,7 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -310,8 +303,7 @@ public class ForkJoinPool8Test extends J // test quietlyJoin() f = new FibAction(8); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); assertEquals(21, f.result); @@ -320,8 +312,7 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); checkCancelled(f); @@ -329,8 +320,7 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); checkCompletedAbnormally(f, f.getException()); @@ -363,7 +353,7 @@ public class ForkJoinPool8Test extends J protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); - assertNull(f.get(5L, SECONDS)); + assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); assertEquals(21, f.result); checkCompletedNormally(f); }}; @@ -379,7 +369,7 @@ public class ForkJoinPool8Test extends J FibAction f = new FibAction(8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -479,7 +469,7 @@ public class ForkJoinPool8Test extends J FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { - f.get(5L, TimeUnit.SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); @@ -571,7 +561,7 @@ public class ForkJoinPool8Test extends J assertTrue(f.cancel(true)); assertSame(f, f.fork()); try { - f.get(5L, SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); @@ -912,7 +902,7 @@ public class ForkJoinPool8Test extends J } } - // Version of CCF with forced failure in left completions + /** Version of CCF with forced failure in left completions. */ abstract static class FailingCCF extends CountedCompleter { int number; int rnumber; @@ -1047,7 +1037,7 @@ public class ForkJoinPool8Test extends J CCF f = new LCCF(null, 8); assertSame(f, f.fork()); try { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -1462,7 +1452,7 @@ public class ForkJoinPool8Test extends J } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionCC() { ForkJoinTask a = new CheckedRecursiveAction() { @@ -1484,4 +1474,106 @@ public class ForkJoinPool8Test extends J checkInvoke(a); } + /** + * awaitQuiescence by a worker is equivalent in effect to + * ForkJoinTask.helpQuiesce() + */ + public void testAwaitQuiescence1() throws Exception { + final ForkJoinPool p = new ForkJoinPool(); + try (PoolCleaner cleaner = cleaner(p)) { + final long startTime = System.nanoTime(); + assertTrue(p.isQuiescent()); + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); + assertSame(p, ForkJoinTask.getPool()); + boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); + assertTrue(quiescent); + assertFalse(p.isQuiescent()); + while (!f.isDone()) { + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertFalse(p.isQuiescent()); + assertEquals(0, ForkJoinTask.getQueuedTaskCount()); + assertEquals(21, f.result); + }}; + p.execute(a); + while (!a.isDone() || !p.isQuiescent()) { + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertEquals(0, p.getQueuedTaskCount()); + assertFalse(p.getAsyncMode()); + assertEquals(0, p.getQueuedSubmissionCount()); + assertFalse(p.hasQueuedSubmissions()); + while (p.getActiveThreadCount() != 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + } + + /** + * awaitQuiescence returns when pool isQuiescent() or the indicated + * timeout elapsed + */ + public void testAwaitQuiescence2() throws Exception { + /** + * """It is possible to disable or limit the use of threads in the + * common pool by setting the parallelism property to zero. However + * doing so may cause unjoined tasks to never be executed.""" + */ + if ("0".equals(System.getProperty( + "java.util.concurrent.ForkJoinPool.common.parallelism"))) + return; + final ForkJoinPool p = new ForkJoinPool(); + try (PoolCleaner cleaner = cleaner(p)) { + assertTrue(p.isQuiescent()); + final long startTime = System.nanoTime(); + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); + while (!f.isDone() + && millisElapsedSince(startTime) < LONG_DELAY_MS) { + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertEquals(0, ForkJoinTask.getQueuedTaskCount()); + assertEquals(21, f.result); + }}; + p.execute(a); + assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isQuiescent()); + assertTrue(a.isDone()); + assertEquals(0, p.getQueuedTaskCount()); + assertFalse(p.getAsyncMode()); + assertEquals(0, p.getQueuedSubmissionCount()); + assertFalse(p.hasQueuedSubmissions()); + while (p.getActiveThreadCount() != 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + } + }