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 |
> |
|
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
+ |
import java.util.concurrent.CountedCompleter; |
13 |
+ |
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
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; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
|
import java.util.concurrent.atomic.AtomicInteger; |
16 |
– |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
18 |
|
import java.util.concurrent.atomic.AtomicReference; |
19 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.HashSet; |
21 |
< |
import junit.framework.*; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class CountedCompleterTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
|
|
29 |
|
public static Test suite() { |
198 |
|
try { |
199 |
|
a.invoke(); |
200 |
|
shouldThrow(); |
201 |
< |
} catch (Throwable ex) { |
202 |
< |
assertSame(t, ex); |
201 |
> |
} catch (Throwable success) { |
202 |
> |
assertSame(t, success); |
203 |
|
} |
204 |
|
} |
205 |
|
|
212 |
|
final AtomicInteger onCompletionN = new AtomicInteger(0); |
213 |
|
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
214 |
|
final AtomicInteger setRawResultN = new AtomicInteger(0); |
215 |
< |
final AtomicReference<Object> rawResult = new AtomicReference<>(null); |
215 |
> |
final AtomicReference<Object> rawResult = new AtomicReference<Object>(null); |
216 |
|
int computeN() { return computeN.get(); } |
217 |
|
int onCompletionN() { return onCompletionN.get(); } |
218 |
|
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
252 |
|
} |
253 |
|
void checkCompletes(Object rawResult) { |
254 |
|
checkIncomplete(); |
255 |
+ |
int pendingCount = getPendingCount(); |
256 |
|
complete(rawResult); |
257 |
+ |
assertEquals(pendingCount, getPendingCount()); |
258 |
|
assertEquals(0, computeN()); |
259 |
|
assertEquals(1, onCompletionN()); |
260 |
|
assertEquals(0, onExceptionalCompletionN()); |
285 |
|
|
286 |
|
/** |
287 |
|
* A newly constructed CountedCompleter is not completed; |
288 |
< |
* complete() causes completion. |
288 |
> |
* complete() causes completion. pendingCount is ignored. |
289 |
|
*/ |
290 |
|
public void testComplete() { |
291 |
|
for (Object x : new Object[] { Boolean.TRUE, null }) { |
292 |
< |
new NoopCC() |
293 |
< |
.checkCompletes(x); |
294 |
< |
new NoopCC(new NoopCC()) |
295 |
< |
.checkCompletes(x); |
292 |
> |
for (int pendingCount : new int[] { 0, 42 }) { |
293 |
> |
testComplete(new NoopCC(), x, pendingCount); |
294 |
> |
testComplete(new NoopCC(new NoopCC()), x, pendingCount); |
295 |
> |
} |
296 |
|
} |
297 |
|
} |
298 |
+ |
void testComplete(NoopCC cc, Object x, int pendingCount) { |
299 |
+ |
cc.setPendingCount(pendingCount); |
300 |
+ |
cc.checkCompletes(x); |
301 |
+ |
} |
302 |
|
|
303 |
|
/** |
304 |
|
* completeExceptionally completes exceptionally |
452 |
|
NoopCC a = new NoopCC(); |
453 |
|
a.setPendingCount(1); |
454 |
|
assertNull(a.firstComplete()); |
455 |
< |
assertEquals(a, a.firstComplete()); |
455 |
> |
a.checkIncomplete(); |
456 |
> |
assertSame(a, a.firstComplete()); |
457 |
> |
a.checkIncomplete(); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
467 |
|
a.setPendingCount(1); |
468 |
|
b.setPendingCount(1); |
469 |
|
assertNull(b.firstComplete()); |
470 |
< |
CountedCompleter c = b.firstComplete(); |
471 |
< |
assertSame(b, c); |
472 |
< |
CountedCompleter d = c.nextComplete(); |
473 |
< |
assertNull(d); |
474 |
< |
CountedCompleter e = c.nextComplete(); |
475 |
< |
assertSame(a, e); |
470 |
> |
assertSame(b, b.firstComplete()); |
471 |
> |
assertNull(b.nextComplete()); |
472 |
> |
a.checkIncomplete(); |
473 |
> |
b.checkIncomplete(); |
474 |
> |
assertSame(a, b.nextComplete()); |
475 |
> |
assertSame(a, b.nextComplete()); |
476 |
> |
a.checkIncomplete(); |
477 |
> |
b.checkIncomplete(); |
478 |
> |
assertNull(a.nextComplete()); |
479 |
> |
b.checkIncomplete(); |
480 |
> |
checkCompletedNormally(a); |
481 |
|
} |
482 |
|
|
483 |
|
/** |
499 |
|
// Invocation tests use some interdependent task classes |
500 |
|
// to better test propagation etc |
501 |
|
|
502 |
< |
|
503 |
< |
// Version of Fibonacci with different classes for left vs right forks |
502 |
> |
/** |
503 |
> |
* Version of Fibonacci with different classes for left vs right forks |
504 |
> |
*/ |
505 |
|
abstract class CCF extends CheckedCC { |
506 |
|
int number; |
507 |
|
int rnumber; |
1137 |
|
} |
1138 |
|
|
1139 |
|
/** |
1140 |
< |
* invokeAll(collection) throws exception if any task does |
1140 |
> |
* invokeAll(collection) throws exception if any task does |
1141 |
|
*/ |
1142 |
|
public void testAbnormalInvokeAllCollection() { |
1143 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1802 |
|
} |
1803 |
|
|
1804 |
|
/** |
1805 |
< |
* invokeAll(collection) throws exception if any task does |
1805 |
> |
* invokeAll(collection) throws exception if any task does |
1806 |
|
*/ |
1807 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1808 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |