--- jsr166/src/test/tck/RecursiveActionTest.java 2010/09/11 07:31:52 1.13 +++ jsr166/src/test/tck/RecursiveActionTest.java 2010/09/13 07:51:18 1.14 @@ -3,11 +3,11 @@ * 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) { @@ -18,11 +18,27 @@ public class RecursiveActionTest extends 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(); } @@ -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,19 +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 = @@ -470,7 +489,7 @@ public class RecursiveActionTest extends threadAssertTrue(idx >= 0); threadAssertTrue(idx < mainPool.getPoolSize()); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool, a); } @@ -501,7 +520,7 @@ public class RecursiveActionTest extends f.invoke(); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -518,7 +537,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -533,7 +552,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 0); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -550,7 +569,7 @@ public class RecursiveActionTest extends threadAssertTrue(g.isDone()); threadAssertTrue(g.result == 34); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -564,7 +583,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertTrue(f.result == 21); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -584,7 +603,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -608,7 +627,7 @@ public class RecursiveActionTest extends threadAssertTrue(h.isDone()); threadAssertTrue(h.result == 13); }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } @@ -627,7 +646,7 @@ public class RecursiveActionTest extends } catch (NullPointerException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -644,7 +663,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -660,7 +679,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -678,7 +697,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -700,7 +719,7 @@ public class RecursiveActionTest extends } catch (FJException success) { } }}; - mainPool.invoke(a); + testInvokeOnPool(mainPool(), a); } /** @@ -719,7 +738,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isDone()); threadAssertTrue(g.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -738,7 +757,7 @@ public class RecursiveActionTest extends threadAssertTrue(getSurplusQueuedTaskCount() > 0); helpQuiesce(); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -756,7 +775,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); helpQuiesce(); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -774,7 +793,7 @@ public class RecursiveActionTest extends helpQuiesce(); threadAssertFalse(f.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -793,7 +812,7 @@ public class RecursiveActionTest extends threadAssertFalse(f.isDone()); threadAssertTrue(g.isDone()); }}; - singletonPool.invoke(a); + testInvokeOnPool(singletonPool(), a); } /** @@ -811,7 +830,7 @@ public class RecursiveActionTest extends helpQuiesce(); threadAssertTrue(f.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -830,7 +849,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertFalse(g.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } /** @@ -849,7 +868,7 @@ public class RecursiveActionTest extends threadAssertTrue(f.isDone()); threadAssertFalse(g.isDone()); }}; - asyncSingletonPool.invoke(a); + testInvokeOnPool(asyncSingletonPool(), a); } }