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