ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.29
Committed: Sat Jun 18 14:33:38 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.28: +0 -3 lines
Log Message:
whitespace

File Contents

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