5 |
|
*/ |
6 |
|
|
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
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; |
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); } |
87 |
|
checkCompletedNormally(a, null); |
88 |
|
} |
89 |
|
|
90 |
< |
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) { |
90 |
> |
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expectedValue) { |
91 |
|
assertTrue(a.isDone()); |
92 |
|
assertFalse(a.isCancelled()); |
93 |
|
assertTrue(a.isCompletedNormally()); |
94 |
|
assertFalse(a.isCompletedAbnormally()); |
95 |
|
assertNull(a.getException()); |
96 |
< |
assertSame(expected, a.getRawResult()); |
96 |
> |
assertSame(expectedValue, a.getRawResult()); |
97 |
|
|
98 |
|
{ |
99 |
|
Thread.currentThread().interrupt(); |
100 |
|
long startTime = System.nanoTime(); |
101 |
< |
assertSame(expected, a.join()); |
102 |
< |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
101 |
> |
assertSame(expectedValue, a.join()); |
102 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
103 |
|
Thread.interrupted(); |
104 |
|
} |
105 |
|
|
107 |
|
Thread.currentThread().interrupt(); |
108 |
|
long startTime = System.nanoTime(); |
109 |
|
a.quietlyJoin(); // should be no-op |
110 |
< |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
110 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
111 |
|
Thread.interrupted(); |
112 |
|
} |
113 |
|
|
114 |
|
assertFalse(a.cancel(false)); |
115 |
|
assertFalse(a.cancel(true)); |
116 |
+ |
|
117 |
+ |
T v1 = null, v2 = null; |
118 |
|
try { |
119 |
< |
assertSame(expected, a.get()); |
120 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
< |
try { |
119 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
119 |
> |
v1 = a.get(); |
120 |
> |
v2 = a.get(randomTimeout(), randomTimeUnit()); |
121 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
+ |
assertSame(expectedValue, v1); |
123 |
+ |
assertSame(expectedValue, v2); |
124 |
|
} |
125 |
|
|
126 |
|
void checkCancelled(ForkJoinTask a) { |
144 |
|
{ |
145 |
|
long startTime = System.nanoTime(); |
146 |
|
a.quietlyJoin(); // should be no-op |
147 |
< |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
147 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
148 |
|
} |
149 |
|
|
150 |
|
try { |
154 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
|
|
156 |
|
try { |
157 |
< |
a.get(5L, SECONDS); |
157 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
158 |
|
shouldThrow(); |
159 |
|
} catch (CancellationException success) { |
160 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
182 |
|
{ |
183 |
|
long startTime = System.nanoTime(); |
184 |
|
a.quietlyJoin(); // should be no-op |
185 |
< |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
185 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
186 |
|
} |
187 |
|
|
188 |
|
try { |
193 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
194 |
|
|
195 |
|
try { |
196 |
< |
a.get(5L, SECONDS); |
196 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
197 |
|
shouldThrow(); |
198 |
|
} catch (ExecutionException success) { |
199 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
256 |
|
super.completeExceptionally(ex); |
257 |
|
} |
258 |
|
|
259 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
260 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
261 |
+ |
completeExceptionally(new FJException()); |
262 |
+ |
return true; |
263 |
+ |
} |
264 |
+ |
return false; |
265 |
+ |
} |
266 |
+ |
|
267 |
|
public final void complete() { |
268 |
|
BinaryAsyncAction a = this; |
269 |
|
for (;;) { |
285 |
|
} |
286 |
|
|
287 |
|
public final void completeExceptionally(Throwable ex) { |
288 |
< |
BinaryAsyncAction a = this; |
278 |
< |
while (!a.isCompletedAbnormally()) { |
288 |
> |
for (BinaryAsyncAction a = this;;) { |
289 |
|
a.completeThisExceptionally(ex); |
290 |
|
BinaryAsyncAction s = a.sibling; |
291 |
< |
if (s != null) |
292 |
< |
s.cancel(false); |
293 |
< |
if (!a.onException() || (a = a.parent) == null) |
291 |
> |
if (s != null && !s.isDone()) |
292 |
> |
s.completeExceptionally(ex); |
293 |
> |
if ((a = a.parent) == null) |
294 |
|
break; |
295 |
|
} |
296 |
|
} |
340 |
|
public final boolean exec() { |
341 |
|
AsyncFib f = this; |
342 |
|
int n = f.number; |
343 |
< |
if (n > 1) { |
344 |
< |
while (n > 1) { |
345 |
< |
AsyncFib p = f; |
346 |
< |
AsyncFib r = new AsyncFib(n - 2); |
347 |
< |
f = new AsyncFib(--n); |
348 |
< |
p.linkSubtasks(r, f); |
339 |
< |
r.fork(); |
340 |
< |
} |
341 |
< |
f.number = n; |
343 |
> |
while (n > 1) { |
344 |
> |
AsyncFib p = f; |
345 |
> |
AsyncFib r = new AsyncFib(n - 2); |
346 |
> |
f = new AsyncFib(--n); |
347 |
> |
p.linkSubtasks(r, f); |
348 |
> |
r.fork(); |
349 |
|
} |
350 |
|
f.complete(); |
351 |
|
return false; |
365 |
|
public final boolean exec() { |
366 |
|
FailingAsyncFib f = this; |
367 |
|
int n = f.number; |
368 |
< |
if (n > 1) { |
369 |
< |
while (n > 1) { |
370 |
< |
FailingAsyncFib p = f; |
371 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
372 |
< |
f = new FailingAsyncFib(--n); |
373 |
< |
p.linkSubtasks(r, f); |
367 |
< |
r.fork(); |
368 |
< |
} |
369 |
< |
f.number = n; |
368 |
> |
while (n > 1) { |
369 |
> |
FailingAsyncFib p = f; |
370 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
371 |
> |
f = new FailingAsyncFib(--n); |
372 |
> |
p.linkSubtasks(r, f); |
373 |
> |
r.fork(); |
374 |
|
} |
375 |
|
f.complete(); |
376 |
|
return false; |
467 |
|
AsyncFib f = new AsyncFib(8); |
468 |
|
assertSame(f, f.fork()); |
469 |
|
try { |
470 |
< |
f.get(5L, null); |
470 |
> |
f.get(randomTimeout(), null); |
471 |
|
shouldThrow(); |
472 |
|
} catch (NullPointerException success) {} |
473 |
|
}}; |
776 |
|
} |
777 |
|
|
778 |
|
/** |
779 |
+ |
* completeExceptionally(null) surprisingly has the same effect as |
780 |
+ |
* completeExceptionally(new RuntimeException()) |
781 |
+ |
*/ |
782 |
+ |
public void testCompleteExceptionally_null() { |
783 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
784 |
+ |
protected void realCompute() { |
785 |
+ |
AsyncFib f = new AsyncFib(8); |
786 |
+ |
f.completeExceptionally(null); |
787 |
+ |
try { |
788 |
+ |
f.invoke(); |
789 |
+ |
shouldThrow(); |
790 |
+ |
} catch (RuntimeException success) { |
791 |
+ |
assertSame(success.getClass(), RuntimeException.class); |
792 |
+ |
assertNull(success.getCause()); |
793 |
+ |
checkCompletedAbnormally(f, success); |
794 |
+ |
} |
795 |
+ |
}}; |
796 |
+ |
testInvokeOnPool(mainPool(), a); |
797 |
+ |
} |
798 |
+ |
|
799 |
+ |
/** |
800 |
|
* invokeAll(t1, t2) invokes all task arguments |
801 |
|
*/ |
802 |
|
public void testInvokeAll2() { |
896 |
|
protected void realCompute() { |
897 |
|
AsyncFib f = new AsyncFib(8); |
898 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
899 |
+ |
ForkJoinTask[] tasks = { f, g }; |
900 |
+ |
shuffle(tasks); |
901 |
|
try { |
902 |
< |
invokeAll(f, g); |
902 |
> |
invokeAll(tasks); |
903 |
|
shouldThrow(); |
904 |
|
} catch (FJException success) { |
905 |
|
checkCompletedAbnormally(g, success); |
934 |
|
AsyncFib f = new AsyncFib(8); |
935 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
936 |
|
AsyncFib h = new AsyncFib(7); |
937 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
938 |
+ |
shuffle(tasks); |
939 |
|
try { |
940 |
< |
invokeAll(f, g, h); |
940 |
> |
invokeAll(tasks); |
941 |
|
shouldThrow(); |
942 |
|
} catch (FJException success) { |
943 |
|
checkCompletedAbnormally(g, success); |
955 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
956 |
|
AsyncFib g = new AsyncFib(9); |
957 |
|
AsyncFib h = new AsyncFib(7); |
958 |
< |
HashSet set = new HashSet(); |
959 |
< |
set.add(f); |
931 |
< |
set.add(g); |
932 |
< |
set.add(h); |
958 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
959 |
> |
shuffle(tasks); |
960 |
|
try { |
961 |
< |
invokeAll(set); |
961 |
> |
invokeAll(Arrays.asList(tasks)); |
962 |
|
shouldThrow(); |
963 |
|
} catch (FJException success) { |
964 |
|
checkCompletedAbnormally(f, success); |
1214 |
|
AsyncFib f = new AsyncFib(8); |
1215 |
|
assertSame(f, f.fork()); |
1216 |
|
try { |
1217 |
< |
f.get(5L, null); |
1217 |
> |
f.get(randomTimeout(), null); |
1218 |
|
shouldThrow(); |
1219 |
|
} catch (NullPointerException success) {} |
1220 |
|
}}; |
1565 |
|
protected void realCompute() { |
1566 |
|
AsyncFib f = new AsyncFib(8); |
1567 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1568 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1569 |
+ |
shuffle(tasks); |
1570 |
|
try { |
1571 |
< |
invokeAll(f, g); |
1571 |
> |
invokeAll(tasks); |
1572 |
|
shouldThrow(); |
1573 |
|
} catch (FJException success) { |
1574 |
|
checkCompletedAbnormally(g, success); |
1603 |
|
AsyncFib f = new AsyncFib(8); |
1604 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1605 |
|
AsyncFib h = new AsyncFib(7); |
1606 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1607 |
+ |
shuffle(tasks); |
1608 |
|
try { |
1609 |
< |
invokeAll(f, g, h); |
1609 |
> |
invokeAll(tasks); |
1610 |
|
shouldThrow(); |
1611 |
|
} catch (FJException success) { |
1612 |
|
checkCompletedAbnormally(g, success); |
1624 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1625 |
|
AsyncFib g = new AsyncFib(9); |
1626 |
|
AsyncFib h = new AsyncFib(7); |
1627 |
< |
HashSet set = new HashSet(); |
1628 |
< |
set.add(f); |
1598 |
< |
set.add(g); |
1599 |
< |
set.add(h); |
1627 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1628 |
> |
shuffle(tasks); |
1629 |
|
try { |
1630 |
< |
invokeAll(set); |
1630 |
> |
invokeAll(Arrays.asList(tasks)); |
1631 |
|
shouldThrow(); |
1632 |
|
} catch (FJException success) { |
1633 |
|
checkCompletedAbnormally(f, success); |
1653 |
|
testInvokeOnPool(mainPool(), a); |
1654 |
|
} |
1655 |
|
|
1656 |
+ |
/** |
1657 |
+ |
* adapt(runnable).toString() contains toString of wrapped task |
1658 |
+ |
*/ |
1659 |
+ |
public void testAdapt_Runnable_toString() { |
1660 |
+ |
if (testImplementationDetails) { |
1661 |
+ |
Runnable r = () -> {}; |
1662 |
+ |
ForkJoinTask<?> task = ForkJoinTask.adapt(r); |
1663 |
+ |
assertEquals( |
1664 |
+ |
identityString(task) + "[Wrapped task = " + r.toString() + "]", |
1665 |
+ |
task.toString()); |
1666 |
+ |
} |
1667 |
+ |
} |
1668 |
+ |
|
1669 |
+ |
/** |
1670 |
+ |
* adapt(runnable, x).toString() contains toString of wrapped task |
1671 |
+ |
*/ |
1672 |
+ |
public void testAdapt_Runnable_withResult_toString() { |
1673 |
+ |
if (testImplementationDetails) { |
1674 |
+ |
Runnable r = () -> {}; |
1675 |
+ |
ForkJoinTask<String> task = ForkJoinTask.adapt(r, ""); |
1676 |
+ |
assertEquals( |
1677 |
+ |
identityString(task) + "[Wrapped task = " + r.toString() + "]", |
1678 |
+ |
task.toString()); |
1679 |
+ |
} |
1680 |
+ |
} |
1681 |
+ |
|
1682 |
+ |
/** |
1683 |
+ |
* adapt(callable).toString() contains toString of wrapped task |
1684 |
+ |
*/ |
1685 |
+ |
public void testAdapt_Callable_toString() { |
1686 |
+ |
if (testImplementationDetails) { |
1687 |
+ |
Callable<String> c = () -> ""; |
1688 |
+ |
ForkJoinTask<String> task = ForkJoinTask.adapt(c); |
1689 |
+ |
assertEquals( |
1690 |
+ |
identityString(task) + "[Wrapped task = " + c.toString() + "]", |
1691 |
+ |
task.toString()); |
1692 |
+ |
} |
1693 |
+ |
} |
1694 |
|
} |