author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 45937 | 646816090183 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
34 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
35394 | 35 |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.HashSet; |
|
38 |
import java.util.concurrent.CancellationException; |
|
39 |
import java.util.concurrent.ExecutionException; |
|
40 |
import java.util.concurrent.ForkJoinPool; |
|
41 |
import java.util.concurrent.ForkJoinTask; |
|
42 |
import java.util.concurrent.ForkJoinWorkerThread; |
|
43 |
import java.util.concurrent.RecursiveAction; |
|
44 |
import java.util.concurrent.SynchronousQueue; |
|
45 |
import java.util.concurrent.ThreadLocalRandom; |
|
46 |
import java.util.concurrent.TimeoutException; |
|
47 |
||
48 |
import junit.framework.Test; |
|
49 |
import junit.framework.TestSuite; |
|
50 |
||
51 |
public class RecursiveActionTest extends JSR166TestCase { |
|
52 |
||
53 |
public static void main(String[] args) { |
|
54 |
main(suite(), args); |
|
55 |
} |
|
56 |
||
57 |
public static Test suite() { |
|
58 |
return new TestSuite(RecursiveActionTest.class); |
|
59 |
} |
|
60 |
||
61 |
private static ForkJoinPool mainPool() { |
|
62 |
return new ForkJoinPool(); |
|
63 |
} |
|
64 |
||
65 |
private static ForkJoinPool singletonPool() { |
|
66 |
return new ForkJoinPool(1); |
|
67 |
} |
|
68 |
||
69 |
private static ForkJoinPool asyncSingletonPool() { |
|
70 |
return new ForkJoinPool(1, |
|
71 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
|
72 |
null, true); |
|
73 |
} |
|
74 |
||
75 |
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
|
76 |
try (PoolCleaner cleaner = cleaner(pool)) { |
|
77 |
checkNotDone(a); |
|
78 |
||
79 |
assertNull(pool.invoke(a)); |
|
80 |
||
81 |
checkCompletedNormally(a); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
void checkNotDone(RecursiveAction a) { |
|
86 |
assertFalse(a.isDone()); |
|
87 |
assertFalse(a.isCompletedNormally()); |
|
88 |
assertFalse(a.isCompletedAbnormally()); |
|
89 |
assertFalse(a.isCancelled()); |
|
90 |
assertNull(a.getException()); |
|
91 |
assertNull(a.getRawResult()); |
|
92 |
||
93 |
if (! ForkJoinTask.inForkJoinPool()) { |
|
94 |
Thread.currentThread().interrupt(); |
|
95 |
try { |
|
96 |
a.get(); |
|
97 |
shouldThrow(); |
|
98 |
} catch (InterruptedException success) { |
|
99 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
100 |
||
101 |
Thread.currentThread().interrupt(); |
|
102 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
103 |
a.get(randomTimeout(), randomTimeUnit()); |
35394 | 104 |
shouldThrow(); |
105 |
} catch (InterruptedException success) { |
|
106 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
107 |
} |
|
108 |
||
109 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
110 |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
35394 | 111 |
shouldThrow(); |
112 |
} catch (TimeoutException success) { |
|
113 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
114 |
} |
|
115 |
||
116 |
void checkCompletedNormally(RecursiveAction a) { |
|
117 |
assertTrue(a.isDone()); |
|
118 |
assertFalse(a.isCancelled()); |
|
119 |
assertTrue(a.isCompletedNormally()); |
|
120 |
assertFalse(a.isCompletedAbnormally()); |
|
121 |
assertNull(a.getException()); |
|
122 |
assertNull(a.getRawResult()); |
|
123 |
assertNull(a.join()); |
|
124 |
assertFalse(a.cancel(false)); |
|
125 |
assertFalse(a.cancel(true)); |
|
126 |
try { |
|
127 |
assertNull(a.get()); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
128 |
assertNull(a.get(randomTimeout(), randomTimeUnit())); |
35394 | 129 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
} |
|
131 |
||
132 |
void checkCancelled(RecursiveAction a) { |
|
133 |
assertTrue(a.isDone()); |
|
134 |
assertTrue(a.isCancelled()); |
|
135 |
assertFalse(a.isCompletedNormally()); |
|
136 |
assertTrue(a.isCompletedAbnormally()); |
|
137 |
assertTrue(a.getException() instanceof CancellationException); |
|
138 |
assertNull(a.getRawResult()); |
|
139 |
||
140 |
try { |
|
141 |
a.join(); |
|
142 |
shouldThrow(); |
|
143 |
} catch (CancellationException success) { |
|
144 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
145 |
||
146 |
try { |
|
147 |
a.get(); |
|
148 |
shouldThrow(); |
|
149 |
} catch (CancellationException success) { |
|
150 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
151 |
||
152 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
153 |
a.get(randomTimeout(), randomTimeUnit()); |
35394 | 154 |
shouldThrow(); |
155 |
} catch (CancellationException success) { |
|
156 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
157 |
} |
|
158 |
||
159 |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
|
160 |
assertTrue(a.isDone()); |
|
161 |
assertFalse(a.isCancelled()); |
|
162 |
assertFalse(a.isCompletedNormally()); |
|
163 |
assertTrue(a.isCompletedAbnormally()); |
|
164 |
assertSame(t.getClass(), a.getException().getClass()); |
|
165 |
assertNull(a.getRawResult()); |
|
166 |
assertFalse(a.cancel(false)); |
|
167 |
assertFalse(a.cancel(true)); |
|
168 |
||
169 |
try { |
|
170 |
a.join(); |
|
171 |
shouldThrow(); |
|
172 |
} catch (Throwable expected) { |
|
173 |
assertSame(expected.getClass(), t.getClass()); |
|
174 |
} |
|
175 |
||
176 |
try { |
|
177 |
a.get(); |
|
178 |
shouldThrow(); |
|
179 |
} catch (ExecutionException success) { |
|
180 |
assertSame(t.getClass(), success.getCause().getClass()); |
|
181 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
182 |
||
183 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
184 |
a.get(randomTimeout(), randomTimeUnit()); |
35394 | 185 |
shouldThrow(); |
186 |
} catch (ExecutionException success) { |
|
187 |
assertSame(t.getClass(), success.getCause().getClass()); |
|
188 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
189 |
} |
|
190 |
||
191 |
public static final class FJException extends RuntimeException { |
|
192 |
public FJException() { super(); } |
|
193 |
public FJException(Throwable cause) { super(cause); } |
|
194 |
} |
|
195 |
||
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
40817
diff
changeset
|
196 |
/** A simple recursive action for testing. */ |
35394 | 197 |
final class FibAction extends CheckedRecursiveAction { |
198 |
final int number; |
|
199 |
int result; |
|
200 |
FibAction(int n) { number = n; } |
|
201 |
protected void realCompute() { |
|
202 |
int n = number; |
|
203 |
if (n <= 1) |
|
204 |
result = n; |
|
205 |
else { |
|
206 |
FibAction f1 = new FibAction(n - 1); |
|
207 |
FibAction f2 = new FibAction(n - 2); |
|
208 |
invokeAll(f1, f2); |
|
209 |
result = f1.result + f2.result; |
|
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
||
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
40817
diff
changeset
|
214 |
/** A recursive action failing in base case. */ |
35394 | 215 |
static final class FailingFibAction extends RecursiveAction { |
216 |
final int number; |
|
217 |
int result; |
|
218 |
FailingFibAction(int n) { number = n; } |
|
219 |
public void compute() { |
|
220 |
int n = number; |
|
221 |
if (n <= 1) |
|
222 |
throw new FJException(); |
|
223 |
else { |
|
224 |
FailingFibAction f1 = new FailingFibAction(n - 1); |
|
225 |
FailingFibAction f2 = new FailingFibAction(n - 2); |
|
226 |
invokeAll(f1, f2); |
|
227 |
result = f1.result + f2.result; |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* invoke returns when task completes normally. |
|
234 |
* isCompletedAbnormally and isCancelled return false for normally |
|
235 |
* completed tasks. getRawResult of a RecursiveAction returns null; |
|
236 |
*/ |
|
237 |
public void testInvoke() { |
|
238 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
239 |
protected void realCompute() { |
|
240 |
FibAction f = new FibAction(8); |
|
241 |
assertNull(f.invoke()); |
|
242 |
assertEquals(21, f.result); |
|
243 |
checkCompletedNormally(f); |
|
244 |
}}; |
|
245 |
testInvokeOnPool(mainPool(), a); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* quietlyInvoke task returns when task completes normally. |
|
250 |
* isCompletedAbnormally and isCancelled return false for normally |
|
251 |
* completed tasks |
|
252 |
*/ |
|
253 |
public void testQuietlyInvoke() { |
|
254 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
255 |
protected void realCompute() { |
|
256 |
FibAction f = new FibAction(8); |
|
257 |
f.quietlyInvoke(); |
|
258 |
assertEquals(21, f.result); |
|
259 |
checkCompletedNormally(f); |
|
260 |
}}; |
|
261 |
testInvokeOnPool(mainPool(), a); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* join of a forked task returns when task completes |
|
266 |
*/ |
|
267 |
public void testForkJoin() { |
|
268 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
269 |
protected void realCompute() { |
|
270 |
FibAction f = new FibAction(8); |
|
271 |
assertSame(f, f.fork()); |
|
272 |
assertNull(f.join()); |
|
273 |
assertEquals(21, f.result); |
|
274 |
checkCompletedNormally(f); |
|
275 |
}}; |
|
276 |
testInvokeOnPool(mainPool(), a); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
|
281 |
*/ |
|
282 |
public void testJoinIgnoresInterrupts() { |
|
283 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
284 |
protected void realCompute() { |
|
285 |
FibAction f = new FibAction(8); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
286 |
final Thread currentThread = Thread.currentThread(); |
35394 | 287 |
|
288 |
// test join() |
|
289 |
assertSame(f, f.fork()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
290 |
currentThread.interrupt(); |
35394 | 291 |
assertNull(f.join()); |
292 |
Thread.interrupted(); |
|
293 |
assertEquals(21, f.result); |
|
294 |
checkCompletedNormally(f); |
|
295 |
||
296 |
f = new FibAction(8); |
|
297 |
f.cancel(true); |
|
298 |
assertSame(f, f.fork()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
299 |
currentThread.interrupt(); |
35394 | 300 |
try { |
301 |
f.join(); |
|
302 |
shouldThrow(); |
|
303 |
} catch (CancellationException success) { |
|
304 |
Thread.interrupted(); |
|
305 |
checkCancelled(f); |
|
306 |
} |
|
307 |
||
308 |
f = new FibAction(8); |
|
309 |
f.completeExceptionally(new FJException()); |
|
310 |
assertSame(f, f.fork()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
311 |
currentThread.interrupt(); |
35394 | 312 |
try { |
313 |
f.join(); |
|
314 |
shouldThrow(); |
|
315 |
} catch (FJException success) { |
|
316 |
Thread.interrupted(); |
|
317 |
checkCompletedAbnormally(f, success); |
|
318 |
} |
|
319 |
||
320 |
// test quietlyJoin() |
|
321 |
f = new FibAction(8); |
|
322 |
assertSame(f, f.fork()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
323 |
currentThread.interrupt(); |
35394 | 324 |
f.quietlyJoin(); |
325 |
Thread.interrupted(); |
|
326 |
assertEquals(21, f.result); |
|
327 |
checkCompletedNormally(f); |
|
328 |
||
329 |
f = new FibAction(8); |
|
330 |
f.cancel(true); |
|
331 |
assertSame(f, f.fork()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
332 |
currentThread.interrupt(); |
35394 | 333 |
f.quietlyJoin(); |
334 |
Thread.interrupted(); |
|
335 |
checkCancelled(f); |
|
336 |
||
337 |
f = new FibAction(8); |
|
338 |
f.completeExceptionally(new FJException()); |
|
339 |
assertSame(f, f.fork()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
340 |
currentThread.interrupt(); |
35394 | 341 |
f.quietlyJoin(); |
342 |
Thread.interrupted(); |
|
343 |
checkCompletedAbnormally(f, f.getException()); |
|
344 |
}}; |
|
345 |
testInvokeOnPool(mainPool(), a); |
|
346 |
a.reinitialize(); |
|
347 |
testInvokeOnPool(singletonPool(), a); |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
|
352 |
* succeeds in the presence of interrupts |
|
353 |
*/ |
|
354 |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
|
355 |
final SynchronousQueue<FibAction[]> sq = |
|
356 |
new SynchronousQueue<FibAction[]>(); |
|
357 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
358 |
protected void realCompute() throws InterruptedException { |
|
359 |
FibAction[] fibActions = new FibAction[6]; |
|
360 |
for (int i = 0; i < fibActions.length; i++) |
|
361 |
fibActions[i] = new FibAction(8); |
|
362 |
||
363 |
fibActions[1].cancel(false); |
|
364 |
fibActions[2].completeExceptionally(new FJException()); |
|
365 |
fibActions[4].cancel(true); |
|
366 |
fibActions[5].completeExceptionally(new FJException()); |
|
367 |
||
368 |
for (int i = 0; i < fibActions.length; i++) |
|
369 |
fibActions[i].fork(); |
|
370 |
||
371 |
sq.put(fibActions); |
|
372 |
||
373 |
helpQuiesce(); |
|
374 |
}}; |
|
375 |
||
376 |
Runnable r = new CheckedRunnable() { |
|
377 |
public void realRun() throws InterruptedException { |
|
378 |
FibAction[] fibActions = sq.take(); |
|
379 |
FibAction f; |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
380 |
final Thread currentThread = Thread.currentThread(); |
35394 | 381 |
|
382 |
// test join() ------------ |
|
383 |
||
384 |
f = fibActions[0]; |
|
385 |
assertFalse(ForkJoinTask.inForkJoinPool()); |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
386 |
currentThread.interrupt(); |
35394 | 387 |
assertNull(f.join()); |
388 |
assertTrue(Thread.interrupted()); |
|
389 |
assertEquals(21, f.result); |
|
390 |
checkCompletedNormally(f); |
|
391 |
||
392 |
f = fibActions[1]; |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
393 |
currentThread.interrupt(); |
35394 | 394 |
try { |
395 |
f.join(); |
|
396 |
shouldThrow(); |
|
397 |
} catch (CancellationException success) { |
|
398 |
assertTrue(Thread.interrupted()); |
|
399 |
checkCancelled(f); |
|
400 |
} |
|
401 |
||
402 |
f = fibActions[2]; |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
403 |
currentThread.interrupt(); |
35394 | 404 |
try { |
405 |
f.join(); |
|
406 |
shouldThrow(); |
|
407 |
} catch (FJException success) { |
|
408 |
assertTrue(Thread.interrupted()); |
|
409 |
checkCompletedAbnormally(f, success); |
|
410 |
} |
|
411 |
||
412 |
// test quietlyJoin() --------- |
|
413 |
||
414 |
f = fibActions[3]; |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
415 |
currentThread.interrupt(); |
35394 | 416 |
f.quietlyJoin(); |
417 |
assertTrue(Thread.interrupted()); |
|
418 |
assertEquals(21, f.result); |
|
419 |
checkCompletedNormally(f); |
|
420 |
||
421 |
f = fibActions[4]; |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
422 |
currentThread.interrupt(); |
35394 | 423 |
f.quietlyJoin(); |
424 |
assertTrue(Thread.interrupted()); |
|
425 |
checkCancelled(f); |
|
426 |
||
427 |
f = fibActions[5]; |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
35394
diff
changeset
|
428 |
currentThread.interrupt(); |
35394 | 429 |
f.quietlyJoin(); |
430 |
assertTrue(Thread.interrupted()); |
|
431 |
assertTrue(f.getException() instanceof FJException); |
|
432 |
checkCompletedAbnormally(f, f.getException()); |
|
433 |
}}; |
|
434 |
||
435 |
Thread t; |
|
436 |
||
437 |
t = newStartedThread(r); |
|
438 |
testInvokeOnPool(mainPool(), a); |
|
439 |
awaitTermination(t); |
|
440 |
||
441 |
a.reinitialize(); |
|
442 |
t = newStartedThread(r); |
|
443 |
testInvokeOnPool(singletonPool(), a); |
|
444 |
awaitTermination(t); |
|
445 |
} |
|
446 |
||
447 |
/** |
|
448 |
* get of a forked task returns when task completes |
|
449 |
*/ |
|
450 |
public void testForkGet() { |
|
451 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
452 |
protected void realCompute() throws Exception { |
|
453 |
FibAction f = new FibAction(8); |
|
454 |
assertSame(f, f.fork()); |
|
455 |
assertNull(f.get()); |
|
456 |
assertEquals(21, f.result); |
|
457 |
checkCompletedNormally(f); |
|
458 |
}}; |
|
459 |
testInvokeOnPool(mainPool(), a); |
|
460 |
} |
|
461 |
||
462 |
/** |
|
463 |
* timed get of a forked task returns when task completes |
|
464 |
*/ |
|
465 |
public void testForkTimedGet() { |
|
466 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
467 |
protected void realCompute() throws Exception { |
|
468 |
FibAction f = new FibAction(8); |
|
469 |
assertSame(f, f.fork()); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
470 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
35394 | 471 |
assertEquals(21, f.result); |
472 |
checkCompletedNormally(f); |
|
473 |
}}; |
|
474 |
testInvokeOnPool(mainPool(), a); |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* timed get with null time unit throws NPE |
|
479 |
*/ |
|
480 |
public void testForkTimedGetNPE() { |
|
481 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
482 |
protected void realCompute() throws Exception { |
|
483 |
FibAction f = new FibAction(8); |
|
484 |
assertSame(f, f.fork()); |
|
485 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
486 |
f.get(randomTimeout(), null); |
35394 | 487 |
shouldThrow(); |
488 |
} catch (NullPointerException success) {} |
|
489 |
}}; |
|
490 |
testInvokeOnPool(mainPool(), a); |
|
491 |
} |
|
492 |
||
493 |
/** |
|
494 |
* quietlyJoin of a forked task returns when task completes |
|
495 |
*/ |
|
496 |
public void testForkQuietlyJoin() { |
|
497 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
498 |
protected void realCompute() { |
|
499 |
FibAction f = new FibAction(8); |
|
500 |
assertSame(f, f.fork()); |
|
501 |
f.quietlyJoin(); |
|
502 |
assertEquals(21, f.result); |
|
503 |
checkCompletedNormally(f); |
|
504 |
}}; |
|
505 |
testInvokeOnPool(mainPool(), a); |
|
506 |
} |
|
507 |
||
508 |
/** |
|
509 |
* helpQuiesce returns when tasks are complete. |
|
510 |
* getQueuedTaskCount returns 0 when quiescent |
|
511 |
*/ |
|
512 |
public void testForkHelpQuiesce() { |
|
513 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
514 |
protected void realCompute() { |
|
515 |
FibAction f = new FibAction(8); |
|
516 |
assertSame(f, f.fork()); |
|
517 |
helpQuiesce(); |
|
518 |
while (!f.isDone()) // wait out race |
|
519 |
; |
|
520 |
assertEquals(21, f.result); |
|
521 |
assertEquals(0, getQueuedTaskCount()); |
|
522 |
checkCompletedNormally(f); |
|
523 |
}}; |
|
524 |
testInvokeOnPool(mainPool(), a); |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* invoke task throws exception when task completes abnormally |
|
529 |
*/ |
|
530 |
public void testAbnormalInvoke() { |
|
531 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
532 |
protected void realCompute() { |
|
533 |
FailingFibAction f = new FailingFibAction(8); |
|
534 |
try { |
|
535 |
f.invoke(); |
|
536 |
shouldThrow(); |
|
537 |
} catch (FJException success) { |
|
538 |
checkCompletedAbnormally(f, success); |
|
539 |
} |
|
540 |
}}; |
|
541 |
testInvokeOnPool(mainPool(), a); |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* quietlyInvoke task returns when task completes abnormally |
|
546 |
*/ |
|
547 |
public void testAbnormalQuietlyInvoke() { |
|
548 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
549 |
protected void realCompute() { |
|
550 |
FailingFibAction f = new FailingFibAction(8); |
|
551 |
f.quietlyInvoke(); |
|
552 |
assertTrue(f.getException() instanceof FJException); |
|
553 |
checkCompletedAbnormally(f, f.getException()); |
|
554 |
}}; |
|
555 |
testInvokeOnPool(mainPool(), a); |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* join of a forked task throws exception when task completes abnormally |
|
560 |
*/ |
|
561 |
public void testAbnormalForkJoin() { |
|
562 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
563 |
protected void realCompute() { |
|
564 |
FailingFibAction f = new FailingFibAction(8); |
|
565 |
assertSame(f, f.fork()); |
|
566 |
try { |
|
567 |
f.join(); |
|
568 |
shouldThrow(); |
|
569 |
} catch (FJException success) { |
|
570 |
checkCompletedAbnormally(f, success); |
|
571 |
} |
|
572 |
}}; |
|
573 |
testInvokeOnPool(mainPool(), a); |
|
574 |
} |
|
575 |
||
576 |
/** |
|
577 |
* get of a forked task throws exception when task completes abnormally |
|
578 |
*/ |
|
579 |
public void testAbnormalForkGet() { |
|
580 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
581 |
protected void realCompute() throws Exception { |
|
582 |
FailingFibAction f = new FailingFibAction(8); |
|
583 |
assertSame(f, f.fork()); |
|
584 |
try { |
|
585 |
f.get(); |
|
586 |
shouldThrow(); |
|
587 |
} catch (ExecutionException success) { |
|
588 |
Throwable cause = success.getCause(); |
|
589 |
assertTrue(cause instanceof FJException); |
|
590 |
checkCompletedAbnormally(f, cause); |
|
591 |
} |
|
592 |
}}; |
|
593 |
testInvokeOnPool(mainPool(), a); |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* timed get of a forked task throws exception when task completes abnormally |
|
598 |
*/ |
|
599 |
public void testAbnormalForkTimedGet() { |
|
600 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
601 |
protected void realCompute() throws Exception { |
|
602 |
FailingFibAction f = new FailingFibAction(8); |
|
603 |
assertSame(f, f.fork()); |
|
604 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
605 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
35394 | 606 |
shouldThrow(); |
607 |
} catch (ExecutionException success) { |
|
608 |
Throwable cause = success.getCause(); |
|
609 |
assertTrue(cause instanceof FJException); |
|
610 |
checkCompletedAbnormally(f, cause); |
|
611 |
} |
|
612 |
}}; |
|
613 |
testInvokeOnPool(mainPool(), a); |
|
614 |
} |
|
615 |
||
616 |
/** |
|
617 |
* quietlyJoin of a forked task returns when task completes abnormally |
|
618 |
*/ |
|
619 |
public void testAbnormalForkQuietlyJoin() { |
|
620 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
621 |
protected void realCompute() { |
|
622 |
FailingFibAction f = new FailingFibAction(8); |
|
623 |
assertSame(f, f.fork()); |
|
624 |
f.quietlyJoin(); |
|
625 |
assertTrue(f.getException() instanceof FJException); |
|
626 |
checkCompletedAbnormally(f, f.getException()); |
|
627 |
}}; |
|
628 |
testInvokeOnPool(mainPool(), a); |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* invoke task throws exception when task cancelled |
|
633 |
*/ |
|
634 |
public void testCancelledInvoke() { |
|
635 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
636 |
protected void realCompute() { |
|
637 |
FibAction f = new FibAction(8); |
|
638 |
assertTrue(f.cancel(true)); |
|
639 |
try { |
|
640 |
f.invoke(); |
|
641 |
shouldThrow(); |
|
642 |
} catch (CancellationException success) { |
|
643 |
checkCancelled(f); |
|
644 |
} |
|
645 |
}}; |
|
646 |
testInvokeOnPool(mainPool(), a); |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* join of a forked task throws exception when task cancelled |
|
651 |
*/ |
|
652 |
public void testCancelledForkJoin() { |
|
653 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
654 |
protected void realCompute() { |
|
655 |
FibAction f = new FibAction(8); |
|
656 |
assertTrue(f.cancel(true)); |
|
657 |
assertSame(f, f.fork()); |
|
658 |
try { |
|
659 |
f.join(); |
|
660 |
shouldThrow(); |
|
661 |
} catch (CancellationException success) { |
|
662 |
checkCancelled(f); |
|
663 |
} |
|
664 |
}}; |
|
665 |
testInvokeOnPool(mainPool(), a); |
|
666 |
} |
|
667 |
||
668 |
/** |
|
669 |
* get of a forked task throws exception when task cancelled |
|
670 |
*/ |
|
671 |
public void testCancelledForkGet() { |
|
672 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
673 |
protected void realCompute() throws Exception { |
|
674 |
FibAction f = new FibAction(8); |
|
675 |
assertTrue(f.cancel(true)); |
|
676 |
assertSame(f, f.fork()); |
|
677 |
try { |
|
678 |
f.get(); |
|
679 |
shouldThrow(); |
|
680 |
} catch (CancellationException success) { |
|
681 |
checkCancelled(f); |
|
682 |
} |
|
683 |
}}; |
|
684 |
testInvokeOnPool(mainPool(), a); |
|
685 |
} |
|
686 |
||
687 |
/** |
|
688 |
* timed get of a forked task throws exception when task cancelled |
|
689 |
*/ |
|
690 |
public void testCancelledForkTimedGet() { |
|
691 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
692 |
protected void realCompute() throws Exception { |
|
693 |
FibAction f = new FibAction(8); |
|
694 |
assertTrue(f.cancel(true)); |
|
695 |
assertSame(f, f.fork()); |
|
696 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
697 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
35394 | 698 |
shouldThrow(); |
699 |
} catch (CancellationException success) { |
|
700 |
checkCancelled(f); |
|
701 |
} |
|
702 |
}}; |
|
703 |
testInvokeOnPool(mainPool(), a); |
|
704 |
} |
|
705 |
||
706 |
/** |
|
707 |
* quietlyJoin of a forked task returns when task cancelled |
|
708 |
*/ |
|
709 |
public void testCancelledForkQuietlyJoin() { |
|
710 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
711 |
protected void realCompute() { |
|
712 |
FibAction f = new FibAction(8); |
|
713 |
assertTrue(f.cancel(true)); |
|
714 |
assertSame(f, f.fork()); |
|
715 |
f.quietlyJoin(); |
|
716 |
checkCancelled(f); |
|
717 |
}}; |
|
718 |
testInvokeOnPool(mainPool(), a); |
|
719 |
} |
|
720 |
||
721 |
/** |
|
722 |
* getPool of executing task returns its pool |
|
723 |
*/ |
|
724 |
public void testGetPool() { |
|
725 |
final ForkJoinPool mainPool = mainPool(); |
|
726 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
727 |
protected void realCompute() { |
|
728 |
assertSame(mainPool, getPool()); |
|
729 |
}}; |
|
730 |
testInvokeOnPool(mainPool, a); |
|
731 |
} |
|
732 |
||
733 |
/** |
|
734 |
* getPool of non-FJ task returns null |
|
735 |
*/ |
|
736 |
public void testGetPool2() { |
|
737 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
738 |
protected void realCompute() { |
|
739 |
assertNull(getPool()); |
|
740 |
}}; |
|
741 |
assertNull(a.invoke()); |
|
742 |
} |
|
743 |
||
744 |
/** |
|
745 |
* inForkJoinPool of executing task returns true |
|
746 |
*/ |
|
747 |
public void testInForkJoinPool() { |
|
748 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
749 |
protected void realCompute() { |
|
750 |
assertTrue(inForkJoinPool()); |
|
751 |
}}; |
|
752 |
testInvokeOnPool(mainPool(), a); |
|
753 |
} |
|
754 |
||
755 |
/** |
|
756 |
* inForkJoinPool of non-FJ task returns false |
|
757 |
*/ |
|
758 |
public void testInForkJoinPool2() { |
|
759 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
760 |
protected void realCompute() { |
|
761 |
assertFalse(inForkJoinPool()); |
|
762 |
}}; |
|
763 |
assertNull(a.invoke()); |
|
764 |
} |
|
765 |
||
766 |
/** |
|
767 |
* getPool of current thread in pool returns its pool |
|
768 |
*/ |
|
769 |
public void testWorkerGetPool() { |
|
770 |
final ForkJoinPool mainPool = mainPool(); |
|
771 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
772 |
protected void realCompute() { |
|
773 |
ForkJoinWorkerThread w = |
|
774 |
(ForkJoinWorkerThread) Thread.currentThread(); |
|
775 |
assertSame(mainPool, w.getPool()); |
|
776 |
}}; |
|
777 |
testInvokeOnPool(mainPool, a); |
|
778 |
} |
|
779 |
||
780 |
/** |
|
781 |
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
|
782 |
*/ |
|
783 |
public void testWorkerGetPoolIndex() { |
|
784 |
final ForkJoinPool mainPool = mainPool(); |
|
785 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
786 |
protected void realCompute() { |
|
787 |
ForkJoinWorkerThread w = |
|
788 |
(ForkJoinWorkerThread) Thread.currentThread(); |
|
789 |
assertTrue(w.getPoolIndex() >= 0); |
|
790 |
// pool size can shrink after assigning index, so cannot check |
|
791 |
// assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
|
792 |
}}; |
|
793 |
testInvokeOnPool(mainPool, a); |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* setRawResult(null) succeeds |
|
798 |
*/ |
|
799 |
public void testSetRawResult() { |
|
800 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
801 |
protected void realCompute() { |
|
802 |
setRawResult(null); |
|
803 |
assertNull(getRawResult()); |
|
804 |
}}; |
|
805 |
assertNull(a.invoke()); |
|
806 |
} |
|
807 |
||
808 |
/** |
|
809 |
* A reinitialized normally completed task may be re-invoked |
|
810 |
*/ |
|
811 |
public void testReinitialize() { |
|
812 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
813 |
protected void realCompute() { |
|
814 |
FibAction f = new FibAction(8); |
|
815 |
checkNotDone(f); |
|
816 |
||
817 |
for (int i = 0; i < 3; i++) { |
|
818 |
assertNull(f.invoke()); |
|
819 |
assertEquals(21, f.result); |
|
820 |
checkCompletedNormally(f); |
|
821 |
f.reinitialize(); |
|
822 |
checkNotDone(f); |
|
823 |
} |
|
824 |
}}; |
|
825 |
testInvokeOnPool(mainPool(), a); |
|
826 |
} |
|
827 |
||
828 |
/** |
|
829 |
* A reinitialized abnormally completed task may be re-invoked |
|
830 |
*/ |
|
831 |
public void testReinitializeAbnormal() { |
|
832 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
833 |
protected void realCompute() { |
|
834 |
FailingFibAction f = new FailingFibAction(8); |
|
835 |
checkNotDone(f); |
|
836 |
||
837 |
for (int i = 0; i < 3; i++) { |
|
838 |
try { |
|
839 |
f.invoke(); |
|
840 |
shouldThrow(); |
|
841 |
} catch (FJException success) { |
|
842 |
checkCompletedAbnormally(f, success); |
|
843 |
} |
|
844 |
f.reinitialize(); |
|
845 |
checkNotDone(f); |
|
846 |
} |
|
847 |
}}; |
|
848 |
testInvokeOnPool(mainPool(), a); |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* invoke task throws exception after invoking completeExceptionally |
|
853 |
*/ |
|
854 |
public void testCompleteExceptionally() { |
|
855 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
856 |
protected void realCompute() { |
|
857 |
FibAction f = new FibAction(8); |
|
858 |
f.completeExceptionally(new FJException()); |
|
859 |
try { |
|
860 |
f.invoke(); |
|
861 |
shouldThrow(); |
|
862 |
} catch (FJException success) { |
|
863 |
checkCompletedAbnormally(f, success); |
|
864 |
} |
|
865 |
}}; |
|
866 |
testInvokeOnPool(mainPool(), a); |
|
867 |
} |
|
868 |
||
869 |
/** |
|
870 |
* invoke task suppresses execution invoking complete |
|
871 |
*/ |
|
872 |
public void testComplete() { |
|
873 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
874 |
protected void realCompute() { |
|
875 |
FibAction f = new FibAction(8); |
|
876 |
f.complete(null); |
|
877 |
assertNull(f.invoke()); |
|
878 |
assertEquals(0, f.result); |
|
879 |
checkCompletedNormally(f); |
|
880 |
}}; |
|
881 |
testInvokeOnPool(mainPool(), a); |
|
882 |
} |
|
883 |
||
884 |
/** |
|
885 |
* invokeAll(t1, t2) invokes all task arguments |
|
886 |
*/ |
|
887 |
public void testInvokeAll2() { |
|
888 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
889 |
protected void realCompute() { |
|
890 |
FibAction f = new FibAction(8); |
|
891 |
FibAction g = new FibAction(9); |
|
892 |
invokeAll(f, g); |
|
893 |
checkCompletedNormally(f); |
|
894 |
assertEquals(21, f.result); |
|
895 |
checkCompletedNormally(g); |
|
896 |
assertEquals(34, g.result); |
|
897 |
}}; |
|
898 |
testInvokeOnPool(mainPool(), a); |
|
899 |
} |
|
900 |
||
901 |
/** |
|
902 |
* invokeAll(tasks) with 1 argument invokes task |
|
903 |
*/ |
|
904 |
public void testInvokeAll1() { |
|
905 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
906 |
protected void realCompute() { |
|
907 |
FibAction f = new FibAction(8); |
|
908 |
invokeAll(f); |
|
909 |
checkCompletedNormally(f); |
|
910 |
assertEquals(21, f.result); |
|
911 |
}}; |
|
912 |
testInvokeOnPool(mainPool(), a); |
|
913 |
} |
|
914 |
||
915 |
/** |
|
916 |
* invokeAll(tasks) with > 2 argument invokes tasks |
|
917 |
*/ |
|
918 |
public void testInvokeAll3() { |
|
919 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
920 |
protected void realCompute() { |
|
921 |
FibAction f = new FibAction(8); |
|
922 |
FibAction g = new FibAction(9); |
|
923 |
FibAction h = new FibAction(7); |
|
924 |
invokeAll(f, g, h); |
|
925 |
assertTrue(f.isDone()); |
|
926 |
assertTrue(g.isDone()); |
|
927 |
assertTrue(h.isDone()); |
|
928 |
checkCompletedNormally(f); |
|
929 |
assertEquals(21, f.result); |
|
930 |
checkCompletedNormally(g); |
|
931 |
assertEquals(34, g.result); |
|
932 |
checkCompletedNormally(g); |
|
933 |
assertEquals(13, h.result); |
|
934 |
}}; |
|
935 |
testInvokeOnPool(mainPool(), a); |
|
936 |
} |
|
937 |
||
938 |
/** |
|
939 |
* invokeAll(collection) invokes all tasks in the collection |
|
940 |
*/ |
|
941 |
public void testInvokeAllCollection() { |
|
942 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
943 |
protected void realCompute() { |
|
944 |
FibAction f = new FibAction(8); |
|
945 |
FibAction g = new FibAction(9); |
|
946 |
FibAction h = new FibAction(7); |
|
947 |
HashSet set = new HashSet(); |
|
948 |
set.add(f); |
|
949 |
set.add(g); |
|
950 |
set.add(h); |
|
951 |
invokeAll(set); |
|
952 |
assertTrue(f.isDone()); |
|
953 |
assertTrue(g.isDone()); |
|
954 |
assertTrue(h.isDone()); |
|
955 |
checkCompletedNormally(f); |
|
956 |
assertEquals(21, f.result); |
|
957 |
checkCompletedNormally(g); |
|
958 |
assertEquals(34, g.result); |
|
959 |
checkCompletedNormally(g); |
|
960 |
assertEquals(13, h.result); |
|
961 |
}}; |
|
962 |
testInvokeOnPool(mainPool(), a); |
|
963 |
} |
|
964 |
||
965 |
/** |
|
966 |
* invokeAll(tasks) with any null task throws NPE |
|
967 |
*/ |
|
968 |
public void testInvokeAllNPE() { |
|
969 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
970 |
protected void realCompute() { |
|
971 |
FibAction f = new FibAction(8); |
|
972 |
FibAction g = new FibAction(9); |
|
973 |
FibAction h = null; |
|
974 |
try { |
|
975 |
invokeAll(f, g, h); |
|
976 |
shouldThrow(); |
|
977 |
} catch (NullPointerException success) {} |
|
978 |
}}; |
|
979 |
testInvokeOnPool(mainPool(), a); |
|
980 |
} |
|
981 |
||
982 |
/** |
|
983 |
* invokeAll(t1, t2) throw exception if any task does |
|
984 |
*/ |
|
985 |
public void testAbnormalInvokeAll2() { |
|
986 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
987 |
protected void realCompute() { |
|
988 |
FibAction f = new FibAction(8); |
|
989 |
FailingFibAction g = new FailingFibAction(9); |
|
990 |
try { |
|
991 |
invokeAll(f, g); |
|
992 |
shouldThrow(); |
|
993 |
} catch (FJException success) { |
|
994 |
checkCompletedAbnormally(g, success); |
|
995 |
} |
|
996 |
}}; |
|
997 |
testInvokeOnPool(mainPool(), a); |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* invokeAll(tasks) with 1 argument throws exception if task does |
|
1002 |
*/ |
|
1003 |
public void testAbnormalInvokeAll1() { |
|
1004 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1005 |
protected void realCompute() { |
|
1006 |
FailingFibAction g = new FailingFibAction(9); |
|
1007 |
try { |
|
1008 |
invokeAll(g); |
|
1009 |
shouldThrow(); |
|
1010 |
} catch (FJException success) { |
|
1011 |
checkCompletedAbnormally(g, success); |
|
1012 |
} |
|
1013 |
}}; |
|
1014 |
testInvokeOnPool(mainPool(), a); |
|
1015 |
} |
|
1016 |
||
1017 |
/** |
|
1018 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
|
1019 |
*/ |
|
1020 |
public void testAbnormalInvokeAll3() { |
|
1021 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1022 |
protected void realCompute() { |
|
1023 |
FibAction f = new FibAction(8); |
|
1024 |
FailingFibAction g = new FailingFibAction(9); |
|
1025 |
FibAction h = new FibAction(7); |
|
1026 |
try { |
|
1027 |
invokeAll(f, g, h); |
|
1028 |
shouldThrow(); |
|
1029 |
} catch (FJException success) { |
|
1030 |
checkCompletedAbnormally(g, success); |
|
1031 |
} |
|
1032 |
}}; |
|
1033 |
testInvokeOnPool(mainPool(), a); |
|
1034 |
} |
|
1035 |
||
1036 |
/** |
|
1037 |
* invokeAll(collection) throws exception if any task does |
|
1038 |
*/ |
|
1039 |
public void testAbnormalInvokeAllCollection() { |
|
1040 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1041 |
protected void realCompute() { |
|
1042 |
FailingFibAction f = new FailingFibAction(8); |
|
1043 |
FibAction g = new FibAction(9); |
|
1044 |
FibAction h = new FibAction(7); |
|
1045 |
HashSet set = new HashSet(); |
|
1046 |
set.add(f); |
|
1047 |
set.add(g); |
|
1048 |
set.add(h); |
|
1049 |
try { |
|
1050 |
invokeAll(set); |
|
1051 |
shouldThrow(); |
|
1052 |
} catch (FJException success) { |
|
1053 |
checkCompletedAbnormally(f, success); |
|
1054 |
} |
|
1055 |
}}; |
|
1056 |
testInvokeOnPool(mainPool(), a); |
|
1057 |
} |
|
1058 |
||
1059 |
/** |
|
1060 |
* tryUnfork returns true for most recent unexecuted task, |
|
1061 |
* and suppresses execution |
|
1062 |
*/ |
|
1063 |
public void testTryUnfork() { |
|
1064 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1065 |
protected void realCompute() { |
|
1066 |
FibAction g = new FibAction(9); |
|
1067 |
assertSame(g, g.fork()); |
|
1068 |
FibAction f = new FibAction(8); |
|
1069 |
assertSame(f, f.fork()); |
|
1070 |
assertTrue(f.tryUnfork()); |
|
1071 |
helpQuiesce(); |
|
1072 |
checkNotDone(f); |
|
1073 |
checkCompletedNormally(g); |
|
1074 |
}}; |
|
1075 |
testInvokeOnPool(singletonPool(), a); |
|
1076 |
} |
|
1077 |
||
1078 |
/** |
|
1079 |
* getSurplusQueuedTaskCount returns > 0 when |
|
1080 |
* there are more tasks than threads |
|
1081 |
*/ |
|
1082 |
public void testGetSurplusQueuedTaskCount() { |
|
1083 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1084 |
protected void realCompute() { |
|
1085 |
FibAction h = new FibAction(7); |
|
1086 |
assertSame(h, h.fork()); |
|
1087 |
FibAction g = new FibAction(9); |
|
1088 |
assertSame(g, g.fork()); |
|
1089 |
FibAction f = new FibAction(8); |
|
1090 |
assertSame(f, f.fork()); |
|
1091 |
assertTrue(getSurplusQueuedTaskCount() > 0); |
|
1092 |
helpQuiesce(); |
|
1093 |
assertEquals(0, getSurplusQueuedTaskCount()); |
|
1094 |
checkCompletedNormally(f); |
|
1095 |
checkCompletedNormally(g); |
|
1096 |
checkCompletedNormally(h); |
|
1097 |
}}; |
|
1098 |
testInvokeOnPool(singletonPool(), a); |
|
1099 |
} |
|
1100 |
||
1101 |
/** |
|
1102 |
* peekNextLocalTask returns most recent unexecuted task. |
|
1103 |
*/ |
|
1104 |
public void testPeekNextLocalTask() { |
|
1105 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1106 |
protected void realCompute() { |
|
1107 |
FibAction g = new FibAction(9); |
|
1108 |
assertSame(g, g.fork()); |
|
1109 |
FibAction f = new FibAction(8); |
|
1110 |
assertSame(f, f.fork()); |
|
1111 |
assertSame(f, peekNextLocalTask()); |
|
1112 |
assertNull(f.join()); |
|
1113 |
checkCompletedNormally(f); |
|
1114 |
helpQuiesce(); |
|
1115 |
checkCompletedNormally(f); |
|
1116 |
checkCompletedNormally(g); |
|
1117 |
}}; |
|
1118 |
testInvokeOnPool(singletonPool(), a); |
|
1119 |
} |
|
1120 |
||
1121 |
/** |
|
1122 |
* pollNextLocalTask returns most recent unexecuted task |
|
1123 |
* without executing it |
|
1124 |
*/ |
|
1125 |
public void testPollNextLocalTask() { |
|
1126 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1127 |
protected void realCompute() { |
|
1128 |
FibAction g = new FibAction(9); |
|
1129 |
assertSame(g, g.fork()); |
|
1130 |
FibAction f = new FibAction(8); |
|
1131 |
assertSame(f, f.fork()); |
|
1132 |
assertSame(f, pollNextLocalTask()); |
|
1133 |
helpQuiesce(); |
|
1134 |
checkNotDone(f); |
|
1135 |
checkCompletedNormally(g); |
|
1136 |
}}; |
|
1137 |
testInvokeOnPool(singletonPool(), a); |
|
1138 |
} |
|
1139 |
||
1140 |
/** |
|
1141 |
* pollTask returns an unexecuted task without executing it |
|
1142 |
*/ |
|
1143 |
public void testPollTask() { |
|
1144 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1145 |
protected void realCompute() { |
|
1146 |
FibAction g = new FibAction(9); |
|
1147 |
assertSame(g, g.fork()); |
|
1148 |
FibAction f = new FibAction(8); |
|
1149 |
assertSame(f, f.fork()); |
|
1150 |
assertSame(f, pollTask()); |
|
1151 |
helpQuiesce(); |
|
1152 |
checkNotDone(f); |
|
1153 |
checkCompletedNormally(g); |
|
1154 |
}}; |
|
1155 |
testInvokeOnPool(singletonPool(), a); |
|
1156 |
} |
|
1157 |
||
1158 |
/** |
|
1159 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
|
1160 |
*/ |
|
1161 |
public void testPeekNextLocalTaskAsync() { |
|
1162 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1163 |
protected void realCompute() { |
|
1164 |
FibAction g = new FibAction(9); |
|
1165 |
assertSame(g, g.fork()); |
|
1166 |
FibAction f = new FibAction(8); |
|
1167 |
assertSame(f, f.fork()); |
|
1168 |
assertSame(g, peekNextLocalTask()); |
|
1169 |
assertNull(f.join()); |
|
1170 |
helpQuiesce(); |
|
1171 |
checkCompletedNormally(f); |
|
1172 |
checkCompletedNormally(g); |
|
1173 |
}}; |
|
1174 |
testInvokeOnPool(asyncSingletonPool(), a); |
|
1175 |
} |
|
1176 |
||
1177 |
/** |
|
1178 |
* pollNextLocalTask returns least recent unexecuted task without |
|
1179 |
* executing it, in async mode |
|
1180 |
*/ |
|
1181 |
public void testPollNextLocalTaskAsync() { |
|
1182 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1183 |
protected void realCompute() { |
|
1184 |
FibAction g = new FibAction(9); |
|
1185 |
assertSame(g, g.fork()); |
|
1186 |
FibAction f = new FibAction(8); |
|
1187 |
assertSame(f, f.fork()); |
|
1188 |
assertSame(g, pollNextLocalTask()); |
|
1189 |
helpQuiesce(); |
|
1190 |
checkCompletedNormally(f); |
|
1191 |
checkNotDone(g); |
|
1192 |
}}; |
|
1193 |
testInvokeOnPool(asyncSingletonPool(), a); |
|
1194 |
} |
|
1195 |
||
1196 |
/** |
|
1197 |
* pollTask returns an unexecuted task without executing it, in |
|
1198 |
* async mode |
|
1199 |
*/ |
|
1200 |
public void testPollTaskAsync() { |
|
1201 |
RecursiveAction a = new CheckedRecursiveAction() { |
|
1202 |
protected void realCompute() { |
|
1203 |
FibAction g = new FibAction(9); |
|
1204 |
assertSame(g, g.fork()); |
|
1205 |
FibAction f = new FibAction(8); |
|
1206 |
assertSame(f, f.fork()); |
|
1207 |
assertSame(g, pollTask()); |
|
1208 |
helpQuiesce(); |
|
1209 |
checkCompletedNormally(f); |
|
1210 |
checkNotDone(g); |
|
1211 |
}}; |
|
1212 |
testInvokeOnPool(asyncSingletonPool(), a); |
|
1213 |
} |
|
1214 |
||
1215 |
/** Demo from RecursiveAction javadoc */ |
|
1216 |
static class SortTask extends RecursiveAction { |
|
1217 |
final long[] array; final int lo, hi; |
|
1218 |
SortTask(long[] array, int lo, int hi) { |
|
1219 |
this.array = array; this.lo = lo; this.hi = hi; |
|
1220 |
} |
|
1221 |
SortTask(long[] array) { this(array, 0, array.length); } |
|
1222 |
protected void compute() { |
|
1223 |
if (hi - lo < THRESHOLD) |
|
1224 |
sortSequentially(lo, hi); |
|
1225 |
else { |
|
1226 |
int mid = (lo + hi) >>> 1; |
|
1227 |
invokeAll(new SortTask(array, lo, mid), |
|
1228 |
new SortTask(array, mid, hi)); |
|
1229 |
merge(lo, mid, hi); |
|
1230 |
} |
|
1231 |
} |
|
1232 |
// implementation details follow: |
|
1233 |
static final int THRESHOLD = 100; |
|
1234 |
void sortSequentially(int lo, int hi) { |
|
1235 |
Arrays.sort(array, lo, hi); |
|
1236 |
} |
|
1237 |
void merge(int lo, int mid, int hi) { |
|
1238 |
long[] buf = Arrays.copyOfRange(array, lo, mid); |
|
1239 |
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
|
1240 |
array[j] = (k == hi || buf[i] < array[k]) ? |
|
1241 |
buf[i++] : array[k++]; |
|
1242 |
} |
|
1243 |
} |
|
1244 |
||
1245 |
/** |
|
1246 |
* SortTask demo works as advertised |
|
1247 |
*/ |
|
1248 |
public void testSortTaskDemo() { |
|
1249 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
|
1250 |
long[] array = new long[1007]; |
|
1251 |
for (int i = 0; i < array.length; i++) |
|
1252 |
array[i] = rnd.nextLong(); |
|
1253 |
long[] arrayClone = array.clone(); |
|
1254 |
testInvokeOnPool(mainPool(), new SortTask(array)); |
|
1255 |
Arrays.sort(arrayClone); |
|
1256 |
assertTrue(Arrays.equals(array, arrayClone)); |
|
1257 |
} |
|
1258 |
} |