ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.15
Committed: Mon Sep 13 20:48:58 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +6 -2 lines
Log Message:
fix imports

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