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