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; |
16 |
< |
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.ThreadLocalRandom; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
|
import java.util.concurrent.atomic.AtomicInteger; |
16 |
– |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
19 |
|
import java.util.concurrent.atomic.AtomicReference; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
22 |
< |
import java.util.HashSet; |
21 |
< |
import junit.framework.*; |
20 |
> |
|
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class CountedCompleterTest extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
|
30 |
|
public static Test suite() { |
50 |
|
} |
51 |
|
|
52 |
|
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
53 |
< |
try { |
53 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
54 |
|
assertFalse(a.isDone()); |
55 |
|
assertFalse(a.isCompletedNormally()); |
56 |
|
assertFalse(a.isCompletedAbnormally()); |
66 |
|
assertFalse(a.isCancelled()); |
67 |
|
assertNull(a.getException()); |
68 |
|
assertNull(a.getRawResult()); |
68 |
– |
} finally { |
69 |
– |
joinPool(pool); |
69 |
|
} |
70 |
|
} |
71 |
|
|
94 |
|
|
95 |
|
{ |
96 |
|
Thread.currentThread().interrupt(); |
97 |
< |
long t0 = System.nanoTime(); |
97 |
> |
long startTime = System.nanoTime(); |
98 |
|
assertNull(a.join()); |
99 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
99 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
100 |
|
Thread.interrupted(); |
101 |
|
} |
102 |
|
|
103 |
|
{ |
104 |
|
Thread.currentThread().interrupt(); |
105 |
< |
long t0 = System.nanoTime(); |
105 |
> |
long startTime = System.nanoTime(); |
106 |
|
a.quietlyJoin(); // should be no-op |
107 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
107 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
108 |
|
Thread.interrupted(); |
109 |
|
} |
110 |
|
|
137 |
|
Thread.interrupted(); |
138 |
|
|
139 |
|
{ |
140 |
< |
long t0 = System.nanoTime(); |
140 |
> |
long startTime = System.nanoTime(); |
141 |
|
a.quietlyJoin(); // should be no-op |
142 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
142 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
143 |
|
} |
144 |
|
|
145 |
|
try { |
175 |
|
Thread.interrupted(); |
176 |
|
|
177 |
|
{ |
178 |
< |
long t0 = System.nanoTime(); |
178 |
> |
long startTime = System.nanoTime(); |
179 |
|
a.quietlyJoin(); // should be no-op |
180 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
180 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
181 |
|
} |
182 |
|
|
183 |
|
try { |
197 |
|
try { |
198 |
|
a.invoke(); |
199 |
|
shouldThrow(); |
200 |
< |
} catch (Throwable ex) { |
201 |
< |
assertSame(t, ex); |
200 |
> |
} catch (Throwable success) { |
201 |
> |
assertSame(t, success); |
202 |
|
} |
203 |
|
} |
204 |
|
|
279 |
|
final class NoopCC extends CheckedCC { |
280 |
|
NoopCC() { super(); } |
281 |
|
NoopCC(CountedCompleter p) { super(p); } |
282 |
+ |
NoopCC(CountedCompleter p, int initialPendingCount) { |
283 |
+ |
super(p, initialPendingCount); |
284 |
+ |
} |
285 |
|
protected void realCompute() {} |
286 |
|
} |
287 |
|
|
300 |
|
void testComplete(NoopCC cc, Object x, int pendingCount) { |
301 |
|
cc.setPendingCount(pendingCount); |
302 |
|
cc.checkCompletes(x); |
303 |
+ |
assertEquals(pendingCount, cc.getPendingCount()); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
314 |
|
} |
315 |
|
|
316 |
|
/** |
317 |
< |
* completeExceptionally(null) throws NullPointerException |
317 |
> |
* completeExceptionally(null) surprisingly has the same effect as |
318 |
> |
* completeExceptionally(new RuntimeException()) |
319 |
|
*/ |
320 |
|
public void testCompleteExceptionally_null() { |
321 |
+ |
NoopCC a = new NoopCC(); |
322 |
+ |
a.completeExceptionally(null); |
323 |
|
try { |
324 |
< |
new NoopCC() |
319 |
< |
.checkCompletesExceptionally(null); |
324 |
> |
a.invoke(); |
325 |
|
shouldThrow(); |
326 |
< |
} catch (NullPointerException success) {} |
326 |
> |
} catch (RuntimeException success) { |
327 |
> |
assertSame(success.getClass(), RuntimeException.class); |
328 |
> |
assertNull(success.getCause()); |
329 |
> |
a.checkCompletedExceptionally(success); |
330 |
> |
} |
331 |
|
} |
332 |
|
|
333 |
|
/** |
336 |
|
public void testSetPendingCount() { |
337 |
|
NoopCC a = new NoopCC(); |
338 |
|
assertEquals(0, a.getPendingCount()); |
339 |
< |
a.setPendingCount(1); |
340 |
< |
assertEquals(1, a.getPendingCount()); |
341 |
< |
a.setPendingCount(27); |
342 |
< |
assertEquals(27, a.getPendingCount()); |
339 |
> |
int[] vals = { |
340 |
> |
-1, 0, 1, |
341 |
> |
Integer.MIN_VALUE, |
342 |
> |
Integer.MAX_VALUE, |
343 |
> |
}; |
344 |
> |
for (int val : vals) { |
345 |
> |
a.setPendingCount(val); |
346 |
> |
assertEquals(val, a.getPendingCount()); |
347 |
> |
} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
357 |
|
assertEquals(1, a.getPendingCount()); |
358 |
|
a.addToPendingCount(27); |
359 |
|
assertEquals(28, a.getPendingCount()); |
360 |
+ |
a.addToPendingCount(-28); |
361 |
+ |
assertEquals(0, a.getPendingCount()); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
|
* decrementPendingCountUnlessZero decrements reported pending |
366 |
|
* count unless zero |
367 |
|
*/ |
368 |
< |
public void testDecrementPendingCount() { |
369 |
< |
NoopCC a = new NoopCC(); |
370 |
< |
assertEquals(0, a.getPendingCount()); |
371 |
< |
a.addToPendingCount(1); |
368 |
> |
public void testDecrementPendingCountUnlessZero() { |
369 |
> |
NoopCC a = new NoopCC(null, 2); |
370 |
> |
assertEquals(2, a.getPendingCount()); |
371 |
> |
assertEquals(2, a.decrementPendingCountUnlessZero()); |
372 |
|
assertEquals(1, a.getPendingCount()); |
373 |
< |
a.decrementPendingCountUnlessZero(); |
373 |
> |
assertEquals(1, a.decrementPendingCountUnlessZero()); |
374 |
|
assertEquals(0, a.getPendingCount()); |
375 |
< |
a.decrementPendingCountUnlessZero(); |
375 |
> |
assertEquals(0, a.decrementPendingCountUnlessZero()); |
376 |
|
assertEquals(0, a.getPendingCount()); |
377 |
+ |
a.setPendingCount(-1); |
378 |
+ |
assertEquals(-1, a.decrementPendingCountUnlessZero()); |
379 |
+ |
assertEquals(-2, a.getPendingCount()); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* quietlyCompleteRoot completes root task |
503 |
> |
* quietlyCompleteRoot completes root task and only root task |
504 |
|
*/ |
505 |
|
public void testQuietlyCompleteRoot() { |
506 |
|
NoopCC a = new NoopCC(); |