--- jsr166/src/test/tck/ForkJoinTaskTest.java 2009/07/31 23:37:31 1.2 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2010/08/25 00:07:03 1.9 @@ -3,18 +3,35 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */ +import java.util.*; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.AbstractExecutorService; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.CancellationException; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.RecursiveAction; +import java.util.concurrent.RecursiveTask; +import java.util.concurrent.TimeUnit; import junit.framework.*; -import java.util.concurrent.*; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.*; import java.util.*; public class ForkJoinTaskTest 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(ForkJoinTaskTest.class); + return new TestSuite(ForkJoinTaskTest.class); } /** @@ -27,11 +44,9 @@ public class ForkJoinTaskTest extends JS static final ForkJoinPool mainPool = new ForkJoinPool(); static final ForkJoinPool singletonPool = new ForkJoinPool(1); - static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); - static { - asyncSingletonPool.setAsyncMode(true); - } - + static final ForkJoinPool asyncSingletonPool = + new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, + null, true); static final class FJException extends RuntimeException { FJException() { super(); } } @@ -148,7 +163,7 @@ public class ForkJoinTaskTest extends JS } - static final class AsyncFib extends BinaryAsyncAction { + static final class AsyncFib extends BinaryAsyncAction { int number; public AsyncFib(int n) { this.number = n; @@ -177,7 +192,7 @@ public class ForkJoinTaskTest extends JS } - static final class FailingAsyncFib extends BinaryAsyncAction { + static final class FailingAsyncFib extends BinaryAsyncAction { int number; public FailingAsyncFib(int n) { this.number = n; @@ -276,7 +291,7 @@ public class ForkJoinTaskTest extends JS threadAssertTrue(f.number == 21); threadAssertTrue(f.isDone()); } catch (Exception ex) { - unexpectedException(); + unexpectedException(ex); } } }; @@ -292,11 +307,11 @@ public class ForkJoinTaskTest extends JS try { AsyncFib f = new AsyncFib(8); f.fork(); - f.get(5L, TimeUnit.SECONDS); + f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadAssertTrue(f.number == 21); threadAssertTrue(f.isDone()); } catch (Exception ex) { - unexpectedException(); + unexpectedException(ex); } } }; @@ -313,9 +328,10 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); f.fork(); f.get(5L, null); + shouldThrow(); } catch (NullPointerException success) { } catch (Exception ex) { - unexpectedException(); + unexpectedException(ex); } } }; @@ -323,22 +339,6 @@ public class ForkJoinTaskTest extends JS } /** - * helpJoin of a forked task returns when task completes - */ - public void testForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.helpJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); - } - - /** * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoin() { @@ -356,23 +356,6 @@ public class ForkJoinTaskTest extends JS /** - * quietlyHelpJoin of a forked task returns when task completes - */ - public void testForkQuietlyHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.number == 21); - threadAssertTrue(f.isDone()); - } - }; - mainPool.invoke(a); - } - - - /** * helpQuiesce returns when tasks are complete. * getQueuedTaskCount returns 0 when quiescent */ @@ -409,7 +392,7 @@ public class ForkJoinTaskTest extends JS } /** - * quietelyInvoke task returns when task completes abnormally + * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new RecursiveAction() { @@ -451,7 +434,9 @@ public class ForkJoinTaskTest extends JS f.fork(); f.get(); shouldThrow(); - } catch (Exception success) { + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; @@ -467,27 +452,11 @@ public class ForkJoinTaskTest extends JS try { FailingAsyncFib f = new FailingAsyncFib(8); f.fork(); - f.get(5L, TimeUnit.SECONDS); - shouldThrow(); - } catch (Exception success) { - } - } - }; - mainPool.invoke(a); - } - - /** - * join of a forked task throws exception when task completes abnormally - */ - public void testAbnormalForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.helpJoin(); + f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); - } catch (FJException success) { + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; @@ -495,27 +464,6 @@ public class ForkJoinTaskTest extends JS } /** - * quietlyHelpJoin of a forked task returns when task completes abnormally. - * getException of failed task returns its exception. - * isCompletedAbnormally of a failed task returns true. - * isCancelled of a failed uncancelled task returns false - */ - public void testAbnormalForkQuietlyHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - FailingAsyncFib f = new FailingAsyncFib(8); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertFalse(f.isCancelled()); - threadAssertTrue(f.getException() instanceof FJException); - } - }; - mainPool.invoke(a); - } - - /** * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoin() { @@ -581,7 +529,9 @@ public class ForkJoinTaskTest extends JS f.fork(); f.get(); shouldThrow(); - } catch (Exception success) { + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; @@ -598,54 +548,15 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); f.cancel(true); f.fork(); - f.get(5L, TimeUnit.SECONDS); - shouldThrow(); - } catch (Exception success) { - } - } - }; - mainPool.invoke(a); - } - - /** - * join of a forked task throws exception when task cancelled - */ - public void testCancelledForkHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - try { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.helpJoin(); + f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); shouldThrow(); } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; mainPool.invoke(a); - } - - /** - * quietlyHelpJoin of a forked task returns when task cancelled. - * getException of cancelled task returns its exception. - * isCompletedAbnormally of a cancelled task returns true. - * isCancelled of a cancelled task returns true - */ - public void testCancelledForkQuietlyHelpJoin() { - RecursiveAction a = new RecursiveAction() { - public void compute() { - AsyncFib f = new AsyncFib(8); - f.cancel(true); - f.fork(); - f.quietlyHelpJoin(); - threadAssertTrue(f.isDone()); - threadAssertTrue(f.isCompletedAbnormally()); - threadAssertTrue(f.isCancelled()); - threadAssertTrue(f.getException() instanceof CancellationException); - } - }; - mainPool.invoke(a); } /**