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