8 |
|
import java.util.concurrent.CancellationException; |
9 |
|
import java.util.concurrent.ExecutionException; |
10 |
|
import java.util.concurrent.ForkJoinPool; |
11 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
|
import java.util.concurrent.RecursiveTask; |
13 |
|
import java.util.concurrent.TimeUnit; |
14 |
+ |
import java.util.concurrent.TimeoutException; |
15 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
import java.util.HashSet; |
17 |
|
|
18 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
40 |
|
|
41 |
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
42 |
|
try { |
43 |
< |
return pool.invoke(a); |
43 |
> |
checkNotDone(a); |
44 |
> |
|
45 |
> |
T result = pool.invoke(a); |
46 |
> |
|
47 |
> |
checkCompletedNormally(a, result); |
48 |
> |
return result; |
49 |
|
} finally { |
50 |
|
joinPool(pool); |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
+ |
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 |
|
static final class FJException extends RuntimeException { |
179 |
|
FJException() { super(); } |
180 |
|
} |
183 |
|
static final Integer NoResult = Integer.valueOf(-17); |
184 |
|
|
185 |
|
// A simple recursive task for testing |
186 |
< |
static final class FibTask extends RecursiveTask<Integer> { |
186 |
> |
final class FibTask extends CheckedRecursiveTask<Integer> { |
187 |
|
final int number; |
188 |
|
FibTask(int n) { number = n; } |
189 |
< |
public Integer compute() { |
189 |
> |
public Integer realCompute() { |
190 |
|
int n = number; |
191 |
|
if (n <= 1) |
192 |
|
return n; |
194 |
|
f1.fork(); |
195 |
|
return (new FibTask(n - 2)).compute() + f1.join(); |
196 |
|
} |
197 |
+ |
|
198 |
+ |
public void publicSetRawResult(Integer result) { |
199 |
+ |
setRawResult(result); |
200 |
+ |
} |
201 |
|
} |
202 |
|
|
203 |
|
// A recursive action failing in base case |
204 |
< |
static final class FailingFibTask extends RecursiveTask<Integer> { |
204 |
> |
final class FailingFibTask extends RecursiveTask<Integer> { |
205 |
|
final int number; |
206 |
|
int result; |
207 |
|
FailingFibTask(int n) { number = n; } |
222 |
|
* returns value; |
223 |
|
*/ |
224 |
|
public void testInvoke() { |
225 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
226 |
< |
public Integer compute() { |
225 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
226 |
> |
public Integer realCompute() { |
227 |
|
FibTask f = new FibTask(8); |
228 |
|
Integer r = f.invoke(); |
229 |
< |
threadAssertTrue(r == 21); |
230 |
< |
threadAssertTrue(f.isDone()); |
95 |
< |
threadAssertFalse(f.isCancelled()); |
96 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
97 |
< |
threadAssertTrue(f.getRawResult() == 21); |
229 |
> |
assertEquals(21, (int) r); |
230 |
> |
checkCompletedNormally(f, r); |
231 |
|
return r; |
232 |
< |
} |
100 |
< |
}; |
232 |
> |
}}; |
233 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
234 |
|
} |
235 |
|
|
239 |
|
* completed tasks |
240 |
|
*/ |
241 |
|
public void testQuietlyInvoke() { |
242 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
243 |
< |
public Integer compute() { |
242 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
243 |
> |
public Integer realCompute() { |
244 |
|
FibTask f = new FibTask(8); |
245 |
|
f.quietlyInvoke(); |
246 |
< |
Integer r = f.getRawResult(); |
247 |
< |
threadAssertTrue(r == 21); |
248 |
< |
threadAssertTrue(f.isDone()); |
249 |
< |
threadAssertFalse(f.isCancelled()); |
118 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
119 |
< |
return r; |
120 |
< |
} |
121 |
< |
}; |
122 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
246 |
> |
checkCompletedNormally(f, 21); |
247 |
> |
return NoResult; |
248 |
> |
}}; |
249 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
|
* join of a forked task returns when task completes |
254 |
|
*/ |
255 |
|
public void testForkJoin() { |
256 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
257 |
< |
public Integer compute() { |
256 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
257 |
> |
public Integer realCompute() { |
258 |
|
FibTask f = new FibTask(8); |
259 |
< |
threadAssertSame(f, f.fork()); |
259 |
> |
assertSame(f, f.fork()); |
260 |
|
Integer r = f.join(); |
261 |
< |
threadAssertTrue(r == 21); |
262 |
< |
threadAssertTrue(f.isDone()); |
261 |
> |
assertEquals(21, (int) r); |
262 |
> |
checkCompletedNormally(f, r); |
263 |
|
return r; |
264 |
< |
} |
138 |
< |
}; |
264 |
> |
}}; |
265 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
266 |
|
} |
267 |
|
|
269 |
|
* get of a forked task returns when task completes |
270 |
|
*/ |
271 |
|
public void testForkGet() { |
272 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
273 |
< |
public Integer compute() { |
274 |
< |
try { |
275 |
< |
FibTask f = new FibTask(8); |
276 |
< |
threadAssertSame(f, f.fork()); |
277 |
< |
Integer r = f.get(); |
278 |
< |
threadAssertTrue(r == 21); |
279 |
< |
threadAssertTrue(f.isDone()); |
280 |
< |
return r; |
155 |
< |
} catch (Exception ex) { |
156 |
< |
unexpectedException(ex); |
157 |
< |
} |
158 |
< |
return NoResult; |
159 |
< |
} |
160 |
< |
}; |
272 |
> |
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 |
> |
checkCompletedNormally(f, r); |
279 |
> |
return r; |
280 |
> |
}}; |
281 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
282 |
|
} |
283 |
|
|
285 |
|
* timed get of a forked task returns when task completes |
286 |
|
*/ |
287 |
|
public void testForkTimedGet() { |
288 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
289 |
< |
public Integer compute() { |
290 |
< |
try { |
291 |
< |
FibTask f = new FibTask(8); |
292 |
< |
threadAssertSame(f, f.fork()); |
293 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
294 |
< |
threadAssertTrue(r == 21); |
295 |
< |
threadAssertTrue(f.isDone()); |
296 |
< |
return r; |
177 |
< |
} catch (Exception ex) { |
178 |
< |
unexpectedException(ex); |
179 |
< |
} |
180 |
< |
return NoResult; |
181 |
< |
} |
182 |
< |
}; |
288 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
289 |
> |
public Integer realCompute() throws Exception { |
290 |
> |
FibTask f = new FibTask(8); |
291 |
> |
assertSame(f, f.fork()); |
292 |
> |
Integer r = f.get(5L, SECONDS); |
293 |
> |
assertEquals(21, (int) r); |
294 |
> |
checkCompletedNormally(f, r); |
295 |
> |
return r; |
296 |
> |
}}; |
297 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
298 |
|
} |
299 |
|
|
301 |
|
* quietlyJoin of a forked task returns when task completes |
302 |
|
*/ |
303 |
|
public void testForkQuietlyJoin() { |
304 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
305 |
< |
public Integer compute() { |
304 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
305 |
> |
public Integer realCompute() { |
306 |
|
FibTask f = new FibTask(8); |
307 |
< |
threadAssertSame(f, f.fork()); |
307 |
> |
assertSame(f, f.fork()); |
308 |
|
f.quietlyJoin(); |
309 |
|
Integer r = f.getRawResult(); |
310 |
< |
threadAssertTrue(r == 21); |
311 |
< |
threadAssertTrue(f.isDone()); |
310 |
> |
assertEquals(21, (int) r); |
311 |
> |
checkCompletedNormally(f, r); |
312 |
|
return r; |
313 |
< |
} |
200 |
< |
}; |
313 |
> |
}}; |
314 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
315 |
|
} |
316 |
|
|
320 |
|
* getQueuedTaskCount returns 0 when quiescent |
321 |
|
*/ |
322 |
|
public void testForkHelpQuiesce() { |
323 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
324 |
< |
public Integer compute() { |
323 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
324 |
> |
public Integer realCompute() { |
325 |
|
FibTask f = new FibTask(8); |
326 |
< |
threadAssertSame(f, f.fork()); |
326 |
> |
assertSame(f, f.fork()); |
327 |
|
f.helpQuiesce(); |
328 |
< |
Integer r = f.getRawResult(); |
329 |
< |
threadAssertTrue(r == 21); |
330 |
< |
threadAssertTrue(f.isDone()); |
331 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
332 |
< |
return r; |
220 |
< |
} |
221 |
< |
}; |
222 |
< |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
328 |
> |
assertEquals(0, getQueuedTaskCount()); |
329 |
> |
checkCompletedNormally(f, 21); |
330 |
> |
return NoResult; |
331 |
> |
}}; |
332 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
333 |
|
} |
334 |
|
|
335 |
|
|
337 |
|
* invoke task throws exception when task completes abnormally |
338 |
|
*/ |
339 |
|
public void testAbnormalInvoke() { |
340 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
341 |
< |
public Integer compute() { |
340 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
341 |
> |
public Integer realCompute() { |
342 |
> |
FailingFibTask f = new FailingFibTask(8); |
343 |
|
try { |
233 |
– |
FailingFibTask f = new FailingFibTask(8); |
344 |
|
f.invoke(); |
345 |
|
shouldThrow(); |
236 |
– |
return NoResult; |
346 |
|
} catch (FJException success) { |
347 |
+ |
checkTaskThrew(f, success); |
348 |
|
} |
349 |
|
return NoResult; |
350 |
< |
} |
241 |
< |
}; |
350 |
> |
}}; |
351 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
352 |
|
} |
353 |
|
|
355 |
|
* quietlyInvoke task returns when task completes abnormally |
356 |
|
*/ |
357 |
|
public void testAbnormalQuietlyInvoke() { |
358 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
359 |
< |
public Integer compute() { |
358 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
359 |
> |
public Integer realCompute() { |
360 |
|
FailingFibTask f = new FailingFibTask(8); |
361 |
|
f.quietlyInvoke(); |
362 |
< |
threadAssertTrue(f.isDone()); |
362 |
> |
assertTrue(f.getException() instanceof FJException); |
363 |
> |
checkTaskThrew(f, f.getException()); |
364 |
|
return NoResult; |
365 |
< |
} |
256 |
< |
}; |
365 |
> |
}}; |
366 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
367 |
|
} |
368 |
|
|
370 |
|
* join of a forked task throws exception when task completes abnormally |
371 |
|
*/ |
372 |
|
public void testAbnormalForkJoin() { |
373 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
374 |
< |
public Integer compute() { |
373 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
374 |
> |
public Integer realCompute() { |
375 |
> |
FailingFibTask f = new FailingFibTask(8); |
376 |
> |
assertSame(f, f.fork()); |
377 |
|
try { |
267 |
– |
FailingFibTask f = new FailingFibTask(8); |
268 |
– |
threadAssertSame(f, f.fork()); |
378 |
|
Integer r = f.join(); |
379 |
|
shouldThrow(); |
271 |
– |
return r; |
380 |
|
} catch (FJException success) { |
381 |
+ |
checkTaskThrew(f, success); |
382 |
|
} |
383 |
|
return NoResult; |
384 |
< |
} |
276 |
< |
}; |
384 |
> |
}}; |
385 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
386 |
|
} |
387 |
|
|
389 |
|
* get of a forked task throws exception when task completes abnormally |
390 |
|
*/ |
391 |
|
public void testAbnormalForkGet() { |
392 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
393 |
< |
public Integer compute() { |
392 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
393 |
> |
public Integer realCompute() throws Exception { |
394 |
> |
FailingFibTask f = new FailingFibTask(8); |
395 |
> |
assertSame(f, f.fork()); |
396 |
|
try { |
287 |
– |
FailingFibTask f = new FailingFibTask(8); |
288 |
– |
threadAssertSame(f, f.fork()); |
397 |
|
Integer r = f.get(); |
398 |
|
shouldThrow(); |
291 |
– |
return r; |
399 |
|
} catch (ExecutionException success) { |
400 |
< |
} catch (Exception ex) { |
294 |
< |
unexpectedException(ex); |
400 |
> |
checkTaskThrew(f, success.getCause()); |
401 |
|
} |
402 |
|
return NoResult; |
403 |
< |
} |
298 |
< |
}; |
403 |
> |
}}; |
404 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
405 |
|
} |
406 |
|
|
408 |
|
* timed get of a forked task throws exception when task completes abnormally |
409 |
|
*/ |
410 |
|
public void testAbnormalForkTimedGet() { |
411 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
412 |
< |
public Integer compute() { |
411 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
412 |
> |
public Integer realCompute() throws Exception { |
413 |
> |
FailingFibTask f = new FailingFibTask(8); |
414 |
> |
assertSame(f, f.fork()); |
415 |
|
try { |
416 |
< |
FailingFibTask f = new FailingFibTask(8); |
310 |
< |
threadAssertSame(f, f.fork()); |
311 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
416 |
> |
Integer r = f.get(5L, SECONDS); |
417 |
|
shouldThrow(); |
313 |
– |
return r; |
418 |
|
} catch (ExecutionException success) { |
419 |
< |
} catch (Exception ex) { |
316 |
< |
unexpectedException(ex); |
419 |
> |
checkTaskThrew(f, success.getCause()); |
420 |
|
} |
421 |
|
return NoResult; |
422 |
< |
} |
320 |
< |
}; |
422 |
> |
}}; |
423 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
424 |
|
} |
425 |
|
|
427 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
428 |
|
*/ |
429 |
|
public void testAbnormalForkQuietlyJoin() { |
430 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
431 |
< |
public Integer compute() { |
430 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
431 |
> |
public Integer realCompute() { |
432 |
|
FailingFibTask f = new FailingFibTask(8); |
433 |
< |
threadAssertSame(f, f.fork()); |
433 |
> |
assertSame(f, f.fork()); |
434 |
|
f.quietlyJoin(); |
435 |
< |
threadAssertTrue(f.isDone()); |
436 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
335 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
435 |
> |
assertTrue(f.getException() instanceof FJException); |
436 |
> |
checkTaskThrew(f, f.getException()); |
437 |
|
return NoResult; |
438 |
< |
} |
338 |
< |
}; |
438 |
> |
}}; |
439 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
440 |
|
} |
441 |
|
|
443 |
|
* invoke task throws exception when task cancelled |
444 |
|
*/ |
445 |
|
public void testCancelledInvoke() { |
446 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
447 |
< |
public Integer compute() { |
446 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
447 |
> |
public Integer realCompute() { |
448 |
> |
FibTask f = new FibTask(8); |
449 |
> |
assertTrue(f.cancel(true)); |
450 |
|
try { |
349 |
– |
FibTask f = new FibTask(8); |
350 |
– |
threadAssertTrue(f.cancel(true)); |
451 |
|
Integer r = f.invoke(); |
452 |
|
shouldThrow(); |
353 |
– |
return r; |
453 |
|
} catch (CancellationException success) { |
454 |
+ |
checkCancelled(f); |
455 |
|
} |
456 |
|
return NoResult; |
457 |
< |
} |
358 |
< |
}; |
457 |
> |
}}; |
458 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
459 |
|
} |
460 |
|
|
462 |
|
* join of a forked task throws exception when task cancelled |
463 |
|
*/ |
464 |
|
public void testCancelledForkJoin() { |
465 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
466 |
< |
public Integer compute() { |
465 |
> |
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 |
|
try { |
369 |
– |
FibTask f = new FibTask(8); |
370 |
– |
threadAssertTrue(f.cancel(true)); |
371 |
– |
threadAssertSame(f, f.fork()); |
471 |
|
Integer r = f.join(); |
472 |
|
shouldThrow(); |
374 |
– |
return r; |
473 |
|
} catch (CancellationException success) { |
474 |
+ |
checkCancelled(f); |
475 |
|
} |
476 |
|
return NoResult; |
477 |
< |
} |
379 |
< |
}; |
477 |
> |
}}; |
478 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
479 |
|
} |
480 |
|
|
482 |
|
* get of a forked task throws exception when task cancelled |
483 |
|
*/ |
484 |
|
public void testCancelledForkGet() { |
485 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
486 |
< |
public Integer compute() { |
485 |
> |
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 |
|
try { |
390 |
– |
FibTask f = new FibTask(8); |
391 |
– |
threadAssertTrue(f.cancel(true)); |
392 |
– |
threadAssertSame(f, f.fork()); |
491 |
|
Integer r = f.get(); |
492 |
|
shouldThrow(); |
395 |
– |
return r; |
493 |
|
} catch (CancellationException success) { |
494 |
< |
} catch (Exception ex) { |
398 |
< |
unexpectedException(ex); |
494 |
> |
checkCancelled(f); |
495 |
|
} |
496 |
|
return NoResult; |
497 |
< |
} |
402 |
< |
}; |
497 |
> |
}}; |
498 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
499 |
|
} |
500 |
|
|
502 |
|
* timed get of a forked task throws exception when task cancelled |
503 |
|
*/ |
504 |
|
public void testCancelledForkTimedGet() { |
505 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
506 |
< |
public Integer compute() { |
505 |
> |
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 |
|
try { |
511 |
< |
FibTask f = new FibTask(8); |
414 |
< |
threadAssertTrue(f.cancel(true)); |
415 |
< |
threadAssertSame(f, f.fork()); |
416 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
511 |
> |
Integer r = f.get(5L, SECONDS); |
512 |
|
shouldThrow(); |
418 |
– |
return r; |
513 |
|
} catch (CancellationException success) { |
514 |
< |
} catch (Exception ex) { |
421 |
< |
unexpectedException(ex); |
514 |
> |
checkCancelled(f); |
515 |
|
} |
516 |
|
return NoResult; |
517 |
< |
} |
425 |
< |
}; |
517 |
> |
}}; |
518 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
519 |
|
} |
520 |
|
|
522 |
|
* quietlyJoin of a forked task returns when task cancelled |
523 |
|
*/ |
524 |
|
public void testCancelledForkQuietlyJoin() { |
525 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
526 |
< |
public Integer compute() { |
525 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
526 |
> |
public Integer realCompute() { |
527 |
|
FibTask f = new FibTask(8); |
528 |
< |
threadAssertTrue(f.cancel(true)); |
529 |
< |
threadAssertSame(f, f.fork()); |
528 |
> |
assertTrue(f.cancel(true)); |
529 |
> |
assertSame(f, f.fork()); |
530 |
|
f.quietlyJoin(); |
531 |
< |
threadAssertTrue(f.isDone()); |
440 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
441 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
531 |
> |
checkCancelled(f); |
532 |
|
return NoResult; |
533 |
< |
} |
444 |
< |
}; |
533 |
> |
}}; |
534 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
535 |
|
} |
536 |
|
|
539 |
|
*/ |
540 |
|
public void testGetPool() { |
541 |
|
final ForkJoinPool mainPool = mainPool(); |
542 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
543 |
< |
public Integer compute() { |
544 |
< |
threadAssertTrue(getPool() == mainPool); |
542 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
543 |
> |
public Integer realCompute() { |
544 |
> |
assertSame(mainPool, getPool()); |
545 |
|
return NoResult; |
546 |
< |
} |
458 |
< |
}; |
546 |
> |
}}; |
547 |
|
assertSame(NoResult, testInvokeOnPool(mainPool, a)); |
548 |
|
} |
549 |
|
|
551 |
|
* getPool of non-FJ task returns null |
552 |
|
*/ |
553 |
|
public void testGetPool2() { |
554 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
555 |
< |
public Integer compute() { |
556 |
< |
threadAssertTrue(getPool() == null); |
554 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
555 |
> |
public Integer realCompute() { |
556 |
> |
assertNull(getPool()); |
557 |
|
return NoResult; |
558 |
< |
} |
471 |
< |
}; |
558 |
> |
}}; |
559 |
|
assertSame(NoResult, a.invoke()); |
560 |
|
} |
561 |
|
|
563 |
|
* inForkJoinPool of executing task returns true |
564 |
|
*/ |
565 |
|
public void testInForkJoinPool() { |
566 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
567 |
< |
public Integer compute() { |
568 |
< |
threadAssertTrue(inForkJoinPool()); |
566 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
567 |
> |
public Integer realCompute() { |
568 |
> |
assertTrue(inForkJoinPool()); |
569 |
|
return NoResult; |
570 |
< |
} |
484 |
< |
}; |
570 |
> |
}}; |
571 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
572 |
|
} |
573 |
|
|
575 |
|
* inForkJoinPool of non-FJ task returns false |
576 |
|
*/ |
577 |
|
public void testInForkJoinPool2() { |
578 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
579 |
< |
public Integer compute() { |
580 |
< |
threadAssertTrue(!inForkJoinPool()); |
578 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
579 |
> |
public Integer realCompute() { |
580 |
> |
assertTrue(!inForkJoinPool()); |
581 |
|
return NoResult; |
582 |
< |
} |
497 |
< |
}; |
582 |
> |
}}; |
583 |
|
assertSame(NoResult, a.invoke()); |
584 |
|
} |
585 |
|
|
587 |
|
* The value set by setRawResult is returned by getRawResult |
588 |
|
*/ |
589 |
|
public void testSetRawResult() { |
590 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
591 |
< |
public Integer compute() { |
590 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
591 |
> |
public Integer realCompute() { |
592 |
|
setRawResult(NoResult); |
593 |
< |
threadAssertSame(NoResult, getRawResult()); |
593 |
> |
assertSame(NoResult, getRawResult()); |
594 |
|
return NoResult; |
595 |
|
} |
596 |
|
}; |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
< |
* A reinitialized task may be re-invoked |
601 |
> |
* A reinitialized normally completed task may be re-invoked |
602 |
|
*/ |
603 |
|
public void testReinitialize() { |
604 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
605 |
< |
public Integer compute() { |
604 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
605 |
> |
public Integer realCompute() { |
606 |
|
FibTask f = new FibTask(8); |
607 |
< |
Integer r = f.invoke(); |
608 |
< |
threadAssertTrue(r == 21); |
609 |
< |
threadAssertTrue(f.isDone()); |
610 |
< |
threadAssertFalse(f.isCancelled()); |
611 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
612 |
< |
f.reinitialize(); |
613 |
< |
r = f.invoke(); |
614 |
< |
threadAssertTrue(r == 21); |
607 |
> |
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 |
< |
}; |
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 |
> |
return NoResult; |
642 |
> |
}}; |
643 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
644 |
|
} |
645 |
|
|
647 |
|
* invoke task throws exception after invoking completeExceptionally |
648 |
|
*/ |
649 |
|
public void testCompleteExceptionally() { |
650 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
651 |
< |
public Integer compute() { |
650 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
651 |
> |
public Integer realCompute() { |
652 |
> |
FibTask f = new FibTask(8); |
653 |
> |
f.completeExceptionally(new FJException()); |
654 |
|
try { |
543 |
– |
FibTask f = new FibTask(8); |
544 |
– |
f.completeExceptionally(new FJException()); |
655 |
|
Integer r = f.invoke(); |
656 |
|
shouldThrow(); |
547 |
– |
return r; |
657 |
|
} catch (FJException success) { |
658 |
+ |
checkTaskThrew(f, success); |
659 |
|
} |
660 |
|
return NoResult; |
661 |
< |
} |
552 |
< |
}; |
661 |
> |
}}; |
662 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
663 |
|
} |
664 |
|
|
666 |
|
* invoke task suppresses execution invoking complete |
667 |
|
*/ |
668 |
|
public void testComplete() { |
669 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
670 |
< |
public Integer compute() { |
669 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
670 |
> |
public Integer realCompute() { |
671 |
|
FibTask f = new FibTask(8); |
672 |
|
f.complete(NoResult); |
673 |
|
Integer r = f.invoke(); |
674 |
< |
threadAssertTrue(f.isDone()); |
675 |
< |
threadAssertTrue(r == NoResult); |
674 |
> |
assertSame(NoResult, r); |
675 |
> |
checkCompletedNormally(f, NoResult); |
676 |
|
return r; |
677 |
< |
} |
569 |
< |
}; |
677 |
> |
}}; |
678 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
679 |
|
} |
680 |
|
|
682 |
|
* invokeAll(t1, t2) invokes all task arguments |
683 |
|
*/ |
684 |
|
public void testInvokeAll2() { |
685 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
686 |
< |
public Integer compute() { |
685 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
686 |
> |
public Integer realCompute() { |
687 |
|
FibTask f = new FibTask(8); |
688 |
|
FibTask g = new FibTask(9); |
689 |
|
invokeAll(f, g); |
690 |
< |
threadAssertTrue(f.isDone()); |
691 |
< |
threadAssertTrue(f.join() == 21); |
584 |
< |
threadAssertTrue(g.isDone()); |
585 |
< |
threadAssertTrue(g.join() == 34); |
690 |
> |
checkCompletesNormally(f, 21); |
691 |
> |
checkCompletesNormally(g, 34); |
692 |
|
return NoResult; |
693 |
< |
} |
588 |
< |
}; |
693 |
> |
}}; |
694 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
695 |
|
} |
696 |
|
|
698 |
|
* invokeAll(tasks) with 1 argument invokes task |
699 |
|
*/ |
700 |
|
public void testInvokeAll1() { |
701 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
702 |
< |
public Integer compute() { |
701 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
702 |
> |
public Integer realCompute() { |
703 |
|
FibTask f = new FibTask(8); |
704 |
|
invokeAll(f); |
705 |
< |
threadAssertTrue(f.isDone()); |
601 |
< |
threadAssertTrue(f.join() == 21); |
705 |
> |
checkCompletesNormally(f, 21); |
706 |
|
return NoResult; |
707 |
< |
} |
604 |
< |
}; |
707 |
> |
}}; |
708 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
709 |
|
} |
710 |
|
|
712 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
713 |
|
*/ |
714 |
|
public void testInvokeAll3() { |
715 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
716 |
< |
public Integer compute() { |
715 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
716 |
> |
public Integer realCompute() { |
717 |
|
FibTask f = new FibTask(8); |
718 |
|
FibTask g = new FibTask(9); |
719 |
|
FibTask h = new FibTask(7); |
720 |
|
invokeAll(f, g, h); |
721 |
< |
threadAssertTrue(f.isDone()); |
722 |
< |
threadAssertTrue(f.join() == 21); |
723 |
< |
threadAssertTrue(g.isDone()); |
621 |
< |
threadAssertTrue(g.join() == 34); |
622 |
< |
threadAssertTrue(h.isDone()); |
623 |
< |
threadAssertTrue(h.join() == 13); |
721 |
> |
checkCompletesNormally(f, 21); |
722 |
> |
checkCompletesNormally(g, 34); |
723 |
> |
checkCompletesNormally(h, 13); |
724 |
|
return NoResult; |
725 |
< |
} |
626 |
< |
}; |
725 |
> |
}}; |
726 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
727 |
|
} |
728 |
|
|
730 |
|
* invokeAll(collection) invokes all tasks in the collection |
731 |
|
*/ |
732 |
|
public void testInvokeAllCollection() { |
733 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
734 |
< |
public Integer compute() { |
733 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
734 |
> |
public Integer realCompute() { |
735 |
|
FibTask f = new FibTask(8); |
736 |
|
FibTask g = new FibTask(9); |
737 |
|
FibTask h = new FibTask(7); |
740 |
|
set.add(g); |
741 |
|
set.add(h); |
742 |
|
invokeAll(set); |
743 |
< |
threadAssertTrue(f.isDone()); |
744 |
< |
threadAssertTrue(f.join() == 21); |
745 |
< |
threadAssertTrue(g.isDone()); |
647 |
< |
threadAssertTrue(g.join() == 34); |
648 |
< |
threadAssertTrue(h.isDone()); |
649 |
< |
threadAssertTrue(h.join() == 13); |
743 |
> |
checkCompletesNormally(f, 21); |
744 |
> |
checkCompletesNormally(g, 34); |
745 |
> |
checkCompletesNormally(h, 13); |
746 |
|
return NoResult; |
747 |
< |
} |
652 |
< |
}; |
747 |
> |
}}; |
748 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
749 |
|
} |
750 |
|
|
751 |
|
|
752 |
|
/** |
753 |
+ |
* invokeAll(tasks) with any null task throws NPE |
754 |
+ |
*/ |
755 |
+ |
public void testInvokeAllNPE() { |
756 |
+ |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
757 |
+ |
public Integer realCompute() { |
758 |
+ |
FibTask f = new FibTask(8); |
759 |
+ |
FibTask g = new FibTask(9); |
760 |
+ |
FibTask h = null; |
761 |
+ |
try { |
762 |
+ |
invokeAll(f, g, h); |
763 |
+ |
shouldThrow(); |
764 |
+ |
} catch (NullPointerException success) {} |
765 |
+ |
return NoResult; |
766 |
+ |
}}; |
767 |
+ |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
768 |
+ |
} |
769 |
+ |
|
770 |
+ |
/** |
771 |
|
* invokeAll(t1, t2) throw exception if any task does |
772 |
|
*/ |
773 |
|
public void testAbnormalInvokeAll2() { |
774 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
775 |
< |
public Integer compute() { |
774 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
775 |
> |
public Integer realCompute() { |
776 |
> |
FibTask f = new FibTask(8); |
777 |
> |
FailingFibTask g = new FailingFibTask(9); |
778 |
|
try { |
664 |
– |
FibTask f = new FibTask(8); |
665 |
– |
FailingFibTask g = new FailingFibTask(9); |
779 |
|
invokeAll(f, g); |
780 |
|
shouldThrow(); |
668 |
– |
return NoResult; |
781 |
|
} catch (FJException success) { |
782 |
+ |
checkTaskThrew(g, success); |
783 |
|
} |
784 |
|
return NoResult; |
785 |
< |
} |
673 |
< |
}; |
785 |
> |
}}; |
786 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
787 |
|
} |
788 |
|
|
790 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
791 |
|
*/ |
792 |
|
public void testAbnormalInvokeAll1() { |
793 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
794 |
< |
public Integer compute() { |
793 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
794 |
> |
public Integer realCompute() { |
795 |
> |
FailingFibTask g = new FailingFibTask(9); |
796 |
|
try { |
684 |
– |
FailingFibTask g = new FailingFibTask(9); |
797 |
|
invokeAll(g); |
798 |
|
shouldThrow(); |
687 |
– |
return NoResult; |
799 |
|
} catch (FJException success) { |
800 |
+ |
checkTaskThrew(g, success); |
801 |
|
} |
802 |
|
return NoResult; |
803 |
< |
} |
692 |
< |
}; |
803 |
> |
}}; |
804 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
805 |
|
} |
806 |
|
|
808 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
809 |
|
*/ |
810 |
|
public void testAbnormalInvokeAll3() { |
811 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
812 |
< |
public Integer compute() { |
811 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
812 |
> |
public Integer realCompute() { |
813 |
> |
FibTask f = new FibTask(8); |
814 |
> |
FailingFibTask g = new FailingFibTask(9); |
815 |
> |
FibTask h = new FibTask(7); |
816 |
|
try { |
703 |
– |
FibTask f = new FibTask(8); |
704 |
– |
FailingFibTask g = new FailingFibTask(9); |
705 |
– |
FibTask h = new FibTask(7); |
817 |
|
invokeAll(f, g, h); |
818 |
|
shouldThrow(); |
708 |
– |
return NoResult; |
819 |
|
} catch (FJException success) { |
820 |
+ |
checkTaskThrew(g, success); |
821 |
|
} |
822 |
|
return NoResult; |
823 |
< |
} |
713 |
< |
}; |
823 |
> |
}}; |
824 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
825 |
|
} |
826 |
|
|
828 |
|
* invokeAll(collection) throws exception if any task does |
829 |
|
*/ |
830 |
|
public void testAbnormalInvokeAllCollection() { |
831 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
832 |
< |
public Integer compute() { |
831 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
832 |
> |
public Integer realCompute() { |
833 |
> |
FailingFibTask f = new FailingFibTask(8); |
834 |
> |
FibTask g = new FibTask(9); |
835 |
> |
FibTask h = new FibTask(7); |
836 |
> |
HashSet set = new HashSet(); |
837 |
> |
set.add(f); |
838 |
> |
set.add(g); |
839 |
> |
set.add(h); |
840 |
|
try { |
724 |
– |
FailingFibTask f = new FailingFibTask(8); |
725 |
– |
FibTask g = new FibTask(9); |
726 |
– |
FibTask h = new FibTask(7); |
727 |
– |
HashSet set = new HashSet(); |
728 |
– |
set.add(f); |
729 |
– |
set.add(g); |
730 |
– |
set.add(h); |
841 |
|
invokeAll(set); |
842 |
|
shouldThrow(); |
733 |
– |
return NoResult; |
843 |
|
} catch (FJException success) { |
844 |
+ |
checkTaskThrew(f, success); |
845 |
|
} |
846 |
|
return NoResult; |
847 |
< |
} |
738 |
< |
}; |
847 |
> |
}}; |
848 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
849 |
|
} |
850 |
|
|
853 |
|
* and suppresses execution |
854 |
|
*/ |
855 |
|
public void testTryUnfork() { |
856 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
857 |
< |
public Integer compute() { |
856 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
857 |
> |
public Integer realCompute() { |
858 |
|
FibTask g = new FibTask(9); |
859 |
< |
threadAssertSame(g, g.fork()); |
859 |
> |
assertSame(g, g.fork()); |
860 |
|
FibTask f = new FibTask(8); |
861 |
< |
threadAssertSame(f, f.fork()); |
862 |
< |
threadAssertTrue(f.tryUnfork()); |
861 |
> |
assertSame(f, f.fork()); |
862 |
> |
assertTrue(f.tryUnfork()); |
863 |
|
helpQuiesce(); |
864 |
< |
threadAssertFalse(f.isDone()); |
865 |
< |
threadAssertTrue(g.isDone()); |
864 |
> |
checkNotDone(f); |
865 |
> |
checkCompletedNormally(g, 34); |
866 |
|
return NoResult; |
867 |
< |
} |
759 |
< |
}; |
867 |
> |
}}; |
868 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
869 |
|
} |
870 |
|
|
873 |
|
* there are more tasks than threads |
874 |
|
*/ |
875 |
|
public void testGetSurplusQueuedTaskCount() { |
876 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
877 |
< |
public Integer compute() { |
876 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
877 |
> |
public Integer realCompute() { |
878 |
|
FibTask h = new FibTask(7); |
879 |
< |
threadAssertSame(h, h.fork()); |
879 |
> |
assertSame(h, h.fork()); |
880 |
|
FibTask g = new FibTask(9); |
881 |
< |
threadAssertSame(g, g.fork()); |
881 |
> |
assertSame(g, g.fork()); |
882 |
|
FibTask f = new FibTask(8); |
883 |
< |
threadAssertSame(f, f.fork()); |
884 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
883 |
> |
assertSame(f, f.fork()); |
884 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
885 |
|
helpQuiesce(); |
886 |
+ |
checkCompletedNormally(f, 21); |
887 |
+ |
checkCompletedNormally(g, 34); |
888 |
+ |
checkCompletedNormally(h, 13); |
889 |
|
return NoResult; |
890 |
< |
} |
780 |
< |
}; |
890 |
> |
}}; |
891 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
892 |
|
} |
893 |
|
|
895 |
|
* peekNextLocalTask returns most recent unexecuted task. |
896 |
|
*/ |
897 |
|
public void testPeekNextLocalTask() { |
898 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
899 |
< |
public Integer compute() { |
898 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
899 |
> |
public Integer realCompute() { |
900 |
|
FibTask g = new FibTask(9); |
901 |
< |
threadAssertSame(g, g.fork()); |
901 |
> |
assertSame(g, g.fork()); |
902 |
|
FibTask f = new FibTask(8); |
903 |
< |
threadAssertSame(f, f.fork()); |
904 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
905 |
< |
threadAssertTrue(f.join() == 21); |
796 |
< |
threadAssertTrue(f.isDone()); |
903 |
> |
assertSame(f, f.fork()); |
904 |
> |
assertSame(f, peekNextLocalTask()); |
905 |
> |
checkCompletesNormally(f, 21); |
906 |
|
helpQuiesce(); |
907 |
+ |
checkCompletedNormally(g, 34); |
908 |
|
return NoResult; |
909 |
< |
} |
800 |
< |
}; |
909 |
> |
}}; |
910 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
911 |
|
} |
912 |
|
|
915 |
|
* without executing it |
916 |
|
*/ |
917 |
|
public void testPollNextLocalTask() { |
918 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
919 |
< |
public Integer compute() { |
918 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
919 |
> |
public Integer realCompute() { |
920 |
|
FibTask g = new FibTask(9); |
921 |
< |
threadAssertSame(g, g.fork()); |
921 |
> |
assertSame(g, g.fork()); |
922 |
|
FibTask f = new FibTask(8); |
923 |
< |
threadAssertSame(f, f.fork()); |
924 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
923 |
> |
assertSame(f, f.fork()); |
924 |
> |
assertSame(f, pollNextLocalTask()); |
925 |
|
helpQuiesce(); |
926 |
< |
threadAssertFalse(f.isDone()); |
926 |
> |
checkNotDone(f); |
927 |
> |
checkCompletedNormally(g, 34); |
928 |
|
return NoResult; |
929 |
< |
} |
820 |
< |
}; |
929 |
> |
}}; |
930 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
931 |
|
} |
932 |
|
|
934 |
|
* pollTask returns an unexecuted task without executing it |
935 |
|
*/ |
936 |
|
public void testPollTask() { |
937 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
938 |
< |
public Integer compute() { |
937 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
938 |
> |
public Integer realCompute() { |
939 |
|
FibTask g = new FibTask(9); |
940 |
< |
threadAssertSame(g, g.fork()); |
940 |
> |
assertSame(g, g.fork()); |
941 |
|
FibTask f = new FibTask(8); |
942 |
< |
threadAssertSame(f, f.fork()); |
943 |
< |
threadAssertTrue(pollTask() == f); |
942 |
> |
assertSame(f, f.fork()); |
943 |
> |
assertSame(f, pollTask()); |
944 |
|
helpQuiesce(); |
945 |
< |
threadAssertFalse(f.isDone()); |
946 |
< |
threadAssertTrue(g.isDone()); |
945 |
> |
checkNotDone(f); |
946 |
> |
checkCompletedNormally(g, 34); |
947 |
|
return NoResult; |
948 |
< |
} |
840 |
< |
}; |
948 |
> |
}}; |
949 |
|
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
950 |
|
} |
951 |
|
|
953 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
954 |
|
*/ |
955 |
|
public void testPeekNextLocalTaskAsync() { |
956 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
957 |
< |
public Integer compute() { |
956 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
957 |
> |
public Integer realCompute() { |
958 |
|
FibTask g = new FibTask(9); |
959 |
< |
threadAssertSame(g, g.fork()); |
959 |
> |
assertSame(g, g.fork()); |
960 |
|
FibTask f = new FibTask(8); |
961 |
< |
threadAssertSame(f, f.fork()); |
962 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
963 |
< |
threadAssertEquals(21, (int) f.join()); |
961 |
> |
assertSame(f, f.fork()); |
962 |
> |
assertSame(g, peekNextLocalTask()); |
963 |
> |
assertEquals(21, (int) f.join()); |
964 |
|
helpQuiesce(); |
965 |
< |
threadAssertTrue(f.isDone()); |
965 |
> |
checkCompletedNormally(f, 21); |
966 |
> |
checkCompletedNormally(g, 34); |
967 |
|
return NoResult; |
968 |
< |
} |
860 |
< |
}; |
968 |
> |
}}; |
969 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
970 |
|
} |
971 |
|
|
972 |
|
/** |
973 |
< |
* pollNextLocalTask returns least recent unexecuted task |
974 |
< |
* without executing it, in async mode |
973 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
974 |
> |
* executing it, in async mode |
975 |
|
*/ |
976 |
|
public void testPollNextLocalTaskAsync() { |
977 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
978 |
< |
public Integer compute() { |
977 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
978 |
> |
public Integer realCompute() { |
979 |
|
FibTask g = new FibTask(9); |
980 |
< |
threadAssertSame(g, g.fork()); |
980 |
> |
assertSame(g, g.fork()); |
981 |
|
FibTask f = new FibTask(8); |
982 |
< |
threadAssertSame(f, f.fork()); |
983 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
982 |
> |
assertSame(f, f.fork()); |
983 |
> |
assertSame(g, pollNextLocalTask()); |
984 |
|
helpQuiesce(); |
985 |
< |
threadAssertTrue(f.isDone()); |
986 |
< |
threadAssertFalse(g.isDone()); |
985 |
> |
checkCompletedNormally(f, 21); |
986 |
> |
checkNotDone(g); |
987 |
|
return NoResult; |
988 |
< |
} |
881 |
< |
}; |
988 |
> |
}}; |
989 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
990 |
|
} |
991 |
|
|
992 |
|
/** |
993 |
< |
* pollTask returns an unexecuted task |
994 |
< |
* without executing it, in async mode |
993 |
> |
* pollTask returns an unexecuted task without executing it, in |
994 |
> |
* async mode |
995 |
|
*/ |
996 |
|
public void testPollTaskAsync() { |
997 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
998 |
< |
public Integer compute() { |
997 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
998 |
> |
public Integer realCompute() { |
999 |
|
FibTask g = new FibTask(9); |
1000 |
< |
threadAssertSame(g, g.fork()); |
1000 |
> |
assertSame(g, g.fork()); |
1001 |
|
FibTask f = new FibTask(8); |
1002 |
< |
threadAssertSame(f, f.fork()); |
1003 |
< |
threadAssertTrue(pollTask() == g); |
1002 |
> |
assertSame(f, f.fork()); |
1003 |
> |
assertSame(g, pollTask()); |
1004 |
|
helpQuiesce(); |
1005 |
< |
threadAssertTrue(f.isDone()); |
1006 |
< |
threadAssertFalse(g.isDone()); |
1005 |
> |
checkCompletedNormally(f, 21); |
1006 |
> |
checkNotDone(g); |
1007 |
|
return NoResult; |
1008 |
< |
} |
902 |
< |
}; |
1008 |
> |
}}; |
1009 |
|
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1010 |
|
} |
1011 |
|
|