10 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
11 |
|
import java.util.concurrent.RecursiveAction; |
12 |
|
import java.util.concurrent.TimeUnit; |
13 |
< |
import java.util.concurrent.atomic.*; |
14 |
< |
import java.util.*; |
13 |
> |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
14 |
> |
import java.util.HashSet; |
15 |
|
import junit.framework.*; |
16 |
|
|
17 |
|
public class ForkJoinTaskTest extends JSR166TestCase { |
24 |
|
return new TestSuite(ForkJoinTaskTest.class); |
25 |
|
} |
26 |
|
|
27 |
< |
/** |
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 |
54 |
|
* differently than supplied Recursive forms. |
55 |
|
*/ |
56 |
|
|
35 |
– |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
36 |
– |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
37 |
– |
static final ForkJoinPool asyncSingletonPool = |
38 |
– |
new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
39 |
– |
null, true); |
57 |
|
static final class FJException extends RuntimeException { |
58 |
|
FJException() { super(); } |
59 |
|
} |
243 |
|
threadAssertFalse(f.isCompletedAbnormally()); |
244 |
|
threadAssertTrue(f.getRawResult() == null); |
245 |
|
}}; |
246 |
< |
mainPool.invoke(a); |
246 |
> |
testInvokeOnPool(mainPool(), a); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
262 |
|
threadAssertFalse(f.isCompletedAbnormally()); |
263 |
|
threadAssertTrue(f.getRawResult() == null); |
264 |
|
}}; |
265 |
< |
mainPool.invoke(a); |
265 |
> |
testInvokeOnPool(mainPool(), a); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
278 |
|
threadAssertTrue(f.isDone()); |
279 |
|
threadAssertTrue(f.getRawResult() == null); |
280 |
|
}}; |
281 |
< |
mainPool.invoke(a); |
281 |
> |
testInvokeOnPool(mainPool(), a); |
282 |
|
} |
283 |
|
|
284 |
|
/** |
297 |
|
unexpectedException(ex); |
298 |
|
} |
299 |
|
}}; |
300 |
< |
mainPool.invoke(a); |
300 |
> |
testInvokeOnPool(mainPool(), a); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
316 |
|
unexpectedException(ex); |
317 |
|
} |
318 |
|
}}; |
319 |
< |
mainPool.invoke(a); |
319 |
> |
testInvokeOnPool(mainPool(), a); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
335 |
|
unexpectedException(ex); |
336 |
|
} |
337 |
|
}}; |
338 |
< |
mainPool.invoke(a); |
338 |
> |
testInvokeOnPool(mainPool(), a); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
350 |
|
threadAssertTrue(f.number == 21); |
351 |
|
threadAssertTrue(f.isDone()); |
352 |
|
}}; |
353 |
< |
mainPool.invoke(a); |
353 |
> |
testInvokeOnPool(mainPool(), a); |
354 |
|
} |
355 |
|
|
356 |
|
|
368 |
|
threadAssertTrue(f.isDone()); |
369 |
|
threadAssertTrue(getQueuedTaskCount() == 0); |
370 |
|
}}; |
371 |
< |
mainPool.invoke(a); |
371 |
> |
testInvokeOnPool(mainPool(), a); |
372 |
|
} |
373 |
|
|
374 |
|
|
385 |
|
} catch (FJException success) { |
386 |
|
} |
387 |
|
}}; |
388 |
< |
mainPool.invoke(a); |
388 |
> |
testInvokeOnPool(mainPool(), a); |
389 |
|
} |
390 |
|
|
391 |
|
/** |
398 |
|
f.quietlyInvoke(); |
399 |
|
threadAssertTrue(f.isDone()); |
400 |
|
}}; |
401 |
< |
mainPool.invoke(a); |
401 |
> |
testInvokeOnPool(mainPool(), a); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
415 |
|
} catch (FJException success) { |
416 |
|
} |
417 |
|
}}; |
418 |
< |
mainPool.invoke(a); |
418 |
> |
testInvokeOnPool(mainPool(), a); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
434 |
|
unexpectedException(ex); |
435 |
|
} |
436 |
|
}}; |
437 |
< |
mainPool.invoke(a); |
437 |
> |
testInvokeOnPool(mainPool(), a); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
453 |
|
unexpectedException(ex); |
454 |
|
} |
455 |
|
}}; |
456 |
< |
mainPool.invoke(a); |
456 |
> |
testInvokeOnPool(mainPool(), a); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
469 |
|
threadAssertTrue(f.isCompletedAbnormally()); |
470 |
|
threadAssertTrue(f.getException() instanceof FJException); |
471 |
|
}}; |
472 |
< |
mainPool.invoke(a); |
472 |
> |
testInvokeOnPool(mainPool(), a); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
486 |
|
} catch (CancellationException success) { |
487 |
|
} |
488 |
|
}}; |
489 |
< |
mainPool.invoke(a); |
489 |
> |
testInvokeOnPool(mainPool(), a); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
504 |
|
} catch (CancellationException success) { |
505 |
|
} |
506 |
|
}}; |
507 |
< |
mainPool.invoke(a); |
507 |
> |
testInvokeOnPool(mainPool(), a); |
508 |
|
} |
509 |
|
|
510 |
|
/** |
524 |
|
unexpectedException(ex); |
525 |
|
} |
526 |
|
}}; |
527 |
< |
mainPool.invoke(a); |
527 |
> |
testInvokeOnPool(mainPool(), a); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
544 |
|
unexpectedException(ex); |
545 |
|
} |
546 |
|
}}; |
547 |
< |
mainPool.invoke(a); |
547 |
> |
testInvokeOnPool(mainPool(), a); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
561 |
|
threadAssertTrue(f.isCompletedAbnormally()); |
562 |
|
threadAssertTrue(f.getException() instanceof CancellationException); |
563 |
|
}}; |
564 |
< |
mainPool.invoke(a); |
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 |
< |
mainPool.invoke(a); |
576 |
> |
testInvokeOnPool(mainPool, a); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
595 |
|
public void compute() { |
596 |
|
threadAssertTrue(inForkJoinPool()); |
597 |
|
}}; |
598 |
< |
mainPool.invoke(a); |
598 |
> |
testInvokeOnPool(mainPool(), a); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
634 |
|
} catch (FJException success) { |
635 |
|
} |
636 |
|
}}; |
637 |
< |
mainPool.invoke(a); |
637 |
> |
testInvokeOnPool(mainPool(), a); |
638 |
|
} |
639 |
|
|
640 |
|
/** |
651 |
|
threadAssertTrue(g.isDone()); |
652 |
|
threadAssertTrue(g.number == 34); |
653 |
|
}}; |
654 |
< |
mainPool.invoke(a); |
654 |
> |
testInvokeOnPool(mainPool(), a); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
665 |
|
threadAssertTrue(f.isDone()); |
666 |
|
threadAssertTrue(f.number == 21); |
667 |
|
}}; |
668 |
< |
mainPool.invoke(a); |
668 |
> |
testInvokeOnPool(mainPool(), a); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
685 |
|
threadAssertTrue(h.isDone()); |
686 |
|
threadAssertTrue(h.number == 13); |
687 |
|
}}; |
688 |
< |
mainPool.invoke(a); |
688 |
> |
testInvokeOnPool(mainPool(), a); |
689 |
|
} |
690 |
|
|
691 |
|
/** |
709 |
|
threadAssertTrue(h.isDone()); |
710 |
|
threadAssertTrue(h.number == 13); |
711 |
|
}}; |
712 |
< |
mainPool.invoke(a); |
712 |
> |
testInvokeOnPool(mainPool(), a); |
713 |
|
} |
714 |
|
|
715 |
|
|
728 |
|
} catch (NullPointerException success) { |
729 |
|
} |
730 |
|
}}; |
731 |
< |
mainPool.invoke(a); |
731 |
> |
testInvokeOnPool(mainPool(), a); |
732 |
|
} |
733 |
|
|
734 |
|
/** |
745 |
|
} catch (FJException success) { |
746 |
|
} |
747 |
|
}}; |
748 |
< |
mainPool.invoke(a); |
748 |
> |
testInvokeOnPool(mainPool(), a); |
749 |
|
} |
750 |
|
|
751 |
|
/** |
761 |
|
} catch (FJException success) { |
762 |
|
} |
763 |
|
}}; |
764 |
< |
mainPool.invoke(a); |
764 |
> |
testInvokeOnPool(mainPool(), a); |
765 |
|
} |
766 |
|
|
767 |
|
/** |
779 |
|
} catch (FJException success) { |
780 |
|
} |
781 |
|
}}; |
782 |
< |
mainPool.invoke(a); |
782 |
> |
testInvokeOnPool(mainPool(), a); |
783 |
|
} |
784 |
|
|
785 |
|
/** |
801 |
|
} catch (FJException success) { |
802 |
|
} |
803 |
|
}}; |
804 |
< |
mainPool.invoke(a); |
804 |
> |
testInvokeOnPool(mainPool(), a); |
805 |
|
} |
806 |
|
|
807 |
|
/** |
820 |
|
threadAssertFalse(f.isDone()); |
821 |
|
threadAssertTrue(g.isDone()); |
822 |
|
}}; |
823 |
< |
singletonPool.invoke(a); |
823 |
> |
testInvokeOnPool(singletonPool(), a); |
824 |
|
} |
825 |
|
|
826 |
|
/** |
839 |
|
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
840 |
|
helpQuiesce(); |
841 |
|
}}; |
842 |
< |
singletonPool.invoke(a); |
842 |
> |
testInvokeOnPool(singletonPool(), a); |
843 |
|
} |
844 |
|
|
845 |
|
/** |
857 |
|
threadAssertTrue(f.isDone()); |
858 |
|
helpQuiesce(); |
859 |
|
}}; |
860 |
< |
singletonPool.invoke(a); |
860 |
> |
testInvokeOnPool(singletonPool(), a); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
875 |
|
helpQuiesce(); |
876 |
|
threadAssertFalse(f.isDone()); |
877 |
|
}}; |
878 |
< |
singletonPool.invoke(a); |
878 |
> |
testInvokeOnPool(singletonPool(), a); |
879 |
|
} |
880 |
|
|
881 |
|
/** |
894 |
|
threadAssertFalse(f.isDone()); |
895 |
|
threadAssertTrue(g.isDone()); |
896 |
|
}}; |
897 |
< |
singletonPool.invoke(a); |
897 |
> |
testInvokeOnPool(singletonPool(), a); |
898 |
|
} |
899 |
|
|
900 |
|
/** |
912 |
|
helpQuiesce(); |
913 |
|
threadAssertTrue(f.isDone()); |
914 |
|
}}; |
915 |
< |
asyncSingletonPool.invoke(a); |
915 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
916 |
|
} |
917 |
|
|
918 |
|
/** |
931 |
|
threadAssertTrue(f.isDone()); |
932 |
|
threadAssertFalse(g.isDone()); |
933 |
|
}}; |
934 |
< |
asyncSingletonPool.invoke(a); |
934 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
935 |
|
} |
936 |
|
|
937 |
|
/** |
950 |
|
threadAssertTrue(f.isDone()); |
951 |
|
threadAssertFalse(g.isDone()); |
952 |
|
}}; |
953 |
< |
asyncSingletonPool.invoke(a); |
953 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
954 |
|
} |
955 |
|
} |