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 checkTaskThrew(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(ex); |
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(ex); |
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(ex); |
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 |
> |
checkTaskThrew(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 |
> |
checkTaskThrew(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 |
> |
checkTaskThrew(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) { |
316 |
< |
unexpectedException(ex); |
317 |
< |
} |
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 |
> |
checkTaskThrew(f, success.getCause()); |
387 |
|
} |
388 |
< |
}; |
389 |
< |
mainPool.invoke(a); |
388 |
> |
}}; |
389 |
> |
testInvokeOnPool(mainPool(), a); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
|
* timed get of a forked task throws exception when task completes abnormally |
394 |
|
*/ |
395 |
|
public void testAbnormalForkTimedGet() { |
396 |
< |
RecursiveAction a = new RecursiveAction() { |
397 |
< |
public void compute() { |
398 |
< |
try { |
399 |
< |
FailingFibAction f = new FailingFibAction(8); |
400 |
< |
f.fork(); |
401 |
< |
f.get(5L, TimeUnit.SECONDS); |
402 |
< |
shouldThrow(); |
403 |
< |
} catch (ExecutionException success) { |
404 |
< |
} catch (Exception ex) { |
336 |
< |
unexpectedException(ex); |
337 |
< |
} |
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); |
396 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
397 |
> |
public void realCompute() throws Exception { |
398 |
> |
FailingFibAction f = new FailingFibAction(8); |
399 |
> |
assertSame(f, f.fork()); |
400 |
> |
try { |
401 |
> |
f.get(5L, TimeUnit.SECONDS); |
402 |
> |
shouldThrow(); |
403 |
> |
} catch (ExecutionException success) { |
404 |
> |
checkTaskThrew(f, success.getCause()); |
405 |
|
} |
406 |
< |
}; |
407 |
< |
mainPool.invoke(a); |
406 |
> |
}}; |
407 |
> |
testInvokeOnPool(mainPool(), a); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
412 |
|
*/ |
413 |
|
public void testAbnormalForkQuietlyJoin() { |
414 |
< |
RecursiveAction a = new RecursiveAction() { |
415 |
< |
public void compute() { |
416 |
< |
FailingFibAction f = new FailingFibAction(8); |
417 |
< |
f.fork(); |
418 |
< |
f.quietlyJoin(); |
419 |
< |
threadAssertTrue(f.isDone()); |
420 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
421 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
422 |
< |
} |
395 |
< |
}; |
396 |
< |
mainPool.invoke(a); |
414 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
415 |
> |
public void realCompute() { |
416 |
> |
FailingFibAction f = new FailingFibAction(8); |
417 |
> |
assertSame(f, f.fork()); |
418 |
> |
f.quietlyJoin(); |
419 |
> |
assertTrue(f.getException() instanceof FJException); |
420 |
> |
checkTaskThrew(f, f.getException()); |
421 |
> |
}}; |
422 |
> |
testInvokeOnPool(mainPool(), a); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* invoke task throws exception when task cancelled |
427 |
|
*/ |
428 |
|
public void testCancelledInvoke() { |
429 |
< |
RecursiveAction a = new RecursiveAction() { |
430 |
< |
public void compute() { |
431 |
< |
try { |
432 |
< |
FibAction f = new FibAction(8); |
433 |
< |
f.cancel(true); |
434 |
< |
f.invoke(); |
435 |
< |
shouldThrow(); |
436 |
< |
} catch (CancellationException success) { |
437 |
< |
} |
429 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
430 |
> |
public void realCompute() { |
431 |
> |
FibAction f = new FibAction(8); |
432 |
> |
assertTrue(f.cancel(true)); |
433 |
> |
try { |
434 |
> |
f.invoke(); |
435 |
> |
shouldThrow(); |
436 |
> |
} catch (CancellationException success) { |
437 |
> |
checkCancelled(f); |
438 |
|
} |
439 |
< |
}; |
440 |
< |
mainPool.invoke(a); |
439 |
> |
}}; |
440 |
> |
testInvokeOnPool(mainPool(), a); |
441 |
|
} |
442 |
|
|
443 |
|
/** |
444 |
|
* join of a forked task throws exception when task cancelled |
445 |
|
*/ |
446 |
|
public void testCancelledForkJoin() { |
447 |
< |
RecursiveAction a = new RecursiveAction() { |
448 |
< |
public void compute() { |
449 |
< |
try { |
450 |
< |
FibAction f = new FibAction(8); |
451 |
< |
f.cancel(true); |
452 |
< |
f.fork(); |
453 |
< |
f.join(); |
454 |
< |
shouldThrow(); |
455 |
< |
} catch (CancellationException success) { |
456 |
< |
} |
447 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
448 |
> |
public void realCompute() { |
449 |
> |
FibAction f = new FibAction(8); |
450 |
> |
assertTrue(f.cancel(true)); |
451 |
> |
assertSame(f, f.fork()); |
452 |
> |
try { |
453 |
> |
f.join(); |
454 |
> |
shouldThrow(); |
455 |
> |
} catch (CancellationException success) { |
456 |
> |
checkCancelled(f); |
457 |
|
} |
458 |
< |
}; |
459 |
< |
mainPool.invoke(a); |
458 |
> |
}}; |
459 |
> |
testInvokeOnPool(mainPool(), a); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
|
* get of a forked task throws exception when task cancelled |
464 |
|
*/ |
465 |
|
public void testCancelledForkGet() { |
466 |
< |
RecursiveAction a = new RecursiveAction() { |
467 |
< |
public void compute() { |
468 |
< |
try { |
469 |
< |
FibAction f = new FibAction(8); |
470 |
< |
f.cancel(true); |
471 |
< |
f.fork(); |
472 |
< |
f.get(); |
473 |
< |
shouldThrow(); |
474 |
< |
} catch (CancellationException success) { |
475 |
< |
} catch (Exception ex) { |
450 |
< |
unexpectedException(ex); |
451 |
< |
} |
466 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
467 |
> |
public void realCompute() throws Exception { |
468 |
> |
FibAction f = new FibAction(8); |
469 |
> |
assertTrue(f.cancel(true)); |
470 |
> |
assertSame(f, f.fork()); |
471 |
> |
try { |
472 |
> |
f.get(); |
473 |
> |
shouldThrow(); |
474 |
> |
} catch (CancellationException success) { |
475 |
> |
checkCancelled(f); |
476 |
|
} |
477 |
< |
}; |
478 |
< |
mainPool.invoke(a); |
477 |
> |
}}; |
478 |
> |
testInvokeOnPool(mainPool(), a); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
|
* timed get of a forked task throws exception when task cancelled |
483 |
|
*/ |
484 |
|
public void testCancelledForkTimedGet() { |
485 |
< |
RecursiveAction a = new RecursiveAction() { |
486 |
< |
public void compute() { |
487 |
< |
try { |
488 |
< |
FibAction f = new FibAction(8); |
489 |
< |
f.cancel(true); |
490 |
< |
f.fork(); |
491 |
< |
f.get(5L, TimeUnit.SECONDS); |
492 |
< |
shouldThrow(); |
493 |
< |
} catch (CancellationException success) { |
494 |
< |
} 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); |
485 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
486 |
> |
public void realCompute() throws Exception { |
487 |
> |
FibAction f = new FibAction(8); |
488 |
> |
assertTrue(f.cancel(true)); |
489 |
> |
assertSame(f, f.fork()); |
490 |
> |
try { |
491 |
> |
f.get(5L, SECONDS); |
492 |
> |
shouldThrow(); |
493 |
> |
} catch (CancellationException success) { |
494 |
> |
checkCancelled(f); |
495 |
|
} |
496 |
< |
}; |
497 |
< |
mainPool.invoke(a); |
496 |
> |
}}; |
497 |
> |
testInvokeOnPool(mainPool(), a); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
501 |
|
* quietlyJoin of a forked task returns when task cancelled |
502 |
|
*/ |
503 |
|
public void testCancelledForkQuietlyJoin() { |
504 |
< |
RecursiveAction a = new RecursiveAction() { |
505 |
< |
public void compute() { |
506 |
< |
FibAction f = new FibAction(8); |
507 |
< |
f.cancel(true); |
508 |
< |
f.fork(); |
509 |
< |
f.quietlyJoin(); |
510 |
< |
threadAssertTrue(f.isDone()); |
511 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
512 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
532 |
< |
} |
533 |
< |
}; |
534 |
< |
mainPool.invoke(a); |
504 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
505 |
> |
public void realCompute() { |
506 |
> |
FibAction f = new FibAction(8); |
507 |
> |
assertTrue(f.cancel(true)); |
508 |
> |
assertSame(f, f.fork()); |
509 |
> |
f.quietlyJoin(); |
510 |
> |
checkCancelled(f); |
511 |
> |
}}; |
512 |
> |
testInvokeOnPool(mainPool(), a); |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
|
* getPool of executing task returns its pool |
517 |
|
*/ |
518 |
|
public void testGetPool() { |
519 |
< |
RecursiveAction a = new RecursiveAction() { |
520 |
< |
public void compute() { |
521 |
< |
threadAssertTrue(getPool() == mainPool); |
522 |
< |
} |
523 |
< |
}; |
524 |
< |
mainPool.invoke(a); |
519 |
> |
final ForkJoinPool mainPool = mainPool(); |
520 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
521 |
> |
public void realCompute() { |
522 |
> |
assertSame(mainPool, getPool()); |
523 |
> |
}}; |
524 |
> |
testInvokeOnPool(mainPool, a); |
525 |
|
} |
526 |
|
|
527 |
|
/** |
528 |
|
* getPool of non-FJ task returns null |
529 |
|
*/ |
530 |
|
public void testGetPool2() { |
531 |
< |
RecursiveAction a = new RecursiveAction() { |
532 |
< |
public void compute() { |
533 |
< |
threadAssertTrue(getPool() == null); |
534 |
< |
} |
535 |
< |
}; |
558 |
< |
a.invoke(); |
531 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
532 |
> |
public void realCompute() { |
533 |
> |
assertNull(getPool()); |
534 |
> |
}}; |
535 |
> |
assertNull(a.invoke()); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
|
* inForkJoinPool of executing task returns true |
540 |
|
*/ |
541 |
|
public void testInForkJoinPool() { |
542 |
< |
RecursiveAction a = new RecursiveAction() { |
543 |
< |
public void compute() { |
544 |
< |
threadAssertTrue(inForkJoinPool()); |
545 |
< |
} |
546 |
< |
}; |
570 |
< |
mainPool.invoke(a); |
542 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
543 |
> |
public void realCompute() { |
544 |
> |
assertTrue(inForkJoinPool()); |
545 |
> |
}}; |
546 |
> |
testInvokeOnPool(mainPool(), a); |
547 |
|
} |
548 |
|
|
549 |
|
/** |
550 |
|
* inForkJoinPool of non-FJ task returns false |
551 |
|
*/ |
552 |
|
public void testInForkJoinPool2() { |
553 |
< |
RecursiveAction a = new RecursiveAction() { |
554 |
< |
public void compute() { |
555 |
< |
threadAssertTrue(!inForkJoinPool()); |
556 |
< |
} |
557 |
< |
}; |
582 |
< |
a.invoke(); |
553 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
554 |
> |
public void realCompute() { |
555 |
> |
assertFalse(inForkJoinPool()); |
556 |
> |
}}; |
557 |
> |
assertNull(a.invoke()); |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
|
* getPool of current thread in pool returns its pool |
562 |
|
*/ |
563 |
|
public void testWorkerGetPool() { |
564 |
< |
RecursiveAction a = new RecursiveAction() { |
565 |
< |
public void compute() { |
566 |
< |
ForkJoinWorkerThread w = |
567 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
568 |
< |
threadAssertTrue(w.getPool() == mainPool); |
569 |
< |
} |
570 |
< |
}; |
571 |
< |
mainPool.invoke(a); |
564 |
> |
final ForkJoinPool mainPool = mainPool(); |
565 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
566 |
> |
public void realCompute() { |
567 |
> |
ForkJoinWorkerThread w = |
568 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
569 |
> |
assertSame(mainPool, w.getPool()); |
570 |
> |
}}; |
571 |
> |
testInvokeOnPool(mainPool, a); |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
601 |
– |
* |
576 |
|
*/ |
577 |
|
public void testWorkerGetPoolIndex() { |
578 |
< |
RecursiveAction a = new RecursiveAction() { |
579 |
< |
public void compute() { |
580 |
< |
ForkJoinWorkerThread w = |
581 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
582 |
< |
int idx = w.getPoolIndex(); |
583 |
< |
threadAssertTrue(idx >= 0); |
584 |
< |
threadAssertTrue(idx < mainPool.getPoolSize()); |
585 |
< |
} |
586 |
< |
}; |
613 |
< |
mainPool.invoke(a); |
578 |
> |
final ForkJoinPool mainPool = mainPool(); |
579 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
580 |
> |
public void realCompute() { |
581 |
> |
ForkJoinWorkerThread w = |
582 |
> |
(ForkJoinWorkerThread)(Thread.currentThread()); |
583 |
> |
assertTrue(w.getPoolIndex() >= 0); |
584 |
> |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
585 |
> |
}}; |
586 |
> |
testInvokeOnPool(mainPool, a); |
587 |
|
} |
588 |
|
|
589 |
|
|
591 |
|
* setRawResult(null) succeeds |
592 |
|
*/ |
593 |
|
public void testSetRawResult() { |
594 |
< |
RecursiveAction a = new RecursiveAction() { |
595 |
< |
public void compute() { |
596 |
< |
setRawResult(null); |
597 |
< |
} |
598 |
< |
}; |
626 |
< |
a.invoke(); |
594 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
595 |
> |
public void realCompute() { |
596 |
> |
setRawResult(null); |
597 |
> |
}}; |
598 |
> |
assertNull(a.invoke()); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
|
* A reinitialized task may be re-invoked |
603 |
|
*/ |
604 |
|
public void testReinitialize() { |
605 |
< |
RecursiveAction a = new RecursiveAction() { |
606 |
< |
public void compute() { |
607 |
< |
FibAction f = new FibAction(8); |
608 |
< |
f.invoke(); |
609 |
< |
threadAssertTrue(f.result == 21); |
610 |
< |
threadAssertTrue(f.isDone()); |
611 |
< |
threadAssertFalse(f.isCancelled()); |
612 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
605 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
606 |
> |
public void realCompute() { |
607 |
> |
FibAction f = new FibAction(8); |
608 |
> |
checkNotDone(f); |
609 |
> |
|
610 |
> |
for (int i = 0; i < 3; i++) { |
611 |
> |
assertNull(f.invoke()); |
612 |
> |
assertEquals(21, f.result); |
613 |
> |
checkCompletedNormally(f); |
614 |
|
f.reinitialize(); |
615 |
< |
f.invoke(); |
643 |
< |
threadAssertTrue(f.result == 21); |
615 |
> |
checkNotDone(f); |
616 |
|
} |
617 |
< |
}; |
618 |
< |
mainPool.invoke(a); |
617 |
> |
}}; |
618 |
> |
testInvokeOnPool(mainPool(), a); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
|
* invoke task throws exception after invoking completeExceptionally |
623 |
|
*/ |
624 |
|
public void testCompleteExceptionally() { |
625 |
< |
RecursiveAction a = new RecursiveAction() { |
626 |
< |
public void compute() { |
627 |
< |
try { |
628 |
< |
FibAction f = new FibAction(8); |
629 |
< |
f.completeExceptionally(new FJException()); |
630 |
< |
f.invoke(); |
631 |
< |
shouldThrow(); |
632 |
< |
} catch (FJException success) { |
633 |
< |
} |
625 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
626 |
> |
public void realCompute() { |
627 |
> |
FibAction f = new FibAction(8); |
628 |
> |
f.completeExceptionally(new FJException()); |
629 |
> |
try { |
630 |
> |
f.invoke(); |
631 |
> |
shouldThrow(); |
632 |
> |
} catch (FJException success) { |
633 |
> |
checkTaskThrew(f, success); |
634 |
|
} |
635 |
< |
}; |
636 |
< |
mainPool.invoke(a); |
635 |
> |
}}; |
636 |
> |
testInvokeOnPool(mainPool(), a); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
|
* invoke task suppresses execution invoking complete |
641 |
|
*/ |
642 |
|
public void testComplete() { |
643 |
< |
RecursiveAction a = new RecursiveAction() { |
644 |
< |
public void compute() { |
645 |
< |
FibAction f = new FibAction(8); |
646 |
< |
f.complete(null); |
647 |
< |
f.invoke(); |
648 |
< |
threadAssertTrue(f.isDone()); |
649 |
< |
threadAssertTrue(f.result == 0); |
650 |
< |
} |
651 |
< |
}; |
680 |
< |
mainPool.invoke(a); |
643 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
644 |
> |
public void realCompute() { |
645 |
> |
FibAction f = new FibAction(8); |
646 |
> |
f.complete(null); |
647 |
> |
assertNull(f.invoke()); |
648 |
> |
assertEquals(0, f.result); |
649 |
> |
checkCompletedNormally(f); |
650 |
> |
}}; |
651 |
> |
testInvokeOnPool(mainPool(), a); |
652 |
|
} |
653 |
|
|
654 |
|
/** |
655 |
|
* invokeAll(t1, t2) invokes all task arguments |
656 |
|
*/ |
657 |
|
public void testInvokeAll2() { |
658 |
< |
RecursiveAction a = new RecursiveAction() { |
659 |
< |
public void compute() { |
660 |
< |
FibAction f = new FibAction(8); |
661 |
< |
FibAction g = new FibAction(9); |
662 |
< |
invokeAll(f, g); |
663 |
< |
threadAssertTrue(f.isDone()); |
664 |
< |
threadAssertTrue(f.result == 21); |
665 |
< |
threadAssertTrue(g.isDone()); |
666 |
< |
threadAssertTrue(g.result == 34); |
667 |
< |
} |
668 |
< |
}; |
698 |
< |
mainPool.invoke(a); |
658 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
659 |
> |
public void realCompute() { |
660 |
> |
FibAction f = new FibAction(8); |
661 |
> |
FibAction g = new FibAction(9); |
662 |
> |
invokeAll(f, g); |
663 |
> |
checkCompletedNormally(f); |
664 |
> |
assertEquals(21, f.result); |
665 |
> |
checkCompletedNormally(g); |
666 |
> |
assertEquals(34, g.result); |
667 |
> |
}}; |
668 |
> |
testInvokeOnPool(mainPool(), a); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
|
* invokeAll(tasks) with 1 argument invokes task |
673 |
|
*/ |
674 |
|
public void testInvokeAll1() { |
675 |
< |
RecursiveAction a = new RecursiveAction() { |
676 |
< |
public void compute() { |
677 |
< |
FibAction f = new FibAction(8); |
678 |
< |
invokeAll(f); |
679 |
< |
threadAssertTrue(f.isDone()); |
680 |
< |
threadAssertTrue(f.result == 21); |
681 |
< |
} |
682 |
< |
}; |
713 |
< |
mainPool.invoke(a); |
675 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
676 |
> |
public void realCompute() { |
677 |
> |
FibAction f = new FibAction(8); |
678 |
> |
invokeAll(f); |
679 |
> |
checkCompletedNormally(f); |
680 |
> |
assertEquals(21, f.result); |
681 |
> |
}}; |
682 |
> |
testInvokeOnPool(mainPool(), a); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
687 |
|
*/ |
688 |
|
public void testInvokeAll3() { |
689 |
< |
RecursiveAction a = new RecursiveAction() { |
690 |
< |
public void compute() { |
691 |
< |
FibAction f = new FibAction(8); |
692 |
< |
FibAction g = new FibAction(9); |
693 |
< |
FibAction h = new FibAction(7); |
694 |
< |
invokeAll(f, g, h); |
695 |
< |
threadAssertTrue(f.isDone()); |
696 |
< |
threadAssertTrue(f.result == 21); |
697 |
< |
threadAssertTrue(g.isDone()); |
698 |
< |
threadAssertTrue(g.result == 34); |
699 |
< |
threadAssertTrue(h.isDone()); |
700 |
< |
threadAssertTrue(h.result == 13); |
701 |
< |
} |
702 |
< |
}; |
734 |
< |
mainPool.invoke(a); |
689 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
690 |
> |
public void realCompute() { |
691 |
> |
FibAction f = new FibAction(8); |
692 |
> |
FibAction g = new FibAction(9); |
693 |
> |
FibAction h = new FibAction(7); |
694 |
> |
invokeAll(f, g, h); |
695 |
> |
checkCompletedNormally(f); |
696 |
> |
assertEquals(21, f.result); |
697 |
> |
checkCompletedNormally(g); |
698 |
> |
assertEquals(34, g.result); |
699 |
> |
checkCompletedNormally(g); |
700 |
> |
assertEquals(13, h.result); |
701 |
> |
}}; |
702 |
> |
testInvokeOnPool(mainPool(), a); |
703 |
|
} |
704 |
|
|
705 |
|
/** |
706 |
|
* invokeAll(collection) invokes all tasks in the collection |
707 |
|
*/ |
708 |
|
public void testInvokeAllCollection() { |
709 |
< |
RecursiveAction a = new RecursiveAction() { |
710 |
< |
public void compute() { |
711 |
< |
FibAction f = new FibAction(8); |
712 |
< |
FibAction g = new FibAction(9); |
713 |
< |
FibAction h = new FibAction(7); |
714 |
< |
HashSet set = new HashSet(); |
715 |
< |
set.add(f); |
716 |
< |
set.add(g); |
717 |
< |
set.add(h); |
718 |
< |
invokeAll(set); |
719 |
< |
threadAssertTrue(f.isDone()); |
720 |
< |
threadAssertTrue(f.result == 21); |
721 |
< |
threadAssertTrue(g.isDone()); |
722 |
< |
threadAssertTrue(g.result == 34); |
723 |
< |
threadAssertTrue(h.isDone()); |
724 |
< |
threadAssertTrue(h.result == 13); |
725 |
< |
} |
726 |
< |
}; |
759 |
< |
mainPool.invoke(a); |
709 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
710 |
> |
public void realCompute() { |
711 |
> |
FibAction f = new FibAction(8); |
712 |
> |
FibAction g = new FibAction(9); |
713 |
> |
FibAction h = new FibAction(7); |
714 |
> |
HashSet set = new HashSet(); |
715 |
> |
set.add(f); |
716 |
> |
set.add(g); |
717 |
> |
set.add(h); |
718 |
> |
invokeAll(set); |
719 |
> |
checkCompletedNormally(f); |
720 |
> |
assertEquals(21, f.result); |
721 |
> |
checkCompletedNormally(g); |
722 |
> |
assertEquals(34, g.result); |
723 |
> |
checkCompletedNormally(g); |
724 |
> |
assertEquals(13, h.result); |
725 |
> |
}}; |
726 |
> |
testInvokeOnPool(mainPool(), a); |
727 |
|
} |
728 |
|
|
729 |
|
|
731 |
|
* invokeAll(tasks) with any null task throws NPE |
732 |
|
*/ |
733 |
|
public void testInvokeAllNPE() { |
734 |
< |
RecursiveAction a = new RecursiveAction() { |
735 |
< |
public void compute() { |
736 |
< |
try { |
737 |
< |
FibAction f = new FibAction(8); |
738 |
< |
FibAction g = new FibAction(9); |
739 |
< |
FibAction h = null; |
740 |
< |
invokeAll(f, g, h); |
741 |
< |
shouldThrow(); |
742 |
< |
} catch (NullPointerException success) { |
743 |
< |
} |
744 |
< |
} |
778 |
< |
}; |
779 |
< |
mainPool.invoke(a); |
734 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
735 |
> |
public void realCompute() { |
736 |
> |
FibAction f = new FibAction(8); |
737 |
> |
FibAction g = new FibAction(9); |
738 |
> |
FibAction h = null; |
739 |
> |
try { |
740 |
> |
invokeAll(f, g, h); |
741 |
> |
shouldThrow(); |
742 |
> |
} catch (NullPointerException success) {} |
743 |
> |
}}; |
744 |
> |
testInvokeOnPool(mainPool(), a); |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* invokeAll(t1, t2) throw exception if any task does |
749 |
|
*/ |
750 |
|
public void testAbnormalInvokeAll2() { |
751 |
< |
RecursiveAction a = new RecursiveAction() { |
752 |
< |
public void compute() { |
753 |
< |
try { |
754 |
< |
FibAction f = new FibAction(8); |
755 |
< |
FailingFibAction g = new FailingFibAction(9); |
756 |
< |
invokeAll(f, g); |
757 |
< |
shouldThrow(); |
758 |
< |
} catch (FJException success) { |
759 |
< |
} |
751 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
752 |
> |
public void realCompute() { |
753 |
> |
FibAction f = new FibAction(8); |
754 |
> |
FailingFibAction g = new FailingFibAction(9); |
755 |
> |
try { |
756 |
> |
invokeAll(f, g); |
757 |
> |
shouldThrow(); |
758 |
> |
} catch (FJException success) { |
759 |
> |
checkTaskThrew(g, success); |
760 |
|
} |
761 |
< |
}; |
762 |
< |
mainPool.invoke(a); |
761 |
> |
}}; |
762 |
> |
testInvokeOnPool(mainPool(), a); |
763 |
|
} |
764 |
|
|
765 |
|
/** |
766 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
767 |
|
*/ |
768 |
|
public void testAbnormalInvokeAll1() { |
769 |
< |
RecursiveAction a = new RecursiveAction() { |
770 |
< |
public void compute() { |
771 |
< |
try { |
772 |
< |
FailingFibAction g = new FailingFibAction(9); |
773 |
< |
invokeAll(g); |
774 |
< |
shouldThrow(); |
775 |
< |
} catch (FJException success) { |
776 |
< |
} |
769 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
> |
public void realCompute() { |
771 |
> |
FailingFibAction g = new FailingFibAction(9); |
772 |
> |
try { |
773 |
> |
invokeAll(g); |
774 |
> |
shouldThrow(); |
775 |
> |
} catch (FJException success) { |
776 |
> |
checkTaskThrew(g, success); |
777 |
|
} |
778 |
< |
}; |
779 |
< |
mainPool.invoke(a); |
778 |
> |
}}; |
779 |
> |
testInvokeOnPool(mainPool(), a); |
780 |
|
} |
781 |
|
|
782 |
|
/** |
783 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
784 |
|
*/ |
785 |
|
public void testAbnormalInvokeAll3() { |
786 |
< |
RecursiveAction a = new RecursiveAction() { |
787 |
< |
public void compute() { |
788 |
< |
try { |
789 |
< |
FibAction f = new FibAction(8); |
790 |
< |
FailingFibAction g = new FailingFibAction(9); |
791 |
< |
FibAction h = new FibAction(7); |
792 |
< |
invokeAll(f, g, h); |
793 |
< |
shouldThrow(); |
794 |
< |
} catch (FJException success) { |
795 |
< |
} |
786 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
787 |
> |
public void realCompute() { |
788 |
> |
FibAction f = new FibAction(8); |
789 |
> |
FailingFibAction g = new FailingFibAction(9); |
790 |
> |
FibAction h = new FibAction(7); |
791 |
> |
try { |
792 |
> |
invokeAll(f, g, h); |
793 |
> |
shouldThrow(); |
794 |
> |
} catch (FJException success) { |
795 |
> |
checkTaskThrew(g, success); |
796 |
|
} |
797 |
< |
}; |
798 |
< |
mainPool.invoke(a); |
797 |
> |
}}; |
798 |
> |
testInvokeOnPool(mainPool(), a); |
799 |
|
} |
800 |
|
|
801 |
|
/** |
802 |
|
* invokeAll(collection) throws exception if any task does |
803 |
|
*/ |
804 |
|
public void testAbnormalInvokeAllCollection() { |
805 |
< |
RecursiveAction a = new RecursiveAction() { |
806 |
< |
public void compute() { |
807 |
< |
try { |
808 |
< |
FailingFibAction f = new FailingFibAction(8); |
809 |
< |
FibAction g = new FibAction(9); |
810 |
< |
FibAction h = new FibAction(7); |
811 |
< |
HashSet set = new HashSet(); |
812 |
< |
set.add(f); |
813 |
< |
set.add(g); |
814 |
< |
set.add(h); |
815 |
< |
invokeAll(set); |
816 |
< |
shouldThrow(); |
817 |
< |
} catch (FJException success) { |
818 |
< |
} |
805 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
806 |
> |
public void realCompute() { |
807 |
> |
FailingFibAction f = new FailingFibAction(8); |
808 |
> |
FibAction g = new FibAction(9); |
809 |
> |
FibAction h = new FibAction(7); |
810 |
> |
HashSet set = new HashSet(); |
811 |
> |
set.add(f); |
812 |
> |
set.add(g); |
813 |
> |
set.add(h); |
814 |
> |
try { |
815 |
> |
invokeAll(set); |
816 |
> |
shouldThrow(); |
817 |
> |
} catch (FJException success) { |
818 |
> |
checkTaskThrew(f, success); |
819 |
|
} |
820 |
< |
}; |
821 |
< |
mainPool.invoke(a); |
820 |
> |
}}; |
821 |
> |
testInvokeOnPool(mainPool(), a); |
822 |
|
} |
823 |
|
|
824 |
|
/** |
826 |
|
* and suppresses execution |
827 |
|
*/ |
828 |
|
public void testTryUnfork() { |
829 |
< |
RecursiveAction a = new RecursiveAction() { |
830 |
< |
public void compute() { |
831 |
< |
FibAction g = new FibAction(9); |
832 |
< |
g.fork(); |
833 |
< |
FibAction f = new FibAction(8); |
834 |
< |
f.fork(); |
835 |
< |
threadAssertTrue(f.tryUnfork()); |
836 |
< |
helpQuiesce(); |
837 |
< |
threadAssertFalse(f.isDone()); |
838 |
< |
threadAssertTrue(g.isDone()); |
839 |
< |
} |
840 |
< |
}; |
876 |
< |
singletonPool.invoke(a); |
829 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
830 |
> |
public void realCompute() { |
831 |
> |
FibAction g = new FibAction(9); |
832 |
> |
assertSame(g, g.fork()); |
833 |
> |
FibAction f = new FibAction(8); |
834 |
> |
assertSame(f, f.fork()); |
835 |
> |
assertTrue(f.tryUnfork()); |
836 |
> |
helpQuiesce(); |
837 |
> |
checkNotDone(f); |
838 |
> |
checkCompletedNormally(g); |
839 |
> |
}}; |
840 |
> |
testInvokeOnPool(singletonPool(), a); |
841 |
|
} |
842 |
|
|
843 |
|
/** |
845 |
|
* there are more tasks than threads |
846 |
|
*/ |
847 |
|
public void testGetSurplusQueuedTaskCount() { |
848 |
< |
RecursiveAction a = new RecursiveAction() { |
849 |
< |
public void compute() { |
850 |
< |
FibAction h = new FibAction(7); |
851 |
< |
h.fork(); |
852 |
< |
FibAction g = new FibAction(9); |
853 |
< |
g.fork(); |
854 |
< |
FibAction f = new FibAction(8); |
855 |
< |
f.fork(); |
856 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
857 |
< |
helpQuiesce(); |
858 |
< |
} |
859 |
< |
}; |
860 |
< |
singletonPool.invoke(a); |
848 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
849 |
> |
public void realCompute() { |
850 |
> |
FibAction h = new FibAction(7); |
851 |
> |
assertSame(h, h.fork()); |
852 |
> |
FibAction g = new FibAction(9); |
853 |
> |
assertSame(g, g.fork()); |
854 |
> |
FibAction f = new FibAction(8); |
855 |
> |
assertSame(f, f.fork()); |
856 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
857 |
> |
helpQuiesce(); |
858 |
> |
checkCompletedNormally(f); |
859 |
> |
checkCompletedNormally(g); |
860 |
> |
checkCompletedNormally(h); |
861 |
> |
}}; |
862 |
> |
testInvokeOnPool(singletonPool(), a); |
863 |
|
} |
864 |
|
|
865 |
|
/** |
866 |
|
* peekNextLocalTask returns most recent unexecuted task. |
867 |
|
*/ |
868 |
|
public void testPeekNextLocalTask() { |
869 |
< |
RecursiveAction a = new RecursiveAction() { |
870 |
< |
public void compute() { |
871 |
< |
FibAction g = new FibAction(9); |
872 |
< |
g.fork(); |
873 |
< |
FibAction f = new FibAction(8); |
874 |
< |
f.fork(); |
875 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
876 |
< |
f.join(); |
877 |
< |
threadAssertTrue(f.isDone()); |
878 |
< |
helpQuiesce(); |
879 |
< |
} |
880 |
< |
}; |
881 |
< |
singletonPool.invoke(a); |
869 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
870 |
> |
public void realCompute() { |
871 |
> |
FibAction g = new FibAction(9); |
872 |
> |
assertSame(g, g.fork()); |
873 |
> |
FibAction f = new FibAction(8); |
874 |
> |
assertSame(f, f.fork()); |
875 |
> |
assertSame(f, peekNextLocalTask()); |
876 |
> |
assertNull(f.join()); |
877 |
> |
checkCompletedNormally(f); |
878 |
> |
helpQuiesce(); |
879 |
> |
checkCompletedNormally(f); |
880 |
> |
checkCompletedNormally(g); |
881 |
> |
}}; |
882 |
> |
testInvokeOnPool(singletonPool(), a); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
887 |
|
* without executing it |
888 |
|
*/ |
889 |
|
public void testPollNextLocalTask() { |
890 |
< |
RecursiveAction a = new RecursiveAction() { |
891 |
< |
public void compute() { |
892 |
< |
FibAction g = new FibAction(9); |
893 |
< |
g.fork(); |
894 |
< |
FibAction f = new FibAction(8); |
895 |
< |
f.fork(); |
896 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
897 |
< |
helpQuiesce(); |
898 |
< |
threadAssertFalse(f.isDone()); |
899 |
< |
} |
900 |
< |
}; |
901 |
< |
singletonPool.invoke(a); |
890 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
891 |
> |
public void realCompute() { |
892 |
> |
FibAction g = new FibAction(9); |
893 |
> |
assertSame(g, g.fork()); |
894 |
> |
FibAction f = new FibAction(8); |
895 |
> |
assertSame(f, f.fork()); |
896 |
> |
assertSame(f, pollNextLocalTask()); |
897 |
> |
helpQuiesce(); |
898 |
> |
checkNotDone(f); |
899 |
> |
checkCompletedNormally(g); |
900 |
> |
}}; |
901 |
> |
testInvokeOnPool(singletonPool(), a); |
902 |
|
} |
903 |
|
|
904 |
|
/** |
905 |
< |
* pollTask returns an unexecuted task |
939 |
< |
* without executing it |
905 |
> |
* pollTask returns an unexecuted task without executing it |
906 |
|
*/ |
907 |
|
public void testPollTask() { |
908 |
< |
RecursiveAction a = new RecursiveAction() { |
909 |
< |
public void compute() { |
910 |
< |
FibAction g = new FibAction(9); |
911 |
< |
g.fork(); |
912 |
< |
FibAction f = new FibAction(8); |
913 |
< |
f.fork(); |
914 |
< |
threadAssertTrue(pollTask() == f); |
915 |
< |
helpQuiesce(); |
916 |
< |
threadAssertFalse(f.isDone()); |
917 |
< |
threadAssertTrue(g.isDone()); |
918 |
< |
} |
919 |
< |
}; |
954 |
< |
singletonPool.invoke(a); |
908 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
909 |
> |
public void realCompute() { |
910 |
> |
FibAction g = new FibAction(9); |
911 |
> |
assertSame(g, g.fork()); |
912 |
> |
FibAction f = new FibAction(8); |
913 |
> |
assertSame(f, f.fork()); |
914 |
> |
assertSame(f, pollTask()); |
915 |
> |
helpQuiesce(); |
916 |
> |
checkNotDone(f); |
917 |
> |
checkCompletedNormally(g); |
918 |
> |
}}; |
919 |
> |
testInvokeOnPool(singletonPool(), a); |
920 |
|
} |
921 |
|
|
922 |
|
/** |
923 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
924 |
|
*/ |
925 |
|
public void testPeekNextLocalTaskAsync() { |
926 |
< |
RecursiveAction a = new RecursiveAction() { |
927 |
< |
public void compute() { |
928 |
< |
FibAction g = new FibAction(9); |
929 |
< |
g.fork(); |
930 |
< |
FibAction f = new FibAction(8); |
931 |
< |
f.fork(); |
932 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
933 |
< |
f.join(); |
934 |
< |
helpQuiesce(); |
935 |
< |
threadAssertTrue(f.isDone()); |
936 |
< |
} |
937 |
< |
}; |
938 |
< |
asyncSingletonPool.invoke(a); |
926 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
927 |
> |
public void realCompute() { |
928 |
> |
FibAction g = new FibAction(9); |
929 |
> |
assertSame(g, g.fork()); |
930 |
> |
FibAction f = new FibAction(8); |
931 |
> |
assertSame(f, f.fork()); |
932 |
> |
assertSame(g, peekNextLocalTask()); |
933 |
> |
assertNull(f.join()); |
934 |
> |
helpQuiesce(); |
935 |
> |
checkCompletedNormally(f); |
936 |
> |
checkCompletedNormally(g); |
937 |
> |
}}; |
938 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
939 |
|
} |
940 |
|
|
941 |
|
/** |
942 |
< |
* pollNextLocalTask returns least recent unexecuted task |
943 |
< |
* without executing it, in async mode |
942 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
943 |
> |
* executing it, in async mode |
944 |
|
*/ |
945 |
|
public void testPollNextLocalTaskAsync() { |
946 |
< |
RecursiveAction a = new RecursiveAction() { |
947 |
< |
public void compute() { |
948 |
< |
FibAction g = new FibAction(9); |
949 |
< |
g.fork(); |
950 |
< |
FibAction f = new FibAction(8); |
951 |
< |
f.fork(); |
952 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
953 |
< |
helpQuiesce(); |
954 |
< |
threadAssertTrue(f.isDone()); |
955 |
< |
threadAssertFalse(g.isDone()); |
956 |
< |
} |
957 |
< |
}; |
993 |
< |
asyncSingletonPool.invoke(a); |
946 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
947 |
> |
public void realCompute() { |
948 |
> |
FibAction g = new FibAction(9); |
949 |
> |
assertSame(g, g.fork()); |
950 |
> |
FibAction f = new FibAction(8); |
951 |
> |
assertSame(f, f.fork()); |
952 |
> |
assertSame(g, pollNextLocalTask()); |
953 |
> |
helpQuiesce(); |
954 |
> |
checkCompletedNormally(f); |
955 |
> |
checkNotDone(g); |
956 |
> |
}}; |
957 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
958 |
|
} |
959 |
|
|
960 |
|
/** |
961 |
< |
* pollTask returns an unexecuted task |
962 |
< |
* without executing it, in async mode |
961 |
> |
* pollTask returns an unexecuted task without executing it, in |
962 |
> |
* async mode |
963 |
|
*/ |
964 |
|
public void testPollTaskAsync() { |
965 |
< |
RecursiveAction a = new RecursiveAction() { |
966 |
< |
public void compute() { |
967 |
< |
FibAction g = new FibAction(9); |
968 |
< |
g.fork(); |
969 |
< |
FibAction f = new FibAction(8); |
970 |
< |
f.fork(); |
971 |
< |
threadAssertTrue(pollTask() == g); |
972 |
< |
helpQuiesce(); |
973 |
< |
threadAssertTrue(f.isDone()); |
974 |
< |
threadAssertFalse(g.isDone()); |
975 |
< |
} |
976 |
< |
}; |
1013 |
< |
asyncSingletonPool.invoke(a); |
965 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
966 |
> |
public void realCompute() { |
967 |
> |
FibAction g = new FibAction(9); |
968 |
> |
assertSame(g, g.fork()); |
969 |
> |
FibAction f = new FibAction(8); |
970 |
> |
assertSame(f, f.fork()); |
971 |
> |
assertSame(g, pollTask()); |
972 |
> |
helpQuiesce(); |
973 |
> |
checkCompletedNormally(f); |
974 |
> |
checkNotDone(g); |
975 |
> |
}}; |
976 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
977 |
|
} |
978 |
|
|
979 |
|
} |