--- jsr166/src/test/tck/RecursiveActionTest.java 2011/06/27 02:47:32 1.37 +++ jsr166/src/test/tck/RecursiveActionTest.java 2017/03/18 18:20:00 1.49 @@ -4,25 +4,27 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.Arrays; +import java.util.HashSet; import java.util.concurrent.CancellationException; -import java.util.concurrent.SynchronousQueue; 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.SynchronousQueue; import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.Arrays; -import java.util.HashSet; + +import junit.framework.Test; +import junit.framework.TestSuite; public class RecursiveActionTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -44,14 +46,12 @@ public class RecursiveActionTest extends } private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { checkNotDone(a); assertNull(pool.invoke(a)); checkCompletedNormally(a); - } finally { - joinPool(pool); } } @@ -168,12 +168,12 @@ public class RecursiveActionTest extends 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; FibAction(int n) { number = n; } - public void realCompute() { + protected void realCompute() { int n = number; if (n <= 1) result = n; @@ -186,7 +186,7 @@ public class RecursiveActionTest extends } } - // 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; @@ -211,7 +211,7 @@ public class RecursiveActionTest extends */ public void testInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertNull(f.invoke()); assertEquals(21, f.result); @@ -227,7 +227,7 @@ public class RecursiveActionTest extends */ public void testQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.quietlyInvoke(); assertEquals(21, f.result); @@ -241,7 +241,7 @@ public class RecursiveActionTest extends */ public void testForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -256,14 +256,13 @@ public class RecursiveActionTest extends */ public void testJoinIgnoresInterrupts() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + 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); @@ -272,8 +271,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -285,8 +283,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -298,8 +295,7 @@ public class RecursiveActionTest extends // 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); @@ -308,8 +304,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); checkCancelled(f); @@ -317,8 +312,7 @@ public class RecursiveActionTest extends 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()); @@ -336,7 +330,7 @@ public class RecursiveActionTest extends final SynchronousQueue sq = new SynchronousQueue(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws InterruptedException { + protected void realCompute() throws InterruptedException { FibAction[] fibActions = new FibAction[6]; for (int i = 0; i < fibActions.length; i++) fibActions[i] = new FibAction(8); @@ -358,22 +352,20 @@ public class RecursiveActionTest extends public void realRun() throws InterruptedException { FibAction[] fibActions = sq.take(); FibAction f; - final Thread myself = Thread.currentThread(); + final Thread currentThread = Thread.currentThread(); // test join() ------------ f = fibActions[0]; assertFalse(ForkJoinTask.inForkJoinPool()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); assertNull(f.join()); assertTrue(Thread.interrupted()); assertEquals(21, f.result); checkCompletedNormally(f); f = fibActions[1]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -383,8 +375,7 @@ public class RecursiveActionTest extends } f = fibActions[2]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -396,23 +387,20 @@ public class RecursiveActionTest extends // test quietlyJoin() --------- f = fibActions[3]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); assertTrue(Thread.interrupted()); assertEquals(21, f.result); checkCompletedNormally(f); f = fibActions[4]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); assertTrue(Thread.interrupted()); checkCancelled(f); f = fibActions[5]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); assertTrue(Thread.interrupted()); assertTrue(f.getException() instanceof FJException); @@ -423,12 +411,12 @@ public class RecursiveActionTest extends t = newStartedThread(r); testInvokeOnPool(mainPool(), a); - awaitTermination(t, LONG_DELAY_MS); + awaitTermination(t); a.reinitialize(); t = newStartedThread(r); testInvokeOnPool(singletonPool(), a); - awaitTermination(t, LONG_DELAY_MS); + awaitTermination(t); } /** @@ -436,7 +424,7 @@ public class RecursiveActionTest extends */ public void testForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -451,7 +439,7 @@ public class RecursiveActionTest extends */ public void testForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); assertNull(f.get(5L, SECONDS)); @@ -466,7 +454,7 @@ public class RecursiveActionTest extends */ public void testForkTimedGetNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); try { @@ -482,7 +470,7 @@ public class RecursiveActionTest extends */ public void testForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -498,10 +486,12 @@ public class RecursiveActionTest extends */ public void testForkHelpQuiesce() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(21, f.result); assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f); @@ -514,7 +504,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); try { f.invoke(); @@ -531,7 +521,7 @@ public class RecursiveActionTest extends */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -545,7 +535,7 @@ public class RecursiveActionTest extends */ public void testAbnormalForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { @@ -563,7 +553,7 @@ public class RecursiveActionTest extends */ public void testAbnormalForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { @@ -583,11 +573,11 @@ public class RecursiveActionTest extends */ public void testAbnormalForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { - f.get(5L, TimeUnit.SECONDS); + f.get(5L, SECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); @@ -603,7 +593,7 @@ public class RecursiveActionTest extends */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -618,7 +608,7 @@ public class RecursiveActionTest extends */ public void testCancelledInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); try { @@ -636,7 +626,7 @@ public class RecursiveActionTest extends */ public void testCancelledForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -655,7 +645,7 @@ public class RecursiveActionTest extends */ public void testCancelledForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -674,7 +664,7 @@ public class RecursiveActionTest extends */ public void testCancelledForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -693,7 +683,7 @@ public class RecursiveActionTest extends */ public void testCancelledForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -709,7 +699,7 @@ public class RecursiveActionTest extends public void testGetPool() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertSame(mainPool, getPool()); }}; testInvokeOnPool(mainPool, a); @@ -720,7 +710,7 @@ public class RecursiveActionTest extends */ public void testGetPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertNull(getPool()); }}; assertNull(a.invoke()); @@ -731,7 +721,7 @@ public class RecursiveActionTest extends */ public void testInForkJoinPool() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertTrue(inForkJoinPool()); }}; testInvokeOnPool(mainPool(), a); @@ -742,7 +732,7 @@ public class RecursiveActionTest extends */ public void testInForkJoinPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); @@ -754,7 +744,7 @@ public class RecursiveActionTest extends public void testWorkerGetPool() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); assertSame(mainPool, w.getPool()); @@ -768,7 +758,7 @@ public class RecursiveActionTest extends public void testWorkerGetPoolIndex() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); assertTrue(w.getPoolIndex() >= 0); @@ -783,7 +773,7 @@ public class RecursiveActionTest extends */ public void testSetRawResult() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { setRawResult(null); assertNull(getRawResult()); }}; @@ -795,7 +785,7 @@ public class RecursiveActionTest extends */ public void testReinitialize() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); checkNotDone(f); @@ -815,7 +805,7 @@ public class RecursiveActionTest extends */ public void testReinitializeAbnormal() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); checkNotDone(f); @@ -838,7 +828,7 @@ public class RecursiveActionTest extends */ public void testCompleteExceptionally() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.completeExceptionally(new FJException()); try { @@ -856,7 +846,7 @@ public class RecursiveActionTest extends */ public void testComplete() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.complete(null); assertNull(f.invoke()); @@ -871,7 +861,7 @@ public class RecursiveActionTest extends */ public void testInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); invokeAll(f, g); @@ -888,7 +878,7 @@ public class RecursiveActionTest extends */ public void testInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); invokeAll(f); checkCompletedNormally(f); @@ -902,7 +892,7 @@ public class RecursiveActionTest extends */ public void testInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -925,7 +915,7 @@ public class RecursiveActionTest extends */ public void testInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -952,7 +942,7 @@ public class RecursiveActionTest extends */ public void testInvokeAllNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = null; @@ -969,7 +959,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FailingFibAction g = new FailingFibAction(9); try { @@ -987,7 +977,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction g = new FailingFibAction(9); try { invokeAll(g); @@ -1004,7 +994,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FailingFibAction g = new FailingFibAction(9); FibAction h = new FibAction(7); @@ -1023,7 +1013,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -1047,7 +1037,7 @@ public class RecursiveActionTest extends */ public void testTryUnfork() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1066,7 +1056,7 @@ public class RecursiveActionTest extends */ public void testGetSurplusQueuedTaskCount() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction h = new FibAction(7); assertSame(h, h.fork()); FibAction g = new FibAction(9); @@ -1088,7 +1078,7 @@ public class RecursiveActionTest extends */ public void testPeekNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1109,7 +1099,7 @@ public class RecursiveActionTest extends */ public void testPollNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1127,7 +1117,7 @@ public class RecursiveActionTest extends */ public void testPollTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1145,7 +1135,7 @@ public class RecursiveActionTest extends */ public void testPeekNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1165,7 +1155,7 @@ public class RecursiveActionTest extends */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1184,7 +1174,7 @@ public class RecursiveActionTest extends */ public void testPollTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1215,7 +1205,7 @@ public class RecursiveActionTest extends } } // implementation details follow: - final static int THRESHOLD = 100; + static final int THRESHOLD = 100; void sortSequentially(int lo, int hi) { Arrays.sort(array, lo, hi); }