1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
6 |
jsr166 |
1.13 |
|
7 |
dl |
1.1 |
import junit.framework.*; |
8 |
jsr166 |
1.15 |
import java.util.concurrent.CancellationException; |
9 |
|
|
import java.util.concurrent.ExecutionException; |
10 |
|
|
import java.util.concurrent.ForkJoinPool; |
11 |
jsr166 |
1.20 |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
jsr166 |
1.15 |
import java.util.concurrent.RecursiveTask; |
13 |
|
|
import java.util.concurrent.TimeUnit; |
14 |
jsr166 |
1.20 |
import java.util.concurrent.TimeoutException; |
15 |
|
|
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
jsr166 |
1.15 |
import java.util.HashSet; |
17 |
dl |
1.1 |
|
18 |
|
|
public class RecursiveTaskTest extends JSR166TestCase { |
19 |
|
|
|
20 |
|
|
public static void main(String[] args) { |
21 |
jsr166 |
1.10 |
junit.textui.TestRunner.run(suite()); |
22 |
dl |
1.1 |
} |
23 |
|
|
public static Test suite() { |
24 |
jsr166 |
1.8 |
return new TestSuite(RecursiveTaskTest.class); |
25 |
dl |
1.1 |
} |
26 |
|
|
|
27 |
jsr166 |
1.13 |
private static ForkJoinPool mainPool() { |
28 |
|
|
return new ForkJoinPool(); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
private static ForkJoinPool singletonPool() { |
32 |
|
|
return new ForkJoinPool(1); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
private static ForkJoinPool asyncSingletonPool() { |
36 |
|
|
return new ForkJoinPool(1, |
37 |
|
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
38 |
|
|
null, true); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
42 |
|
|
try { |
43 |
jsr166 |
1.20 |
checkNotDone(a); |
44 |
jsr166 |
1.19 |
|
45 |
|
|
T result = pool.invoke(a); |
46 |
|
|
|
47 |
jsr166 |
1.20 |
checkCompletedNormally(a, result); |
48 |
jsr166 |
1.19 |
return result; |
49 |
jsr166 |
1.13 |
} finally { |
50 |
|
|
joinPool(pool); |
51 |
|
|
} |
52 |
|
|
} |
53 |
dl |
1.1 |
|
54 |
jsr166 |
1.20 |
void checkNotDone(RecursiveTask a) { |
55 |
|
|
assertFalse(a.isDone()); |
56 |
|
|
assertFalse(a.isCompletedNormally()); |
57 |
|
|
assertFalse(a.isCompletedAbnormally()); |
58 |
|
|
assertFalse(a.isCancelled()); |
59 |
|
|
assertNull(a.getException()); |
60 |
|
|
assertNull(a.getRawResult()); |
61 |
|
|
|
62 |
|
|
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
63 |
|
|
Thread.currentThread().interrupt(); |
64 |
|
|
try { |
65 |
|
|
a.get(); |
66 |
|
|
shouldThrow(); |
67 |
|
|
} catch (InterruptedException success) { |
68 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
|
|
70 |
|
|
Thread.currentThread().interrupt(); |
71 |
|
|
try { |
72 |
|
|
a.get(5L, SECONDS); |
73 |
|
|
shouldThrow(); |
74 |
|
|
} catch (InterruptedException success) { |
75 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
try { |
79 |
|
|
a.get(0L, SECONDS); |
80 |
|
|
shouldThrow(); |
81 |
|
|
} catch (TimeoutException success) { |
82 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
<T> void checkCompletedNormally(RecursiveTask<T> a, T expected) { |
86 |
|
|
assertTrue(a.isDone()); |
87 |
|
|
assertFalse(a.isCancelled()); |
88 |
|
|
assertTrue(a.isCompletedNormally()); |
89 |
|
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
|
assertNull(a.getException()); |
91 |
|
|
assertSame(expected, a.getRawResult()); |
92 |
|
|
assertSame(expected, a.join()); |
93 |
|
|
try { |
94 |
|
|
assertSame(expected, a.get()); |
95 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
96 |
|
|
try { |
97 |
|
|
assertSame(expected, a.get(5L, SECONDS)); |
98 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/** |
102 |
|
|
* Waits for the task to complete, and checks that when it does, |
103 |
|
|
* it will have an Integer result equals to the given int. |
104 |
|
|
*/ |
105 |
|
|
void checkCompletesNormally(RecursiveTask<Integer> a, int expected) { |
106 |
|
|
Integer r = a.join(); |
107 |
|
|
assertEquals(expected, (int) r); |
108 |
|
|
checkCompletedNormally(a, r); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
|
|
* Like checkCompletesNormally, but verifies that the task has |
113 |
|
|
* already completed. |
114 |
|
|
*/ |
115 |
|
|
void checkCompletedNormally(RecursiveTask<Integer> a, int expected) { |
116 |
|
|
Integer r = a.getRawResult(); |
117 |
|
|
assertEquals(expected, (int) r); |
118 |
|
|
checkCompletedNormally(a, r); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
void checkCancelled(RecursiveTask a) { |
122 |
|
|
assertTrue(a.isDone()); |
123 |
|
|
assertTrue(a.isCancelled()); |
124 |
|
|
assertFalse(a.isCompletedNormally()); |
125 |
|
|
assertTrue(a.isCompletedAbnormally()); |
126 |
|
|
assertTrue(a.getException() instanceof CancellationException); |
127 |
|
|
assertNull(a.getRawResult()); |
128 |
|
|
|
129 |
|
|
try { |
130 |
|
|
a.join(); |
131 |
|
|
shouldThrow(); |
132 |
|
|
} catch (CancellationException success) { |
133 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
|
|
|
135 |
|
|
try { |
136 |
|
|
a.get(); |
137 |
|
|
shouldThrow(); |
138 |
|
|
} catch (CancellationException success) { |
139 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
140 |
|
|
|
141 |
|
|
try { |
142 |
|
|
a.get(5L, SECONDS); |
143 |
|
|
shouldThrow(); |
144 |
|
|
} catch (CancellationException success) { |
145 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
void checkTaskThrew(RecursiveTask a, Throwable t) { |
149 |
|
|
assertTrue(a.isDone()); |
150 |
|
|
assertFalse(a.isCancelled()); |
151 |
|
|
assertFalse(a.isCompletedNormally()); |
152 |
|
|
assertTrue(a.isCompletedAbnormally()); |
153 |
|
|
assertSame(t, a.getException()); |
154 |
|
|
assertNull(a.getRawResult()); |
155 |
|
|
|
156 |
|
|
try { |
157 |
|
|
a.join(); |
158 |
|
|
shouldThrow(); |
159 |
|
|
} catch (Throwable expected) { |
160 |
|
|
assertSame(t, expected); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
try { |
164 |
|
|
a.get(); |
165 |
|
|
shouldThrow(); |
166 |
|
|
} catch (ExecutionException success) { |
167 |
|
|
assertSame(t, success.getCause()); |
168 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
169 |
|
|
|
170 |
|
|
try { |
171 |
|
|
a.get(5L, SECONDS); |
172 |
|
|
shouldThrow(); |
173 |
|
|
} catch (ExecutionException success) { |
174 |
|
|
assertSame(t, success.getCause()); |
175 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
|
|
} |
177 |
|
|
|
178 |
dl |
1.1 |
static final class FJException extends RuntimeException { |
179 |
|
|
FJException() { super(); } |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
// An invalid return value for Fib |
183 |
|
|
static final Integer NoResult = Integer.valueOf(-17); |
184 |
|
|
|
185 |
|
|
// A simple recursive task for testing |
186 |
jsr166 |
1.19 |
final class FibTask extends CheckedRecursiveTask<Integer> { |
187 |
dl |
1.1 |
final int number; |
188 |
|
|
FibTask(int n) { number = n; } |
189 |
jsr166 |
1.19 |
public Integer realCompute() { |
190 |
dl |
1.1 |
int n = number; |
191 |
|
|
if (n <= 1) |
192 |
|
|
return n; |
193 |
|
|
FibTask f1 = new FibTask(n - 1); |
194 |
|
|
f1.fork(); |
195 |
|
|
return (new FibTask(n - 2)).compute() + f1.join(); |
196 |
|
|
} |
197 |
jsr166 |
1.20 |
|
198 |
|
|
public void publicSetRawResult(Integer result) { |
199 |
|
|
setRawResult(result); |
200 |
|
|
} |
201 |
dl |
1.1 |
} |
202 |
|
|
|
203 |
|
|
// A recursive action failing in base case |
204 |
jsr166 |
1.19 |
final class FailingFibTask extends RecursiveTask<Integer> { |
205 |
dl |
1.1 |
final int number; |
206 |
|
|
int result; |
207 |
|
|
FailingFibTask(int n) { number = n; } |
208 |
|
|
public Integer compute() { |
209 |
|
|
int n = number; |
210 |
|
|
if (n <= 1) |
211 |
|
|
throw new FJException(); |
212 |
|
|
FailingFibTask f1 = new FailingFibTask(n - 1); |
213 |
|
|
f1.fork(); |
214 |
|
|
return (new FibTask(n - 2)).compute() + f1.join(); |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
jsr166 |
1.2 |
/** |
219 |
dl |
1.1 |
* invoke returns value when task completes normally. |
220 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
221 |
|
|
* completed tasks. getRawResult of a completed non-null task |
222 |
|
|
* returns value; |
223 |
|
|
*/ |
224 |
|
|
public void testInvoke() { |
225 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
226 |
|
|
public Integer realCompute() { |
227 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
228 |
|
|
Integer r = f.invoke(); |
229 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
230 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
231 |
jsr166 |
1.7 |
return r; |
232 |
jsr166 |
1.19 |
}}; |
233 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
234 |
dl |
1.1 |
} |
235 |
|
|
|
236 |
jsr166 |
1.2 |
/** |
237 |
dl |
1.1 |
* quietlyInvoke task returns when task completes normally. |
238 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
239 |
|
|
* completed tasks |
240 |
|
|
*/ |
241 |
|
|
public void testQuietlyInvoke() { |
242 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
243 |
|
|
public Integer realCompute() { |
244 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
245 |
|
|
f.quietlyInvoke(); |
246 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
247 |
|
|
return NoResult; |
248 |
jsr166 |
1.19 |
}}; |
249 |
jsr166 |
1.20 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
250 |
dl |
1.1 |
} |
251 |
|
|
|
252 |
jsr166 |
1.2 |
/** |
253 |
dl |
1.1 |
* join of a forked task returns when task completes |
254 |
|
|
*/ |
255 |
|
|
public void testForkJoin() { |
256 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
257 |
|
|
public Integer realCompute() { |
258 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
259 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
260 |
jsr166 |
1.7 |
Integer r = f.join(); |
261 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
262 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
263 |
jsr166 |
1.7 |
return r; |
264 |
jsr166 |
1.19 |
}}; |
265 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
266 |
dl |
1.1 |
} |
267 |
|
|
|
268 |
jsr166 |
1.2 |
/** |
269 |
dl |
1.1 |
* get of a forked task returns when task completes |
270 |
|
|
*/ |
271 |
|
|
public void testForkGet() { |
272 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
273 |
|
|
public Integer realCompute() throws Exception { |
274 |
|
|
FibTask f = new FibTask(8); |
275 |
|
|
assertSame(f, f.fork()); |
276 |
|
|
Integer r = f.get(); |
277 |
|
|
assertEquals(21, (int) r); |
278 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
279 |
jsr166 |
1.19 |
return r; |
280 |
|
|
}}; |
281 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
282 |
dl |
1.1 |
} |
283 |
|
|
|
284 |
jsr166 |
1.2 |
/** |
285 |
dl |
1.1 |
* timed get of a forked task returns when task completes |
286 |
|
|
*/ |
287 |
|
|
public void testForkTimedGet() { |
288 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
289 |
|
|
public Integer realCompute() throws Exception { |
290 |
|
|
FibTask f = new FibTask(8); |
291 |
|
|
assertSame(f, f.fork()); |
292 |
jsr166 |
1.20 |
Integer r = f.get(5L, SECONDS); |
293 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
294 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
295 |
jsr166 |
1.19 |
return r; |
296 |
|
|
}}; |
297 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
298 |
dl |
1.1 |
} |
299 |
|
|
|
300 |
jsr166 |
1.2 |
/** |
301 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes |
302 |
|
|
*/ |
303 |
|
|
public void testForkQuietlyJoin() { |
304 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
305 |
|
|
public Integer realCompute() { |
306 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
307 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
308 |
jsr166 |
1.7 |
f.quietlyJoin(); |
309 |
|
|
Integer r = f.getRawResult(); |
310 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
311 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
312 |
jsr166 |
1.7 |
return r; |
313 |
jsr166 |
1.19 |
}}; |
314 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
315 |
dl |
1.1 |
} |
316 |
|
|
|
317 |
|
|
|
318 |
jsr166 |
1.2 |
/** |
319 |
dl |
1.1 |
* helpQuiesce returns when tasks are complete. |
320 |
|
|
* getQueuedTaskCount returns 0 when quiescent |
321 |
|
|
*/ |
322 |
|
|
public void testForkHelpQuiesce() { |
323 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
324 |
|
|
public Integer realCompute() { |
325 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
326 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
327 |
jsr166 |
1.7 |
f.helpQuiesce(); |
328 |
jsr166 |
1.19 |
assertEquals(0, getQueuedTaskCount()); |
329 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
330 |
|
|
return NoResult; |
331 |
jsr166 |
1.19 |
}}; |
332 |
jsr166 |
1.20 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
333 |
dl |
1.1 |
} |
334 |
|
|
|
335 |
|
|
|
336 |
jsr166 |
1.2 |
/** |
337 |
dl |
1.1 |
* invoke task throws exception when task completes abnormally |
338 |
|
|
*/ |
339 |
|
|
public void testAbnormalInvoke() { |
340 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
341 |
|
|
public Integer realCompute() { |
342 |
|
|
FailingFibTask f = new FailingFibTask(8); |
343 |
jsr166 |
1.7 |
try { |
344 |
|
|
f.invoke(); |
345 |
|
|
shouldThrow(); |
346 |
jsr166 |
1.20 |
} catch (FJException success) { |
347 |
|
|
checkTaskThrew(f, success); |
348 |
|
|
} |
349 |
jsr166 |
1.7 |
return NoResult; |
350 |
jsr166 |
1.19 |
}}; |
351 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
352 |
dl |
1.1 |
} |
353 |
|
|
|
354 |
jsr166 |
1.2 |
/** |
355 |
jsr166 |
1.4 |
* quietlyInvoke task returns when task completes abnormally |
356 |
dl |
1.1 |
*/ |
357 |
|
|
public void testAbnormalQuietlyInvoke() { |
358 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
359 |
|
|
public Integer realCompute() { |
360 |
jsr166 |
1.7 |
FailingFibTask f = new FailingFibTask(8); |
361 |
|
|
f.quietlyInvoke(); |
362 |
jsr166 |
1.20 |
assertTrue(f.getException() instanceof FJException); |
363 |
|
|
checkTaskThrew(f, f.getException()); |
364 |
jsr166 |
1.7 |
return NoResult; |
365 |
jsr166 |
1.19 |
}}; |
366 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
367 |
dl |
1.1 |
} |
368 |
|
|
|
369 |
jsr166 |
1.2 |
/** |
370 |
dl |
1.1 |
* join of a forked task throws exception when task completes abnormally |
371 |
|
|
*/ |
372 |
|
|
public void testAbnormalForkJoin() { |
373 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
374 |
|
|
public Integer realCompute() { |
375 |
|
|
FailingFibTask f = new FailingFibTask(8); |
376 |
|
|
assertSame(f, f.fork()); |
377 |
jsr166 |
1.7 |
try { |
378 |
|
|
Integer r = f.join(); |
379 |
|
|
shouldThrow(); |
380 |
jsr166 |
1.20 |
} catch (FJException success) { |
381 |
|
|
checkTaskThrew(f, success); |
382 |
|
|
} |
383 |
jsr166 |
1.7 |
return NoResult; |
384 |
jsr166 |
1.19 |
}}; |
385 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
386 |
dl |
1.1 |
} |
387 |
|
|
|
388 |
jsr166 |
1.2 |
/** |
389 |
dl |
1.1 |
* get of a forked task throws exception when task completes abnormally |
390 |
|
|
*/ |
391 |
|
|
public void testAbnormalForkGet() { |
392 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
393 |
|
|
public Integer realCompute() throws Exception { |
394 |
|
|
FailingFibTask f = new FailingFibTask(8); |
395 |
|
|
assertSame(f, f.fork()); |
396 |
jsr166 |
1.7 |
try { |
397 |
|
|
Integer r = f.get(); |
398 |
|
|
shouldThrow(); |
399 |
jsr166 |
1.20 |
} catch (ExecutionException success) { |
400 |
|
|
checkTaskThrew(f, success.getCause()); |
401 |
|
|
} |
402 |
jsr166 |
1.7 |
return NoResult; |
403 |
jsr166 |
1.19 |
}}; |
404 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
405 |
dl |
1.1 |
} |
406 |
|
|
|
407 |
jsr166 |
1.2 |
/** |
408 |
dl |
1.1 |
* timed get of a forked task throws exception when task completes abnormally |
409 |
|
|
*/ |
410 |
|
|
public void testAbnormalForkTimedGet() { |
411 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
412 |
|
|
public Integer realCompute() throws Exception { |
413 |
|
|
FailingFibTask f = new FailingFibTask(8); |
414 |
|
|
assertSame(f, f.fork()); |
415 |
jsr166 |
1.7 |
try { |
416 |
jsr166 |
1.20 |
Integer r = f.get(5L, SECONDS); |
417 |
jsr166 |
1.7 |
shouldThrow(); |
418 |
jsr166 |
1.20 |
} catch (ExecutionException success) { |
419 |
|
|
checkTaskThrew(f, success.getCause()); |
420 |
|
|
} |
421 |
jsr166 |
1.7 |
return NoResult; |
422 |
jsr166 |
1.19 |
}}; |
423 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
424 |
dl |
1.1 |
} |
425 |
|
|
|
426 |
jsr166 |
1.2 |
/** |
427 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes abnormally |
428 |
|
|
*/ |
429 |
|
|
public void testAbnormalForkQuietlyJoin() { |
430 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
431 |
|
|
public Integer realCompute() { |
432 |
jsr166 |
1.7 |
FailingFibTask f = new FailingFibTask(8); |
433 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
434 |
jsr166 |
1.7 |
f.quietlyJoin(); |
435 |
jsr166 |
1.19 |
assertTrue(f.getException() instanceof FJException); |
436 |
jsr166 |
1.20 |
checkTaskThrew(f, f.getException()); |
437 |
jsr166 |
1.7 |
return NoResult; |
438 |
jsr166 |
1.19 |
}}; |
439 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
440 |
dl |
1.1 |
} |
441 |
|
|
|
442 |
jsr166 |
1.2 |
/** |
443 |
dl |
1.1 |
* invoke task throws exception when task cancelled |
444 |
|
|
*/ |
445 |
|
|
public void testCancelledInvoke() { |
446 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
447 |
|
|
public Integer realCompute() { |
448 |
|
|
FibTask f = new FibTask(8); |
449 |
|
|
assertTrue(f.cancel(true)); |
450 |
jsr166 |
1.7 |
try { |
451 |
|
|
Integer r = f.invoke(); |
452 |
|
|
shouldThrow(); |
453 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
454 |
|
|
checkCancelled(f); |
455 |
|
|
} |
456 |
jsr166 |
1.7 |
return NoResult; |
457 |
jsr166 |
1.19 |
}}; |
458 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
459 |
dl |
1.1 |
} |
460 |
|
|
|
461 |
jsr166 |
1.2 |
/** |
462 |
dl |
1.1 |
* join of a forked task throws exception when task cancelled |
463 |
|
|
*/ |
464 |
|
|
public void testCancelledForkJoin() { |
465 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
466 |
|
|
public Integer realCompute() { |
467 |
|
|
FibTask f = new FibTask(8); |
468 |
|
|
assertTrue(f.cancel(true)); |
469 |
|
|
assertSame(f, f.fork()); |
470 |
jsr166 |
1.7 |
try { |
471 |
|
|
Integer r = f.join(); |
472 |
|
|
shouldThrow(); |
473 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
474 |
|
|
checkCancelled(f); |
475 |
|
|
} |
476 |
jsr166 |
1.7 |
return NoResult; |
477 |
jsr166 |
1.19 |
}}; |
478 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
479 |
dl |
1.1 |
} |
480 |
|
|
|
481 |
jsr166 |
1.2 |
/** |
482 |
dl |
1.1 |
* get of a forked task throws exception when task cancelled |
483 |
|
|
*/ |
484 |
|
|
public void testCancelledForkGet() { |
485 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
486 |
|
|
public Integer realCompute() throws Exception { |
487 |
|
|
FibTask f = new FibTask(8); |
488 |
|
|
assertTrue(f.cancel(true)); |
489 |
|
|
assertSame(f, f.fork()); |
490 |
jsr166 |
1.7 |
try { |
491 |
|
|
Integer r = f.get(); |
492 |
|
|
shouldThrow(); |
493 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
494 |
|
|
checkCancelled(f); |
495 |
|
|
} |
496 |
jsr166 |
1.7 |
return NoResult; |
497 |
jsr166 |
1.19 |
}}; |
498 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
499 |
dl |
1.1 |
} |
500 |
|
|
|
501 |
jsr166 |
1.2 |
/** |
502 |
dl |
1.1 |
* timed get of a forked task throws exception when task cancelled |
503 |
|
|
*/ |
504 |
|
|
public void testCancelledForkTimedGet() { |
505 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
506 |
|
|
public Integer realCompute() throws Exception { |
507 |
|
|
FibTask f = new FibTask(8); |
508 |
|
|
assertTrue(f.cancel(true)); |
509 |
|
|
assertSame(f, f.fork()); |
510 |
jsr166 |
1.7 |
try { |
511 |
jsr166 |
1.20 |
Integer r = f.get(5L, SECONDS); |
512 |
jsr166 |
1.7 |
shouldThrow(); |
513 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
514 |
|
|
checkCancelled(f); |
515 |
|
|
} |
516 |
jsr166 |
1.7 |
return NoResult; |
517 |
jsr166 |
1.19 |
}}; |
518 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
519 |
dl |
1.1 |
} |
520 |
|
|
|
521 |
jsr166 |
1.2 |
/** |
522 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task cancelled |
523 |
|
|
*/ |
524 |
|
|
public void testCancelledForkQuietlyJoin() { |
525 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
526 |
|
|
public Integer realCompute() { |
527 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
528 |
jsr166 |
1.19 |
assertTrue(f.cancel(true)); |
529 |
|
|
assertSame(f, f.fork()); |
530 |
jsr166 |
1.7 |
f.quietlyJoin(); |
531 |
jsr166 |
1.20 |
checkCancelled(f); |
532 |
jsr166 |
1.7 |
return NoResult; |
533 |
jsr166 |
1.19 |
}}; |
534 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
535 |
dl |
1.1 |
} |
536 |
|
|
|
537 |
|
|
/** |
538 |
|
|
* getPool of executing task returns its pool |
539 |
|
|
*/ |
540 |
|
|
public void testGetPool() { |
541 |
jsr166 |
1.13 |
final ForkJoinPool mainPool = mainPool(); |
542 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
543 |
|
|
public Integer realCompute() { |
544 |
|
|
assertSame(mainPool, getPool()); |
545 |
jsr166 |
1.7 |
return NoResult; |
546 |
jsr166 |
1.19 |
}}; |
547 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool, a)); |
548 |
dl |
1.1 |
} |
549 |
|
|
|
550 |
|
|
/** |
551 |
|
|
* getPool of non-FJ task returns null |
552 |
|
|
*/ |
553 |
|
|
public void testGetPool2() { |
554 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
555 |
|
|
public Integer realCompute() { |
556 |
|
|
assertNull(getPool()); |
557 |
dl |
1.1 |
return NoResult; |
558 |
jsr166 |
1.19 |
}}; |
559 |
jsr166 |
1.16 |
assertSame(NoResult, a.invoke()); |
560 |
dl |
1.1 |
} |
561 |
jsr166 |
1.2 |
|
562 |
dl |
1.1 |
/** |
563 |
|
|
* inForkJoinPool of executing task returns true |
564 |
|
|
*/ |
565 |
|
|
public void testInForkJoinPool() { |
566 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
567 |
|
|
public Integer realCompute() { |
568 |
|
|
assertTrue(inForkJoinPool()); |
569 |
jsr166 |
1.7 |
return NoResult; |
570 |
jsr166 |
1.19 |
}}; |
571 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
572 |
dl |
1.1 |
} |
573 |
|
|
|
574 |
|
|
/** |
575 |
|
|
* inForkJoinPool of non-FJ task returns false |
576 |
|
|
*/ |
577 |
|
|
public void testInForkJoinPool2() { |
578 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
579 |
|
|
public Integer realCompute() { |
580 |
|
|
assertTrue(!inForkJoinPool()); |
581 |
jsr166 |
1.7 |
return NoResult; |
582 |
jsr166 |
1.19 |
}}; |
583 |
jsr166 |
1.16 |
assertSame(NoResult, a.invoke()); |
584 |
dl |
1.1 |
} |
585 |
|
|
|
586 |
|
|
/** |
587 |
dl |
1.17 |
* The value set by setRawResult is returned by getRawResult |
588 |
dl |
1.1 |
*/ |
589 |
|
|
public void testSetRawResult() { |
590 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
591 |
|
|
public Integer realCompute() { |
592 |
jsr166 |
1.7 |
setRawResult(NoResult); |
593 |
jsr166 |
1.19 |
assertSame(NoResult, getRawResult()); |
594 |
jsr166 |
1.7 |
return NoResult; |
595 |
|
|
} |
596 |
|
|
}; |
597 |
dl |
1.17 |
a.invoke(); |
598 |
dl |
1.1 |
} |
599 |
|
|
|
600 |
jsr166 |
1.2 |
/** |
601 |
jsr166 |
1.20 |
* A reinitialized normally completed task may be re-invoked |
602 |
dl |
1.1 |
*/ |
603 |
|
|
public void testReinitialize() { |
604 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
605 |
|
|
public Integer realCompute() { |
606 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
607 |
jsr166 |
1.20 |
checkNotDone(f); |
608 |
|
|
|
609 |
|
|
for (int i = 0; i < 3; i++) { |
610 |
|
|
Integer r = f.invoke(); |
611 |
|
|
assertEquals(21, (int) r); |
612 |
|
|
checkCompletedNormally(f, r); |
613 |
|
|
f.reinitialize(); |
614 |
|
|
f.publicSetRawResult(null); |
615 |
|
|
checkNotDone(f); |
616 |
|
|
} |
617 |
|
|
return NoResult; |
618 |
|
|
}}; |
619 |
|
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
620 |
|
|
} |
621 |
|
|
|
622 |
|
|
/** |
623 |
|
|
* A reinitialized abnormally completed task may be re-invoked |
624 |
|
|
*/ |
625 |
|
|
public void testReinitializeAbnormal() { |
626 |
|
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
627 |
|
|
public Integer realCompute() { |
628 |
|
|
FailingFibTask f = new FailingFibTask(8); |
629 |
|
|
checkNotDone(f); |
630 |
|
|
|
631 |
|
|
for (int i = 0; i < 3; i++) { |
632 |
|
|
try { |
633 |
|
|
f.invoke(); |
634 |
|
|
shouldThrow(); |
635 |
|
|
} catch (FJException success) { |
636 |
|
|
checkTaskThrew(f, success); |
637 |
|
|
} |
638 |
|
|
f.reinitialize(); |
639 |
|
|
checkNotDone(f); |
640 |
|
|
} |
641 |
jsr166 |
1.7 |
return NoResult; |
642 |
jsr166 |
1.19 |
}}; |
643 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
644 |
dl |
1.1 |
} |
645 |
|
|
|
646 |
jsr166 |
1.2 |
/** |
647 |
dl |
1.1 |
* invoke task throws exception after invoking completeExceptionally |
648 |
|
|
*/ |
649 |
|
|
public void testCompleteExceptionally() { |
650 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
651 |
|
|
public Integer realCompute() { |
652 |
|
|
FibTask f = new FibTask(8); |
653 |
|
|
f.completeExceptionally(new FJException()); |
654 |
jsr166 |
1.7 |
try { |
655 |
|
|
Integer r = f.invoke(); |
656 |
|
|
shouldThrow(); |
657 |
jsr166 |
1.20 |
} catch (FJException success) { |
658 |
|
|
checkTaskThrew(f, success); |
659 |
|
|
} |
660 |
jsr166 |
1.7 |
return NoResult; |
661 |
jsr166 |
1.19 |
}}; |
662 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
663 |
dl |
1.1 |
} |
664 |
|
|
|
665 |
jsr166 |
1.2 |
/** |
666 |
dl |
1.1 |
* invoke task suppresses execution invoking complete |
667 |
|
|
*/ |
668 |
|
|
public void testComplete() { |
669 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
670 |
|
|
public Integer realCompute() { |
671 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
672 |
|
|
f.complete(NoResult); |
673 |
|
|
Integer r = f.invoke(); |
674 |
jsr166 |
1.19 |
assertSame(NoResult, r); |
675 |
jsr166 |
1.20 |
checkCompletedNormally(f, NoResult); |
676 |
jsr166 |
1.7 |
return r; |
677 |
jsr166 |
1.19 |
}}; |
678 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
679 |
dl |
1.1 |
} |
680 |
|
|
|
681 |
jsr166 |
1.2 |
/** |
682 |
dl |
1.1 |
* invokeAll(t1, t2) invokes all task arguments |
683 |
|
|
*/ |
684 |
|
|
public void testInvokeAll2() { |
685 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
686 |
|
|
public Integer realCompute() { |
687 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
688 |
|
|
FibTask g = new FibTask(9); |
689 |
|
|
invokeAll(f, g); |
690 |
jsr166 |
1.21 |
checkCompletedNormally(f, 21); |
691 |
|
|
checkCompletedNormally(g, 34); |
692 |
jsr166 |
1.7 |
return NoResult; |
693 |
jsr166 |
1.19 |
}}; |
694 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
695 |
dl |
1.1 |
} |
696 |
|
|
|
697 |
jsr166 |
1.2 |
/** |
698 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument invokes task |
699 |
|
|
*/ |
700 |
|
|
public void testInvokeAll1() { |
701 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
702 |
|
|
public Integer realCompute() { |
703 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
704 |
|
|
invokeAll(f); |
705 |
jsr166 |
1.21 |
checkCompletedNormally(f, 21); |
706 |
jsr166 |
1.7 |
return NoResult; |
707 |
jsr166 |
1.19 |
}}; |
708 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
709 |
dl |
1.1 |
} |
710 |
|
|
|
711 |
jsr166 |
1.2 |
/** |
712 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument invokes tasks |
713 |
|
|
*/ |
714 |
|
|
public void testInvokeAll3() { |
715 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
716 |
|
|
public Integer realCompute() { |
717 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
718 |
|
|
FibTask g = new FibTask(9); |
719 |
|
|
FibTask h = new FibTask(7); |
720 |
|
|
invokeAll(f, g, h); |
721 |
jsr166 |
1.21 |
assertTrue(f.isDone()); |
722 |
|
|
assertTrue(g.isDone()); |
723 |
|
|
assertTrue(h.isDone()); |
724 |
|
|
checkCompletedNormally(f, 21); |
725 |
|
|
checkCompletedNormally(g, 34); |
726 |
|
|
checkCompletedNormally(h, 13); |
727 |
jsr166 |
1.7 |
return NoResult; |
728 |
jsr166 |
1.19 |
}}; |
729 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
730 |
dl |
1.1 |
} |
731 |
|
|
|
732 |
jsr166 |
1.2 |
/** |
733 |
dl |
1.1 |
* invokeAll(collection) invokes all tasks in the collection |
734 |
|
|
*/ |
735 |
|
|
public void testInvokeAllCollection() { |
736 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
737 |
|
|
public Integer realCompute() { |
738 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
739 |
|
|
FibTask g = new FibTask(9); |
740 |
|
|
FibTask h = new FibTask(7); |
741 |
|
|
HashSet set = new HashSet(); |
742 |
|
|
set.add(f); |
743 |
|
|
set.add(g); |
744 |
|
|
set.add(h); |
745 |
|
|
invokeAll(set); |
746 |
jsr166 |
1.21 |
assertTrue(f.isDone()); |
747 |
|
|
assertTrue(g.isDone()); |
748 |
|
|
assertTrue(h.isDone()); |
749 |
|
|
checkCompletedNormally(f, 21); |
750 |
|
|
checkCompletedNormally(g, 34); |
751 |
|
|
checkCompletedNormally(h, 13); |
752 |
jsr166 |
1.7 |
return NoResult; |
753 |
jsr166 |
1.19 |
}}; |
754 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
755 |
dl |
1.1 |
} |
756 |
|
|
|
757 |
|
|
|
758 |
jsr166 |
1.2 |
/** |
759 |
jsr166 |
1.20 |
* invokeAll(tasks) with any null task throws NPE |
760 |
|
|
*/ |
761 |
|
|
public void testInvokeAllNPE() { |
762 |
|
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
763 |
|
|
public Integer realCompute() { |
764 |
|
|
FibTask f = new FibTask(8); |
765 |
|
|
FibTask g = new FibTask(9); |
766 |
|
|
FibTask h = null; |
767 |
|
|
try { |
768 |
|
|
invokeAll(f, g, h); |
769 |
|
|
shouldThrow(); |
770 |
|
|
} catch (NullPointerException success) {} |
771 |
|
|
return NoResult; |
772 |
|
|
}}; |
773 |
|
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
774 |
|
|
} |
775 |
|
|
|
776 |
|
|
/** |
777 |
dl |
1.1 |
* invokeAll(t1, t2) throw exception if any task does |
778 |
|
|
*/ |
779 |
|
|
public void testAbnormalInvokeAll2() { |
780 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
781 |
|
|
public Integer realCompute() { |
782 |
|
|
FibTask f = new FibTask(8); |
783 |
|
|
FailingFibTask g = new FailingFibTask(9); |
784 |
jsr166 |
1.7 |
try { |
785 |
|
|
invokeAll(f, g); |
786 |
|
|
shouldThrow(); |
787 |
jsr166 |
1.20 |
} catch (FJException success) { |
788 |
|
|
checkTaskThrew(g, success); |
789 |
|
|
} |
790 |
jsr166 |
1.7 |
return NoResult; |
791 |
jsr166 |
1.19 |
}}; |
792 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
793 |
dl |
1.1 |
} |
794 |
|
|
|
795 |
jsr166 |
1.2 |
/** |
796 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument throws exception if task does |
797 |
|
|
*/ |
798 |
|
|
public void testAbnormalInvokeAll1() { |
799 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
800 |
|
|
public Integer realCompute() { |
801 |
|
|
FailingFibTask g = new FailingFibTask(9); |
802 |
jsr166 |
1.7 |
try { |
803 |
|
|
invokeAll(g); |
804 |
|
|
shouldThrow(); |
805 |
jsr166 |
1.20 |
} catch (FJException success) { |
806 |
|
|
checkTaskThrew(g, success); |
807 |
|
|
} |
808 |
jsr166 |
1.7 |
return NoResult; |
809 |
jsr166 |
1.19 |
}}; |
810 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
811 |
dl |
1.1 |
} |
812 |
|
|
|
813 |
jsr166 |
1.2 |
/** |
814 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
815 |
|
|
*/ |
816 |
|
|
public void testAbnormalInvokeAll3() { |
817 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
818 |
|
|
public Integer realCompute() { |
819 |
|
|
FibTask f = new FibTask(8); |
820 |
|
|
FailingFibTask g = new FailingFibTask(9); |
821 |
|
|
FibTask h = new FibTask(7); |
822 |
jsr166 |
1.7 |
try { |
823 |
|
|
invokeAll(f, g, h); |
824 |
|
|
shouldThrow(); |
825 |
jsr166 |
1.20 |
} catch (FJException success) { |
826 |
|
|
checkTaskThrew(g, success); |
827 |
|
|
} |
828 |
jsr166 |
1.7 |
return NoResult; |
829 |
jsr166 |
1.19 |
}}; |
830 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
831 |
dl |
1.1 |
} |
832 |
|
|
|
833 |
jsr166 |
1.2 |
/** |
834 |
jsr166 |
1.3 |
* invokeAll(collection) throws exception if any task does |
835 |
dl |
1.1 |
*/ |
836 |
|
|
public void testAbnormalInvokeAllCollection() { |
837 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
838 |
|
|
public Integer realCompute() { |
839 |
|
|
FailingFibTask f = new FailingFibTask(8); |
840 |
|
|
FibTask g = new FibTask(9); |
841 |
|
|
FibTask h = new FibTask(7); |
842 |
|
|
HashSet set = new HashSet(); |
843 |
|
|
set.add(f); |
844 |
|
|
set.add(g); |
845 |
|
|
set.add(h); |
846 |
jsr166 |
1.7 |
try { |
847 |
|
|
invokeAll(set); |
848 |
|
|
shouldThrow(); |
849 |
jsr166 |
1.20 |
} catch (FJException success) { |
850 |
|
|
checkTaskThrew(f, success); |
851 |
|
|
} |
852 |
jsr166 |
1.7 |
return NoResult; |
853 |
jsr166 |
1.19 |
}}; |
854 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
855 |
dl |
1.1 |
} |
856 |
|
|
|
857 |
jsr166 |
1.2 |
/** |
858 |
dl |
1.1 |
* tryUnfork returns true for most recent unexecuted task, |
859 |
|
|
* and suppresses execution |
860 |
|
|
*/ |
861 |
|
|
public void testTryUnfork() { |
862 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
863 |
|
|
public Integer realCompute() { |
864 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
865 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
866 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
867 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
868 |
|
|
assertTrue(f.tryUnfork()); |
869 |
jsr166 |
1.7 |
helpQuiesce(); |
870 |
jsr166 |
1.20 |
checkNotDone(f); |
871 |
|
|
checkCompletedNormally(g, 34); |
872 |
jsr166 |
1.7 |
return NoResult; |
873 |
jsr166 |
1.19 |
}}; |
874 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
875 |
dl |
1.1 |
} |
876 |
|
|
|
877 |
jsr166 |
1.2 |
/** |
878 |
dl |
1.1 |
* getSurplusQueuedTaskCount returns > 0 when |
879 |
|
|
* there are more tasks than threads |
880 |
|
|
*/ |
881 |
|
|
public void testGetSurplusQueuedTaskCount() { |
882 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
883 |
|
|
public Integer realCompute() { |
884 |
jsr166 |
1.7 |
FibTask h = new FibTask(7); |
885 |
jsr166 |
1.19 |
assertSame(h, h.fork()); |
886 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
887 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
888 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
889 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
890 |
|
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
891 |
jsr166 |
1.7 |
helpQuiesce(); |
892 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
893 |
|
|
checkCompletedNormally(g, 34); |
894 |
|
|
checkCompletedNormally(h, 13); |
895 |
jsr166 |
1.7 |
return NoResult; |
896 |
jsr166 |
1.19 |
}}; |
897 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
898 |
dl |
1.1 |
} |
899 |
|
|
|
900 |
jsr166 |
1.2 |
/** |
901 |
dl |
1.1 |
* peekNextLocalTask returns most recent unexecuted task. |
902 |
|
|
*/ |
903 |
|
|
public void testPeekNextLocalTask() { |
904 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
905 |
|
|
public Integer realCompute() { |
906 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
907 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
908 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
909 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
910 |
|
|
assertSame(f, peekNextLocalTask()); |
911 |
jsr166 |
1.20 |
checkCompletesNormally(f, 21); |
912 |
jsr166 |
1.7 |
helpQuiesce(); |
913 |
jsr166 |
1.20 |
checkCompletedNormally(g, 34); |
914 |
jsr166 |
1.7 |
return NoResult; |
915 |
jsr166 |
1.19 |
}}; |
916 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
917 |
dl |
1.1 |
} |
918 |
|
|
|
919 |
jsr166 |
1.2 |
/** |
920 |
dl |
1.1 |
* pollNextLocalTask returns most recent unexecuted task |
921 |
|
|
* without executing it |
922 |
|
|
*/ |
923 |
|
|
public void testPollNextLocalTask() { |
924 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
925 |
|
|
public Integer realCompute() { |
926 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
927 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
928 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
929 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
930 |
|
|
assertSame(f, pollNextLocalTask()); |
931 |
jsr166 |
1.7 |
helpQuiesce(); |
932 |
jsr166 |
1.20 |
checkNotDone(f); |
933 |
|
|
checkCompletedNormally(g, 34); |
934 |
jsr166 |
1.7 |
return NoResult; |
935 |
jsr166 |
1.19 |
}}; |
936 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
937 |
dl |
1.1 |
} |
938 |
|
|
|
939 |
jsr166 |
1.2 |
/** |
940 |
jsr166 |
1.13 |
* pollTask returns an unexecuted task without executing it |
941 |
dl |
1.1 |
*/ |
942 |
|
|
public void testPollTask() { |
943 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
944 |
|
|
public Integer realCompute() { |
945 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
946 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
947 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
948 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
949 |
|
|
assertSame(f, pollTask()); |
950 |
jsr166 |
1.7 |
helpQuiesce(); |
951 |
jsr166 |
1.20 |
checkNotDone(f); |
952 |
|
|
checkCompletedNormally(g, 34); |
953 |
jsr166 |
1.7 |
return NoResult; |
954 |
jsr166 |
1.19 |
}}; |
955 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
956 |
dl |
1.1 |
} |
957 |
|
|
|
958 |
jsr166 |
1.2 |
/** |
959 |
dl |
1.1 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
960 |
|
|
*/ |
961 |
|
|
public void testPeekNextLocalTaskAsync() { |
962 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
963 |
|
|
public Integer realCompute() { |
964 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
965 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
966 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
967 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
968 |
|
|
assertSame(g, peekNextLocalTask()); |
969 |
|
|
assertEquals(21, (int) f.join()); |
970 |
jsr166 |
1.7 |
helpQuiesce(); |
971 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
972 |
|
|
checkCompletedNormally(g, 34); |
973 |
jsr166 |
1.7 |
return NoResult; |
974 |
jsr166 |
1.19 |
}}; |
975 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
976 |
dl |
1.1 |
} |
977 |
|
|
|
978 |
jsr166 |
1.2 |
/** |
979 |
jsr166 |
1.20 |
* pollNextLocalTask returns least recent unexecuted task without |
980 |
|
|
* executing it, in async mode |
981 |
dl |
1.1 |
*/ |
982 |
|
|
public void testPollNextLocalTaskAsync() { |
983 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
984 |
|
|
public Integer realCompute() { |
985 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
986 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
987 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
988 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
989 |
|
|
assertSame(g, pollNextLocalTask()); |
990 |
jsr166 |
1.7 |
helpQuiesce(); |
991 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
992 |
|
|
checkNotDone(g); |
993 |
jsr166 |
1.7 |
return NoResult; |
994 |
jsr166 |
1.19 |
}}; |
995 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
996 |
dl |
1.1 |
} |
997 |
|
|
|
998 |
jsr166 |
1.2 |
/** |
999 |
jsr166 |
1.20 |
* pollTask returns an unexecuted task without executing it, in |
1000 |
|
|
* async mode |
1001 |
dl |
1.1 |
*/ |
1002 |
|
|
public void testPollTaskAsync() { |
1003 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1004 |
|
|
public Integer realCompute() { |
1005 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
1006 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
1007 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
1008 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
1009 |
|
|
assertSame(g, pollTask()); |
1010 |
jsr166 |
1.7 |
helpQuiesce(); |
1011 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
1012 |
|
|
checkNotDone(g); |
1013 |
jsr166 |
1.7 |
return NoResult; |
1014 |
jsr166 |
1.19 |
}}; |
1015 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1016 |
dl |
1.1 |
} |
1017 |
|
|
|
1018 |
|
|
} |