3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
< |
import java.util.concurrent.ExecutionException; |
6 |
> |
import java.util.HashSet; |
7 |
|
import java.util.concurrent.CancellationException; |
8 |
+ |
import java.util.concurrent.CountedCompleter; |
9 |
+ |
import java.util.concurrent.ExecutionException; |
10 |
|
import java.util.concurrent.ForkJoinPool; |
11 |
|
import java.util.concurrent.ForkJoinTask; |
10 |
– |
import java.util.concurrent.CountedCompleter; |
11 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
– |
import java.util.concurrent.RecursiveAction; |
13 |
– |
import java.util.concurrent.TimeUnit; |
12 |
|
import java.util.concurrent.TimeoutException; |
13 |
|
import java.util.concurrent.atomic.AtomicInteger; |
16 |
– |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
14 |
|
import java.util.concurrent.atomic.AtomicReference; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
20 |
– |
import java.util.HashSet; |
17 |
|
import junit.framework.*; |
18 |
|
|
19 |
|
public class CountedCompleterTest extends JSR166TestCase { |
208 |
|
final AtomicInteger onCompletionN = new AtomicInteger(0); |
209 |
|
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
210 |
|
final AtomicInteger setRawResultN = new AtomicInteger(0); |
211 |
< |
final AtomicReference<Object> rawResult = new AtomicReference<>(null); |
211 |
> |
final AtomicReference<Object> rawResult = new AtomicReference<Object>(null); |
212 |
|
int computeN() { return computeN.get(); } |
213 |
|
int onCompletionN() { return onCompletionN.get(); } |
214 |
|
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
248 |
|
} |
249 |
|
void checkCompletes(Object rawResult) { |
250 |
|
checkIncomplete(); |
251 |
+ |
int pendingCount = getPendingCount(); |
252 |
|
complete(rawResult); |
253 |
+ |
assertEquals(pendingCount, getPendingCount()); |
254 |
|
assertEquals(0, computeN()); |
255 |
|
assertEquals(1, onCompletionN()); |
256 |
|
assertEquals(0, onExceptionalCompletionN()); |
281 |
|
|
282 |
|
/** |
283 |
|
* A newly constructed CountedCompleter is not completed; |
284 |
< |
* complete() causes completion. |
284 |
> |
* complete() causes completion. pendingCount is ignored. |
285 |
|
*/ |
286 |
|
public void testComplete() { |
287 |
|
for (Object x : new Object[] { Boolean.TRUE, null }) { |
288 |
< |
new NoopCC() |
289 |
< |
.checkCompletes(x); |
290 |
< |
new NoopCC(new NoopCC()) |
291 |
< |
.checkCompletes(x); |
288 |
> |
for (int pendingCount : new int[] { 0, 42 }) { |
289 |
> |
testComplete(new NoopCC(), x, pendingCount); |
290 |
> |
testComplete(new NoopCC(new NoopCC()), x, pendingCount); |
291 |
> |
} |
292 |
|
} |
293 |
|
} |
294 |
+ |
void testComplete(NoopCC cc, Object x, int pendingCount) { |
295 |
+ |
cc.setPendingCount(pendingCount); |
296 |
+ |
cc.checkCompletes(x); |
297 |
+ |
} |
298 |
|
|
299 |
|
/** |
300 |
|
* completeExceptionally completes exceptionally |
448 |
|
NoopCC a = new NoopCC(); |
449 |
|
a.setPendingCount(1); |
450 |
|
assertNull(a.firstComplete()); |
451 |
< |
assertEquals(a, a.firstComplete()); |
451 |
> |
a.checkIncomplete(); |
452 |
> |
assertSame(a, a.firstComplete()); |
453 |
> |
a.checkIncomplete(); |
454 |
|
} |
455 |
|
|
456 |
|
/** |
463 |
|
a.setPendingCount(1); |
464 |
|
b.setPendingCount(1); |
465 |
|
assertNull(b.firstComplete()); |
466 |
< |
CountedCompleter c = b.firstComplete(); |
467 |
< |
assertSame(b, c); |
468 |
< |
CountedCompleter d = c.nextComplete(); |
469 |
< |
assertNull(d); |
470 |
< |
CountedCompleter e = c.nextComplete(); |
471 |
< |
assertSame(a, e); |
466 |
> |
assertSame(b, b.firstComplete()); |
467 |
> |
assertNull(b.nextComplete()); |
468 |
> |
a.checkIncomplete(); |
469 |
> |
b.checkIncomplete(); |
470 |
> |
assertSame(a, b.nextComplete()); |
471 |
> |
assertSame(a, b.nextComplete()); |
472 |
> |
a.checkIncomplete(); |
473 |
> |
b.checkIncomplete(); |
474 |
> |
assertNull(a.nextComplete()); |
475 |
> |
b.checkIncomplete(); |
476 |
> |
checkCompletedNormally(a); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
495 |
|
// Invocation tests use some interdependent task classes |
496 |
|
// to better test propagation etc |
497 |
|
|
498 |
< |
|
499 |
< |
// Version of Fibonacci with different classes for left vs right forks |
498 |
> |
/** |
499 |
> |
* Version of Fibonacci with different classes for left vs right forks |
500 |
> |
*/ |
501 |
|
abstract class CCF extends CheckedCC { |
502 |
|
int number; |
503 |
|
int rnumber; |
1133 |
|
} |
1134 |
|
|
1135 |
|
/** |
1136 |
< |
* invokeAll(collection) throws exception if any task does |
1136 |
> |
* invokeAll(collection) throws exception if any task does |
1137 |
|
*/ |
1138 |
|
public void testAbnormalInvokeAllCollection() { |
1139 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1798 |
|
} |
1799 |
|
|
1800 |
|
/** |
1801 |
< |
* invokeAll(collection) throws exception if any task does |
1801 |
> |
* invokeAll(collection) throws exception if any task does |
1802 |
|
*/ |
1803 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1804 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |