ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.19
Committed: Thu Sep 16 00:52:49 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +297 -356 lines
Log Message:
testcase hygiene: introduce CheckedRecursiveAction and CheckedRecursiveTask; eliminate almost all threadAssertXXX; use preferred junit conventions;narrow the scope of exception checking code; make sure test failures in non-junit threads produce proper stacktraces

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