3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
– |
import junit.framework.*; |
7 |
– |
import java.util.concurrent.*; |
8 |
– |
import java.util.*; |
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.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 { |
19 |
|
|
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
21 |
> |
junit.textui.TestRunner.run(suite()); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(RecursiveTaskTest.class); |
24 |
> |
return new TestSuite(RecursiveTaskTest.class); |
25 |
> |
} |
26 |
> |
|
27 |
> |
private static ForkJoinPool mainPool() { |
28 |
> |
return new ForkJoinPool(); |
29 |
> |
} |
30 |
> |
|
31 |
> |
private static ForkJoinPool singletonPool() { |
32 |
> |
return new ForkJoinPool(1); |
33 |
> |
} |
34 |
> |
|
35 |
> |
private static ForkJoinPool asyncSingletonPool() { |
36 |
> |
return new ForkJoinPool(1, |
37 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
38 |
> |
null, true); |
39 |
|
} |
40 |
|
|
41 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
42 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
43 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
44 |
< |
static { |
45 |
< |
asyncSingletonPool.setAsyncMode(true); |
41 |
> |
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
42 |
> |
try { |
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 checkCompletedAbnormally(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 { |
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; } |
220 |
|
* isCompletedAbnormally and isCancelled return false for normally |
221 |
|
* completed tasks. getRawResult of a completed non-null task |
222 |
|
* returns value; |
68 |
– |
* |
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()); |
77 |
< |
threadAssertFalse(f.isCancelled()); |
78 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
79 |
< |
threadAssertTrue(f.getRawResult() == 21); |
229 |
> |
assertEquals(21, (int) r); |
230 |
> |
checkCompletedNormally(f, r); |
231 |
|
return r; |
232 |
< |
} |
233 |
< |
}; |
83 |
< |
assertTrue(mainPool.invoke(a) == 21); |
232 |
> |
}}; |
233 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
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()); |
100 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
101 |
< |
return r; |
102 |
< |
} |
103 |
< |
}; |
104 |
< |
assertTrue(mainPool.invoke(a) == 21); |
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 |
< |
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 |
< |
} |
265 |
< |
}; |
121 |
< |
assertTrue(mainPool.invoke(a) == 21); |
264 |
> |
}}; |
265 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
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 |
< |
f.fork(); |
277 |
< |
Integer r = f.get(); |
278 |
< |
threadAssertTrue(r == 21); |
279 |
< |
threadAssertTrue(f.isDone()); |
280 |
< |
return r; |
281 |
< |
} catch (Exception ex) { |
138 |
< |
unexpectedException(ex); |
139 |
< |
} |
140 |
< |
return NoResult; |
141 |
< |
} |
142 |
< |
}; |
143 |
< |
assertTrue(mainPool.invoke(a) == 21); |
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 |
|
|
284 |
|
/** |
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() { |
152 |
< |
try { |
153 |
< |
FibTask f = new FibTask(8); |
154 |
< |
f.fork(); |
155 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
156 |
< |
threadAssertTrue(r == 21); |
157 |
< |
threadAssertTrue(f.isDone()); |
158 |
< |
return r; |
159 |
< |
} catch (Exception ex) { |
160 |
< |
unexpectedException(ex); |
161 |
< |
} |
162 |
< |
return NoResult; |
163 |
< |
} |
164 |
< |
}; |
165 |
< |
assertTrue(mainPool.invoke(a) == 21); |
166 |
< |
} |
167 |
< |
|
168 |
< |
/** |
169 |
< |
* helpJoin of a forked task returns when task completes |
170 |
< |
*/ |
171 |
< |
public void testForkHelpJoin() { |
172 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
173 |
< |
public Integer compute() { |
288 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
289 |
> |
public Integer realCompute() throws Exception { |
290 |
|
FibTask f = new FibTask(8); |
291 |
< |
f.fork(); |
292 |
< |
Integer r = f.helpJoin(); |
293 |
< |
threadAssertTrue(r == 21); |
294 |
< |
threadAssertTrue(f.isDone()); |
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 |
< |
}; |
182 |
< |
assertTrue(mainPool.invoke(a) == 21); |
296 |
> |
}}; |
297 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
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 |
< |
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 |
< |
} |
314 |
< |
}; |
200 |
< |
assertTrue(mainPool.invoke(a) == 21); |
201 |
< |
} |
202 |
< |
|
203 |
< |
|
204 |
< |
/** |
205 |
< |
* quietlyHelpJoin of a forked task returns when task completes |
206 |
< |
*/ |
207 |
< |
public void testForkQuietlyHelpJoin() { |
208 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
209 |
< |
public Integer compute() { |
210 |
< |
FibTask f = new FibTask(8); |
211 |
< |
f.fork(); |
212 |
< |
f.quietlyHelpJoin(); |
213 |
< |
Integer r = f.getRawResult(); |
214 |
< |
threadAssertTrue(r == 21); |
215 |
< |
threadAssertTrue(f.isDone()); |
216 |
< |
return r; |
217 |
< |
} |
218 |
< |
}; |
219 |
< |
assertTrue(mainPool.invoke(a) == 21); |
313 |
> |
}}; |
314 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
315 |
|
} |
316 |
|
|
317 |
|
|
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 |
< |
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; |
238 |
< |
} |
239 |
< |
}; |
240 |
< |
assertTrue(mainPool.invoke(a) == 21); |
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 { |
251 |
– |
FailingFibTask f = new FailingFibTask(8); |
344 |
|
f.invoke(); |
345 |
|
shouldThrow(); |
254 |
– |
return NoResult; |
346 |
|
} catch (FJException success) { |
347 |
+ |
checkCompletedAbnormally(f, success); |
348 |
|
} |
349 |
|
return NoResult; |
350 |
< |
} |
351 |
< |
}; |
260 |
< |
mainPool.invoke(a); |
350 |
> |
}}; |
351 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
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 |
> |
checkCompletedAbnormally(f, f.getException()); |
364 |
|
return NoResult; |
365 |
< |
} |
366 |
< |
}; |
275 |
< |
mainPool.invoke(a); |
365 |
> |
}}; |
366 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
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 { |
285 |
– |
FailingFibTask f = new FailingFibTask(8); |
286 |
– |
f.fork(); |
378 |
|
Integer r = f.join(); |
379 |
|
shouldThrow(); |
289 |
– |
return r; |
380 |
|
} catch (FJException success) { |
381 |
+ |
checkCompletedAbnormally(f, success); |
382 |
|
} |
383 |
|
return NoResult; |
384 |
< |
} |
385 |
< |
}; |
295 |
< |
mainPool.invoke(a); |
384 |
> |
}}; |
385 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
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 { |
305 |
– |
FailingFibTask f = new FailingFibTask(8); |
306 |
– |
f.fork(); |
397 |
|
Integer r = f.get(); |
398 |
|
shouldThrow(); |
309 |
– |
return r; |
399 |
|
} catch (ExecutionException success) { |
400 |
< |
} catch (Exception ex) { |
401 |
< |
unexpectedException(ex); |
400 |
> |
Throwable cause = success.getCause(); |
401 |
> |
assertTrue(cause instanceof FJException); |
402 |
> |
checkCompletedAbnormally(f, cause); |
403 |
|
} |
404 |
|
return NoResult; |
405 |
< |
} |
406 |
< |
}; |
317 |
< |
mainPool.invoke(a); |
405 |
> |
}}; |
406 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
|
* timed get of a forked task throws exception when task completes abnormally |
411 |
|
*/ |
412 |
|
public void testAbnormalForkTimedGet() { |
413 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
414 |
< |
public Integer compute() { |
413 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
414 |
> |
public Integer realCompute() throws Exception { |
415 |
> |
FailingFibTask f = new FailingFibTask(8); |
416 |
> |
assertSame(f, f.fork()); |
417 |
|
try { |
418 |
< |
FailingFibTask f = new FailingFibTask(8); |
328 |
< |
f.fork(); |
329 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
418 |
> |
Integer r = f.get(5L, SECONDS); |
419 |
|
shouldThrow(); |
331 |
– |
return r; |
420 |
|
} catch (ExecutionException success) { |
421 |
< |
} catch (Exception ex) { |
422 |
< |
unexpectedException(ex); |
421 |
> |
Throwable cause = success.getCause(); |
422 |
> |
assertTrue(cause instanceof FJException); |
423 |
> |
checkCompletedAbnormally(f, cause); |
424 |
|
} |
425 |
|
return NoResult; |
426 |
< |
} |
427 |
< |
}; |
339 |
< |
mainPool.invoke(a); |
340 |
< |
} |
341 |
< |
|
342 |
< |
/** |
343 |
< |
* join of a forked task throws exception when task completes abnormally |
344 |
< |
*/ |
345 |
< |
public void testAbnormalForkHelpJoin() { |
346 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
347 |
< |
public Integer compute() { |
348 |
< |
try { |
349 |
< |
FailingFibTask f = new FailingFibTask(8); |
350 |
< |
f.fork(); |
351 |
< |
Integer r = f.helpJoin(); |
352 |
< |
shouldThrow(); |
353 |
< |
return r; |
354 |
< |
} catch (FJException success) { |
355 |
< |
} |
356 |
< |
return NoResult; |
357 |
< |
} |
358 |
< |
}; |
359 |
< |
mainPool.invoke(a); |
360 |
< |
} |
361 |
< |
|
362 |
< |
/** |
363 |
< |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
364 |
< |
* getException of failed task returns its exception. |
365 |
< |
* isCompletedAbnormally of a failed task returns true. |
366 |
< |
* isCancelled of a failed uncancelled task returns false |
367 |
< |
*/ |
368 |
< |
public void testAbnormalForkQuietlyHelpJoin() { |
369 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
370 |
< |
public Integer compute() { |
371 |
< |
FailingFibTask f = new FailingFibTask(8); |
372 |
< |
f.fork(); |
373 |
< |
f.quietlyHelpJoin(); |
374 |
< |
threadAssertTrue(f.isDone()); |
375 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
376 |
< |
threadAssertFalse(f.isCancelled()); |
377 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
378 |
< |
return NoResult; |
379 |
< |
} |
380 |
< |
}; |
381 |
< |
mainPool.invoke(a); |
426 |
> |
}}; |
427 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
432 |
|
*/ |
433 |
|
public void testAbnormalForkQuietlyJoin() { |
434 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
435 |
< |
public Integer compute() { |
434 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
435 |
> |
public Integer realCompute() { |
436 |
|
FailingFibTask f = new FailingFibTask(8); |
437 |
< |
f.fork(); |
437 |
> |
assertSame(f, f.fork()); |
438 |
|
f.quietlyJoin(); |
439 |
< |
threadAssertTrue(f.isDone()); |
440 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
395 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
439 |
> |
assertTrue(f.getException() instanceof FJException); |
440 |
> |
checkCompletedAbnormally(f, f.getException()); |
441 |
|
return NoResult; |
442 |
< |
} |
443 |
< |
}; |
399 |
< |
mainPool.invoke(a); |
442 |
> |
}}; |
443 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
|
* invoke task throws exception when task cancelled |
448 |
|
*/ |
449 |
|
public void testCancelledInvoke() { |
450 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
451 |
< |
public Integer compute() { |
450 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
451 |
> |
public Integer realCompute() { |
452 |
> |
FibTask f = new FibTask(8); |
453 |
> |
assertTrue(f.cancel(true)); |
454 |
|
try { |
409 |
– |
FibTask f = new FibTask(8); |
410 |
– |
f.cancel(true); |
455 |
|
Integer r = f.invoke(); |
456 |
|
shouldThrow(); |
413 |
– |
return r; |
457 |
|
} catch (CancellationException success) { |
458 |
+ |
checkCancelled(f); |
459 |
|
} |
460 |
|
return NoResult; |
461 |
< |
} |
462 |
< |
}; |
419 |
< |
mainPool.invoke(a); |
461 |
> |
}}; |
462 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
|
* join of a forked task throws exception when task cancelled |
467 |
|
*/ |
468 |
|
public void testCancelledForkJoin() { |
469 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
470 |
< |
public Integer compute() { |
469 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
470 |
> |
public Integer realCompute() { |
471 |
> |
FibTask f = new FibTask(8); |
472 |
> |
assertTrue(f.cancel(true)); |
473 |
> |
assertSame(f, f.fork()); |
474 |
|
try { |
429 |
– |
FibTask f = new FibTask(8); |
430 |
– |
f.cancel(true); |
431 |
– |
f.fork(); |
475 |
|
Integer r = f.join(); |
476 |
|
shouldThrow(); |
434 |
– |
return r; |
477 |
|
} catch (CancellationException success) { |
478 |
+ |
checkCancelled(f); |
479 |
|
} |
480 |
|
return NoResult; |
481 |
< |
} |
482 |
< |
}; |
440 |
< |
mainPool.invoke(a); |
481 |
> |
}}; |
482 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
|
* get of a forked task throws exception when task cancelled |
487 |
|
*/ |
488 |
|
public void testCancelledForkGet() { |
489 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
490 |
< |
public Integer compute() { |
489 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
490 |
> |
public Integer realCompute() throws Exception { |
491 |
> |
FibTask f = new FibTask(8); |
492 |
> |
assertTrue(f.cancel(true)); |
493 |
> |
assertSame(f, f.fork()); |
494 |
|
try { |
450 |
– |
FibTask f = new FibTask(8); |
451 |
– |
f.cancel(true); |
452 |
– |
f.fork(); |
495 |
|
Integer r = f.get(); |
496 |
|
shouldThrow(); |
455 |
– |
return r; |
497 |
|
} catch (CancellationException success) { |
498 |
< |
} catch (Exception ex) { |
458 |
< |
unexpectedException(ex); |
498 |
> |
checkCancelled(f); |
499 |
|
} |
500 |
|
return NoResult; |
501 |
< |
} |
502 |
< |
}; |
463 |
< |
mainPool.invoke(a); |
501 |
> |
}}; |
502 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
|
* timed get of a forked task throws exception when task cancelled |
507 |
|
*/ |
508 |
|
public void testCancelledForkTimedGet() { |
509 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
510 |
< |
public Integer compute() { |
509 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
510 |
> |
public Integer realCompute() throws Exception { |
511 |
> |
FibTask f = new FibTask(8); |
512 |
> |
assertTrue(f.cancel(true)); |
513 |
> |
assertSame(f, f.fork()); |
514 |
|
try { |
515 |
< |
FibTask f = new FibTask(8); |
474 |
< |
f.cancel(true); |
475 |
< |
f.fork(); |
476 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
515 |
> |
Integer r = f.get(5L, SECONDS); |
516 |
|
shouldThrow(); |
478 |
– |
return r; |
517 |
|
} catch (CancellationException success) { |
518 |
< |
} catch (Exception ex) { |
481 |
< |
unexpectedException(ex); |
518 |
> |
checkCancelled(f); |
519 |
|
} |
520 |
|
return NoResult; |
521 |
< |
} |
522 |
< |
}; |
486 |
< |
mainPool.invoke(a); |
487 |
< |
} |
488 |
< |
|
489 |
< |
/** |
490 |
< |
* join of a forked task throws exception when task cancelled |
491 |
< |
*/ |
492 |
< |
public void testCancelledForkHelpJoin() { |
493 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
494 |
< |
public Integer compute() { |
495 |
< |
try { |
496 |
< |
FibTask f = new FibTask(8); |
497 |
< |
f.cancel(true); |
498 |
< |
f.fork(); |
499 |
< |
Integer r = f.helpJoin(); |
500 |
< |
shouldThrow(); |
501 |
< |
return r; |
502 |
< |
} catch (CancellationException success) { |
503 |
< |
} |
504 |
< |
return NoResult; |
505 |
< |
} |
506 |
< |
}; |
507 |
< |
mainPool.invoke(a); |
508 |
< |
} |
509 |
< |
|
510 |
< |
/** |
511 |
< |
* quietlyHelpJoin of a forked task returns when task cancelled. |
512 |
< |
* getException of cancelled task returns its exception |
513 |
< |
* isCompletedAbnormally of a cancelled task returns true. |
514 |
< |
* isCancelled of a cancelled task returns true |
515 |
< |
*/ |
516 |
< |
public void testCancelledForkQuietlyHelpJoin() { |
517 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
518 |
< |
public Integer compute() { |
519 |
< |
FibTask f = new FibTask(8); |
520 |
< |
f.cancel(true); |
521 |
< |
f.fork(); |
522 |
< |
f.quietlyHelpJoin(); |
523 |
< |
threadAssertTrue(f.isDone()); |
524 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
525 |
< |
threadAssertTrue(f.isCancelled()); |
526 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
527 |
< |
return NoResult; |
528 |
< |
} |
529 |
< |
}; |
530 |
< |
mainPool.invoke(a); |
521 |
> |
}}; |
522 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
523 |
|
} |
524 |
|
|
525 |
|
/** |
526 |
|
* quietlyJoin of a forked task returns when task cancelled |
527 |
|
*/ |
528 |
|
public void testCancelledForkQuietlyJoin() { |
529 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
530 |
< |
public Integer compute() { |
529 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
530 |
> |
public Integer realCompute() { |
531 |
|
FibTask f = new FibTask(8); |
532 |
< |
f.cancel(true); |
533 |
< |
f.fork(); |
532 |
> |
assertTrue(f.cancel(true)); |
533 |
> |
assertSame(f, f.fork()); |
534 |
|
f.quietlyJoin(); |
535 |
< |
threadAssertTrue(f.isDone()); |
544 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
545 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
535 |
> |
checkCancelled(f); |
536 |
|
return NoResult; |
537 |
< |
} |
538 |
< |
}; |
549 |
< |
mainPool.invoke(a); |
537 |
> |
}}; |
538 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
539 |
|
} |
540 |
|
|
541 |
|
/** |
542 |
|
* getPool of executing task returns its pool |
543 |
|
*/ |
544 |
|
public void testGetPool() { |
545 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
546 |
< |
public Integer compute() { |
547 |
< |
threadAssertTrue(getPool() == mainPool); |
545 |
> |
final ForkJoinPool mainPool = mainPool(); |
546 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
547 |
> |
public Integer realCompute() { |
548 |
> |
assertSame(mainPool, getPool()); |
549 |
|
return NoResult; |
550 |
< |
} |
551 |
< |
}; |
562 |
< |
mainPool.invoke(a); |
550 |
> |
}}; |
551 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool, a)); |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
|
* getPool of non-FJ task returns null |
556 |
|
*/ |
557 |
|
public void testGetPool2() { |
558 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
559 |
< |
public Integer compute() { |
560 |
< |
threadAssertTrue(getPool() == null); |
558 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
559 |
> |
public Integer realCompute() { |
560 |
> |
assertNull(getPool()); |
561 |
|
return NoResult; |
562 |
< |
} |
563 |
< |
}; |
575 |
< |
a.invoke(); |
562 |
> |
}}; |
563 |
> |
assertSame(NoResult, a.invoke()); |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
|
* inForkJoinPool of executing task returns true |
568 |
|
*/ |
569 |
|
public void testInForkJoinPool() { |
570 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
571 |
< |
public Integer compute() { |
572 |
< |
threadAssertTrue(inForkJoinPool()); |
570 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
571 |
> |
public Integer realCompute() { |
572 |
> |
assertTrue(inForkJoinPool()); |
573 |
|
return NoResult; |
574 |
< |
} |
575 |
< |
}; |
588 |
< |
mainPool.invoke(a); |
574 |
> |
}}; |
575 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
579 |
|
* inForkJoinPool of non-FJ task returns false |
580 |
|
*/ |
581 |
|
public void testInForkJoinPool2() { |
582 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
583 |
< |
public Integer compute() { |
584 |
< |
threadAssertTrue(!inForkJoinPool()); |
582 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
583 |
> |
public Integer realCompute() { |
584 |
> |
assertFalse(inForkJoinPool()); |
585 |
|
return NoResult; |
586 |
< |
} |
587 |
< |
}; |
601 |
< |
a.invoke(); |
586 |
> |
}}; |
587 |
> |
assertSame(NoResult, a.invoke()); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
< |
* setRawResult(null) succeeds |
591 |
> |
* The value set by setRawResult is returned by getRawResult |
592 |
|
*/ |
593 |
|
public void testSetRawResult() { |
594 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
595 |
< |
public Integer compute() { |
594 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
595 |
> |
public Integer realCompute() { |
596 |
|
setRawResult(NoResult); |
597 |
+ |
assertSame(NoResult, getRawResult()); |
598 |
|
return NoResult; |
599 |
|
} |
600 |
|
}; |
601 |
< |
assertEquals(a.invoke(), NoResult); |
601 |
> |
a.invoke(); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
< |
* A reinitialized task may be re-invoked |
605 |
> |
* A reinitialized normally completed task may be re-invoked |
606 |
|
*/ |
607 |
|
public void testReinitialize() { |
608 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
609 |
< |
public Integer compute() { |
608 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
609 |
> |
public Integer realCompute() { |
610 |
|
FibTask f = new FibTask(8); |
611 |
< |
Integer r = f.invoke(); |
612 |
< |
threadAssertTrue(r == 21); |
613 |
< |
threadAssertTrue(f.isDone()); |
614 |
< |
threadAssertFalse(f.isCancelled()); |
615 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
616 |
< |
f.reinitialize(); |
617 |
< |
r = f.invoke(); |
618 |
< |
threadAssertTrue(r == 21); |
611 |
> |
checkNotDone(f); |
612 |
> |
|
613 |
> |
for (int i = 0; i < 3; i++) { |
614 |
> |
Integer r = f.invoke(); |
615 |
> |
assertEquals(21, (int) r); |
616 |
> |
checkCompletedNormally(f, r); |
617 |
> |
f.reinitialize(); |
618 |
> |
f.publicSetRawResult(null); |
619 |
> |
checkNotDone(f); |
620 |
> |
} |
621 |
|
return NoResult; |
622 |
< |
} |
623 |
< |
}; |
624 |
< |
mainPool.invoke(a); |
622 |
> |
}}; |
623 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
624 |
> |
} |
625 |
> |
|
626 |
> |
/** |
627 |
> |
* A reinitialized abnormally completed task may be re-invoked |
628 |
> |
*/ |
629 |
> |
public void testReinitializeAbnormal() { |
630 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
631 |
> |
public Integer realCompute() { |
632 |
> |
FailingFibTask f = new FailingFibTask(8); |
633 |
> |
checkNotDone(f); |
634 |
> |
|
635 |
> |
for (int i = 0; i < 3; i++) { |
636 |
> |
try { |
637 |
> |
f.invoke(); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (FJException success) { |
640 |
> |
checkCompletedAbnormally(f, success); |
641 |
> |
} |
642 |
> |
f.reinitialize(); |
643 |
> |
checkNotDone(f); |
644 |
> |
} |
645 |
> |
return NoResult; |
646 |
> |
}}; |
647 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
|
* invoke task throws exception after invoking completeExceptionally |
652 |
|
*/ |
653 |
|
public void testCompleteExceptionally() { |
654 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
655 |
< |
public Integer compute() { |
654 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
655 |
> |
public Integer realCompute() { |
656 |
> |
FibTask f = new FibTask(8); |
657 |
> |
f.completeExceptionally(new FJException()); |
658 |
|
try { |
645 |
– |
FibTask f = new FibTask(8); |
646 |
– |
f.completeExceptionally(new FJException()); |
659 |
|
Integer r = f.invoke(); |
660 |
|
shouldThrow(); |
649 |
– |
return r; |
661 |
|
} catch (FJException success) { |
662 |
+ |
checkCompletedAbnormally(f, success); |
663 |
|
} |
664 |
|
return NoResult; |
665 |
< |
} |
666 |
< |
}; |
655 |
< |
mainPool.invoke(a); |
665 |
> |
}}; |
666 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
|
* invoke task suppresses execution invoking complete |
671 |
|
*/ |
672 |
|
public void testComplete() { |
673 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
674 |
< |
public Integer compute() { |
673 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
674 |
> |
public Integer realCompute() { |
675 |
|
FibTask f = new FibTask(8); |
676 |
|
f.complete(NoResult); |
677 |
|
Integer r = f.invoke(); |
678 |
< |
threadAssertTrue(f.isDone()); |
679 |
< |
threadAssertTrue(r == NoResult); |
678 |
> |
assertSame(NoResult, r); |
679 |
> |
checkCompletedNormally(f, NoResult); |
680 |
|
return r; |
681 |
< |
} |
682 |
< |
}; |
672 |
< |
mainPool.invoke(a); |
681 |
> |
}}; |
682 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* invokeAll(t1, t2) invokes all task arguments |
687 |
|
*/ |
688 |
|
public void testInvokeAll2() { |
689 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
690 |
< |
public Integer compute() { |
689 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
690 |
> |
public Integer realCompute() { |
691 |
|
FibTask f = new FibTask(8); |
692 |
|
FibTask g = new FibTask(9); |
693 |
|
invokeAll(f, g); |
694 |
< |
threadAssertTrue(f.isDone()); |
695 |
< |
threadAssertTrue(f.join() == 21); |
686 |
< |
threadAssertTrue(g.isDone()); |
687 |
< |
threadAssertTrue(g.join() == 34); |
694 |
> |
checkCompletedNormally(f, 21); |
695 |
> |
checkCompletedNormally(g, 34); |
696 |
|
return NoResult; |
697 |
< |
} |
698 |
< |
}; |
691 |
< |
mainPool.invoke(a); |
697 |
> |
}}; |
698 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
|
* invokeAll(tasks) with 1 argument invokes task |
703 |
|
*/ |
704 |
|
public void testInvokeAll1() { |
705 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
706 |
< |
public Integer compute() { |
705 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
706 |
> |
public Integer realCompute() { |
707 |
|
FibTask f = new FibTask(8); |
708 |
|
invokeAll(f); |
709 |
< |
threadAssertTrue(f.isDone()); |
703 |
< |
threadAssertTrue(f.join() == 21); |
709 |
> |
checkCompletedNormally(f, 21); |
710 |
|
return NoResult; |
711 |
< |
} |
712 |
< |
}; |
707 |
< |
mainPool.invoke(a); |
711 |
> |
}}; |
712 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
713 |
|
} |
714 |
|
|
715 |
|
/** |
716 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
717 |
|
*/ |
718 |
|
public void testInvokeAll3() { |
719 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
720 |
< |
public Integer compute() { |
719 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
720 |
> |
public Integer realCompute() { |
721 |
|
FibTask f = new FibTask(8); |
722 |
|
FibTask g = new FibTask(9); |
723 |
|
FibTask h = new FibTask(7); |
724 |
|
invokeAll(f, g, h); |
725 |
< |
threadAssertTrue(f.isDone()); |
726 |
< |
threadAssertTrue(f.join() == 21); |
727 |
< |
threadAssertTrue(g.isDone()); |
728 |
< |
threadAssertTrue(g.join() == 34); |
729 |
< |
threadAssertTrue(h.isDone()); |
730 |
< |
threadAssertTrue(h.join() == 13); |
725 |
> |
assertTrue(f.isDone()); |
726 |
> |
assertTrue(g.isDone()); |
727 |
> |
assertTrue(h.isDone()); |
728 |
> |
checkCompletedNormally(f, 21); |
729 |
> |
checkCompletedNormally(g, 34); |
730 |
> |
checkCompletedNormally(h, 13); |
731 |
|
return NoResult; |
732 |
< |
} |
733 |
< |
}; |
729 |
< |
mainPool.invoke(a); |
732 |
> |
}}; |
733 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
|
* invokeAll(collection) invokes all tasks in the collection |
738 |
|
*/ |
739 |
|
public void testInvokeAllCollection() { |
740 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
741 |
< |
public Integer compute() { |
740 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
741 |
> |
public Integer realCompute() { |
742 |
|
FibTask f = new FibTask(8); |
743 |
|
FibTask g = new FibTask(9); |
744 |
|
FibTask h = new FibTask(7); |
747 |
|
set.add(g); |
748 |
|
set.add(h); |
749 |
|
invokeAll(set); |
750 |
< |
threadAssertTrue(f.isDone()); |
751 |
< |
threadAssertTrue(f.join() == 21); |
752 |
< |
threadAssertTrue(g.isDone()); |
753 |
< |
threadAssertTrue(g.join() == 34); |
754 |
< |
threadAssertTrue(h.isDone()); |
755 |
< |
threadAssertTrue(h.join() == 13); |
750 |
> |
assertTrue(f.isDone()); |
751 |
> |
assertTrue(g.isDone()); |
752 |
> |
assertTrue(h.isDone()); |
753 |
> |
checkCompletedNormally(f, 21); |
754 |
> |
checkCompletedNormally(g, 34); |
755 |
> |
checkCompletedNormally(h, 13); |
756 |
|
return NoResult; |
757 |
< |
} |
758 |
< |
}; |
755 |
< |
mainPool.invoke(a); |
757 |
> |
}}; |
758 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
759 |
|
} |
760 |
|
|
761 |
|
|
762 |
|
/** |
763 |
+ |
* invokeAll(tasks) with any null task throws NPE |
764 |
+ |
*/ |
765 |
+ |
public void testInvokeAllNPE() { |
766 |
+ |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
767 |
+ |
public Integer realCompute() { |
768 |
+ |
FibTask f = new FibTask(8); |
769 |
+ |
FibTask g = new FibTask(9); |
770 |
+ |
FibTask h = null; |
771 |
+ |
try { |
772 |
+ |
invokeAll(f, g, h); |
773 |
+ |
shouldThrow(); |
774 |
+ |
} catch (NullPointerException success) {} |
775 |
+ |
return NoResult; |
776 |
+ |
}}; |
777 |
+ |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
778 |
+ |
} |
779 |
+ |
|
780 |
+ |
/** |
781 |
|
* invokeAll(t1, t2) throw exception if any task does |
782 |
|
*/ |
783 |
|
public void testAbnormalInvokeAll2() { |
784 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
785 |
< |
public Integer compute() { |
784 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
785 |
> |
public Integer realCompute() { |
786 |
> |
FibTask f = new FibTask(8); |
787 |
> |
FailingFibTask g = new FailingFibTask(9); |
788 |
|
try { |
766 |
– |
FibTask f = new FibTask(8); |
767 |
– |
FailingFibTask g = new FailingFibTask(9); |
789 |
|
invokeAll(f, g); |
790 |
|
shouldThrow(); |
770 |
– |
return NoResult; |
791 |
|
} catch (FJException success) { |
792 |
+ |
checkCompletedAbnormally(g, success); |
793 |
|
} |
794 |
|
return NoResult; |
795 |
< |
} |
796 |
< |
}; |
776 |
< |
mainPool.invoke(a); |
795 |
> |
}}; |
796 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
801 |
|
*/ |
802 |
|
public void testAbnormalInvokeAll1() { |
803 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
804 |
< |
public Integer compute() { |
803 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
804 |
> |
public Integer realCompute() { |
805 |
> |
FailingFibTask g = new FailingFibTask(9); |
806 |
|
try { |
786 |
– |
FailingFibTask g = new FailingFibTask(9); |
807 |
|
invokeAll(g); |
808 |
|
shouldThrow(); |
789 |
– |
return NoResult; |
809 |
|
} catch (FJException success) { |
810 |
+ |
checkCompletedAbnormally(g, success); |
811 |
|
} |
812 |
|
return NoResult; |
813 |
< |
} |
814 |
< |
}; |
795 |
< |
mainPool.invoke(a); |
813 |
> |
}}; |
814 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
815 |
|
} |
816 |
|
|
817 |
|
/** |
818 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
819 |
|
*/ |
820 |
|
public void testAbnormalInvokeAll3() { |
821 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
822 |
< |
public Integer compute() { |
821 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
822 |
> |
public Integer realCompute() { |
823 |
> |
FibTask f = new FibTask(8); |
824 |
> |
FailingFibTask g = new FailingFibTask(9); |
825 |
> |
FibTask h = new FibTask(7); |
826 |
|
try { |
805 |
– |
FibTask f = new FibTask(8); |
806 |
– |
FailingFibTask g = new FailingFibTask(9); |
807 |
– |
FibTask h = new FibTask(7); |
827 |
|
invokeAll(f, g, h); |
828 |
|
shouldThrow(); |
810 |
– |
return NoResult; |
829 |
|
} catch (FJException success) { |
830 |
+ |
checkCompletedAbnormally(g, success); |
831 |
|
} |
832 |
|
return NoResult; |
833 |
< |
} |
834 |
< |
}; |
816 |
< |
mainPool.invoke(a); |
833 |
> |
}}; |
834 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
|
* invokeAll(collection) throws exception if any task does |
839 |
|
*/ |
840 |
|
public void testAbnormalInvokeAllCollection() { |
841 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
842 |
< |
public Integer compute() { |
841 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
842 |
> |
public Integer realCompute() { |
843 |
> |
FailingFibTask f = new FailingFibTask(8); |
844 |
> |
FibTask g = new FibTask(9); |
845 |
> |
FibTask h = new FibTask(7); |
846 |
> |
HashSet set = new HashSet(); |
847 |
> |
set.add(f); |
848 |
> |
set.add(g); |
849 |
> |
set.add(h); |
850 |
|
try { |
826 |
– |
FailingFibTask f = new FailingFibTask(8); |
827 |
– |
FibTask g = new FibTask(9); |
828 |
– |
FibTask h = new FibTask(7); |
829 |
– |
HashSet set = new HashSet(); |
830 |
– |
set.add(f); |
831 |
– |
set.add(g); |
832 |
– |
set.add(h); |
851 |
|
invokeAll(set); |
852 |
|
shouldThrow(); |
835 |
– |
return NoResult; |
853 |
|
} catch (FJException success) { |
854 |
+ |
checkCompletedAbnormally(f, success); |
855 |
|
} |
856 |
|
return NoResult; |
857 |
< |
} |
858 |
< |
}; |
841 |
< |
mainPool.invoke(a); |
857 |
> |
}}; |
858 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
859 |
|
} |
860 |
|
|
861 |
|
/** |
863 |
|
* and suppresses execution |
864 |
|
*/ |
865 |
|
public void testTryUnfork() { |
866 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
867 |
< |
public Integer compute() { |
866 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
867 |
> |
public Integer realCompute() { |
868 |
|
FibTask g = new FibTask(9); |
869 |
< |
g.fork(); |
869 |
> |
assertSame(g, g.fork()); |
870 |
|
FibTask f = new FibTask(8); |
871 |
< |
f.fork(); |
872 |
< |
threadAssertTrue(f.tryUnfork()); |
871 |
> |
assertSame(f, f.fork()); |
872 |
> |
assertTrue(f.tryUnfork()); |
873 |
|
helpQuiesce(); |
874 |
< |
threadAssertFalse(f.isDone()); |
875 |
< |
threadAssertTrue(g.isDone()); |
874 |
> |
checkNotDone(f); |
875 |
> |
checkCompletedNormally(g, 34); |
876 |
|
return NoResult; |
877 |
< |
} |
878 |
< |
}; |
862 |
< |
singletonPool.invoke(a); |
877 |
> |
}}; |
878 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
879 |
|
} |
880 |
|
|
881 |
|
/** |
883 |
|
* there are more tasks than threads |
884 |
|
*/ |
885 |
|
public void testGetSurplusQueuedTaskCount() { |
886 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
887 |
< |
public Integer compute() { |
886 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
887 |
> |
public Integer realCompute() { |
888 |
|
FibTask h = new FibTask(7); |
889 |
< |
h.fork(); |
889 |
> |
assertSame(h, h.fork()); |
890 |
|
FibTask g = new FibTask(9); |
891 |
< |
g.fork(); |
891 |
> |
assertSame(g, g.fork()); |
892 |
|
FibTask f = new FibTask(8); |
893 |
< |
f.fork(); |
894 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
893 |
> |
assertSame(f, f.fork()); |
894 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
895 |
|
helpQuiesce(); |
896 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
897 |
+ |
checkCompletedNormally(f, 21); |
898 |
+ |
checkCompletedNormally(g, 34); |
899 |
+ |
checkCompletedNormally(h, 13); |
900 |
|
return NoResult; |
901 |
< |
} |
902 |
< |
}; |
883 |
< |
singletonPool.invoke(a); |
901 |
> |
}}; |
902 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
903 |
|
} |
904 |
|
|
905 |
|
/** |
906 |
|
* peekNextLocalTask returns most recent unexecuted task. |
907 |
|
*/ |
908 |
|
public void testPeekNextLocalTask() { |
909 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
910 |
< |
public Integer compute() { |
909 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
910 |
> |
public Integer realCompute() { |
911 |
|
FibTask g = new FibTask(9); |
912 |
< |
g.fork(); |
912 |
> |
assertSame(g, g.fork()); |
913 |
|
FibTask f = new FibTask(8); |
914 |
< |
f.fork(); |
915 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
916 |
< |
f.join(); |
898 |
< |
threadAssertTrue(f.isDone()); |
914 |
> |
assertSame(f, f.fork()); |
915 |
> |
assertSame(f, peekNextLocalTask()); |
916 |
> |
checkCompletesNormally(f, 21); |
917 |
|
helpQuiesce(); |
918 |
+ |
checkCompletedNormally(g, 34); |
919 |
|
return NoResult; |
920 |
< |
} |
921 |
< |
}; |
903 |
< |
singletonPool.invoke(a); |
920 |
> |
}}; |
921 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
922 |
|
} |
923 |
|
|
924 |
|
/** |
926 |
|
* without executing it |
927 |
|
*/ |
928 |
|
public void testPollNextLocalTask() { |
929 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
930 |
< |
public Integer compute() { |
929 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
930 |
> |
public Integer realCompute() { |
931 |
|
FibTask g = new FibTask(9); |
932 |
< |
g.fork(); |
932 |
> |
assertSame(g, g.fork()); |
933 |
|
FibTask f = new FibTask(8); |
934 |
< |
f.fork(); |
935 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
934 |
> |
assertSame(f, f.fork()); |
935 |
> |
assertSame(f, pollNextLocalTask()); |
936 |
|
helpQuiesce(); |
937 |
< |
threadAssertFalse(f.isDone()); |
937 |
> |
checkNotDone(f); |
938 |
> |
checkCompletedNormally(g, 34); |
939 |
|
return NoResult; |
940 |
< |
} |
941 |
< |
}; |
923 |
< |
singletonPool.invoke(a); |
940 |
> |
}}; |
941 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
942 |
|
} |
943 |
|
|
944 |
|
/** |
945 |
< |
* pollTask returns an unexecuted task |
928 |
< |
* without executing it |
945 |
> |
* pollTask returns an unexecuted task without executing it |
946 |
|
*/ |
947 |
|
public void testPollTask() { |
948 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
949 |
< |
public Integer compute() { |
948 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
949 |
> |
public Integer realCompute() { |
950 |
|
FibTask g = new FibTask(9); |
951 |
< |
g.fork(); |
951 |
> |
assertSame(g, g.fork()); |
952 |
|
FibTask f = new FibTask(8); |
953 |
< |
f.fork(); |
954 |
< |
threadAssertTrue(pollTask() == f); |
953 |
> |
assertSame(f, f.fork()); |
954 |
> |
assertSame(f, pollTask()); |
955 |
|
helpQuiesce(); |
956 |
< |
threadAssertFalse(f.isDone()); |
957 |
< |
threadAssertTrue(g.isDone()); |
956 |
> |
checkNotDone(f); |
957 |
> |
checkCompletedNormally(g, 34); |
958 |
|
return NoResult; |
959 |
< |
} |
960 |
< |
}; |
944 |
< |
singletonPool.invoke(a); |
959 |
> |
}}; |
960 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
965 |
|
*/ |
966 |
|
public void testPeekNextLocalTaskAsync() { |
967 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
968 |
< |
public Integer compute() { |
967 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
968 |
> |
public Integer realCompute() { |
969 |
|
FibTask g = new FibTask(9); |
970 |
< |
g.fork(); |
970 |
> |
assertSame(g, g.fork()); |
971 |
|
FibTask f = new FibTask(8); |
972 |
< |
f.fork(); |
973 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
974 |
< |
f.join(); |
972 |
> |
assertSame(f, f.fork()); |
973 |
> |
assertSame(g, peekNextLocalTask()); |
974 |
> |
assertEquals(21, (int) f.join()); |
975 |
|
helpQuiesce(); |
976 |
< |
threadAssertTrue(f.isDone()); |
976 |
> |
checkCompletedNormally(f, 21); |
977 |
> |
checkCompletedNormally(g, 34); |
978 |
|
return NoResult; |
979 |
< |
} |
980 |
< |
}; |
964 |
< |
asyncSingletonPool.invoke(a); |
979 |
> |
}}; |
980 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
981 |
|
} |
982 |
|
|
983 |
|
/** |
984 |
< |
* pollNextLocalTask returns least recent unexecuted task |
985 |
< |
* without executing it, in async mode |
984 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
985 |
> |
* executing it, in async mode |
986 |
|
*/ |
987 |
|
public void testPollNextLocalTaskAsync() { |
988 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
989 |
< |
public Integer compute() { |
988 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
989 |
> |
public Integer realCompute() { |
990 |
|
FibTask g = new FibTask(9); |
991 |
< |
g.fork(); |
991 |
> |
assertSame(g, g.fork()); |
992 |
|
FibTask f = new FibTask(8); |
993 |
< |
f.fork(); |
994 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
993 |
> |
assertSame(f, f.fork()); |
994 |
> |
assertSame(g, pollNextLocalTask()); |
995 |
|
helpQuiesce(); |
996 |
< |
threadAssertTrue(f.isDone()); |
997 |
< |
threadAssertFalse(g.isDone()); |
996 |
> |
checkCompletedNormally(f, 21); |
997 |
> |
checkNotDone(g); |
998 |
|
return NoResult; |
999 |
< |
} |
1000 |
< |
}; |
985 |
< |
asyncSingletonPool.invoke(a); |
999 |
> |
}}; |
1000 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* pollTask returns an unexecuted task |
1005 |
< |
* without executing it, in async mode |
1004 |
> |
* pollTask returns an unexecuted task without executing it, in |
1005 |
> |
* async mode |
1006 |
|
*/ |
1007 |
|
public void testPollTaskAsync() { |
1008 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
1009 |
< |
public Integer compute() { |
1008 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1009 |
> |
public Integer realCompute() { |
1010 |
|
FibTask g = new FibTask(9); |
1011 |
< |
g.fork(); |
1011 |
> |
assertSame(g, g.fork()); |
1012 |
|
FibTask f = new FibTask(8); |
1013 |
< |
f.fork(); |
1014 |
< |
threadAssertTrue(pollTask() == g); |
1013 |
> |
assertSame(f, f.fork()); |
1014 |
> |
assertSame(g, pollTask()); |
1015 |
|
helpQuiesce(); |
1016 |
< |
threadAssertTrue(f.isDone()); |
1017 |
< |
threadAssertFalse(g.isDone()); |
1016 |
> |
checkCompletedNormally(f, 21); |
1017 |
> |
checkNotDone(g); |
1018 |
|
return NoResult; |
1019 |
< |
} |
1020 |
< |
}; |
1006 |
< |
asyncSingletonPool.invoke(a); |
1019 |
> |
}}; |
1020 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1021 |
|
} |
1022 |
|
|
1023 |
|
} |