--- jsr166/src/test/tck/RecursiveActionTest.java 2010/08/11 19:50:02 1.10 +++ jsr166/src/test/tck/RecursiveActionTest.java 2010/09/13 07:51:18 1.14 @@ -3,25 +3,42 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */ + import junit.framework.*; import java.util.concurrent.*; import java.util.*; - public class RecursiveActionTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } + public static Test suite() { return new TestSuite(RecursiveActionTest.class); } - static final ForkJoinPool mainPool = new ForkJoinPool(); - static final ForkJoinPool singletonPool = new ForkJoinPool(1); - static final ForkJoinPool asyncSingletonPool = - new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, - null, true); + private static ForkJoinPool mainPool() { + return new ForkJoinPool(); + } + + private static ForkJoinPool singletonPool() { + return new ForkJoinPool(1); + } + + private static ForkJoinPool asyncSingletonPool() { + return new ForkJoinPool(1, + ForkJoinPool.defaultForkJoinWorkerThreadFactory, + null, true); + } + + private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { + try { + assertTrue(pool.invoke(a) == null); + } finally { + joinPool(pool); + } + } static final class FJException extends RuntimeException { FJException() { super(); } @@ -67,7 +84,6 @@ public class RecursiveActionTest extends * invoke returns when task completes normally. * isCompletedAbnormally and isCancelled return false for normally * completed tasks. getRawResult of a RecursiveAction returns null; - * */ public void testInvoke() { RecursiveAction a = new RecursiveAction() { @@ -80,7 +96,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isCompletedAbnormally()); threadAssertTrue(f.getRawResult() == null); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -99,7 +115,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isCompletedAbnormally()); threadAssertTrue(f.getRawResult() == null); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -115,7 +131,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.getRawResult() == null); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -134,7 +150,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -153,7 +169,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -172,7 +188,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -187,7 +203,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.result == 21); threadAssertTrue(f.isDone()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -205,7 +221,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(getQueuedTaskCount() == 0); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -222,7 +238,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -235,7 +251,7 @@ public class RecursiveActionTest extends f.quietlyInvoke(); threadAssertTrue(f.isDone()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -252,7 +268,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -271,7 +287,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -290,7 +306,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -306,7 +322,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isCompletedAbnormally()); threadAssertTrue(f.getException() instanceof FJException); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -323,7 +339,7 @@ public class RecursiveActionTest extends } catch (CancellationException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -341,7 +357,7 @@ public class RecursiveActionTest extends } catch (CancellationException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -361,7 +377,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -381,7 +397,7 @@ public class RecursiveActionTest extends unexpectedException(ex); } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -398,18 +414,19 @@ public class RecursiveActionTest extends threadAssertTrue(f.isCompletedAbnormally()); threadAssertTrue(f.getException() instanceof CancellationException); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** * getPool of executing task returns its pool */ public void testGetPool() { + final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new RecursiveAction() { public void compute() { threadAssertTrue(getPool() == mainPool); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } /** @@ -431,7 +448,7 @@ public class RecursiveActionTest extends public void compute() { threadAssertTrue(inForkJoinPool()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -449,20 +466,21 @@ public class RecursiveActionTest extends * getPool of current thread in pool returns its pool */ public void testWorkerGetPool() { + final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new RecursiveAction() { public void compute() { ForkJoinWorkerThread w = - (ForkJoinWorkerThread)(Thread.currentThread()); + (ForkJoinWorkerThread) Thread.currentThread(); threadAssertTrue(w.getPool() == mainPool); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } /** * getPoolIndex of current thread in pool returns 0 <= value < poolSize - * */ public void testWorkerGetPoolIndex() { + final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new RecursiveAction() { public void compute() { ForkJoinWorkerThread w = @@ -471,7 +489,7 @@ public class RecursiveActionTest extends threadAssertTrue(idx >= 0); threadAssertTrue(idx < mainPool.getPoolSize()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } @@ -502,7 +520,7 @@ public class RecursiveActionTest extends f.invoke(); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -519,7 +537,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -534,7 +552,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 0); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -551,7 +569,7 @@ public class RecursiveActionTest extends threadAssertTrue(g.isDone()); threadAssertTrue(g.result == 34); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -565,7 +583,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -585,7 +603,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -609,7 +627,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -628,7 +646,7 @@ public class RecursiveActionTest extends } catch (NullPointerException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -645,7 +663,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -661,7 +679,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -679,7 +697,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -701,7 +719,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -720,7 +738,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isDone()); threadAssertTrue(g.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -739,7 +757,7 @@ public class RecursiveActionTest extends threadAssertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -757,7 +775,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); helpQuiesce(); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -775,7 +793,7 @@ public class RecursiveActionTest extends helpQuiesce(); threadAssertFalse(f.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -794,7 +812,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isDone()); threadAssertTrue(g.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -812,7 +830,7 @@ public class RecursiveActionTest extends helpQuiesce(); threadAssertTrue(f.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -831,7 +849,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertFalse(g.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -850,7 +868,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertFalse(g.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } }