3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
+ |
import java.util.*; |
7 |
+ |
import java.util.concurrent.Executor; |
8 |
+ |
import java.util.concurrent.Executors; |
9 |
+ |
import java.util.concurrent.ExecutorService; |
10 |
+ |
import java.util.concurrent.AbstractExecutorService; |
11 |
+ |
import java.util.concurrent.CountDownLatch; |
12 |
+ |
import java.util.concurrent.Callable; |
13 |
+ |
import java.util.concurrent.Future; |
14 |
+ |
import java.util.concurrent.ExecutionException; |
15 |
+ |
import java.util.concurrent.CancellationException; |
16 |
+ |
import java.util.concurrent.RejectedExecutionException; |
17 |
+ |
import java.util.concurrent.ForkJoinPool; |
18 |
+ |
import java.util.concurrent.ForkJoinTask; |
19 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
20 |
+ |
import java.util.concurrent.RecursiveAction; |
21 |
+ |
import java.util.concurrent.RecursiveTask; |
22 |
+ |
import java.util.concurrent.TimeUnit; |
23 |
|
import junit.framework.*; |
24 |
< |
import java.util.concurrent.*; |
24 |
> |
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.atomic.*; |
26 |
|
import java.util.*; |
27 |
|
|
28 |
|
public class ForkJoinTaskTest extends JSR166TestCase { |
29 |
|
|
30 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(ForkJoinTaskTest.class); |
34 |
> |
return new TestSuite(ForkJoinTaskTest.class); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
44 |
|
|
45 |
|
static final ForkJoinPool mainPool = new ForkJoinPool(); |
46 |
|
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
47 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
48 |
< |
static { |
49 |
< |
asyncSingletonPool.setAsyncMode(true); |
33 |
< |
} |
34 |
< |
|
47 |
> |
static final ForkJoinPool asyncSingletonPool = |
48 |
> |
new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
49 |
> |
null, true); |
50 |
|
static final class FJException extends RuntimeException { |
51 |
|
FJException() { super(); } |
52 |
|
} |
163 |
|
|
164 |
|
} |
165 |
|
|
166 |
< |
static final class AsyncFib extends BinaryAsyncAction { |
166 |
> |
static final class AsyncFib extends BinaryAsyncAction { |
167 |
|
int number; |
168 |
|
public AsyncFib(int n) { |
169 |
|
this.number = n; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
< |
static final class FailingAsyncFib extends BinaryAsyncAction { |
195 |
> |
static final class FailingAsyncFib extends BinaryAsyncAction { |
196 |
|
int number; |
197 |
|
public FailingAsyncFib(int n) { |
198 |
|
this.number = n; |
224 |
|
* invoke returns when task completes normally. |
225 |
|
* isCompletedAbnormally and isCancelled return false for normally |
226 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
212 |
– |
* |
227 |
|
*/ |
228 |
|
public void testInvoke() { |
229 |
|
RecursiveAction a = new RecursiveAction() { |
290 |
|
threadAssertTrue(f.number == 21); |
291 |
|
threadAssertTrue(f.isDone()); |
292 |
|
} catch (Exception ex) { |
293 |
< |
unexpectedException(); |
293 |
> |
unexpectedException(ex); |
294 |
|
} |
295 |
|
} |
296 |
|
}; |
306 |
|
try { |
307 |
|
AsyncFib f = new AsyncFib(8); |
308 |
|
f.fork(); |
309 |
< |
f.get(5L, TimeUnit.SECONDS); |
309 |
> |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
310 |
|
threadAssertTrue(f.number == 21); |
311 |
|
threadAssertTrue(f.isDone()); |
312 |
|
} catch (Exception ex) { |
313 |
< |
unexpectedException(); |
313 |
> |
unexpectedException(ex); |
314 |
|
} |
315 |
|
} |
316 |
|
}; |
327 |
|
AsyncFib f = new AsyncFib(8); |
328 |
|
f.fork(); |
329 |
|
f.get(5L, null); |
330 |
+ |
shouldThrow(); |
331 |
|
} catch (NullPointerException success) { |
332 |
|
} catch (Exception ex) { |
333 |
< |
unexpectedException(); |
333 |
> |
unexpectedException(ex); |
334 |
|
} |
335 |
|
} |
336 |
|
}; |
338 |
|
} |
339 |
|
|
340 |
|
/** |
326 |
– |
* helpJoin of a forked task returns when task completes |
327 |
– |
*/ |
328 |
– |
public void testForkHelpJoin() { |
329 |
– |
RecursiveAction a = new RecursiveAction() { |
330 |
– |
public void compute() { |
331 |
– |
AsyncFib f = new AsyncFib(8); |
332 |
– |
f.fork(); |
333 |
– |
f.helpJoin(); |
334 |
– |
threadAssertTrue(f.number == 21); |
335 |
– |
threadAssertTrue(f.isDone()); |
336 |
– |
} |
337 |
– |
}; |
338 |
– |
mainPool.invoke(a); |
339 |
– |
} |
340 |
– |
|
341 |
– |
/** |
341 |
|
* quietlyJoin of a forked task returns when task completes |
342 |
|
*/ |
343 |
|
public void testForkQuietlyJoin() { |
355 |
|
|
356 |
|
|
357 |
|
/** |
359 |
– |
* quietlyHelpJoin of a forked task returns when task completes |
360 |
– |
*/ |
361 |
– |
public void testForkQuietlyHelpJoin() { |
362 |
– |
RecursiveAction a = new RecursiveAction() { |
363 |
– |
public void compute() { |
364 |
– |
AsyncFib f = new AsyncFib(8); |
365 |
– |
f.fork(); |
366 |
– |
f.quietlyHelpJoin(); |
367 |
– |
threadAssertTrue(f.number == 21); |
368 |
– |
threadAssertTrue(f.isDone()); |
369 |
– |
} |
370 |
– |
}; |
371 |
– |
mainPool.invoke(a); |
372 |
– |
} |
373 |
– |
|
374 |
– |
|
375 |
– |
/** |
358 |
|
* helpQuiesce returns when tasks are complete. |
359 |
|
* getQueuedTaskCount returns 0 when quiescent |
360 |
|
*/ |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
< |
* quietelyInvoke task returns when task completes abnormally |
394 |
> |
* quietlyInvoke task returns when task completes abnormally |
395 |
|
*/ |
396 |
|
public void testAbnormalQuietlyInvoke() { |
397 |
|
RecursiveAction a = new RecursiveAction() { |
433 |
|
f.fork(); |
434 |
|
f.get(); |
435 |
|
shouldThrow(); |
436 |
< |
} catch (Exception success) { |
436 |
> |
} catch (ExecutionException success) { |
437 |
> |
} catch (Exception ex) { |
438 |
> |
unexpectedException(ex); |
439 |
|
} |
440 |
|
} |
441 |
|
}; |
451 |
|
try { |
452 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
453 |
|
f.fork(); |
454 |
< |
f.get(5L, TimeUnit.SECONDS); |
471 |
< |
shouldThrow(); |
472 |
< |
} catch (Exception success) { |
473 |
< |
} |
474 |
< |
} |
475 |
< |
}; |
476 |
< |
mainPool.invoke(a); |
477 |
< |
} |
478 |
< |
|
479 |
< |
/** |
480 |
< |
* join of a forked task throws exception when task completes abnormally |
481 |
< |
*/ |
482 |
< |
public void testAbnormalForkHelpJoin() { |
483 |
< |
RecursiveAction a = new RecursiveAction() { |
484 |
< |
public void compute() { |
485 |
< |
try { |
486 |
< |
FailingAsyncFib f = new FailingAsyncFib(8); |
487 |
< |
f.fork(); |
488 |
< |
f.helpJoin(); |
454 |
> |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
455 |
|
shouldThrow(); |
456 |
< |
} catch (FJException success) { |
456 |
> |
} catch (ExecutionException success) { |
457 |
> |
} catch (Exception ex) { |
458 |
> |
unexpectedException(ex); |
459 |
|
} |
460 |
|
} |
461 |
|
}; |
463 |
|
} |
464 |
|
|
465 |
|
/** |
498 |
– |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
499 |
– |
* getException of failed task returns its exception. |
500 |
– |
* isCompletedAbnormally of a failed task returns true. |
501 |
– |
* isCancelled of a failed uncancelled task returns false |
502 |
– |
*/ |
503 |
– |
public void testAbnormalForkQuietlyHelpJoin() { |
504 |
– |
RecursiveAction a = new RecursiveAction() { |
505 |
– |
public void compute() { |
506 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
507 |
– |
f.fork(); |
508 |
– |
f.quietlyHelpJoin(); |
509 |
– |
threadAssertTrue(f.isDone()); |
510 |
– |
threadAssertTrue(f.isCompletedAbnormally()); |
511 |
– |
threadAssertFalse(f.isCancelled()); |
512 |
– |
threadAssertTrue(f.getException() instanceof FJException); |
513 |
– |
} |
514 |
– |
}; |
515 |
– |
mainPool.invoke(a); |
516 |
– |
} |
517 |
– |
|
518 |
– |
/** |
466 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
467 |
|
*/ |
468 |
|
public void testAbnormalForkQuietlyJoin() { |
528 |
|
f.fork(); |
529 |
|
f.get(); |
530 |
|
shouldThrow(); |
531 |
< |
} catch (Exception success) { |
531 |
> |
} catch (CancellationException success) { |
532 |
> |
} catch (Exception ex) { |
533 |
> |
unexpectedException(ex); |
534 |
|
} |
535 |
|
} |
536 |
|
}; |
547 |
|
AsyncFib f = new AsyncFib(8); |
548 |
|
f.cancel(true); |
549 |
|
f.fork(); |
550 |
< |
f.get(5L, TimeUnit.SECONDS); |
602 |
< |
shouldThrow(); |
603 |
< |
} catch (Exception success) { |
604 |
< |
} |
605 |
< |
} |
606 |
< |
}; |
607 |
< |
mainPool.invoke(a); |
608 |
< |
} |
609 |
< |
|
610 |
< |
/** |
611 |
< |
* join of a forked task throws exception when task cancelled |
612 |
< |
*/ |
613 |
< |
public void testCancelledForkHelpJoin() { |
614 |
< |
RecursiveAction a = new RecursiveAction() { |
615 |
< |
public void compute() { |
616 |
< |
try { |
617 |
< |
AsyncFib f = new AsyncFib(8); |
618 |
< |
f.cancel(true); |
619 |
< |
f.fork(); |
620 |
< |
f.helpJoin(); |
550 |
> |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
551 |
|
shouldThrow(); |
552 |
|
} catch (CancellationException success) { |
553 |
+ |
} catch (Exception ex) { |
554 |
+ |
unexpectedException(ex); |
555 |
|
} |
556 |
|
} |
557 |
|
}; |
558 |
|
mainPool.invoke(a); |
627 |
– |
} |
628 |
– |
|
629 |
– |
/** |
630 |
– |
* quietlyHelpJoin of a forked task returns when task cancelled. |
631 |
– |
* getException of cancelled task returns its exception. |
632 |
– |
* isCompletedAbnormally of a cancelled task returns true. |
633 |
– |
* isCancelled of a cancelled task returns true |
634 |
– |
*/ |
635 |
– |
public void testCancelledForkQuietlyHelpJoin() { |
636 |
– |
RecursiveAction a = new RecursiveAction() { |
637 |
– |
public void compute() { |
638 |
– |
AsyncFib f = new AsyncFib(8); |
639 |
– |
f.cancel(true); |
640 |
– |
f.fork(); |
641 |
– |
f.quietlyHelpJoin(); |
642 |
– |
threadAssertTrue(f.isDone()); |
643 |
– |
threadAssertTrue(f.isCompletedAbnormally()); |
644 |
– |
threadAssertTrue(f.isCancelled()); |
645 |
– |
threadAssertTrue(f.getException() instanceof CancellationException); |
646 |
– |
} |
647 |
– |
}; |
648 |
– |
mainPool.invoke(a); |
559 |
|
} |
560 |
|
|
561 |
|
/** |