3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
< |
import java.util.concurrent.ExecutionException; |
6 |
> |
|
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.HashSet; |
11 |
> |
import java.util.concurrent.Callable; |
12 |
|
import java.util.concurrent.CancellationException; |
13 |
+ |
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
16 |
|
import java.util.concurrent.RecursiveAction; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
19 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.HashSet; |
16 |
< |
import junit.framework.*; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ForkJoinTaskTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
|
|
29 |
|
public static Test suite() { |
49 |
|
} |
50 |
|
|
51 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
52 |
< |
try { |
52 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
53 |
|
assertFalse(a.isDone()); |
54 |
|
assertFalse(a.isCompletedNormally()); |
55 |
|
assertFalse(a.isCompletedAbnormally()); |
65 |
|
assertFalse(a.isCancelled()); |
66 |
|
assertNull(a.getException()); |
67 |
|
assertNull(a.getRawResult()); |
63 |
– |
} finally { |
64 |
– |
joinPool(pool); |
68 |
|
} |
69 |
|
} |
70 |
|
|
77 |
|
assertNull(a.getRawResult()); |
78 |
|
|
79 |
|
try { |
80 |
< |
a.get(0L, SECONDS); |
80 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
81 |
|
shouldThrow(); |
82 |
|
} catch (TimeoutException success) { |
83 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
97 |
|
|
98 |
|
{ |
99 |
|
Thread.currentThread().interrupt(); |
100 |
< |
long t0 = System.nanoTime(); |
100 |
> |
long startTime = System.nanoTime(); |
101 |
|
assertSame(expected, a.join()); |
102 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
102 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
103 |
|
Thread.interrupted(); |
104 |
|
} |
105 |
|
|
106 |
|
{ |
107 |
|
Thread.currentThread().interrupt(); |
108 |
< |
long t0 = System.nanoTime(); |
108 |
> |
long startTime = System.nanoTime(); |
109 |
|
a.quietlyJoin(); // should be no-op |
110 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
110 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
111 |
|
Thread.interrupted(); |
112 |
|
} |
113 |
|
|
115 |
|
assertFalse(a.cancel(true)); |
116 |
|
try { |
117 |
|
assertSame(expected, a.get()); |
118 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
< |
try { |
117 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
118 |
> |
assertSame(expected, a.get(randomTimeout(), randomTimeUnit())); |
119 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
120 |
|
} |
121 |
|
|
138 |
|
Thread.interrupted(); |
139 |
|
|
140 |
|
{ |
141 |
< |
long t0 = System.nanoTime(); |
141 |
> |
long startTime = System.nanoTime(); |
142 |
|
a.quietlyJoin(); // should be no-op |
143 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
143 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
144 |
|
} |
145 |
|
|
146 |
|
try { |
150 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
151 |
|
|
152 |
|
try { |
153 |
< |
a.get(5L, SECONDS); |
153 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
154 |
|
shouldThrow(); |
155 |
|
} catch (CancellationException success) { |
156 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
|
Thread.interrupted(); |
177 |
|
|
178 |
|
{ |
179 |
< |
long t0 = System.nanoTime(); |
179 |
> |
long startTime = System.nanoTime(); |
180 |
|
a.quietlyJoin(); // should be no-op |
181 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
181 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
182 |
|
} |
183 |
|
|
184 |
|
try { |
189 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
190 |
|
|
191 |
|
try { |
192 |
< |
a.get(5L, SECONDS); |
192 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
193 |
|
shouldThrow(); |
194 |
|
} catch (ExecutionException success) { |
195 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
215 |
|
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
216 |
|
"controlState"); |
217 |
|
|
218 |
< |
private BinaryAsyncAction parent; |
218 |
> |
private volatile BinaryAsyncAction parent; |
219 |
|
|
220 |
< |
private BinaryAsyncAction sibling; |
220 |
> |
private volatile BinaryAsyncAction sibling; |
221 |
|
|
222 |
|
protected BinaryAsyncAction() { |
223 |
|
} |
252 |
|
super.completeExceptionally(ex); |
253 |
|
} |
254 |
|
|
255 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
256 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
257 |
+ |
completeExceptionally(new FJException()); |
258 |
+ |
return true; |
259 |
+ |
} |
260 |
+ |
return false; |
261 |
+ |
} |
262 |
+ |
|
263 |
|
public final void complete() { |
264 |
|
BinaryAsyncAction a = this; |
265 |
|
for (;;) { |
281 |
|
} |
282 |
|
|
283 |
|
public final void completeExceptionally(Throwable ex) { |
284 |
< |
BinaryAsyncAction a = this; |
276 |
< |
while (!a.isCompletedAbnormally()) { |
284 |
> |
for (BinaryAsyncAction a = this;;) { |
285 |
|
a.completeThisExceptionally(ex); |
286 |
|
BinaryAsyncAction s = a.sibling; |
287 |
< |
if (s != null) |
288 |
< |
s.cancel(false); |
289 |
< |
if (!a.onException() || (a = a.parent) == null) |
287 |
> |
if (s != null && !s.isDone()) |
288 |
> |
s.completeExceptionally(ex); |
289 |
> |
if ((a = a.parent) == null) |
290 |
|
break; |
291 |
|
} |
292 |
|
} |
336 |
|
public final boolean exec() { |
337 |
|
AsyncFib f = this; |
338 |
|
int n = f.number; |
339 |
< |
if (n > 1) { |
340 |
< |
while (n > 1) { |
341 |
< |
AsyncFib p = f; |
342 |
< |
AsyncFib r = new AsyncFib(n - 2); |
343 |
< |
f = new AsyncFib(--n); |
344 |
< |
p.linkSubtasks(r, f); |
337 |
< |
r.fork(); |
338 |
< |
} |
339 |
< |
f.number = n; |
339 |
> |
while (n > 1) { |
340 |
> |
AsyncFib p = f; |
341 |
> |
AsyncFib r = new AsyncFib(n - 2); |
342 |
> |
f = new AsyncFib(--n); |
343 |
> |
p.linkSubtasks(r, f); |
344 |
> |
r.fork(); |
345 |
|
} |
346 |
|
f.complete(); |
347 |
|
return false; |
361 |
|
public final boolean exec() { |
362 |
|
FailingAsyncFib f = this; |
363 |
|
int n = f.number; |
364 |
< |
if (n > 1) { |
365 |
< |
while (n > 1) { |
366 |
< |
FailingAsyncFib p = f; |
367 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
368 |
< |
f = new FailingAsyncFib(--n); |
369 |
< |
p.linkSubtasks(r, f); |
365 |
< |
r.fork(); |
366 |
< |
} |
367 |
< |
f.number = n; |
364 |
> |
while (n > 1) { |
365 |
> |
FailingAsyncFib p = f; |
366 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
367 |
> |
f = new FailingAsyncFib(--n); |
368 |
> |
p.linkSubtasks(r, f); |
369 |
> |
r.fork(); |
370 |
|
} |
371 |
|
f.complete(); |
372 |
|
return false; |
463 |
|
AsyncFib f = new AsyncFib(8); |
464 |
|
assertSame(f, f.fork()); |
465 |
|
try { |
466 |
< |
f.get(5L, null); |
466 |
> |
f.get(randomTimeout(), null); |
467 |
|
shouldThrow(); |
468 |
|
} catch (NullPointerException success) {} |
469 |
|
}}; |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
+ |
* completeExceptionally(null) surprisingly has the same effect as |
776 |
+ |
* completeExceptionally(new RuntimeException()) |
777 |
+ |
*/ |
778 |
+ |
public void testCompleteExceptionally_null() { |
779 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
780 |
+ |
protected void realCompute() { |
781 |
+ |
AsyncFib f = new AsyncFib(8); |
782 |
+ |
f.completeExceptionally(null); |
783 |
+ |
try { |
784 |
+ |
f.invoke(); |
785 |
+ |
shouldThrow(); |
786 |
+ |
} catch (RuntimeException success) { |
787 |
+ |
assertSame(success.getClass(), RuntimeException.class); |
788 |
+ |
assertNull(success.getCause()); |
789 |
+ |
checkCompletedAbnormally(f, success); |
790 |
+ |
} |
791 |
+ |
}}; |
792 |
+ |
testInvokeOnPool(mainPool(), a); |
793 |
+ |
} |
794 |
+ |
|
795 |
+ |
/** |
796 |
|
* invokeAll(t1, t2) invokes all task arguments |
797 |
|
*/ |
798 |
|
public void testInvokeAll2() { |
892 |
|
protected void realCompute() { |
893 |
|
AsyncFib f = new AsyncFib(8); |
894 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
895 |
+ |
ForkJoinTask[] tasks = { f, g }; |
896 |
+ |
shuffle(tasks); |
897 |
|
try { |
898 |
< |
invokeAll(f, g); |
898 |
> |
invokeAll(tasks); |
899 |
|
shouldThrow(); |
900 |
|
} catch (FJException success) { |
901 |
|
checkCompletedAbnormally(g, success); |
930 |
|
AsyncFib f = new AsyncFib(8); |
931 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
932 |
|
AsyncFib h = new AsyncFib(7); |
933 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
934 |
+ |
shuffle(tasks); |
935 |
|
try { |
936 |
< |
invokeAll(f, g, h); |
936 |
> |
invokeAll(tasks); |
937 |
|
shouldThrow(); |
938 |
|
} catch (FJException success) { |
939 |
|
checkCompletedAbnormally(g, success); |
951 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
952 |
|
AsyncFib g = new AsyncFib(9); |
953 |
|
AsyncFib h = new AsyncFib(7); |
954 |
< |
HashSet set = new HashSet(); |
955 |
< |
set.add(f); |
929 |
< |
set.add(g); |
930 |
< |
set.add(h); |
954 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
955 |
> |
shuffle(tasks); |
956 |
|
try { |
957 |
< |
invokeAll(set); |
957 |
> |
invokeAll(Arrays.asList(tasks)); |
958 |
|
shouldThrow(); |
959 |
|
} catch (FJException success) { |
960 |
|
checkCompletedAbnormally(f, success); |
1210 |
|
AsyncFib f = new AsyncFib(8); |
1211 |
|
assertSame(f, f.fork()); |
1212 |
|
try { |
1213 |
< |
f.get(5L, null); |
1213 |
> |
f.get(randomTimeout(), null); |
1214 |
|
shouldThrow(); |
1215 |
|
} catch (NullPointerException success) {} |
1216 |
|
}}; |
1561 |
|
protected void realCompute() { |
1562 |
|
AsyncFib f = new AsyncFib(8); |
1563 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1564 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1565 |
+ |
shuffle(tasks); |
1566 |
|
try { |
1567 |
< |
invokeAll(f, g); |
1567 |
> |
invokeAll(tasks); |
1568 |
|
shouldThrow(); |
1569 |
|
} catch (FJException success) { |
1570 |
|
checkCompletedAbnormally(g, success); |
1599 |
|
AsyncFib f = new AsyncFib(8); |
1600 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1601 |
|
AsyncFib h = new AsyncFib(7); |
1602 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1603 |
+ |
shuffle(tasks); |
1604 |
|
try { |
1605 |
< |
invokeAll(f, g, h); |
1605 |
> |
invokeAll(tasks); |
1606 |
|
shouldThrow(); |
1607 |
|
} catch (FJException success) { |
1608 |
|
checkCompletedAbnormally(g, success); |
1620 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1621 |
|
AsyncFib g = new AsyncFib(9); |
1622 |
|
AsyncFib h = new AsyncFib(7); |
1623 |
< |
HashSet set = new HashSet(); |
1624 |
< |
set.add(f); |
1596 |
< |
set.add(g); |
1597 |
< |
set.add(h); |
1623 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1624 |
> |
shuffle(tasks); |
1625 |
|
try { |
1626 |
< |
invokeAll(set); |
1626 |
> |
invokeAll(Arrays.asList(tasks)); |
1627 |
|
shouldThrow(); |
1628 |
|
} catch (FJException success) { |
1629 |
|
checkCompletedAbnormally(f, success); |
1649 |
|
testInvokeOnPool(mainPool(), a); |
1650 |
|
} |
1651 |
|
|
1652 |
+ |
/** |
1653 |
+ |
* adapt(runnable).toString() contains toString of wrapped task |
1654 |
+ |
*/ |
1655 |
+ |
public void testAdapt_Runnable_toString() { |
1656 |
+ |
if (testImplementationDetails) { |
1657 |
+ |
Runnable r = () -> {}; |
1658 |
+ |
ForkJoinTask<?> task = ForkJoinTask.adapt(r); |
1659 |
+ |
assertEquals( |
1660 |
+ |
identityString(task) + "[Wrapped task = " + r.toString() + "]", |
1661 |
+ |
task.toString()); |
1662 |
+ |
} |
1663 |
+ |
} |
1664 |
+ |
|
1665 |
+ |
/** |
1666 |
+ |
* adapt(runnable, x).toString() contains toString of wrapped task |
1667 |
+ |
*/ |
1668 |
+ |
public void testAdapt_Runnable_withResult_toString() { |
1669 |
+ |
if (testImplementationDetails) { |
1670 |
+ |
Runnable r = () -> {}; |
1671 |
+ |
ForkJoinTask<String> task = ForkJoinTask.adapt(r, ""); |
1672 |
+ |
assertEquals( |
1673 |
+ |
identityString(task) + "[Wrapped task = " + r.toString() + "]", |
1674 |
+ |
task.toString()); |
1675 |
+ |
} |
1676 |
+ |
} |
1677 |
+ |
|
1678 |
+ |
/** |
1679 |
+ |
* adapt(callable).toString() contains toString of wrapped task |
1680 |
+ |
*/ |
1681 |
+ |
public void testAdapt_Callable_toString() { |
1682 |
+ |
if (testImplementationDetails) { |
1683 |
+ |
Callable<String> c = () -> ""; |
1684 |
+ |
ForkJoinTask<String> task = ForkJoinTask.adapt(c); |
1685 |
+ |
assertEquals( |
1686 |
+ |
identityString(task) + "[Wrapped task = " + c.toString() + "]", |
1687 |
+ |
task.toString()); |
1688 |
+ |
} |
1689 |
+ |
} |
1690 |
|
} |