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() { |
49 |
|
} |
50 |
|
|
51 |
|
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
52 |
< |
try { |
52 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
53 |
|
assertFalse(a.isDone()); |
54 |
|
assertFalse(a.isCompletedNormally()); |
55 |
|
assertFalse(a.isCompletedAbnormally()); |
65 |
|
assertFalse(a.isCancelled()); |
66 |
|
assertNull(a.getException()); |
67 |
|
assertNull(a.getRawResult()); |
68 |
– |
} finally { |
69 |
– |
joinPool(pool); |
68 |
|
} |
69 |
|
} |
70 |
|
|
93 |
|
|
94 |
|
{ |
95 |
|
Thread.currentThread().interrupt(); |
96 |
< |
long t0 = System.nanoTime(); |
96 |
> |
long startTime = System.nanoTime(); |
97 |
|
assertNull(a.join()); |
98 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
98 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
99 |
|
Thread.interrupted(); |
100 |
|
} |
101 |
|
|
102 |
|
{ |
103 |
|
Thread.currentThread().interrupt(); |
104 |
< |
long t0 = System.nanoTime(); |
104 |
> |
long startTime = System.nanoTime(); |
105 |
|
a.quietlyJoin(); // should be no-op |
106 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
106 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
107 |
|
Thread.interrupted(); |
108 |
|
} |
109 |
|
|
136 |
|
Thread.interrupted(); |
137 |
|
|
138 |
|
{ |
139 |
< |
long t0 = System.nanoTime(); |
139 |
> |
long startTime = System.nanoTime(); |
140 |
|
a.quietlyJoin(); // should be no-op |
141 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
141 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
142 |
|
} |
143 |
|
|
144 |
|
try { |
174 |
|
Thread.interrupted(); |
175 |
|
|
176 |
|
{ |
177 |
< |
long t0 = System.nanoTime(); |
177 |
> |
long startTime = System.nanoTime(); |
178 |
|
a.quietlyJoin(); // should be no-op |
179 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
179 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
180 |
|
} |
181 |
|
|
182 |
|
try { |
196 |
|
try { |
197 |
|
a.invoke(); |
198 |
|
shouldThrow(); |
199 |
< |
} catch (Throwable ex) { |
200 |
< |
assertSame(t, ex); |
199 |
> |
} catch (Throwable success) { |
200 |
> |
assertSame(t, success); |
201 |
|
} |
202 |
|
} |
203 |
|
|
210 |
|
final AtomicInteger onCompletionN = new AtomicInteger(0); |
211 |
|
final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0); |
212 |
|
final AtomicInteger setRawResultN = new AtomicInteger(0); |
213 |
< |
final AtomicReference<Object> rawResult = new AtomicReference<>(null); |
213 |
> |
final AtomicReference<Object> rawResult = new AtomicReference<Object>(null); |
214 |
|
int computeN() { return computeN.get(); } |
215 |
|
int onCompletionN() { return onCompletionN.get(); } |
216 |
|
int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); } |
497 |
|
// Invocation tests use some interdependent task classes |
498 |
|
// to better test propagation etc |
499 |
|
|
500 |
< |
|
501 |
< |
// Version of Fibonacci with different classes for left vs right forks |
500 |
> |
/** |
501 |
> |
* Version of Fibonacci with different classes for left vs right forks |
502 |
> |
*/ |
503 |
|
abstract class CCF extends CheckedCC { |
504 |
|
int number; |
505 |
|
int rnumber; |
1135 |
|
} |
1136 |
|
|
1137 |
|
/** |
1138 |
< |
* invokeAll(collection) throws exception if any task does |
1138 |
> |
* invokeAll(collection) throws exception if any task does |
1139 |
|
*/ |
1140 |
|
public void testAbnormalInvokeAllCollection() { |
1141 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1800 |
|
} |
1801 |
|
|
1802 |
|
/** |
1803 |
< |
* invokeAll(collection) throws exception if any task does |
1803 |
> |
* invokeAll(collection) throws exception if any task does |
1804 |
|
*/ |
1805 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1806 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |