--- jsr166/src/test/tck/ForkJoinTaskTest.java 2009/07/31 23:02:49 1.1 +++ jsr166/src/test/tck/ForkJoinTaskTest.java 2010/08/11 19:50:02 1.8 @@ -3,35 +3,50 @@ * 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); } /** * Testing coverage notes: - * + * * To test extension methods and overrides, most tests use * BinaryAsyncAction extension class that processes joins * differently than supplied Recursive forms. - */ + */ 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(); } } @@ -40,7 +55,7 @@ public class ForkJoinTaskTest extends JS private volatile int controlState; static final AtomicIntegerFieldUpdater controlStateUpdater = - AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, + AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, "controlState"); private BinaryAsyncAction parent; @@ -92,7 +107,7 @@ public class ForkJoinTaskTest extends JS break; try { p.onComplete(a, s); - } catch(Throwable rex) { + } catch (Throwable rex) { p.completeExceptionally(rex); return; } @@ -129,7 +144,7 @@ public class ForkJoinTaskTest extends JS return controlState; } - protected final boolean compareAndSetControlState(int expect, + protected final boolean compareAndSetControlState(int expect, int update) { return controlStateUpdater.compareAndSet(this, expect, update); } @@ -148,12 +163,12 @@ public class ForkJoinTaskTest extends JS } - static final class AsyncFib extends BinaryAsyncAction { + static final class AsyncFib extends BinaryAsyncAction { int number; - public AsyncFib(int n) { + public AsyncFib(int n) { this.number = n; } - + public final boolean exec() { AsyncFib f = this; int n = f.number; @@ -177,12 +192,12 @@ public class ForkJoinTaskTest extends JS } - static final class FailingAsyncFib extends BinaryAsyncAction { + static final class FailingAsyncFib extends BinaryAsyncAction { int number; - public FailingAsyncFib(int n) { + public FailingAsyncFib(int n) { this.number = n; } - + public final boolean exec() { FailingAsyncFib f = this; int n = f.number; @@ -205,11 +220,11 @@ public class ForkJoinTaskTest extends JS } } - /** + /** * 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() { @@ -226,7 +241,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * quietlyInvoke task returns when task completes normally. * isCompletedAbnormally and isCancelled return false for normally * completed tasks @@ -246,7 +261,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * join of a forked task returns when task completes */ public void testForkJoin() { @@ -263,7 +278,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * get of a forked task returns when task completes */ public void testForkGet() { @@ -275,15 +290,15 @@ public class ForkJoinTaskTest extends JS f.get(); threadAssertTrue(f.number == 21); threadAssertTrue(f.isDone()); - } catch(Exception ex) { - unexpectedException(); + } catch (Exception ex) { + unexpectedException(ex); } } }; mainPool.invoke(a); } - /** + /** * timed get of a forked task returns when task completes */ public void testForkTimedGet() { @@ -292,18 +307,18 @@ 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(); + } catch (Exception ex) { + unexpectedException(ex); } } }; mainPool.invoke(a); } - /** + /** * timed get with null time unit throws NPE */ public void testForkTimedGetNPE() { @@ -313,32 +328,17 @@ public class ForkJoinTaskTest extends JS AsyncFib f = new AsyncFib(8); f.fork(); f.get(5L, null); - } catch(NullPointerException success) { - } catch(Exception ex) { - unexpectedException(); + shouldThrow(); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; mainPool.invoke(a); } - /** - * 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() { @@ -355,24 +355,7 @@ 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 */ @@ -391,7 +374,7 @@ public class ForkJoinTaskTest extends JS } - /** + /** * invoke task throws exception when task completes abnormally */ public void testAbnormalInvoke() { @@ -401,15 +384,15 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib f = new FailingAsyncFib(8); f.invoke(); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** - * quietelyInvoke task returns when task completes abnormally + /** + * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new RecursiveAction() { @@ -422,7 +405,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoin() { @@ -433,14 +416,14 @@ public class ForkJoinTaskTest extends JS f.fork(); f.join(); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** + /** * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGet() { @@ -451,14 +434,16 @@ public class ForkJoinTaskTest extends JS f.fork(); f.get(); shouldThrow(); - } catch(Exception success) { + } catch (ExecutionException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; mainPool.invoke(a); } - /** + /** * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGet() { @@ -467,55 +452,18 @@ 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); } } }; mainPool.invoke(a); } - /** - * 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() { @@ -532,7 +480,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * invoke task throws exception when task cancelled */ public void testCancelledInvoke() { @@ -543,14 +491,14 @@ public class ForkJoinTaskTest extends JS f.cancel(true); f.invoke(); shouldThrow(); - } catch(CancellationException success) { + } catch (CancellationException success) { } } }; mainPool.invoke(a); } - /** + /** * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoin() { @@ -562,14 +510,14 @@ public class ForkJoinTaskTest extends JS f.fork(); f.join(); shouldThrow(); - } catch(CancellationException success) { + } catch (CancellationException success) { } } }; mainPool.invoke(a); } - /** + /** * get of a forked task throws exception when task cancelled */ public void testCancelledForkGet() { @@ -581,14 +529,16 @@ public class ForkJoinTaskTest extends JS f.fork(); f.get(); shouldThrow(); - } catch(Exception success) { + } catch (CancellationException success) { + } catch (Exception ex) { + unexpectedException(ex); } } }; mainPool.invoke(a); } - /** + /** * timed get of a forked task throws exception when task cancelled */ public void testCancelledForkTimedGet() { @@ -598,57 +548,18 @@ 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 (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); - } - - /** + /** * quietlyJoin of a forked task returns when task cancelled */ public void testCancelledForkQuietlyJoin() { @@ -726,7 +637,7 @@ public class ForkJoinTaskTest extends JS a.invoke(); } - /** + /** * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { @@ -737,14 +648,14 @@ public class ForkJoinTaskTest extends JS f.completeExceptionally(new FJException()); f.invoke(); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** + /** * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2() { @@ -762,7 +673,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * invokeAll(tasks) with 1 argument invokes task */ public void testInvokeAll1() { @@ -777,7 +688,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3() { @@ -798,7 +709,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollection() { @@ -824,7 +735,7 @@ public class ForkJoinTaskTest extends JS } - /** + /** * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPE() { @@ -843,7 +754,7 @@ public class ForkJoinTaskTest extends JS mainPool.invoke(a); } - /** + /** * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2() { @@ -854,14 +765,14 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib g = new FailingAsyncFib(9); invokeAll(f, g); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** + /** * invokeAll(tasks) with 1 argument throws exception if task does */ public void testAbnormalInvokeAll1() { @@ -871,14 +782,14 @@ public class ForkJoinTaskTest extends JS FailingAsyncFib g = new FailingAsyncFib(9); invokeAll(g); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** + /** * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3() { @@ -890,14 +801,14 @@ public class ForkJoinTaskTest extends JS AsyncFib h = new AsyncFib(7); invokeAll(f, g, h); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** + /** * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollection() { @@ -913,14 +824,14 @@ public class ForkJoinTaskTest extends JS set.add(h); invokeAll(set); shouldThrow(); - } catch(FJException success) { + } catch (FJException success) { } } }; mainPool.invoke(a); } - /** + /** * tryUnfork returns true for most recent unexecuted task, * and suppresses execution */ @@ -940,7 +851,7 @@ public class ForkJoinTaskTest extends JS singletonPool.invoke(a); } - /** + /** * getSurplusQueuedTaskCount returns > 0 when * there are more tasks than threads */ @@ -960,7 +871,7 @@ public class ForkJoinTaskTest extends JS singletonPool.invoke(a); } - /** + /** * peekNextLocalTask returns most recent unexecuted task. */ public void testPeekNextLocalTask() { @@ -979,7 +890,7 @@ public class ForkJoinTaskTest extends JS singletonPool.invoke(a); } - /** + /** * pollNextLocalTask returns most recent unexecuted task * without executing it */ @@ -998,7 +909,7 @@ public class ForkJoinTaskTest extends JS singletonPool.invoke(a); } - /** + /** * pollTask returns an unexecuted task * without executing it */ @@ -1018,7 +929,7 @@ public class ForkJoinTaskTest extends JS singletonPool.invoke(a); } - /** + /** * peekNextLocalTask returns least recent unexecuted task in async mode */ public void testPeekNextLocalTaskAsync() { @@ -1037,7 +948,7 @@ public class ForkJoinTaskTest extends JS asyncSingletonPool.invoke(a); } - /** + /** * pollNextLocalTask returns least recent unexecuted task * without executing it, in async mode */ @@ -1057,7 +968,7 @@ public class ForkJoinTaskTest extends JS asyncSingletonPool.invoke(a); } - /** + /** * pollTask returns an unexecuted task * without executing it, in async mode */