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