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