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