1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 */ 33 34 import static java.util.concurrent.TimeUnit.MILLISECONDS; 35 36 import java.util.HashSet; 37 import java.util.concurrent.CancellationException; 38 import java.util.concurrent.CountedCompleter; 39 import java.util.concurrent.ExecutionException; 40 import java.util.concurrent.ForkJoinPool; 41 import java.util.concurrent.ForkJoinTask; 42 import java.util.concurrent.TimeoutException; 43 import java.util.concurrent.atomic.AtomicInteger; 44 import java.util.concurrent.atomic.AtomicReference; 45 46 import junit.framework.Test; 47 import junit.framework.TestSuite; 48 49 public class CountedCompleterTest extends JSR166TestCase { 50 main(String[] args)51 public static void main(String[] args) { 52 main(suite(), args); 53 } 54 suite()55 public static Test suite() { 56 return new TestSuite(CountedCompleterTest.class); 57 } 58 59 // Runs with "mainPool" use > 1 thread. singletonPool tests use 1 60 static final int mainPoolSize = 61 Math.max(2, Runtime.getRuntime().availableProcessors()); 62 mainPool()63 private static ForkJoinPool mainPool() { 64 return new ForkJoinPool(mainPoolSize); 65 } 66 singletonPool()67 private static ForkJoinPool singletonPool() { 68 return new ForkJoinPool(1); 69 } 70 asyncSingletonPool()71 private static ForkJoinPool asyncSingletonPool() { 72 return new ForkJoinPool(1, 73 ForkJoinPool.defaultForkJoinWorkerThreadFactory, 74 null, true); 75 } 76 testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a)77 private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { 78 try (PoolCleaner cleaner = cleaner(pool)) { 79 assertFalse(a.isDone()); 80 assertFalse(a.isCompletedNormally()); 81 assertFalse(a.isCompletedAbnormally()); 82 assertFalse(a.isCancelled()); 83 assertNull(a.getException()); 84 assertNull(a.getRawResult()); 85 86 assertNull(pool.invoke(a)); 87 88 assertTrue(a.isDone()); 89 assertTrue(a.isCompletedNormally()); 90 assertFalse(a.isCompletedAbnormally()); 91 assertFalse(a.isCancelled()); 92 assertNull(a.getException()); 93 assertNull(a.getRawResult()); 94 } 95 } 96 checkNotDone(CountedCompleter a)97 void checkNotDone(CountedCompleter a) { 98 assertFalse(a.isDone()); 99 assertFalse(a.isCompletedNormally()); 100 assertFalse(a.isCompletedAbnormally()); 101 assertFalse(a.isCancelled()); 102 assertNull(a.getException()); 103 assertNull(a.getRawResult()); 104 105 try { 106 a.get(randomExpiredTimeout(), randomTimeUnit()); 107 shouldThrow(); 108 } catch (TimeoutException success) { 109 } catch (Throwable fail) { threadUnexpectedException(fail); } 110 } 111 checkCompletedNormally(CountedCompleter<?> a)112 void checkCompletedNormally(CountedCompleter<?> a) { 113 assertTrue(a.isDone()); 114 assertFalse(a.isCancelled()); 115 assertTrue(a.isCompletedNormally()); 116 assertFalse(a.isCompletedAbnormally()); 117 assertNull(a.getException()); 118 assertNull(a.getRawResult()); 119 120 { 121 Thread.currentThread().interrupt(); 122 long startTime = System.nanoTime(); 123 assertNull(a.join()); 124 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); 125 Thread.interrupted(); 126 } 127 128 { 129 Thread.currentThread().interrupt(); 130 long startTime = System.nanoTime(); 131 a.quietlyJoin(); // should be no-op 132 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); 133 Thread.interrupted(); 134 } 135 136 assertFalse(a.cancel(false)); 137 assertFalse(a.cancel(true)); 138 try { 139 assertNull(a.get()); 140 assertNull(a.get(randomTimeout(), randomTimeUnit())); 141 } catch (Throwable fail) { threadUnexpectedException(fail); } 142 } 143 144 void checkCancelled(CountedCompleter a) { 145 assertTrue(a.isDone()); 146 assertTrue(a.isCancelled()); 147 assertFalse(a.isCompletedNormally()); 148 assertTrue(a.isCompletedAbnormally()); 149 assertTrue(a.getException() instanceof CancellationException); 150 assertNull(a.getRawResult()); 151 assertTrue(a.cancel(false)); 152 assertTrue(a.cancel(true)); 153 154 try { 155 Thread.currentThread().interrupt(); 156 a.join(); 157 shouldThrow(); 158 } catch (CancellationException success) { 159 } catch (Throwable fail) { threadUnexpectedException(fail); } 160 Thread.interrupted(); 161 162 { 163 long startTime = System.nanoTime(); 164 a.quietlyJoin(); // should be no-op 165 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); 166 } 167 168 try { 169 a.get(); 170 shouldThrow(); 171 } catch (CancellationException success) { 172 } catch (Throwable fail) { threadUnexpectedException(fail); } 173 174 try { 175 a.get(randomTimeout(), randomTimeUnit()); 176 shouldThrow(); 177 } catch (CancellationException success) { 178 } catch (Throwable fail) { threadUnexpectedException(fail); } 179 } 180 181 void checkCompletedAbnormally(CountedCompleter a, Throwable t) { 182 assertTrue(a.isDone()); 183 assertFalse(a.isCancelled()); 184 assertFalse(a.isCompletedNormally()); 185 assertTrue(a.isCompletedAbnormally()); 186 assertSame(t.getClass(), a.getException().getClass()); 187 assertNull(a.getRawResult()); 188 assertFalse(a.cancel(false)); 189 assertFalse(a.cancel(true)); 190 191 try { 192 Thread.currentThread().interrupt(); 193 a.join(); 194 shouldThrow(); 195 } catch (Throwable expected) { 196 assertSame(t.getClass(), expected.getClass()); 197 } 198 Thread.interrupted(); 199 200 { 201 long startTime = System.nanoTime(); 202 a.quietlyJoin(); // should be no-op 203 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); 204 } 205 206 try { 207 a.get(); 208 shouldThrow(); 209 } catch (ExecutionException success) { 210 assertSame(t.getClass(), success.getCause().getClass()); 211 } catch (Throwable fail) { threadUnexpectedException(fail); } 212 213 try { 214 a.get(randomTimeout(), randomTimeUnit()); 215 shouldThrow(); 216 } catch (ExecutionException success) { 217 assertSame(t.getClass(), success.getCause().getClass()); 218 } catch (Throwable fail) { threadUnexpectedException(fail); } 219 220 try { 221 a.invoke(); 222 shouldThrow(); 223 } catch (Throwable success) { 224 assertSame(t, success); 225 } 226 } 227 228 public static final class FJException extends RuntimeException { 229 FJException() { super(); } 230 } 231 232 abstract class CheckedCC extends CountedCompleter<Object> { 233 final AtomicInteger computeN = new AtomicInteger(0); 234 final AtomicInteger onCompletionN = new AtomicInteger(0); 235 final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); 236 final AtomicInteger setRawResultN = new AtomicInteger(0); 237 final AtomicReference<Object> rawResult = new AtomicReference<>(null); 238 int computeN() { return computeN.get(); } 239 int onCompletionN() { return onCompletionN.get(); } 240 int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } 241 int setRawResultN() { return setRawResultN.get(); } 242 243 CheckedCC() { super(); } 244 CheckedCC(CountedCompleter p) { super(p); } 245 CheckedCC(CountedCompleter p, int n) { super(p, n); } 246 abstract void realCompute(); 247 public final void compute() { 248 computeN.incrementAndGet(); 249 realCompute(); 250 } 251 public void onCompletion(CountedCompleter caller) { 252 onCompletionN.incrementAndGet(); 253 super.onCompletion(caller); 254 } 255 public boolean onExceptionalCompletion(Throwable ex, 256 CountedCompleter caller) { 257 onExceptionalCompletionN.incrementAndGet(); 258 assertNotNull(ex); 259 assertTrue(isCompletedAbnormally()); 260 assertTrue(super.onExceptionalCompletion(ex, caller)); 261 return true; 262 } 263 protected void setRawResult(Object t) { 264 setRawResultN.incrementAndGet(); 265 rawResult.set(t); 266 super.setRawResult(t); 267 } 268 void checkIncomplete() { 269 assertEquals(0, computeN()); 270 assertEquals(0, onCompletionN()); 271 assertEquals(0, onExceptionalCompletionN()); 272 assertEquals(0, setRawResultN()); 273 checkNotDone(this); 274 } 275 void checkCompletes(Object rawResult) { 276 checkIncomplete(); 277 int pendingCount = getPendingCount(); 278 complete(rawResult); 279 assertEquals(pendingCount, getPendingCount()); 280 assertEquals(0, computeN()); 281 assertEquals(1, onCompletionN()); 282 assertEquals(0, onExceptionalCompletionN()); 283 assertEquals(1, setRawResultN()); 284 assertSame(rawResult, this.rawResult.get()); 285 checkCompletedNormally(this); 286 } 287 void checkCompletesExceptionally(Throwable ex) { 288 checkIncomplete(); 289 completeExceptionally(ex); 290 checkCompletedExceptionally(ex); 291 } 292 void checkCompletedExceptionally(Throwable ex) { 293 assertEquals(0, computeN()); 294 assertEquals(0, onCompletionN()); 295 assertEquals(1, onExceptionalCompletionN()); 296 assertEquals(0, setRawResultN()); 297 assertNull(this.rawResult.get()); 298 checkCompletedAbnormally(this, ex); 299 } 300 } 301 302 final class NoopCC extends CheckedCC { 303 NoopCC() { super(); } 304 NoopCC(CountedCompleter p) { super(p); } 305 NoopCC(CountedCompleter p, int initialPendingCount) { 306 super(p, initialPendingCount); 307 } 308 protected void realCompute() {} 309 } 310 311 /** 312 * A newly constructed CountedCompleter is not completed; 313 * complete() causes completion. pendingCount is ignored. 314 */ 315 public void testComplete() { 316 for (Object x : new Object[] { Boolean.TRUE, null }) { 317 for (int pendingCount : new int[] { 0, 42 }) { 318 testComplete(new NoopCC(), x, pendingCount); 319 testComplete(new NoopCC(new NoopCC()), x, pendingCount); 320 } 321 } 322 } 323 void testComplete(NoopCC cc, Object x, int pendingCount) { 324 cc.setPendingCount(pendingCount); 325 cc.checkCompletes(x); 326 assertEquals(pendingCount, cc.getPendingCount()); 327 } 328 329 /** 330 * completeExceptionally completes exceptionally 331 */ 332 public void testCompleteExceptionally() { 333 new NoopCC() 334 .checkCompletesExceptionally(new FJException()); 335 new NoopCC(new NoopCC()) 336 .checkCompletesExceptionally(new FJException()); 337 } 338 339 /** 340 * completeExceptionally(null) surprisingly has the same effect as 341 * completeExceptionally(new RuntimeException()) 342 */ 343 public void testCompleteExceptionally_null() { 344 NoopCC a = new NoopCC(); 345 a.completeExceptionally(null); 346 try { 347 a.invoke(); 348 shouldThrow(); 349 } catch (RuntimeException success) { 350 assertSame(success.getClass(), RuntimeException.class); 351 assertNull(success.getCause()); 352 a.checkCompletedExceptionally(success); 353 } 354 } 355 356 /** 357 * setPendingCount sets the reported pending count 358 */ 359 public void testSetPendingCount() { 360 NoopCC a = new NoopCC(); 361 assertEquals(0, a.getPendingCount()); 362 int[] vals = { 363 -1, 0, 1, 364 Integer.MIN_VALUE, 365 Integer.MAX_VALUE, 366 }; 367 for (int val : vals) { 368 a.setPendingCount(val); 369 assertEquals(val, a.getPendingCount()); 370 } 371 } 372 373 /** 374 * addToPendingCount adds to the reported pending count 375 */ 376 public void testAddToPendingCount() { 377 NoopCC a = new NoopCC(); 378 assertEquals(0, a.getPendingCount()); 379 a.addToPendingCount(1); 380 assertEquals(1, a.getPendingCount()); 381 a.addToPendingCount(27); 382 assertEquals(28, a.getPendingCount()); 383 a.addToPendingCount(-28); 384 assertEquals(0, a.getPendingCount()); 385 } 386 387 /** 388 * decrementPendingCountUnlessZero decrements reported pending 389 * count unless zero 390 */ 391 public void testDecrementPendingCountUnlessZero() { 392 NoopCC a = new NoopCC(null, 2); 393 assertEquals(2, a.getPendingCount()); 394 assertEquals(2, a.decrementPendingCountUnlessZero()); 395 assertEquals(1, a.getPendingCount()); 396 assertEquals(1, a.decrementPendingCountUnlessZero()); 397 assertEquals(0, a.getPendingCount()); 398 assertEquals(0, a.decrementPendingCountUnlessZero()); 399 assertEquals(0, a.getPendingCount()); 400 a.setPendingCount(-1); 401 assertEquals(-1, a.decrementPendingCountUnlessZero()); 402 assertEquals(-2, a.getPendingCount()); 403 } 404 405 /** 406 * compareAndSetPendingCount compares and sets the reported 407 * pending count 408 */ 409 public void testCompareAndSetPendingCount() { 410 NoopCC a = new NoopCC(); 411 assertEquals(0, a.getPendingCount()); 412 assertTrue(a.compareAndSetPendingCount(0, 1)); 413 assertEquals(1, a.getPendingCount()); 414 assertTrue(a.compareAndSetPendingCount(1, 2)); 415 assertEquals(2, a.getPendingCount()); 416 assertFalse(a.compareAndSetPendingCount(1, 3)); 417 assertEquals(2, a.getPendingCount()); 418 } 419 420 /** 421 * getCompleter returns parent or null if at root 422 */ 423 public void testGetCompleter() { 424 NoopCC a = new NoopCC(); 425 assertNull(a.getCompleter()); 426 CountedCompleter b = new NoopCC(a); 427 assertSame(a, b.getCompleter()); 428 CountedCompleter c = new NoopCC(b); 429 assertSame(b, c.getCompleter()); 430 } 431 432 /** 433 * getRoot returns self if no parent, else parent's root 434 */ 435 public void testGetRoot() { 436 NoopCC a = new NoopCC(); 437 NoopCC b = new NoopCC(a); 438 NoopCC c = new NoopCC(b); 439 assertSame(a, a.getRoot()); 440 assertSame(a, b.getRoot()); 441 assertSame(a, c.getRoot()); 442 } 443 444 /** 445 * tryComplete decrements pending count unless zero, in which case 446 * causes completion 447 */ 448 public void testTryComplete() { 449 NoopCC a = new NoopCC(); 450 assertEquals(0, a.getPendingCount()); 451 int n = 3; 452 a.setPendingCount(n); 453 for (; n > 0; n--) { 454 assertEquals(n, a.getPendingCount()); 455 a.tryComplete(); 456 a.checkIncomplete(); 457 assertEquals(n - 1, a.getPendingCount()); 458 } 459 a.tryComplete(); 460 assertEquals(0, a.computeN()); 461 assertEquals(1, a.onCompletionN()); 462 assertEquals(0, a.onExceptionalCompletionN()); 463 assertEquals(0, a.setRawResultN()); 464 checkCompletedNormally(a); 465 } 466 467 /** 468 * propagateCompletion decrements pending count unless zero, in 469 * which case causes completion, without invoking onCompletion 470 */ 471 public void testPropagateCompletion() { 472 NoopCC a = new NoopCC(); 473 assertEquals(0, a.getPendingCount()); 474 int n = 3; 475 a.setPendingCount(n); 476 for (; n > 0; n--) { 477 assertEquals(n, a.getPendingCount()); 478 a.propagateCompletion(); 479 a.checkIncomplete(); 480 assertEquals(n - 1, a.getPendingCount()); 481 } 482 a.propagateCompletion(); 483 assertEquals(0, a.computeN()); 484 assertEquals(0, a.onCompletionN()); 485 assertEquals(0, a.onExceptionalCompletionN()); 486 assertEquals(0, a.setRawResultN()); 487 checkCompletedNormally(a); 488 } 489 490 /** 491 * firstComplete returns this if pending count is zero else null 492 */ 493 public void testFirstComplete() { 494 NoopCC a = new NoopCC(); 495 a.setPendingCount(1); 496 assertNull(a.firstComplete()); 497 a.checkIncomplete(); 498 assertSame(a, a.firstComplete()); 499 a.checkIncomplete(); 500 } 501 502 /** 503 * firstComplete.nextComplete returns parent if pending count is 504 * zero else null 505 */ 506 public void testNextComplete() { 507 NoopCC a = new NoopCC(); 508 NoopCC b = new NoopCC(a); 509 a.setPendingCount(1); 510 b.setPendingCount(1); 511 assertNull(b.firstComplete()); 512 assertSame(b, b.firstComplete()); 513 assertNull(b.nextComplete()); 514 a.checkIncomplete(); 515 b.checkIncomplete(); 516 assertSame(a, b.nextComplete()); 517 assertSame(a, b.nextComplete()); 518 a.checkIncomplete(); 519 b.checkIncomplete(); 520 assertNull(a.nextComplete()); 521 b.checkIncomplete(); 522 checkCompletedNormally(a); 523 } 524 525 /** 526 * quietlyCompleteRoot completes root task and only root task 527 */ 528 public void testQuietlyCompleteRoot() { 529 NoopCC a = new NoopCC(); 530 NoopCC b = new NoopCC(a); 531 NoopCC c = new NoopCC(b); 532 a.setPendingCount(1); 533 b.setPendingCount(1); 534 c.setPendingCount(1); 535 c.quietlyCompleteRoot(); 536 assertTrue(a.isDone()); 537 assertFalse(b.isDone()); 538 assertFalse(c.isDone()); 539 } 540 541 // Invocation tests use some interdependent task classes 542 // to better test propagation etc 543 544 /** 545 * Version of Fibonacci with different classes for left vs right forks 546 */ 547 abstract class CCF extends CheckedCC { 548 int number; 549 int rnumber; 550 551 public CCF(CountedCompleter parent, int n) { 552 super(parent, 1); 553 this.number = n; 554 } 555 556 protected final void realCompute() { 557 CCF f = this; 558 int n = number; 559 while (n >= 2) { 560 new RCCF(f, n - 2).fork(); 561 f = new LCCF(f, --n); 562 } 563 f.complete(null); 564 } 565 } 566 567 final class LCCF extends CCF { 568 public LCCF(int n) { this(null, n); } 569 public LCCF(CountedCompleter parent, int n) { 570 super(parent, n); 571 } 572 public final void onCompletion(CountedCompleter caller) { 573 super.onCompletion(caller); 574 CCF p = (CCF)getCompleter(); 575 int n = number + rnumber; 576 if (p != null) 577 p.number = n; 578 else 579 number = n; 580 } 581 } 582 final class RCCF extends CCF { 583 public RCCF(CountedCompleter parent, int n) { 584 super(parent, n); 585 } 586 public final void onCompletion(CountedCompleter caller) { 587 super.onCompletion(caller); 588 CCF p = (CCF)getCompleter(); 589 int n = number + rnumber; 590 if (p != null) 591 p.rnumber = n; 592 else 593 number = n; 594 } 595 } 596 597 // Version of CCF with forced failure in left completions 598 abstract class FailingCCF extends CheckedCC { 599 int number; 600 int rnumber; 601 602 public FailingCCF(CountedCompleter parent, int n) { 603 super(parent, 1); 604 this.number = n; 605 } 606 607 protected final void realCompute() { 608 FailingCCF f = this; 609 int n = number; 610 while (n >= 2) { 611 new RFCCF(f, n - 2).fork(); 612 f = new LFCCF(f, --n); 613 } 614 f.complete(null); 615 } 616 } 617 618 final class LFCCF extends FailingCCF { LFCCF(int n)619 public LFCCF(int n) { this(null, n); } LFCCF(CountedCompleter parent, int n)620 public LFCCF(CountedCompleter parent, int n) { 621 super(parent, n); 622 } onCompletion(CountedCompleter caller)623 public final void onCompletion(CountedCompleter caller) { 624 super.onCompletion(caller); 625 FailingCCF p = (FailingCCF)getCompleter(); 626 int n = number + rnumber; 627 if (p != null) 628 p.number = n; 629 else 630 number = n; 631 } 632 } 633 final class RFCCF extends FailingCCF { RFCCF(CountedCompleter parent, int n)634 public RFCCF(CountedCompleter parent, int n) { 635 super(parent, n); 636 } onCompletion(CountedCompleter caller)637 public final void onCompletion(CountedCompleter caller) { 638 super.onCompletion(caller); 639 completeExceptionally(new FJException()); 640 } 641 } 642 643 /** 644 * invoke returns when task completes normally. 645 * isCompletedAbnormally and isCancelled return false for normally 646 * completed tasks; getRawResult returns null. 647 */ testInvoke()648 public void testInvoke() { 649 ForkJoinTask a = new CheckedRecursiveAction() { 650 protected void realCompute() { 651 CCF f = new LCCF(8); 652 assertNull(f.invoke()); 653 assertEquals(21, f.number); 654 checkCompletedNormally(f); 655 }}; 656 testInvokeOnPool(mainPool(), a); 657 } 658 659 /** 660 * quietlyInvoke task returns when task completes normally. 661 * isCompletedAbnormally and isCancelled return false for normally 662 * completed tasks 663 */ testQuietlyInvoke()664 public void testQuietlyInvoke() { 665 ForkJoinTask a = new CheckedRecursiveAction() { 666 protected void realCompute() { 667 CCF f = new LCCF(8); 668 f.quietlyInvoke(); 669 assertEquals(21, f.number); 670 checkCompletedNormally(f); 671 }}; 672 testInvokeOnPool(mainPool(), a); 673 } 674 675 /** 676 * join of a forked task returns when task completes 677 */ testForkJoin()678 public void testForkJoin() { 679 ForkJoinTask a = new CheckedRecursiveAction() { 680 protected void realCompute() { 681 CCF f = new LCCF(8); 682 assertSame(f, f.fork()); 683 assertNull(f.join()); 684 assertEquals(21, f.number); 685 checkCompletedNormally(f); 686 }}; 687 testInvokeOnPool(mainPool(), a); 688 } 689 690 /** 691 * get of a forked task returns when task completes 692 */ testForkGet()693 public void testForkGet() { 694 ForkJoinTask a = new CheckedRecursiveAction() { 695 protected void realCompute() throws Exception { 696 CCF f = new LCCF(8); 697 assertSame(f, f.fork()); 698 assertNull(f.get()); 699 assertEquals(21, f.number); 700 checkCompletedNormally(f); 701 }}; 702 testInvokeOnPool(mainPool(), a); 703 } 704 705 /** 706 * timed get of a forked task returns when task completes 707 */ testForkTimedGet()708 public void testForkTimedGet() { 709 ForkJoinTask a = new CheckedRecursiveAction() { 710 protected void realCompute() throws Exception { 711 CCF f = new LCCF(8); 712 assertSame(f, f.fork()); 713 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); 714 assertEquals(21, f.number); 715 checkCompletedNormally(f); 716 }}; 717 testInvokeOnPool(mainPool(), a); 718 } 719 720 /** 721 * timed get with null time unit throws NPE 722 */ testForkTimedGetNPE()723 public void testForkTimedGetNPE() { 724 ForkJoinTask a = new CheckedRecursiveAction() { 725 protected void realCompute() throws Exception { 726 CCF f = new LCCF(8); 727 assertSame(f, f.fork()); 728 try { 729 f.get(randomTimeout(), null); 730 shouldThrow(); 731 } catch (NullPointerException success) {} 732 }}; 733 testInvokeOnPool(mainPool(), a); 734 } 735 736 /** 737 * quietlyJoin of a forked task returns when task completes 738 */ testForkQuietlyJoin()739 public void testForkQuietlyJoin() { 740 ForkJoinTask a = new CheckedRecursiveAction() { 741 protected void realCompute() { 742 CCF f = new LCCF(8); 743 assertSame(f, f.fork()); 744 f.quietlyJoin(); 745 assertEquals(21, f.number); 746 checkCompletedNormally(f); 747 }}; 748 testInvokeOnPool(mainPool(), a); 749 } 750 751 /** 752 * helpQuiesce returns when tasks are complete. 753 * getQueuedTaskCount returns 0 when quiescent 754 */ testForkHelpQuiesce()755 public void testForkHelpQuiesce() { 756 ForkJoinTask a = new CheckedRecursiveAction() { 757 protected void realCompute() { 758 CCF f = new LCCF(8); 759 assertSame(f, f.fork()); 760 helpQuiesce(); 761 while (!f.isDone()) // wait out race 762 ; 763 assertEquals(21, f.number); 764 assertEquals(0, getQueuedTaskCount()); 765 checkCompletedNormally(f); 766 }}; 767 testInvokeOnPool(mainPool(), a); 768 } 769 770 /** 771 * invoke task throws exception when task completes abnormally 772 */ testAbnormalInvoke()773 public void testAbnormalInvoke() { 774 ForkJoinTask a = new CheckedRecursiveAction() { 775 protected void realCompute() { 776 FailingCCF f = new LFCCF(8); 777 try { 778 f.invoke(); 779 shouldThrow(); 780 } catch (FJException success) { 781 checkCompletedAbnormally(f, success); 782 } 783 }}; 784 testInvokeOnPool(mainPool(), a); 785 } 786 787 /** 788 * quietlyInvoke task returns when task completes abnormally 789 */ testAbnormalQuietlyInvoke()790 public void testAbnormalQuietlyInvoke() { 791 ForkJoinTask a = new CheckedRecursiveAction() { 792 protected void realCompute() { 793 FailingCCF f = new LFCCF(8); 794 f.quietlyInvoke(); 795 assertTrue(f.getException() instanceof FJException); 796 checkCompletedAbnormally(f, f.getException()); 797 }}; 798 testInvokeOnPool(mainPool(), a); 799 } 800 801 /** 802 * join of a forked task throws exception when task completes abnormally 803 */ testAbnormalForkJoin()804 public void testAbnormalForkJoin() { 805 ForkJoinTask a = new CheckedRecursiveAction() { 806 protected void realCompute() { 807 FailingCCF f = new LFCCF(8); 808 assertSame(f, f.fork()); 809 try { 810 f.join(); 811 shouldThrow(); 812 } catch (FJException success) { 813 checkCompletedAbnormally(f, success); 814 } 815 }}; 816 testInvokeOnPool(mainPool(), a); 817 } 818 819 /** 820 * get of a forked task throws exception when task completes abnormally 821 */ testAbnormalForkGet()822 public void testAbnormalForkGet() { 823 ForkJoinTask a = new CheckedRecursiveAction() { 824 protected void realCompute() throws Exception { 825 FailingCCF f = new LFCCF(8); 826 assertSame(f, f.fork()); 827 try { 828 f.get(); 829 shouldThrow(); 830 } catch (ExecutionException success) { 831 Throwable cause = success.getCause(); 832 assertTrue(cause instanceof FJException); 833 checkCompletedAbnormally(f, cause); 834 } 835 }}; 836 testInvokeOnPool(mainPool(), a); 837 } 838 839 /** 840 * timed get of a forked task throws exception when task completes abnormally 841 */ testAbnormalForkTimedGet()842 public void testAbnormalForkTimedGet() { 843 ForkJoinTask a = new CheckedRecursiveAction() { 844 protected void realCompute() throws Exception { 845 FailingCCF f = new LFCCF(8); 846 assertSame(f, f.fork()); 847 try { 848 f.get(LONG_DELAY_MS, MILLISECONDS); 849 shouldThrow(); 850 } catch (ExecutionException success) { 851 Throwable cause = success.getCause(); 852 assertTrue(cause instanceof FJException); 853 checkCompletedAbnormally(f, cause); 854 } 855 }}; 856 testInvokeOnPool(mainPool(), a); 857 } 858 859 /** 860 * quietlyJoin of a forked task returns when task completes abnormally 861 */ testAbnormalForkQuietlyJoin()862 public void testAbnormalForkQuietlyJoin() { 863 ForkJoinTask a = new CheckedRecursiveAction() { 864 protected void realCompute() { 865 FailingCCF f = new LFCCF(8); 866 assertSame(f, f.fork()); 867 f.quietlyJoin(); 868 assertTrue(f.getException() instanceof FJException); 869 checkCompletedAbnormally(f, f.getException()); 870 }}; 871 testInvokeOnPool(mainPool(), a); 872 } 873 874 /** 875 * invoke task throws exception when task cancelled 876 */ testCancelledInvoke()877 public void testCancelledInvoke() { 878 ForkJoinTask a = new CheckedRecursiveAction() { 879 protected void realCompute() { 880 CCF f = new LCCF(8); 881 assertTrue(f.cancel(true)); 882 try { 883 f.invoke(); 884 shouldThrow(); 885 } catch (CancellationException success) { 886 checkCancelled(f); 887 } 888 }}; 889 testInvokeOnPool(mainPool(), a); 890 } 891 892 /** 893 * join of a forked task throws exception when task cancelled 894 */ testCancelledForkJoin()895 public void testCancelledForkJoin() { 896 ForkJoinTask a = new CheckedRecursiveAction() { 897 protected void realCompute() { 898 CCF f = new LCCF(8); 899 assertTrue(f.cancel(true)); 900 assertSame(f, f.fork()); 901 try { 902 f.join(); 903 shouldThrow(); 904 } catch (CancellationException success) { 905 checkCancelled(f); 906 } 907 }}; 908 testInvokeOnPool(mainPool(), a); 909 } 910 911 /** 912 * get of a forked task throws exception when task cancelled 913 */ testCancelledForkGet()914 public void testCancelledForkGet() { 915 ForkJoinTask a = new CheckedRecursiveAction() { 916 protected void realCompute() throws Exception { 917 CCF f = new LCCF(8); 918 assertTrue(f.cancel(true)); 919 assertSame(f, f.fork()); 920 try { 921 f.get(); 922 shouldThrow(); 923 } catch (CancellationException success) { 924 checkCancelled(f); 925 } 926 }}; 927 testInvokeOnPool(mainPool(), a); 928 } 929 930 /** 931 * timed get of a forked task throws exception when task cancelled 932 */ testCancelledForkTimedGet()933 public void testCancelledForkTimedGet() throws Exception { 934 ForkJoinTask a = new CheckedRecursiveAction() { 935 protected void realCompute() throws Exception { 936 CCF f = new LCCF(8); 937 assertTrue(f.cancel(true)); 938 assertSame(f, f.fork()); 939 try { 940 f.get(LONG_DELAY_MS, MILLISECONDS); 941 shouldThrow(); 942 } catch (CancellationException success) { 943 checkCancelled(f); 944 } 945 }}; 946 testInvokeOnPool(mainPool(), a); 947 } 948 949 /** 950 * quietlyJoin of a forked task returns when task cancelled 951 */ testCancelledForkQuietlyJoin()952 public void testCancelledForkQuietlyJoin() { 953 ForkJoinTask a = new CheckedRecursiveAction() { 954 protected void realCompute() { 955 CCF f = new LCCF(8); 956 assertTrue(f.cancel(true)); 957 assertSame(f, f.fork()); 958 f.quietlyJoin(); 959 checkCancelled(f); 960 }}; 961 testInvokeOnPool(mainPool(), a); 962 } 963 964 /** 965 * getPool of executing task returns its pool 966 */ testGetPool()967 public void testGetPool() { 968 final ForkJoinPool mainPool = mainPool(); 969 ForkJoinTask a = new CheckedRecursiveAction() { 970 protected void realCompute() { 971 assertSame(mainPool, getPool()); 972 }}; 973 testInvokeOnPool(mainPool, a); 974 } 975 976 /** 977 * getPool of non-FJ task returns null 978 */ testGetPool2()979 public void testGetPool2() { 980 ForkJoinTask a = new CheckedRecursiveAction() { 981 protected void realCompute() { 982 assertNull(getPool()); 983 }}; 984 assertNull(a.invoke()); 985 } 986 987 /** 988 * inForkJoinPool of executing task returns true 989 */ testInForkJoinPool()990 public void testInForkJoinPool() { 991 ForkJoinTask a = new CheckedRecursiveAction() { 992 protected void realCompute() { 993 assertTrue(inForkJoinPool()); 994 }}; 995 testInvokeOnPool(mainPool(), a); 996 } 997 998 /** 999 * inForkJoinPool of non-FJ task returns false 1000 */ testInForkJoinPool2()1001 public void testInForkJoinPool2() { 1002 ForkJoinTask a = new CheckedRecursiveAction() { 1003 protected void realCompute() { 1004 assertFalse(inForkJoinPool()); 1005 }}; 1006 assertNull(a.invoke()); 1007 } 1008 1009 /** 1010 * setRawResult(null) succeeds 1011 */ testSetRawResult()1012 public void testSetRawResult() { 1013 ForkJoinTask a = new CheckedRecursiveAction() { 1014 protected void realCompute() { 1015 setRawResult(null); 1016 assertNull(getRawResult()); 1017 }}; 1018 assertNull(a.invoke()); 1019 } 1020 1021 /** 1022 * invoke task throws exception after invoking completeExceptionally 1023 */ testCompleteExceptionally2()1024 public void testCompleteExceptionally2() { 1025 ForkJoinTask a = new CheckedRecursiveAction() { 1026 protected void realCompute() { 1027 CCF n = new LCCF(8); 1028 CCF f = new LCCF(n, 8); 1029 FJException ex = new FJException(); 1030 f.completeExceptionally(ex); 1031 f.checkCompletedExceptionally(ex); 1032 n.checkCompletedExceptionally(ex); 1033 }}; 1034 testInvokeOnPool(mainPool(), a); 1035 } 1036 1037 /** 1038 * invokeAll(t1, t2) invokes all task arguments 1039 */ testInvokeAll2()1040 public void testInvokeAll2() { 1041 ForkJoinTask a = new CheckedRecursiveAction() { 1042 protected void realCompute() { 1043 CCF f = new LCCF(8); 1044 CCF g = new LCCF(9); 1045 invokeAll(f, g); 1046 assertEquals(21, f.number); 1047 assertEquals(34, g.number); 1048 checkCompletedNormally(f); 1049 checkCompletedNormally(g); 1050 }}; 1051 testInvokeOnPool(mainPool(), a); 1052 } 1053 1054 /** 1055 * invokeAll(tasks) with 1 argument invokes task 1056 */ testInvokeAll1()1057 public void testInvokeAll1() { 1058 ForkJoinTask a = new CheckedRecursiveAction() { 1059 protected void realCompute() { 1060 CCF f = new LCCF(8); 1061 invokeAll(f); 1062 checkCompletedNormally(f); 1063 assertEquals(21, f.number); 1064 }}; 1065 testInvokeOnPool(mainPool(), a); 1066 } 1067 1068 /** 1069 * invokeAll(tasks) with > 2 argument invokes tasks 1070 */ testInvokeAll3()1071 public void testInvokeAll3() { 1072 ForkJoinTask a = new CheckedRecursiveAction() { 1073 protected void realCompute() { 1074 CCF f = new LCCF(8); 1075 CCF g = new LCCF(9); 1076 CCF h = new LCCF(7); 1077 invokeAll(f, g, h); 1078 assertEquals(21, f.number); 1079 assertEquals(34, g.number); 1080 assertEquals(13, h.number); 1081 checkCompletedNormally(f); 1082 checkCompletedNormally(g); 1083 checkCompletedNormally(h); 1084 }}; 1085 testInvokeOnPool(mainPool(), a); 1086 } 1087 1088 /** 1089 * invokeAll(collection) invokes all tasks in the collection 1090 */ testInvokeAllCollection()1091 public void testInvokeAllCollection() { 1092 ForkJoinTask a = new CheckedRecursiveAction() { 1093 protected void realCompute() { 1094 CCF f = new LCCF(8); 1095 CCF g = new LCCF(9); 1096 CCF h = new LCCF(7); 1097 HashSet set = new HashSet(); 1098 set.add(f); 1099 set.add(g); 1100 set.add(h); 1101 invokeAll(set); 1102 assertEquals(21, f.number); 1103 assertEquals(34, g.number); 1104 assertEquals(13, h.number); 1105 checkCompletedNormally(f); 1106 checkCompletedNormally(g); 1107 checkCompletedNormally(h); 1108 }}; 1109 testInvokeOnPool(mainPool(), a); 1110 } 1111 1112 /** 1113 * invokeAll(tasks) with any null task throws NPE 1114 */ testInvokeAllNPE()1115 public void testInvokeAllNPE() { 1116 ForkJoinTask a = new CheckedRecursiveAction() { 1117 protected void realCompute() { 1118 CCF f = new LCCF(8); 1119 CCF g = new LCCF(9); 1120 CCF h = null; 1121 try { 1122 invokeAll(f, g, h); 1123 shouldThrow(); 1124 } catch (NullPointerException success) {} 1125 }}; 1126 testInvokeOnPool(mainPool(), a); 1127 } 1128 1129 /** 1130 * invokeAll(t1, t2) throw exception if any task does 1131 */ testAbnormalInvokeAll2()1132 public void testAbnormalInvokeAll2() { 1133 ForkJoinTask a = new CheckedRecursiveAction() { 1134 protected void realCompute() { 1135 CCF f = new LCCF(8); 1136 FailingCCF g = new LFCCF(9); 1137 try { 1138 invokeAll(f, g); 1139 shouldThrow(); 1140 } catch (FJException success) { 1141 checkCompletedAbnormally(g, success); 1142 } 1143 }}; 1144 testInvokeOnPool(mainPool(), a); 1145 } 1146 1147 /** 1148 * invokeAll(tasks) with 1 argument throws exception if task does 1149 */ testAbnormalInvokeAll1()1150 public void testAbnormalInvokeAll1() { 1151 ForkJoinTask a = new CheckedRecursiveAction() { 1152 protected void realCompute() { 1153 FailingCCF g = new LFCCF(9); 1154 try { 1155 invokeAll(g); 1156 shouldThrow(); 1157 } catch (FJException success) { 1158 checkCompletedAbnormally(g, success); 1159 } 1160 }}; 1161 testInvokeOnPool(mainPool(), a); 1162 } 1163 1164 /** 1165 * invokeAll(tasks) with > 2 argument throws exception if any task does 1166 */ testAbnormalInvokeAll3()1167 public void testAbnormalInvokeAll3() { 1168 ForkJoinTask a = new CheckedRecursiveAction() { 1169 protected void realCompute() { 1170 CCF f = new LCCF(8); 1171 FailingCCF g = new LFCCF(9); 1172 CCF h = new LCCF(7); 1173 try { 1174 invokeAll(f, g, h); 1175 shouldThrow(); 1176 } catch (FJException success) { 1177 checkCompletedAbnormally(g, success); 1178 } 1179 }}; 1180 testInvokeOnPool(mainPool(), a); 1181 } 1182 1183 /** 1184 * invokeAll(collection) throws exception if any task does 1185 */ testAbnormalInvokeAllCollection()1186 public void testAbnormalInvokeAllCollection() { 1187 ForkJoinTask a = new CheckedRecursiveAction() { 1188 protected void realCompute() { 1189 FailingCCF f = new LFCCF(8); 1190 CCF g = new LCCF(9); 1191 CCF h = new LCCF(7); 1192 HashSet set = new HashSet(); 1193 set.add(f); 1194 set.add(g); 1195 set.add(h); 1196 try { 1197 invokeAll(set); 1198 shouldThrow(); 1199 } catch (FJException success) { 1200 checkCompletedAbnormally(f, success); 1201 } 1202 }}; 1203 testInvokeOnPool(mainPool(), a); 1204 } 1205 1206 /** 1207 * tryUnfork returns true for most recent unexecuted task, 1208 * and suppresses execution 1209 */ testTryUnfork()1210 public void testTryUnfork() { 1211 ForkJoinTask a = new CheckedRecursiveAction() { 1212 protected void realCompute() { 1213 CCF g = new LCCF(9); 1214 assertSame(g, g.fork()); 1215 CCF f = new LCCF(8); 1216 assertSame(f, f.fork()); 1217 assertTrue(f.tryUnfork()); 1218 helpQuiesce(); 1219 checkNotDone(f); 1220 checkCompletedNormally(g); 1221 }}; 1222 testInvokeOnPool(singletonPool(), a); 1223 } 1224 1225 /** 1226 * getSurplusQueuedTaskCount returns > 0 when 1227 * there are more tasks than threads 1228 */ testGetSurplusQueuedTaskCount()1229 public void testGetSurplusQueuedTaskCount() { 1230 ForkJoinTask a = new CheckedRecursiveAction() { 1231 protected void realCompute() { 1232 CCF h = new LCCF(7); 1233 assertSame(h, h.fork()); 1234 CCF g = new LCCF(9); 1235 assertSame(g, g.fork()); 1236 CCF f = new LCCF(8); 1237 assertSame(f, f.fork()); 1238 assertTrue(getSurplusQueuedTaskCount() > 0); 1239 helpQuiesce(); 1240 assertEquals(0, getSurplusQueuedTaskCount()); 1241 checkCompletedNormally(f); 1242 checkCompletedNormally(g); 1243 checkCompletedNormally(h); 1244 }}; 1245 testInvokeOnPool(singletonPool(), a); 1246 } 1247 1248 /** 1249 * peekNextLocalTask returns most recent unexecuted task. 1250 */ testPeekNextLocalTask()1251 public void testPeekNextLocalTask() { 1252 ForkJoinTask a = new CheckedRecursiveAction() { 1253 protected void realCompute() { 1254 CCF g = new LCCF(9); 1255 assertSame(g, g.fork()); 1256 CCF f = new LCCF(8); 1257 assertSame(f, f.fork()); 1258 assertSame(f, peekNextLocalTask()); 1259 assertNull(f.join()); 1260 checkCompletedNormally(f); 1261 helpQuiesce(); 1262 checkCompletedNormally(g); 1263 }}; 1264 testInvokeOnPool(singletonPool(), a); 1265 } 1266 1267 /** 1268 * pollNextLocalTask returns most recent unexecuted task without 1269 * executing it 1270 */ testPollNextLocalTask()1271 public void testPollNextLocalTask() { 1272 ForkJoinTask a = new CheckedRecursiveAction() { 1273 protected void realCompute() { 1274 CCF g = new LCCF(9); 1275 assertSame(g, g.fork()); 1276 CCF f = new LCCF(8); 1277 assertSame(f, f.fork()); 1278 assertSame(f, pollNextLocalTask()); 1279 helpQuiesce(); 1280 checkNotDone(f); 1281 assertEquals(34, g.number); 1282 checkCompletedNormally(g); 1283 }}; 1284 testInvokeOnPool(singletonPool(), a); 1285 } 1286 1287 /** 1288 * pollTask returns an unexecuted task without executing it 1289 */ testPollTask()1290 public void testPollTask() { 1291 ForkJoinTask a = new CheckedRecursiveAction() { 1292 protected void realCompute() { 1293 CCF g = new LCCF(9); 1294 assertSame(g, g.fork()); 1295 CCF f = new LCCF(8); 1296 assertSame(f, f.fork()); 1297 assertSame(f, pollTask()); 1298 helpQuiesce(); 1299 checkNotDone(f); 1300 checkCompletedNormally(g); 1301 }}; 1302 testInvokeOnPool(singletonPool(), a); 1303 } 1304 1305 /** 1306 * peekNextLocalTask returns least recent unexecuted task in async mode 1307 */ testPeekNextLocalTaskAsync()1308 public void testPeekNextLocalTaskAsync() { 1309 ForkJoinTask a = new CheckedRecursiveAction() { 1310 protected void realCompute() { 1311 CCF g = new LCCF(9); 1312 assertSame(g, g.fork()); 1313 CCF f = new LCCF(8); 1314 assertSame(f, f.fork()); 1315 assertSame(g, peekNextLocalTask()); 1316 assertNull(f.join()); 1317 helpQuiesce(); 1318 checkCompletedNormally(f); 1319 assertEquals(34, g.number); 1320 checkCompletedNormally(g); 1321 }}; 1322 testInvokeOnPool(asyncSingletonPool(), a); 1323 } 1324 1325 /** 1326 * pollNextLocalTask returns least recent unexecuted task without 1327 * executing it, in async mode 1328 */ testPollNextLocalTaskAsync()1329 public void testPollNextLocalTaskAsync() { 1330 ForkJoinTask a = new CheckedRecursiveAction() { 1331 protected void realCompute() { 1332 CCF g = new LCCF(9); 1333 assertSame(g, g.fork()); 1334 CCF f = new LCCF(8); 1335 assertSame(f, f.fork()); 1336 assertSame(g, pollNextLocalTask()); 1337 helpQuiesce(); 1338 assertEquals(21, f.number); 1339 checkCompletedNormally(f); 1340 checkNotDone(g); 1341 }}; 1342 testInvokeOnPool(asyncSingletonPool(), a); 1343 } 1344 1345 /** 1346 * pollTask returns an unexecuted task without executing it, in 1347 * async mode 1348 */ testPollTaskAsync()1349 public void testPollTaskAsync() { 1350 ForkJoinTask a = new CheckedRecursiveAction() { 1351 protected void realCompute() { 1352 CCF g = new LCCF(9); 1353 assertSame(g, g.fork()); 1354 CCF f = new LCCF(8); 1355 assertSame(f, f.fork()); 1356 assertSame(g, pollTask()); 1357 helpQuiesce(); 1358 assertEquals(21, f.number); 1359 checkCompletedNormally(f); 1360 checkNotDone(g); 1361 }}; 1362 testInvokeOnPool(asyncSingletonPool(), a); 1363 } 1364 1365 // versions for singleton pools 1366 1367 /** 1368 * invoke returns when task completes normally. 1369 * isCompletedAbnormally and isCancelled return false for normally 1370 * completed tasks; getRawResult returns null. 1371 */ testInvokeSingleton()1372 public void testInvokeSingleton() { 1373 ForkJoinTask a = new CheckedRecursiveAction() { 1374 protected void realCompute() { 1375 CCF f = new LCCF(8); 1376 assertNull(f.invoke()); 1377 assertEquals(21, f.number); 1378 checkCompletedNormally(f); 1379 }}; 1380 testInvokeOnPool(singletonPool(), a); 1381 } 1382 1383 /** 1384 * quietlyInvoke task returns when task completes normally. 1385 * isCompletedAbnormally and isCancelled return false for normally 1386 * completed tasks 1387 */ testQuietlyInvokeSingleton()1388 public void testQuietlyInvokeSingleton() { 1389 ForkJoinTask a = new CheckedRecursiveAction() { 1390 protected void realCompute() { 1391 CCF f = new LCCF(8); 1392 f.quietlyInvoke(); 1393 assertEquals(21, f.number); 1394 checkCompletedNormally(f); 1395 }}; 1396 testInvokeOnPool(singletonPool(), a); 1397 } 1398 1399 /** 1400 * join of a forked task returns when task completes 1401 */ testForkJoinSingleton()1402 public void testForkJoinSingleton() { 1403 ForkJoinTask a = new CheckedRecursiveAction() { 1404 protected void realCompute() { 1405 CCF f = new LCCF(8); 1406 assertSame(f, f.fork()); 1407 assertNull(f.join()); 1408 assertEquals(21, f.number); 1409 checkCompletedNormally(f); 1410 }}; 1411 testInvokeOnPool(singletonPool(), a); 1412 } 1413 1414 /** 1415 * get of a forked task returns when task completes 1416 */ testForkGetSingleton()1417 public void testForkGetSingleton() { 1418 ForkJoinTask a = new CheckedRecursiveAction() { 1419 protected void realCompute() throws Exception { 1420 CCF f = new LCCF(8); 1421 assertSame(f, f.fork()); 1422 assertNull(f.get()); 1423 assertEquals(21, f.number); 1424 checkCompletedNormally(f); 1425 }}; 1426 testInvokeOnPool(singletonPool(), a); 1427 } 1428 1429 /** 1430 * timed get of a forked task returns when task completes 1431 */ testForkTimedGetSingleton()1432 public void testForkTimedGetSingleton() { 1433 ForkJoinTask a = new CheckedRecursiveAction() { 1434 protected void realCompute() throws Exception { 1435 CCF f = new LCCF(8); 1436 assertSame(f, f.fork()); 1437 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); 1438 assertEquals(21, f.number); 1439 checkCompletedNormally(f); 1440 }}; 1441 testInvokeOnPool(singletonPool(), a); 1442 } 1443 1444 /** 1445 * timed get with null time unit throws NPE 1446 */ testForkTimedGetNPESingleton()1447 public void testForkTimedGetNPESingleton() { 1448 ForkJoinTask a = new CheckedRecursiveAction() { 1449 protected void realCompute() throws Exception { 1450 CCF f = new LCCF(8); 1451 assertSame(f, f.fork()); 1452 try { 1453 f.get(randomTimeout(), null); 1454 shouldThrow(); 1455 } catch (NullPointerException success) {} 1456 }}; 1457 testInvokeOnPool(singletonPool(), a); 1458 } 1459 1460 /** 1461 * quietlyJoin of a forked task returns when task completes 1462 */ testForkQuietlyJoinSingleton()1463 public void testForkQuietlyJoinSingleton() { 1464 ForkJoinTask a = new CheckedRecursiveAction() { 1465 protected void realCompute() { 1466 CCF f = new LCCF(8); 1467 assertSame(f, f.fork()); 1468 f.quietlyJoin(); 1469 assertEquals(21, f.number); 1470 checkCompletedNormally(f); 1471 }}; 1472 testInvokeOnPool(singletonPool(), a); 1473 } 1474 1475 /** 1476 * helpQuiesce returns when tasks are complete. 1477 * getQueuedTaskCount returns 0 when quiescent 1478 */ testForkHelpQuiesceSingleton()1479 public void testForkHelpQuiesceSingleton() { 1480 ForkJoinTask a = new CheckedRecursiveAction() { 1481 protected void realCompute() { 1482 CCF f = new LCCF(8); 1483 assertSame(f, f.fork()); 1484 helpQuiesce(); 1485 assertEquals(0, getQueuedTaskCount()); 1486 assertEquals(21, f.number); 1487 checkCompletedNormally(f); 1488 }}; 1489 testInvokeOnPool(singletonPool(), a); 1490 } 1491 1492 /** 1493 * invoke task throws exception when task completes abnormally 1494 */ testAbnormalInvokeSingleton()1495 public void testAbnormalInvokeSingleton() { 1496 ForkJoinTask a = new CheckedRecursiveAction() { 1497 protected void realCompute() { 1498 FailingCCF f = new LFCCF(8); 1499 try { 1500 f.invoke(); 1501 shouldThrow(); 1502 } catch (FJException success) { 1503 checkCompletedAbnormally(f, success); 1504 } 1505 }}; 1506 testInvokeOnPool(singletonPool(), a); 1507 } 1508 1509 /** 1510 * quietlyInvoke task returns when task completes abnormally 1511 */ testAbnormalQuietlyInvokeSingleton()1512 public void testAbnormalQuietlyInvokeSingleton() { 1513 ForkJoinTask a = new CheckedRecursiveAction() { 1514 protected void realCompute() { 1515 FailingCCF f = new LFCCF(8); 1516 f.quietlyInvoke(); 1517 assertTrue(f.getException() instanceof FJException); 1518 checkCompletedAbnormally(f, f.getException()); 1519 }}; 1520 testInvokeOnPool(singletonPool(), a); 1521 } 1522 1523 /** 1524 * join of a forked task throws exception when task completes abnormally 1525 */ testAbnormalForkJoinSingleton()1526 public void testAbnormalForkJoinSingleton() { 1527 ForkJoinTask a = new CheckedRecursiveAction() { 1528 protected void realCompute() { 1529 FailingCCF f = new LFCCF(8); 1530 assertSame(f, f.fork()); 1531 try { 1532 f.join(); 1533 shouldThrow(); 1534 } catch (FJException success) { 1535 checkCompletedAbnormally(f, success); 1536 } 1537 }}; 1538 testInvokeOnPool(singletonPool(), a); 1539 } 1540 1541 /** 1542 * get of a forked task throws exception when task completes abnormally 1543 */ testAbnormalForkGetSingleton()1544 public void testAbnormalForkGetSingleton() { 1545 ForkJoinTask a = new CheckedRecursiveAction() { 1546 protected void realCompute() throws Exception { 1547 FailingCCF f = new LFCCF(8); 1548 assertSame(f, f.fork()); 1549 try { 1550 f.get(); 1551 shouldThrow(); 1552 } catch (ExecutionException success) { 1553 Throwable cause = success.getCause(); 1554 assertTrue(cause instanceof FJException); 1555 checkCompletedAbnormally(f, cause); 1556 } 1557 }}; 1558 testInvokeOnPool(singletonPool(), a); 1559 } 1560 1561 /** 1562 * timed get of a forked task throws exception when task completes abnormally 1563 */ testAbnormalForkTimedGetSingleton()1564 public void testAbnormalForkTimedGetSingleton() { 1565 ForkJoinTask a = new CheckedRecursiveAction() { 1566 protected void realCompute() throws Exception { 1567 FailingCCF f = new LFCCF(8); 1568 assertSame(f, f.fork()); 1569 try { 1570 f.get(LONG_DELAY_MS, MILLISECONDS); 1571 shouldThrow(); 1572 } catch (ExecutionException success) { 1573 Throwable cause = success.getCause(); 1574 assertTrue(cause instanceof FJException); 1575 checkCompletedAbnormally(f, cause); 1576 } 1577 }}; 1578 testInvokeOnPool(singletonPool(), a); 1579 } 1580 1581 /** 1582 * quietlyJoin of a forked task returns when task completes abnormally 1583 */ testAbnormalForkQuietlyJoinSingleton()1584 public void testAbnormalForkQuietlyJoinSingleton() { 1585 ForkJoinTask a = new CheckedRecursiveAction() { 1586 protected void realCompute() { 1587 FailingCCF f = new LFCCF(8); 1588 assertSame(f, f.fork()); 1589 f.quietlyJoin(); 1590 assertTrue(f.getException() instanceof FJException); 1591 checkCompletedAbnormally(f, f.getException()); 1592 }}; 1593 testInvokeOnPool(singletonPool(), a); 1594 } 1595 1596 /** 1597 * invoke task throws exception when task cancelled 1598 */ testCancelledInvokeSingleton()1599 public void testCancelledInvokeSingleton() { 1600 ForkJoinTask a = new CheckedRecursiveAction() { 1601 protected void realCompute() { 1602 CCF f = new LCCF(8); 1603 assertTrue(f.cancel(true)); 1604 try { 1605 f.invoke(); 1606 shouldThrow(); 1607 } catch (CancellationException success) { 1608 checkCancelled(f); 1609 } 1610 }}; 1611 testInvokeOnPool(singletonPool(), a); 1612 } 1613 1614 /** 1615 * join of a forked task throws exception when task cancelled 1616 */ testCancelledForkJoinSingleton()1617 public void testCancelledForkJoinSingleton() { 1618 ForkJoinTask a = new CheckedRecursiveAction() { 1619 protected void realCompute() { 1620 CCF f = new LCCF(8); 1621 assertTrue(f.cancel(true)); 1622 assertSame(f, f.fork()); 1623 try { 1624 f.join(); 1625 shouldThrow(); 1626 } catch (CancellationException success) { 1627 checkCancelled(f); 1628 } 1629 }}; 1630 testInvokeOnPool(singletonPool(), a); 1631 } 1632 1633 /** 1634 * get of a forked task throws exception when task cancelled 1635 */ testCancelledForkGetSingleton()1636 public void testCancelledForkGetSingleton() { 1637 ForkJoinTask a = new CheckedRecursiveAction() { 1638 protected void realCompute() throws Exception { 1639 CCF f = new LCCF(8); 1640 assertTrue(f.cancel(true)); 1641 assertSame(f, f.fork()); 1642 try { 1643 f.get(); 1644 shouldThrow(); 1645 } catch (CancellationException success) { 1646 checkCancelled(f); 1647 } 1648 }}; 1649 testInvokeOnPool(singletonPool(), a); 1650 } 1651 1652 /** 1653 * timed get of a forked task throws exception when task cancelled 1654 */ testCancelledForkTimedGetSingleton()1655 public void testCancelledForkTimedGetSingleton() throws Exception { 1656 ForkJoinTask a = new CheckedRecursiveAction() { 1657 protected void realCompute() throws Exception { 1658 CCF f = new LCCF(8); 1659 assertTrue(f.cancel(true)); 1660 assertSame(f, f.fork()); 1661 try { 1662 f.get(LONG_DELAY_MS, MILLISECONDS); 1663 shouldThrow(); 1664 } catch (CancellationException success) { 1665 checkCancelled(f); 1666 } 1667 }}; 1668 testInvokeOnPool(singletonPool(), a); 1669 } 1670 1671 /** 1672 * quietlyJoin of a forked task returns when task cancelled 1673 */ testCancelledForkQuietlyJoinSingleton()1674 public void testCancelledForkQuietlyJoinSingleton() { 1675 ForkJoinTask a = new CheckedRecursiveAction() { 1676 protected void realCompute() { 1677 CCF f = new LCCF(8); 1678 assertTrue(f.cancel(true)); 1679 assertSame(f, f.fork()); 1680 f.quietlyJoin(); 1681 checkCancelled(f); 1682 }}; 1683 testInvokeOnPool(singletonPool(), a); 1684 } 1685 1686 /** 1687 * invoke task throws exception after invoking completeExceptionally 1688 */ testCompleteExceptionallySingleton()1689 public void testCompleteExceptionallySingleton() { 1690 ForkJoinTask a = new CheckedRecursiveAction() { 1691 protected void realCompute() { 1692 CCF n = new LCCF(8); 1693 CCF f = new LCCF(n, 8); 1694 FJException ex = new FJException(); 1695 f.completeExceptionally(ex); 1696 f.checkCompletedExceptionally(ex); 1697 n.checkCompletedExceptionally(ex); 1698 }}; 1699 testInvokeOnPool(singletonPool(), a); 1700 } 1701 1702 /** 1703 * invokeAll(t1, t2) invokes all task arguments 1704 */ testInvokeAll2Singleton()1705 public void testInvokeAll2Singleton() { 1706 ForkJoinTask a = new CheckedRecursiveAction() { 1707 protected void realCompute() { 1708 CCF f = new LCCF(8); 1709 CCF g = new LCCF(9); 1710 invokeAll(f, g); 1711 assertEquals(21, f.number); 1712 assertEquals(34, g.number); 1713 checkCompletedNormally(f); 1714 checkCompletedNormally(g); 1715 }}; 1716 testInvokeOnPool(singletonPool(), a); 1717 } 1718 1719 /** 1720 * invokeAll(tasks) with 1 argument invokes task 1721 */ testInvokeAll1Singleton()1722 public void testInvokeAll1Singleton() { 1723 ForkJoinTask a = new CheckedRecursiveAction() { 1724 protected void realCompute() { 1725 CCF f = new LCCF(8); 1726 invokeAll(f); 1727 checkCompletedNormally(f); 1728 assertEquals(21, f.number); 1729 }}; 1730 testInvokeOnPool(singletonPool(), a); 1731 } 1732 1733 /** 1734 * invokeAll(tasks) with > 2 argument invokes tasks 1735 */ testInvokeAll3Singleton()1736 public void testInvokeAll3Singleton() { 1737 ForkJoinTask a = new CheckedRecursiveAction() { 1738 protected void realCompute() { 1739 CCF f = new LCCF(8); 1740 CCF g = new LCCF(9); 1741 CCF h = new LCCF(7); 1742 invokeAll(f, g, h); 1743 assertEquals(21, f.number); 1744 assertEquals(34, g.number); 1745 assertEquals(13, h.number); 1746 checkCompletedNormally(f); 1747 checkCompletedNormally(g); 1748 checkCompletedNormally(h); 1749 }}; 1750 testInvokeOnPool(singletonPool(), a); 1751 } 1752 1753 /** 1754 * invokeAll(collection) invokes all tasks in the collection 1755 */ testInvokeAllCollectionSingleton()1756 public void testInvokeAllCollectionSingleton() { 1757 ForkJoinTask a = new CheckedRecursiveAction() { 1758 protected void realCompute() { 1759 CCF f = new LCCF(8); 1760 CCF g = new LCCF(9); 1761 CCF h = new LCCF(7); 1762 HashSet set = new HashSet(); 1763 set.add(f); 1764 set.add(g); 1765 set.add(h); 1766 invokeAll(set); 1767 assertEquals(21, f.number); 1768 assertEquals(34, g.number); 1769 assertEquals(13, h.number); 1770 checkCompletedNormally(f); 1771 checkCompletedNormally(g); 1772 checkCompletedNormally(h); 1773 }}; 1774 testInvokeOnPool(singletonPool(), a); 1775 } 1776 1777 /** 1778 * invokeAll(tasks) with any null task throws NPE 1779 */ testInvokeAllNPESingleton()1780 public void testInvokeAllNPESingleton() { 1781 ForkJoinTask a = new CheckedRecursiveAction() { 1782 protected void realCompute() { 1783 CCF f = new LCCF(8); 1784 CCF g = new LCCF(9); 1785 CCF h = null; 1786 try { 1787 invokeAll(f, g, h); 1788 shouldThrow(); 1789 } catch (NullPointerException success) {} 1790 }}; 1791 testInvokeOnPool(singletonPool(), a); 1792 } 1793 1794 /** 1795 * invokeAll(t1, t2) throw exception if any task does 1796 */ testAbnormalInvokeAll2Singleton()1797 public void testAbnormalInvokeAll2Singleton() { 1798 ForkJoinTask a = new CheckedRecursiveAction() { 1799 protected void realCompute() { 1800 CCF f = new LCCF(8); 1801 FailingCCF g = new LFCCF(9); 1802 try { 1803 invokeAll(f, g); 1804 shouldThrow(); 1805 } catch (FJException success) { 1806 checkCompletedAbnormally(g, success); 1807 } 1808 }}; 1809 testInvokeOnPool(singletonPool(), a); 1810 } 1811 1812 /** 1813 * invokeAll(tasks) with 1 argument throws exception if task does 1814 */ testAbnormalInvokeAll1Singleton()1815 public void testAbnormalInvokeAll1Singleton() { 1816 ForkJoinTask a = new CheckedRecursiveAction() { 1817 protected void realCompute() { 1818 FailingCCF g = new LFCCF(9); 1819 try { 1820 invokeAll(g); 1821 shouldThrow(); 1822 } catch (FJException success) { 1823 checkCompletedAbnormally(g, success); 1824 } 1825 }}; 1826 testInvokeOnPool(singletonPool(), a); 1827 } 1828 1829 /** 1830 * invokeAll(tasks) with > 2 argument throws exception if any task does 1831 */ testAbnormalInvokeAll3Singleton()1832 public void testAbnormalInvokeAll3Singleton() { 1833 ForkJoinTask a = new CheckedRecursiveAction() { 1834 protected void realCompute() { 1835 CCF f = new LCCF(8); 1836 FailingCCF g = new LFCCF(9); 1837 CCF h = new LCCF(7); 1838 try { 1839 invokeAll(f, g, h); 1840 shouldThrow(); 1841 } catch (FJException success) { 1842 checkCompletedAbnormally(g, success); 1843 } 1844 }}; 1845 testInvokeOnPool(singletonPool(), a); 1846 } 1847 1848 /** 1849 * invokeAll(collection) throws exception if any task does 1850 */ testAbnormalInvokeAllCollectionSingleton()1851 public void testAbnormalInvokeAllCollectionSingleton() { 1852 ForkJoinTask a = new CheckedRecursiveAction() { 1853 protected void realCompute() { 1854 FailingCCF f = new LFCCF(8); 1855 CCF g = new LCCF(9); 1856 CCF h = new LCCF(7); 1857 HashSet set = new HashSet(); 1858 set.add(f); 1859 set.add(g); 1860 set.add(h); 1861 try { 1862 invokeAll(set); 1863 shouldThrow(); 1864 } catch (FJException success) { 1865 checkCompletedAbnormally(f, success); 1866 } 1867 }}; 1868 testInvokeOnPool(singletonPool(), a); 1869 } 1870 1871 } 1872