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