ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveTaskTest.java
Revision: 1.27
Committed: Tue Mar 15 19:47:07 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.26: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

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