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