--- jsr166/src/test/tck/RecursiveActionTest.java 2010/09/13 07:51:18 1.14 +++ jsr166/src/test/tck/RecursiveActionTest.java 2010/11/21 20:32:15 1.22 @@ -5,8 +5,15 @@ */ import junit.framework.*; -import java.util.concurrent.*; -import java.util.*; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.RecursiveAction; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import static java.util.concurrent.TimeUnit.SECONDS; +import java.util.HashSet; public class RecursiveActionTest extends JSR166TestCase { @@ -34,22 +41,134 @@ public class RecursiveActionTest extends private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { try { - assertTrue(pool.invoke(a) == null); + checkNotDone(a); + + assertNull(pool.invoke(a)); + + checkCompletedNormally(a); } finally { joinPool(pool); } } + void checkNotDone(RecursiveAction a) { + assertFalse(a.isDone()); + assertFalse(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertFalse(a.isCancelled()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + + if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { + Thread.currentThread().interrupt(); + try { + a.get(); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + Thread.currentThread().interrupt(); + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + try { + a.get(0L, SECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedNormally(RecursiveAction a) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertTrue(a.isCompletedNormally()); + assertFalse(a.isCompletedAbnormally()); + assertNull(a.getException()); + assertNull(a.getRawResult()); + assertNull(a.join()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + try { + assertNull(a.get()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + try { + assertNull(a.get(5L, SECONDS)); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCancelled(RecursiveAction a) { + assertTrue(a.isDone()); + assertTrue(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertTrue(a.getException() instanceof CancellationException); + assertNull(a.getRawResult()); + + try { + a.join(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (CancellationException success) { + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + + void checkCompletedAbnormally(RecursiveAction a, Throwable t) { + assertTrue(a.isDone()); + assertFalse(a.isCancelled()); + assertFalse(a.isCompletedNormally()); + assertTrue(a.isCompletedAbnormally()); + assertSame(t, a.getException()); + assertNull(a.getRawResult()); + assertFalse(a.cancel(false)); + assertFalse(a.cancel(true)); + + try { + a.join(); + shouldThrow(); + } catch (Throwable expected) { + assertSame(t, expected); + } + + try { + a.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t, success.getCause()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + + try { + a.get(5L, SECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertSame(t, success.getCause()); + } catch (Throwable fail) { threadUnexpectedException(fail); } + } + static final class FJException extends RuntimeException { FJException() { super(); } } // A simple recursive action for testing - static final class FibAction extends RecursiveAction { + final class FibAction extends CheckedRecursiveAction { final int number; int result; FibAction(int n) { number = n; } - public void compute() { + public void realCompute() { int n = number; if (n <= 1) result = n; @@ -86,15 +205,12 @@ public class RecursiveActionTest extends * completed tasks. getRawResult of a RecursiveAction returns null; */ public void testInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); - f.invoke(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); + assertNull(f.invoke()); + assertEquals(21, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -105,15 +221,12 @@ public class RecursiveActionTest extends * completed tasks */ public void testQuietlyInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); f.quietlyInvoke(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - threadAssertTrue(f.getRawResult() == null); + assertEquals(21, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -122,14 +235,13 @@ public class RecursiveActionTest extends * join of a forked task returns when task completes */ public void testForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); - f.fork(); - f.join(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.getRawResult() == null); + assertSame(f, f.fork()); + assertNull(f.join()); + assertEquals(21, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -138,17 +250,13 @@ public class RecursiveActionTest extends * get of a forked task returns when task completes */ public void testForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.fork(); - f.get(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); + assertNull(f.get()); + assertEquals(21, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -157,17 +265,13 @@ public class RecursiveActionTest extends * timed get of a forked task returns when task completes */ public void testForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FibAction f = new FibAction(8); - f.fork(); - f.get(5L, TimeUnit.SECONDS); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - } catch (Exception ex) { - unexpectedException(ex); - } + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); + assertNull(f.get(5L, SECONDS)); + assertEquals(21, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -176,17 +280,14 @@ public class RecursiveActionTest extends * timed get with null time unit throws NPE */ public void testForkTimedGetNPE() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); try { - FibAction f = new FibAction(8); - f.fork(); f.get(5L, null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(ex); - } + } catch (NullPointerException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -195,13 +296,13 @@ public class RecursiveActionTest extends * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); + assertEquals(21, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -212,14 +313,14 @@ public class RecursiveActionTest extends * getQueuedTaskCount returns 0 when quiescent */ public void testForkHelpQuiesce() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); - f.fork(); + assertSame(f, f.fork()); f.helpQuiesce(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertTrue(getQueuedTaskCount() == 0); + assertEquals(21, f.result); + assertEquals(0, getQueuedTaskCount()); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -229,13 +330,14 @@ public class RecursiveActionTest extends * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingFibAction f = new FailingFibAction(8); try { - FailingFibAction f = new FailingFibAction(8); f.invoke(); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; testInvokeOnPool(mainPool(), a); @@ -245,11 +347,12 @@ public class RecursiveActionTest extends * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FailingFibAction f = new FailingFibAction(8); f.quietlyInvoke(); - threadAssertTrue(f.isDone()); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); } @@ -258,14 +361,15 @@ public class RecursiveActionTest extends * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingFibAction f = new FailingFibAction(8); + assertSame(f, f.fork()); try { - FailingFibAction f = new FailingFibAction(8); - f.fork(); f.join(); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; testInvokeOnPool(mainPool(), a); @@ -275,16 +379,17 @@ public class RecursiveActionTest extends * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FailingFibAction f = new FailingFibAction(8); + assertSame(f, f.fork()); try { - FailingFibAction f = new FailingFibAction(8); - f.fork(); f.get(); shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(mainPool(), a); @@ -294,16 +399,17 @@ public class RecursiveActionTest extends * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FailingFibAction f = new FailingFibAction(8); + assertSame(f, f.fork()); try { - FailingFibAction f = new FailingFibAction(8); - f.fork(); f.get(5L, TimeUnit.SECONDS); shouldThrow(); } catch (ExecutionException success) { - } catch (Exception ex) { - unexpectedException(ex); + Throwable cause = success.getCause(); + assertTrue(cause instanceof FJException); + checkCompletedAbnormally(f, cause); } }}; testInvokeOnPool(mainPool(), a); @@ -313,14 +419,13 @@ public class RecursiveActionTest extends * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FailingFibAction f = new FailingFibAction(8); - f.fork(); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof FJException); + assertTrue(f.getException() instanceof FJException); + checkCompletedAbnormally(f, f.getException()); }}; testInvokeOnPool(mainPool(), a); } @@ -329,14 +434,15 @@ public class RecursiveActionTest extends * invoke task throws exception when task cancelled */ public void testCancelledInvoke() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + assertTrue(f.cancel(true)); try { - FibAction f = new FibAction(8); - f.cancel(true); f.invoke(); shouldThrow(); } catch (CancellationException success) { + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -346,15 +452,16 @@ public class RecursiveActionTest extends * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); f.join(); shouldThrow(); } catch (CancellationException success) { + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -364,17 +471,16 @@ public class RecursiveActionTest extends * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FibAction f = new FibAction(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); f.get(); shouldThrow(); } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -384,17 +490,16 @@ public class RecursiveActionTest extends * timed get of a forked task throws exception when task cancelled */ public void testCancelledForkTimedGet() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() throws Exception { + FibAction f = new FibAction(8); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); try { - FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); - f.get(5L, TimeUnit.SECONDS); + f.get(5L, SECONDS); shouldThrow(); } catch (CancellationException success) { - } catch (Exception ex) { - unexpectedException(ex); + checkCancelled(f); } }}; testInvokeOnPool(mainPool(), a); @@ -404,15 +509,13 @@ public class RecursiveActionTest extends * quietlyJoin of a forked task returns when task cancelled */ public void testCancelledForkQuietlyJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); - f.cancel(true); - f.fork(); + assertTrue(f.cancel(true)); + assertSame(f, f.fork()); f.quietlyJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.getException() instanceof CancellationException); + checkCancelled(f); }}; testInvokeOnPool(mainPool(), a); } @@ -422,9 +525,9 @@ public class RecursiveActionTest extends */ public void testGetPool() { final ForkJoinPool mainPool = mainPool(); - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == mainPool); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertSame(mainPool, getPool()); }}; testInvokeOnPool(mainPool, a); } @@ -433,20 +536,20 @@ public class RecursiveActionTest extends * getPool of non-FJ task returns null */ public void testGetPool2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(getPool() == null); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertNull(getPool()); }}; - a.invoke(); + assertNull(a.invoke()); } /** * inForkJoinPool of executing task returns true */ public void testInForkJoinPool() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(inForkJoinPool()); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertTrue(inForkJoinPool()); }}; testInvokeOnPool(mainPool(), a); } @@ -455,11 +558,11 @@ public class RecursiveActionTest extends * inForkJoinPool of non-FJ task returns false */ public void testInForkJoinPool2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - threadAssertTrue(!inForkJoinPool()); + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + assertFalse(inForkJoinPool()); }}; - a.invoke(); + assertNull(a.invoke()); } /** @@ -467,11 +570,11 @@ public class RecursiveActionTest extends */ public void testWorkerGetPool() { final ForkJoinPool mainPool = mainPool(); - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); - threadAssertTrue(w.getPool() == mainPool); + assertSame(mainPool, w.getPool()); }}; testInvokeOnPool(mainPool, a); } @@ -481,13 +584,12 @@ public class RecursiveActionTest extends */ public void testWorkerGetPoolIndex() { final ForkJoinPool mainPool = mainPool(); - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); - int idx = w.getPoolIndex(); - threadAssertTrue(idx >= 0); - threadAssertTrue(idx < mainPool.getPoolSize()); + assertTrue(w.getPoolIndex() >= 0); + assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); }}; testInvokeOnPool(mainPool, a); } @@ -497,28 +599,29 @@ public class RecursiveActionTest extends * setRawResult(null) succeeds */ public void testSetRawResult() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { setRawResult(null); }}; - a.invoke(); + assertNull(a.invoke()); } /** * A reinitialized task may be re-invoked */ public void testReinitialize() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); - f.invoke(); - threadAssertTrue(f.result == 21); - threadAssertTrue(f.isDone()); - threadAssertFalse(f.isCancelled()); - threadAssertFalse(f.isCompletedAbnormally()); - f.reinitialize(); - f.invoke(); - threadAssertTrue(f.result == 21); + checkNotDone(f); + + for (int i = 0; i < 3; i++) { + assertNull(f.invoke()); + assertEquals(21, f.result); + checkCompletedNormally(f); + f.reinitialize(); + checkNotDone(f); + } }}; testInvokeOnPool(mainPool(), a); } @@ -527,14 +630,15 @@ public class RecursiveActionTest extends * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + f.completeExceptionally(new FJException()); try { - FibAction f = new FibAction(8); - f.completeExceptionally(new FJException()); f.invoke(); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; testInvokeOnPool(mainPool(), a); @@ -544,13 +648,13 @@ public class RecursiveActionTest extends * invoke task suppresses execution invoking complete */ public void testComplete() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); f.complete(null); - f.invoke(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 0); + assertNull(f.invoke()); + assertEquals(0, f.result); + checkCompletedNormally(f); }}; testInvokeOnPool(mainPool(), a); } @@ -559,15 +663,15 @@ public class RecursiveActionTest extends * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); invokeAll(f, g); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.result == 34); + checkCompletedNormally(f); + assertEquals(21, f.result); + checkCompletedNormally(g); + assertEquals(34, g.result); }}; testInvokeOnPool(mainPool(), a); } @@ -576,12 +680,12 @@ public class RecursiveActionTest extends * invokeAll(tasks) with 1 argument invokes task */ public void testInvokeAll1() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); invokeAll(f); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); + checkCompletedNormally(f); + assertEquals(21, f.result); }}; testInvokeOnPool(mainPool(), a); } @@ -590,18 +694,21 @@ public class RecursiveActionTest extends * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); invokeAll(f, g, h); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.result == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.result == 13); + assertTrue(f.isDone()); + assertTrue(g.isDone()); + assertTrue(h.isDone()); + checkCompletedNormally(f); + assertEquals(21, f.result); + checkCompletedNormally(g); + assertEquals(34, g.result); + checkCompletedNormally(g); + assertEquals(13, h.result); }}; testInvokeOnPool(mainPool(), a); } @@ -610,8 +717,8 @@ public class RecursiveActionTest extends * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollection() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -620,12 +727,15 @@ public class RecursiveActionTest extends set.add(g); set.add(h); invokeAll(set); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.result == 21); - threadAssertTrue(g.isDone()); - threadAssertTrue(g.result == 34); - threadAssertTrue(h.isDone()); - threadAssertTrue(h.result == 13); + assertTrue(f.isDone()); + assertTrue(g.isDone()); + assertTrue(h.isDone()); + checkCompletedNormally(f); + assertEquals(21, f.result); + checkCompletedNormally(g); + assertEquals(34, g.result); + checkCompletedNormally(g); + assertEquals(13, h.result); }}; testInvokeOnPool(mainPool(), a); } @@ -635,16 +745,15 @@ public class RecursiveActionTest extends * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPE() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + FibAction g = new FibAction(9); + FibAction h = null; try { - FibAction f = new FibAction(8); - FibAction g = new FibAction(9); - FibAction h = null; invokeAll(f, g, h); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} }}; testInvokeOnPool(mainPool(), a); } @@ -653,14 +762,15 @@ public class RecursiveActionTest extends * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + FailingFibAction g = new FailingFibAction(9); try { - FibAction f = new FibAction(8); - FailingFibAction g = new FailingFibAction(9); invokeAll(f, g); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(g, success); } }}; testInvokeOnPool(mainPool(), a); @@ -670,13 +780,14 @@ public class RecursiveActionTest extends * invokeAll(tasks) with 1 argument throws exception if task does */ public void testAbnormalInvokeAll1() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingFibAction g = new FailingFibAction(9); try { - FailingFibAction g = new FailingFibAction(9); invokeAll(g); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(g, success); } }}; testInvokeOnPool(mainPool(), a); @@ -686,15 +797,16 @@ public class RecursiveActionTest extends * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FibAction f = new FibAction(8); + FailingFibAction g = new FailingFibAction(9); + FibAction h = new FibAction(7); try { - FibAction f = new FibAction(8); - FailingFibAction g = new FailingFibAction(9); - FibAction h = new FibAction(7); invokeAll(f, g, h); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(g, success); } }}; testInvokeOnPool(mainPool(), a); @@ -704,19 +816,20 @@ public class RecursiveActionTest extends * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { + FailingFibAction f = new FailingFibAction(8); + FibAction g = new FibAction(9); + FibAction h = new FibAction(7); + HashSet set = new HashSet(); + set.add(f); + set.add(g); + set.add(h); try { - FailingFibAction f = new FailingFibAction(8); - FibAction g = new FibAction(9); - FibAction h = new FibAction(7); - HashSet set = new HashSet(); - set.add(f); - set.add(g); - set.add(h); invokeAll(set); shouldThrow(); } catch (FJException success) { + checkCompletedAbnormally(f, success); } }}; testInvokeOnPool(mainPool(), a); @@ -727,16 +840,16 @@ public class RecursiveActionTest extends * and suppresses execution */ public void testTryUnfork() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(f.tryUnfork()); + assertSame(f, f.fork()); + assertTrue(f.tryUnfork()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -746,16 +859,20 @@ public class RecursiveActionTest extends * there are more tasks than threads */ public void testGetSurplusQueuedTaskCount() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction h = new FibAction(7); - h.fork(); + assertSame(h, h.fork()); FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(getSurplusQueuedTaskCount() > 0); + assertSame(f, f.fork()); + assertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); + assertEquals(0, getSurplusQueuedTaskCount()); + checkCompletedNormally(f); + checkCompletedNormally(g); + checkCompletedNormally(h); }}; testInvokeOnPool(singletonPool(), a); } @@ -764,16 +881,18 @@ public class RecursiveActionTest extends * peekNextLocalTask returns most recent unexecuted task. */ public void testPeekNextLocalTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == f); - f.join(); - threadAssertTrue(f.isDone()); + assertSame(f, f.fork()); + assertSame(f, peekNextLocalTask()); + assertNull(f.join()); + checkCompletedNormally(f); helpQuiesce(); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -783,34 +902,34 @@ public class RecursiveActionTest extends * without executing it */ public void testPollNextLocalTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollNextLocalTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } /** - * pollTask returns an unexecuted task - * without executing it + * pollTask returns an unexecuted task without executing it */ public void testPollTask() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollTask() == f); + assertSame(f, f.fork()); + assertSame(f, pollTask()); helpQuiesce(); - threadAssertFalse(f.isDone()); - threadAssertTrue(g.isDone()); + checkNotDone(f); + checkCompletedNormally(g); }}; testInvokeOnPool(singletonPool(), a); } @@ -819,54 +938,55 @@ public class RecursiveActionTest extends * peekNextLocalTask returns least recent unexecuted task in async mode */ public void testPeekNextLocalTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(peekNextLocalTask() == g); - f.join(); + assertSame(f, f.fork()); + assertSame(g, peekNextLocalTask()); + assertNull(f.join()); helpQuiesce(); - threadAssertTrue(f.isDone()); + checkCompletedNormally(f); + checkCompletedNormally(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } /** - * pollNextLocalTask returns least recent unexecuted task - * without executing it, in async mode + * pollNextLocalTask returns least recent unexecuted task without + * executing it, in async mode */ public void testPollNextLocalTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollNextLocalTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollNextLocalTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + checkCompletedNormally(f); + checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); } /** - * pollTask returns an unexecuted task - * without executing it, in async mode + * pollTask returns an unexecuted task without executing it, in + * async mode */ public void testPollTaskAsync() { - RecursiveAction a = new RecursiveAction() { - public void compute() { + RecursiveAction a = new CheckedRecursiveAction() { + public void realCompute() { FibAction g = new FibAction(9); - g.fork(); + assertSame(g, g.fork()); FibAction f = new FibAction(8); - f.fork(); - threadAssertTrue(pollTask() == g); + assertSame(f, f.fork()); + assertSame(g, pollTask()); helpQuiesce(); - threadAssertTrue(f.isDone()); - threadAssertFalse(g.isDone()); + checkCompletedNormally(f); + checkNotDone(g); }}; testInvokeOnPool(asyncSingletonPool(), a); }