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