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