ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.18
Committed: Tue Sep 14 18:38:28 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.17: +1 -1 lines
Log Message:
first arg to junit method calls should be expected result

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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertSame(f, 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 threadAssertTrue(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 threadAssertTrue(f.cancel(true));
371 threadAssertSame(f, 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 threadAssertTrue(f.cancel(true));
392 threadAssertSame(f, 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 threadAssertTrue(f.cancel(true));
415 threadAssertSame(f, 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 threadAssertTrue(f.cancel(true));
437 threadAssertSame(f, 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 assertSame(NoResult, 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 assertSame(NoResult, a.invoke());
499 }
500
501 /**
502 * The value set by setRawResult is returned by getRawResult
503 */
504 public void testSetRawResult() {
505 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
506 public Integer compute() {
507 setRawResult(NoResult);
508 threadAssertSame(NoResult, getRawResult());
509 return NoResult;
510 }
511 };
512 a.invoke();
513 }
514
515 /**
516 * A reinitialized task may be re-invoked
517 */
518 public void testReinitialize() {
519 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
520 public Integer compute() {
521 FibTask f = new FibTask(8);
522 Integer r = f.invoke();
523 threadAssertTrue(r == 21);
524 threadAssertTrue(f.isDone());
525 threadAssertFalse(f.isCancelled());
526 threadAssertFalse(f.isCompletedAbnormally());
527 f.reinitialize();
528 r = f.invoke();
529 threadAssertTrue(r == 21);
530 return NoResult;
531 }
532 };
533 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
534 }
535
536 /**
537 * invoke task throws exception after invoking completeExceptionally
538 */
539 public void testCompleteExceptionally() {
540 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
541 public Integer compute() {
542 try {
543 FibTask f = new FibTask(8);
544 f.completeExceptionally(new FJException());
545 Integer r = f.invoke();
546 shouldThrow();
547 return r;
548 } catch (FJException success) {
549 }
550 return NoResult;
551 }
552 };
553 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
554 }
555
556 /**
557 * invoke task suppresses execution invoking complete
558 */
559 public void testComplete() {
560 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
561 public Integer compute() {
562 FibTask f = new FibTask(8);
563 f.complete(NoResult);
564 Integer r = f.invoke();
565 threadAssertTrue(f.isDone());
566 threadAssertTrue(r == NoResult);
567 return r;
568 }
569 };
570 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
571 }
572
573 /**
574 * invokeAll(t1, t2) invokes all task arguments
575 */
576 public void testInvokeAll2() {
577 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
578 public Integer compute() {
579 FibTask f = new FibTask(8);
580 FibTask g = new FibTask(9);
581 invokeAll(f, g);
582 threadAssertTrue(f.isDone());
583 threadAssertTrue(f.join() == 21);
584 threadAssertTrue(g.isDone());
585 threadAssertTrue(g.join() == 34);
586 return NoResult;
587 }
588 };
589 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
590 }
591
592 /**
593 * invokeAll(tasks) with 1 argument invokes task
594 */
595 public void testInvokeAll1() {
596 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
597 public Integer compute() {
598 FibTask f = new FibTask(8);
599 invokeAll(f);
600 threadAssertTrue(f.isDone());
601 threadAssertTrue(f.join() == 21);
602 return NoResult;
603 }
604 };
605 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
606 }
607
608 /**
609 * invokeAll(tasks) with > 2 argument invokes tasks
610 */
611 public void testInvokeAll3() {
612 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
613 public Integer compute() {
614 FibTask f = new FibTask(8);
615 FibTask g = new FibTask(9);
616 FibTask h = new FibTask(7);
617 invokeAll(f, g, h);
618 threadAssertTrue(f.isDone());
619 threadAssertTrue(f.join() == 21);
620 threadAssertTrue(g.isDone());
621 threadAssertTrue(g.join() == 34);
622 threadAssertTrue(h.isDone());
623 threadAssertTrue(h.join() == 13);
624 return NoResult;
625 }
626 };
627 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
628 }
629
630 /**
631 * invokeAll(collection) invokes all tasks in the collection
632 */
633 public void testInvokeAllCollection() {
634 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
635 public Integer compute() {
636 FibTask f = new FibTask(8);
637 FibTask g = new FibTask(9);
638 FibTask h = new FibTask(7);
639 HashSet set = new HashSet();
640 set.add(f);
641 set.add(g);
642 set.add(h);
643 invokeAll(set);
644 threadAssertTrue(f.isDone());
645 threadAssertTrue(f.join() == 21);
646 threadAssertTrue(g.isDone());
647 threadAssertTrue(g.join() == 34);
648 threadAssertTrue(h.isDone());
649 threadAssertTrue(h.join() == 13);
650 return NoResult;
651 }
652 };
653 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
654 }
655
656
657 /**
658 * invokeAll(t1, t2) throw exception if any task does
659 */
660 public void testAbnormalInvokeAll2() {
661 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
662 public Integer compute() {
663 try {
664 FibTask f = new FibTask(8);
665 FailingFibTask g = new FailingFibTask(9);
666 invokeAll(f, g);
667 shouldThrow();
668 return NoResult;
669 } catch (FJException success) {
670 }
671 return NoResult;
672 }
673 };
674 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
675 }
676
677 /**
678 * invokeAll(tasks) with 1 argument throws exception if task does
679 */
680 public void testAbnormalInvokeAll1() {
681 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
682 public Integer compute() {
683 try {
684 FailingFibTask g = new FailingFibTask(9);
685 invokeAll(g);
686 shouldThrow();
687 return NoResult;
688 } catch (FJException success) {
689 }
690 return NoResult;
691 }
692 };
693 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
694 }
695
696 /**
697 * invokeAll(tasks) with > 2 argument throws exception if any task does
698 */
699 public void testAbnormalInvokeAll3() {
700 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
701 public Integer compute() {
702 try {
703 FibTask f = new FibTask(8);
704 FailingFibTask g = new FailingFibTask(9);
705 FibTask h = new FibTask(7);
706 invokeAll(f, g, h);
707 shouldThrow();
708 return NoResult;
709 } catch (FJException success) {
710 }
711 return NoResult;
712 }
713 };
714 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
715 }
716
717 /**
718 * invokeAll(collection) throws exception if any task does
719 */
720 public void testAbnormalInvokeAllCollection() {
721 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
722 public Integer compute() {
723 try {
724 FailingFibTask f = new FailingFibTask(8);
725 FibTask g = new FibTask(9);
726 FibTask h = new FibTask(7);
727 HashSet set = new HashSet();
728 set.add(f);
729 set.add(g);
730 set.add(h);
731 invokeAll(set);
732 shouldThrow();
733 return NoResult;
734 } catch (FJException success) {
735 }
736 return NoResult;
737 }
738 };
739 assertSame(NoResult, testInvokeOnPool(mainPool(), a));
740 }
741
742 /**
743 * tryUnfork returns true for most recent unexecuted task,
744 * and suppresses execution
745 */
746 public void testTryUnfork() {
747 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
748 public Integer compute() {
749 FibTask g = new FibTask(9);
750 threadAssertSame(g, g.fork());
751 FibTask f = new FibTask(8);
752 threadAssertSame(f, f.fork());
753 threadAssertTrue(f.tryUnfork());
754 helpQuiesce();
755 threadAssertFalse(f.isDone());
756 threadAssertTrue(g.isDone());
757 return NoResult;
758 }
759 };
760 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
761 }
762
763 /**
764 * getSurplusQueuedTaskCount returns > 0 when
765 * there are more tasks than threads
766 */
767 public void testGetSurplusQueuedTaskCount() {
768 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
769 public Integer compute() {
770 FibTask h = new FibTask(7);
771 threadAssertSame(h, h.fork());
772 FibTask g = new FibTask(9);
773 threadAssertSame(g, g.fork());
774 FibTask f = new FibTask(8);
775 threadAssertSame(f, f.fork());
776 threadAssertTrue(getSurplusQueuedTaskCount() > 0);
777 helpQuiesce();
778 return NoResult;
779 }
780 };
781 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
782 }
783
784 /**
785 * peekNextLocalTask returns most recent unexecuted task.
786 */
787 public void testPeekNextLocalTask() {
788 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
789 public Integer compute() {
790 FibTask g = new FibTask(9);
791 threadAssertSame(g, g.fork());
792 FibTask f = new FibTask(8);
793 threadAssertSame(f, f.fork());
794 threadAssertTrue(peekNextLocalTask() == f);
795 threadAssertTrue(f.join() == 21);
796 threadAssertTrue(f.isDone());
797 helpQuiesce();
798 return NoResult;
799 }
800 };
801 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
802 }
803
804 /**
805 * pollNextLocalTask returns most recent unexecuted task
806 * without executing it
807 */
808 public void testPollNextLocalTask() {
809 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
810 public Integer compute() {
811 FibTask g = new FibTask(9);
812 threadAssertSame(g, g.fork());
813 FibTask f = new FibTask(8);
814 threadAssertSame(f, f.fork());
815 threadAssertTrue(pollNextLocalTask() == f);
816 helpQuiesce();
817 threadAssertFalse(f.isDone());
818 return NoResult;
819 }
820 };
821 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
822 }
823
824 /**
825 * pollTask returns an unexecuted task without executing it
826 */
827 public void testPollTask() {
828 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
829 public Integer compute() {
830 FibTask g = new FibTask(9);
831 threadAssertSame(g, g.fork());
832 FibTask f = new FibTask(8);
833 threadAssertSame(f, f.fork());
834 threadAssertTrue(pollTask() == f);
835 helpQuiesce();
836 threadAssertFalse(f.isDone());
837 threadAssertTrue(g.isDone());
838 return NoResult;
839 }
840 };
841 assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
842 }
843
844 /**
845 * peekNextLocalTask returns least recent unexecuted task in async mode
846 */
847 public void testPeekNextLocalTaskAsync() {
848 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
849 public Integer compute() {
850 FibTask g = new FibTask(9);
851 threadAssertSame(g, g.fork());
852 FibTask f = new FibTask(8);
853 threadAssertSame(f, f.fork());
854 threadAssertTrue(peekNextLocalTask() == g);
855 threadAssertEquals(21, (int) f.join());
856 helpQuiesce();
857 threadAssertTrue(f.isDone());
858 return NoResult;
859 }
860 };
861 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
862 }
863
864 /**
865 * pollNextLocalTask returns least recent unexecuted task
866 * without executing it, in async mode
867 */
868 public void testPollNextLocalTaskAsync() {
869 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
870 public Integer compute() {
871 FibTask g = new FibTask(9);
872 threadAssertSame(g, g.fork());
873 FibTask f = new FibTask(8);
874 threadAssertSame(f, f.fork());
875 threadAssertTrue(pollNextLocalTask() == g);
876 helpQuiesce();
877 threadAssertTrue(f.isDone());
878 threadAssertFalse(g.isDone());
879 return NoResult;
880 }
881 };
882 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
883 }
884
885 /**
886 * pollTask returns an unexecuted task
887 * without executing it, in async mode
888 */
889 public void testPollTaskAsync() {
890 RecursiveTask<Integer> a = new RecursiveTask<Integer>() {
891 public Integer compute() {
892 FibTask g = new FibTask(9);
893 threadAssertSame(g, g.fork());
894 FibTask f = new FibTask(8);
895 threadAssertSame(f, f.fork());
896 threadAssertTrue(pollTask() == g);
897 helpQuiesce();
898 threadAssertTrue(f.isDone());
899 threadAssertFalse(g.isDone());
900 return NoResult;
901 }
902 };
903 assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
904 }
905
906 }