3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
– |
import junit.framework.*; |
7 |
– |
import java.util.concurrent.*; |
8 |
– |
import java.util.*; |
6 |
|
|
7 |
+ |
import junit.framework.*; |
8 |
+ |
import java.util.concurrent.CancellationException; |
9 |
+ |
import java.util.concurrent.ExecutionException; |
10 |
+ |
import java.util.concurrent.ForkJoinPool; |
11 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
+ |
import java.util.concurrent.RecursiveAction; |
13 |
+ |
import java.util.concurrent.TimeUnit; |
14 |
+ |
import java.util.HashSet; |
15 |
|
|
16 |
|
public class RecursiveActionTest extends JSR166TestCase { |
17 |
|
|
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
+ |
|
22 |
|
public static Test suite() { |
23 |
< |
return new TestSuite(RecursiveActionTest.class); |
23 |
> |
return new TestSuite(RecursiveActionTest.class); |
24 |
> |
} |
25 |
> |
|
26 |
> |
private static ForkJoinPool mainPool() { |
27 |
> |
return new ForkJoinPool(); |
28 |
> |
} |
29 |
> |
|
30 |
> |
private static ForkJoinPool singletonPool() { |
31 |
> |
return new ForkJoinPool(1); |
32 |
|
} |
33 |
|
|
34 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
35 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
36 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
37 |
< |
static { |
38 |
< |
asyncSingletonPool.setAsyncMode(true); |
34 |
> |
private static ForkJoinPool asyncSingletonPool() { |
35 |
> |
return new ForkJoinPool(1, |
36 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
37 |
> |
null, true); |
38 |
> |
} |
39 |
> |
|
40 |
> |
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
41 |
> |
try { |
42 |
> |
assertFalse(a.isDone()); |
43 |
> |
assertFalse(a.isCompletedNormally()); |
44 |
> |
assertFalse(a.isCompletedAbnormally()); |
45 |
> |
assertFalse(a.isCancelled()); |
46 |
> |
assertNull(a.getException()); |
47 |
> |
|
48 |
> |
assertNull(pool.invoke(a)); |
49 |
> |
|
50 |
> |
assertTrue(a.isDone()); |
51 |
> |
assertTrue(a.isCompletedNormally()); |
52 |
> |
assertFalse(a.isCompletedAbnormally()); |
53 |
> |
assertFalse(a.isCancelled()); |
54 |
> |
assertNull(a.getException()); |
55 |
> |
} finally { |
56 |
> |
joinPool(pool); |
57 |
> |
} |
58 |
|
} |
59 |
|
|
60 |
|
static final class FJException extends RuntimeException { |
62 |
|
} |
63 |
|
|
64 |
|
// A simple recursive action for testing |
65 |
< |
static final class FibAction extends RecursiveAction { |
65 |
> |
final class FibAction extends CheckedRecursiveAction { |
66 |
|
final int number; |
67 |
|
int result; |
68 |
|
FibAction(int n) { number = n; } |
69 |
< |
public void compute() { |
69 |
> |
public void realCompute() { |
70 |
|
int n = number; |
71 |
|
if (n <= 1) |
72 |
|
result = n; |
101 |
|
* invoke returns when task completes normally. |
102 |
|
* isCompletedAbnormally and isCancelled return false for normally |
103 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
71 |
– |
* |
104 |
|
*/ |
105 |
|
public void testInvoke() { |
106 |
< |
RecursiveAction a = new RecursiveAction() { |
107 |
< |
public void compute() { |
108 |
< |
FibAction f = new FibAction(8); |
109 |
< |
f.invoke(); |
110 |
< |
threadAssertTrue(f.result == 21); |
111 |
< |
threadAssertTrue(f.isDone()); |
112 |
< |
threadAssertFalse(f.isCancelled()); |
113 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
114 |
< |
threadAssertTrue(f.getRawResult() == null); |
115 |
< |
} |
116 |
< |
}; |
85 |
< |
mainPool.invoke(a); |
106 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
107 |
> |
public void realCompute() { |
108 |
> |
FibAction f = new FibAction(8); |
109 |
> |
assertNull(f.invoke()); |
110 |
> |
assertEquals(21, f.result); |
111 |
> |
assertTrue(f.isDone()); |
112 |
> |
assertFalse(f.isCancelled()); |
113 |
> |
assertFalse(f.isCompletedAbnormally()); |
114 |
> |
assertNull(f.getRawResult()); |
115 |
> |
}}; |
116 |
> |
testInvokeOnPool(mainPool(), a); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
122 |
|
* completed tasks |
123 |
|
*/ |
124 |
|
public void testQuietlyInvoke() { |
125 |
< |
RecursiveAction a = new RecursiveAction() { |
126 |
< |
public void compute() { |
127 |
< |
FibAction f = new FibAction(8); |
128 |
< |
f.quietlyInvoke(); |
129 |
< |
threadAssertTrue(f.result == 21); |
130 |
< |
threadAssertTrue(f.isDone()); |
131 |
< |
threadAssertFalse(f.isCancelled()); |
132 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
133 |
< |
threadAssertTrue(f.getRawResult() == null); |
134 |
< |
} |
135 |
< |
}; |
105 |
< |
mainPool.invoke(a); |
125 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
126 |
> |
public void realCompute() { |
127 |
> |
FibAction f = new FibAction(8); |
128 |
> |
f.quietlyInvoke(); |
129 |
> |
assertEquals(21, f.result); |
130 |
> |
assertTrue(f.isDone()); |
131 |
> |
assertFalse(f.isCancelled()); |
132 |
> |
assertFalse(f.isCompletedAbnormally()); |
133 |
> |
assertNull(f.getRawResult()); |
134 |
> |
}}; |
135 |
> |
testInvokeOnPool(mainPool(), a); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
|
* join of a forked task returns when task completes |
140 |
|
*/ |
141 |
|
public void testForkJoin() { |
142 |
< |
RecursiveAction a = new RecursiveAction() { |
143 |
< |
public void compute() { |
144 |
< |
FibAction f = new FibAction(8); |
145 |
< |
f.fork(); |
146 |
< |
f.join(); |
147 |
< |
threadAssertTrue(f.result == 21); |
148 |
< |
threadAssertTrue(f.isDone()); |
149 |
< |
threadAssertTrue(f.getRawResult() == null); |
150 |
< |
} |
151 |
< |
}; |
122 |
< |
mainPool.invoke(a); |
142 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
143 |
> |
public void realCompute() { |
144 |
> |
FibAction f = new FibAction(8); |
145 |
> |
assertSame(f, f.fork()); |
146 |
> |
assertNull(f.join()); |
147 |
> |
assertEquals(21, f.result); |
148 |
> |
assertTrue(f.isDone()); |
149 |
> |
assertNull(f.getRawResult()); |
150 |
> |
}}; |
151 |
> |
testInvokeOnPool(mainPool(), a); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
|
* get of a forked task returns when task completes |
156 |
|
*/ |
157 |
|
public void testForkGet() { |
158 |
< |
RecursiveAction a = new RecursiveAction() { |
159 |
< |
public void compute() { |
160 |
< |
try { |
161 |
< |
FibAction f = new FibAction(8); |
162 |
< |
f.fork(); |
163 |
< |
f.get(); |
164 |
< |
threadAssertTrue(f.result == 21); |
165 |
< |
threadAssertTrue(f.isDone()); |
166 |
< |
} catch (Exception ex) { |
138 |
< |
unexpectedException(); |
139 |
< |
} |
140 |
< |
} |
141 |
< |
}; |
142 |
< |
mainPool.invoke(a); |
158 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
159 |
> |
public void realCompute() throws Exception { |
160 |
> |
FibAction f = new FibAction(8); |
161 |
> |
assertSame(f, f.fork()); |
162 |
> |
assertNull(f.get()); |
163 |
> |
assertEquals(21, f.result); |
164 |
> |
assertTrue(f.isDone()); |
165 |
> |
}}; |
166 |
> |
testInvokeOnPool(mainPool(), a); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
|
* timed get of a forked task returns when task completes |
171 |
|
*/ |
172 |
|
public void testForkTimedGet() { |
173 |
< |
RecursiveAction a = new RecursiveAction() { |
174 |
< |
public void compute() { |
175 |
< |
try { |
176 |
< |
FibAction f = new FibAction(8); |
177 |
< |
f.fork(); |
178 |
< |
f.get(5L, TimeUnit.SECONDS); |
179 |
< |
threadAssertTrue(f.result == 21); |
180 |
< |
threadAssertTrue(f.isDone()); |
181 |
< |
} catch (Exception ex) { |
158 |
< |
unexpectedException(); |
159 |
< |
} |
160 |
< |
} |
161 |
< |
}; |
162 |
< |
mainPool.invoke(a); |
173 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
174 |
> |
public void realCompute() throws Exception { |
175 |
> |
FibAction f = new FibAction(8); |
176 |
> |
assertSame(f, f.fork()); |
177 |
> |
assertNull(f.get(5L, TimeUnit.SECONDS)); |
178 |
> |
assertEquals(21, f.result); |
179 |
> |
assertTrue(f.isDone()); |
180 |
> |
}}; |
181 |
> |
testInvokeOnPool(mainPool(), a); |
182 |
|
} |
183 |
|
|
184 |
|
/** |
185 |
|
* timed get with null time unit throws NPE |
186 |
|
*/ |
187 |
|
public void testForkTimedGetNPE() { |
188 |
< |
RecursiveAction a = new RecursiveAction() { |
189 |
< |
public void compute() { |
190 |
< |
try { |
191 |
< |
FibAction f = new FibAction(8); |
192 |
< |
f.fork(); |
193 |
< |
f.get(5L, null); |
194 |
< |
} catch (NullPointerException success) { |
195 |
< |
} catch (Exception ex) { |
196 |
< |
unexpectedException(); |
197 |
< |
} |
179 |
< |
} |
180 |
< |
}; |
181 |
< |
mainPool.invoke(a); |
182 |
< |
} |
183 |
< |
|
184 |
< |
/** |
185 |
< |
* helpJoin of a forked task returns when task completes |
186 |
< |
*/ |
187 |
< |
public void testForkHelpJoin() { |
188 |
< |
RecursiveAction a = new RecursiveAction() { |
189 |
< |
public void compute() { |
190 |
< |
FibAction f = new FibAction(8); |
191 |
< |
f.fork(); |
192 |
< |
f.helpJoin(); |
193 |
< |
threadAssertTrue(f.result == 21); |
194 |
< |
threadAssertTrue(f.isDone()); |
195 |
< |
} |
196 |
< |
}; |
197 |
< |
mainPool.invoke(a); |
188 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
189 |
> |
public void realCompute() throws Exception { |
190 |
> |
FibAction f = new FibAction(8); |
191 |
> |
assertSame(f, f.fork()); |
192 |
> |
try { |
193 |
> |
f.get(5L, null); |
194 |
> |
shouldThrow(); |
195 |
> |
} catch (NullPointerException success) {} |
196 |
> |
}}; |
197 |
> |
testInvokeOnPool(mainPool(), a); |
198 |
|
} |
199 |
|
|
200 |
|
/** |
201 |
|
* quietlyJoin of a forked task returns when task completes |
202 |
|
*/ |
203 |
|
public void testForkQuietlyJoin() { |
204 |
< |
RecursiveAction a = new RecursiveAction() { |
205 |
< |
public void compute() { |
206 |
< |
FibAction f = new FibAction(8); |
207 |
< |
f.fork(); |
208 |
< |
f.quietlyJoin(); |
209 |
< |
threadAssertTrue(f.result == 21); |
210 |
< |
threadAssertTrue(f.isDone()); |
211 |
< |
} |
212 |
< |
}; |
213 |
< |
mainPool.invoke(a); |
214 |
< |
} |
215 |
< |
|
216 |
< |
|
217 |
< |
/** |
218 |
< |
* quietlyHelpJoin of a forked task returns when task completes |
219 |
< |
*/ |
220 |
< |
public void testForkQuietlyHelpJoin() { |
221 |
< |
RecursiveAction a = new RecursiveAction() { |
222 |
< |
public void compute() { |
223 |
< |
FibAction f = new FibAction(8); |
224 |
< |
f.fork(); |
225 |
< |
f.quietlyHelpJoin(); |
226 |
< |
threadAssertTrue(f.result == 21); |
227 |
< |
threadAssertTrue(f.isDone()); |
228 |
< |
} |
229 |
< |
}; |
230 |
< |
mainPool.invoke(a); |
204 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
205 |
> |
public void realCompute() { |
206 |
> |
FibAction f = new FibAction(8); |
207 |
> |
assertSame(f, f.fork()); |
208 |
> |
f.quietlyJoin(); |
209 |
> |
assertEquals(21, f.result); |
210 |
> |
assertTrue(f.isDone()); |
211 |
> |
}}; |
212 |
> |
testInvokeOnPool(mainPool(), a); |
213 |
|
} |
214 |
|
|
215 |
|
|
218 |
|
* getQueuedTaskCount returns 0 when quiescent |
219 |
|
*/ |
220 |
|
public void testForkHelpQuiesce() { |
221 |
< |
RecursiveAction a = new RecursiveAction() { |
222 |
< |
public void compute() { |
223 |
< |
FibAction f = new FibAction(8); |
224 |
< |
f.fork(); |
225 |
< |
f.helpQuiesce(); |
226 |
< |
threadAssertTrue(f.result == 21); |
227 |
< |
threadAssertTrue(f.isDone()); |
228 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
229 |
< |
} |
230 |
< |
}; |
249 |
< |
mainPool.invoke(a); |
221 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
222 |
> |
public void realCompute() { |
223 |
> |
FibAction f = new FibAction(8); |
224 |
> |
assertSame(f, f.fork()); |
225 |
> |
f.helpQuiesce(); |
226 |
> |
assertEquals(21, f.result); |
227 |
> |
assertTrue(f.isDone()); |
228 |
> |
assertEquals(0, getQueuedTaskCount()); |
229 |
> |
}}; |
230 |
> |
testInvokeOnPool(mainPool(), a); |
231 |
|
} |
232 |
|
|
233 |
|
|
235 |
|
* invoke task throws exception when task completes abnormally |
236 |
|
*/ |
237 |
|
public void testAbnormalInvoke() { |
238 |
< |
RecursiveAction a = new RecursiveAction() { |
239 |
< |
public void compute() { |
240 |
< |
try { |
241 |
< |
FailingFibAction f = new FailingFibAction(8); |
242 |
< |
f.invoke(); |
243 |
< |
shouldThrow(); |
244 |
< |
} catch (FJException success) { |
245 |
< |
} |
246 |
< |
} |
266 |
< |
}; |
267 |
< |
mainPool.invoke(a); |
238 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
239 |
> |
public void realCompute() { |
240 |
> |
FailingFibAction f = new FailingFibAction(8); |
241 |
> |
try { |
242 |
> |
f.invoke(); |
243 |
> |
shouldThrow(); |
244 |
> |
} catch (FJException success) {} |
245 |
> |
}}; |
246 |
> |
testInvokeOnPool(mainPool(), a); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* quietelyInvoke task returns when task completes abnormally |
250 |
> |
* quietlyInvoke task returns when task completes abnormally |
251 |
|
*/ |
252 |
|
public void testAbnormalQuietlyInvoke() { |
253 |
< |
RecursiveAction a = new RecursiveAction() { |
254 |
< |
public void compute() { |
255 |
< |
FailingFibAction f = new FailingFibAction(8); |
256 |
< |
f.quietlyInvoke(); |
257 |
< |
threadAssertTrue(f.isDone()); |
258 |
< |
} |
259 |
< |
}; |
281 |
< |
mainPool.invoke(a); |
253 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
254 |
> |
public void realCompute() { |
255 |
> |
FailingFibAction f = new FailingFibAction(8); |
256 |
> |
f.quietlyInvoke(); |
257 |
> |
assertTrue(f.isDone()); |
258 |
> |
}}; |
259 |
> |
testInvokeOnPool(mainPool(), a); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
|
* join of a forked task throws exception when task completes abnormally |
264 |
|
*/ |
265 |
|
public void testAbnormalForkJoin() { |
266 |
< |
RecursiveAction a = new RecursiveAction() { |
267 |
< |
public void compute() { |
268 |
< |
try { |
269 |
< |
FailingFibAction f = new FailingFibAction(8); |
270 |
< |
f.fork(); |
271 |
< |
f.join(); |
272 |
< |
shouldThrow(); |
273 |
< |
} catch (FJException success) { |
274 |
< |
} |
275 |
< |
} |
298 |
< |
}; |
299 |
< |
mainPool.invoke(a); |
266 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
267 |
> |
public void realCompute() { |
268 |
> |
FailingFibAction f = new FailingFibAction(8); |
269 |
> |
assertSame(f, f.fork()); |
270 |
> |
try { |
271 |
> |
f.join(); |
272 |
> |
shouldThrow(); |
273 |
> |
} catch (FJException success) {} |
274 |
> |
}}; |
275 |
> |
testInvokeOnPool(mainPool(), a); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
|
* get of a forked task throws exception when task completes abnormally |
280 |
|
*/ |
281 |
|
public void testAbnormalForkGet() { |
282 |
< |
RecursiveAction a = new RecursiveAction() { |
283 |
< |
public void compute() { |
284 |
< |
try { |
285 |
< |
FailingFibAction f = new FailingFibAction(8); |
286 |
< |
f.fork(); |
287 |
< |
f.get(); |
288 |
< |
shouldThrow(); |
289 |
< |
} catch (Exception success) { |
290 |
< |
} |
291 |
< |
} |
316 |
< |
}; |
317 |
< |
mainPool.invoke(a); |
282 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
283 |
> |
public void realCompute() throws Exception { |
284 |
> |
FailingFibAction f = new FailingFibAction(8); |
285 |
> |
assertSame(f, f.fork()); |
286 |
> |
try { |
287 |
> |
f.get(); |
288 |
> |
shouldThrow(); |
289 |
> |
} catch (ExecutionException success) {} |
290 |
> |
}}; |
291 |
> |
testInvokeOnPool(mainPool(), a); |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
|
* timed get of a forked task throws exception when task completes abnormally |
296 |
|
*/ |
297 |
|
public void testAbnormalForkTimedGet() { |
298 |
< |
RecursiveAction a = new RecursiveAction() { |
299 |
< |
public void compute() { |
300 |
< |
try { |
301 |
< |
FailingFibAction f = new FailingFibAction(8); |
302 |
< |
f.fork(); |
303 |
< |
f.get(5L, TimeUnit.SECONDS); |
304 |
< |
shouldThrow(); |
305 |
< |
} catch (Exception success) { |
306 |
< |
} |
307 |
< |
} |
334 |
< |
}; |
335 |
< |
mainPool.invoke(a); |
336 |
< |
} |
337 |
< |
|
338 |
< |
/** |
339 |
< |
* join of a forked task throws exception when task completes abnormally |
340 |
< |
*/ |
341 |
< |
public void testAbnormalForkHelpJoin() { |
342 |
< |
RecursiveAction a = new RecursiveAction() { |
343 |
< |
public void compute() { |
344 |
< |
try { |
345 |
< |
FailingFibAction f = new FailingFibAction(8); |
346 |
< |
f.fork(); |
347 |
< |
f.helpJoin(); |
348 |
< |
shouldThrow(); |
349 |
< |
} catch (FJException success) { |
350 |
< |
} |
351 |
< |
} |
352 |
< |
}; |
353 |
< |
mainPool.invoke(a); |
354 |
< |
} |
355 |
< |
|
356 |
< |
/** |
357 |
< |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
358 |
< |
* getException of failed task returns its exception. |
359 |
< |
* isCompletedAbnormally of a failed task returns true. |
360 |
< |
* isCancelled of a failed uncancelled task returns false |
361 |
< |
*/ |
362 |
< |
public void testAbnormalForkQuietlyHelpJoin() { |
363 |
< |
RecursiveAction a = new RecursiveAction() { |
364 |
< |
public void compute() { |
365 |
< |
FailingFibAction f = new FailingFibAction(8); |
366 |
< |
f.fork(); |
367 |
< |
f.quietlyHelpJoin(); |
368 |
< |
threadAssertTrue(f.isDone()); |
369 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
370 |
< |
threadAssertFalse(f.isCancelled()); |
371 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
372 |
< |
} |
373 |
< |
}; |
374 |
< |
mainPool.invoke(a); |
298 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
299 |
> |
public void realCompute() throws Exception { |
300 |
> |
FailingFibAction f = new FailingFibAction(8); |
301 |
> |
assertSame(f, f.fork()); |
302 |
> |
try { |
303 |
> |
f.get(5L, TimeUnit.SECONDS); |
304 |
> |
shouldThrow(); |
305 |
> |
} catch (ExecutionException success) {} |
306 |
> |
}}; |
307 |
> |
testInvokeOnPool(mainPool(), a); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
312 |
|
*/ |
313 |
|
public void testAbnormalForkQuietlyJoin() { |
314 |
< |
RecursiveAction a = new RecursiveAction() { |
315 |
< |
public void compute() { |
316 |
< |
FailingFibAction f = new FailingFibAction(8); |
317 |
< |
f.fork(); |
318 |
< |
f.quietlyJoin(); |
319 |
< |
threadAssertTrue(f.isDone()); |
320 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
321 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
322 |
< |
} |
323 |
< |
}; |
391 |
< |
mainPool.invoke(a); |
314 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
315 |
> |
public void realCompute() { |
316 |
> |
FailingFibAction f = new FailingFibAction(8); |
317 |
> |
assertSame(f, f.fork()); |
318 |
> |
f.quietlyJoin(); |
319 |
> |
assertTrue(f.isDone()); |
320 |
> |
assertTrue(f.isCompletedAbnormally()); |
321 |
> |
assertTrue(f.getException() instanceof FJException); |
322 |
> |
}}; |
323 |
> |
testInvokeOnPool(mainPool(), a); |
324 |
|
} |
325 |
|
|
326 |
|
/** |
327 |
|
* invoke task throws exception when task cancelled |
328 |
|
*/ |
329 |
|
public void testCancelledInvoke() { |
330 |
< |
RecursiveAction a = new RecursiveAction() { |
331 |
< |
public void compute() { |
332 |
< |
try { |
333 |
< |
FibAction f = new FibAction(8); |
334 |
< |
f.cancel(true); |
335 |
< |
f.invoke(); |
336 |
< |
shouldThrow(); |
337 |
< |
} catch (CancellationException success) { |
338 |
< |
} |
339 |
< |
} |
408 |
< |
}; |
409 |
< |
mainPool.invoke(a); |
330 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
331 |
> |
public void realCompute() { |
332 |
> |
FibAction f = new FibAction(8); |
333 |
> |
assertTrue(f.cancel(true)); |
334 |
> |
try { |
335 |
> |
f.invoke(); |
336 |
> |
shouldThrow(); |
337 |
> |
} catch (CancellationException success) {} |
338 |
> |
}}; |
339 |
> |
testInvokeOnPool(mainPool(), a); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
|
* join of a forked task throws exception when task cancelled |
344 |
|
*/ |
345 |
|
public void testCancelledForkJoin() { |
346 |
< |
RecursiveAction a = new RecursiveAction() { |
347 |
< |
public void compute() { |
348 |
< |
try { |
349 |
< |
FibAction f = new FibAction(8); |
350 |
< |
f.cancel(true); |
351 |
< |
f.fork(); |
352 |
< |
f.join(); |
353 |
< |
shouldThrow(); |
354 |
< |
} catch (CancellationException success) { |
355 |
< |
} |
356 |
< |
} |
427 |
< |
}; |
428 |
< |
mainPool.invoke(a); |
346 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
347 |
> |
public void realCompute() { |
348 |
> |
FibAction f = new FibAction(8); |
349 |
> |
assertTrue(f.cancel(true)); |
350 |
> |
assertSame(f, f.fork()); |
351 |
> |
try { |
352 |
> |
f.join(); |
353 |
> |
shouldThrow(); |
354 |
> |
} catch (CancellationException success) {} |
355 |
> |
}}; |
356 |
> |
testInvokeOnPool(mainPool(), a); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
|
* get of a forked task throws exception when task cancelled |
361 |
|
*/ |
362 |
|
public void testCancelledForkGet() { |
363 |
< |
RecursiveAction a = new RecursiveAction() { |
364 |
< |
public void compute() { |
365 |
< |
try { |
366 |
< |
FibAction f = new FibAction(8); |
367 |
< |
f.cancel(true); |
368 |
< |
f.fork(); |
369 |
< |
f.get(); |
370 |
< |
shouldThrow(); |
371 |
< |
} catch (Exception success) { |
372 |
< |
} |
373 |
< |
} |
446 |
< |
}; |
447 |
< |
mainPool.invoke(a); |
363 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
364 |
> |
public void realCompute() throws Exception { |
365 |
> |
FibAction f = new FibAction(8); |
366 |
> |
assertTrue(f.cancel(true)); |
367 |
> |
assertSame(f, f.fork()); |
368 |
> |
try { |
369 |
> |
f.get(); |
370 |
> |
shouldThrow(); |
371 |
> |
} catch (CancellationException success) {} |
372 |
> |
}}; |
373 |
> |
testInvokeOnPool(mainPool(), a); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* timed get of a forked task throws exception when task cancelled |
378 |
|
*/ |
379 |
|
public void testCancelledForkTimedGet() { |
380 |
< |
RecursiveAction a = new RecursiveAction() { |
381 |
< |
public void compute() { |
382 |
< |
try { |
383 |
< |
FibAction f = new FibAction(8); |
384 |
< |
f.cancel(true); |
385 |
< |
f.fork(); |
386 |
< |
f.get(5L, TimeUnit.SECONDS); |
387 |
< |
shouldThrow(); |
388 |
< |
} catch (Exception success) { |
389 |
< |
} |
390 |
< |
} |
465 |
< |
}; |
466 |
< |
mainPool.invoke(a); |
467 |
< |
} |
468 |
< |
|
469 |
< |
/** |
470 |
< |
* join of a forked task throws exception when task cancelled |
471 |
< |
*/ |
472 |
< |
public void testCancelledForkHelpJoin() { |
473 |
< |
RecursiveAction a = new RecursiveAction() { |
474 |
< |
public void compute() { |
475 |
< |
try { |
476 |
< |
FibAction f = new FibAction(8); |
477 |
< |
f.cancel(true); |
478 |
< |
f.fork(); |
479 |
< |
f.helpJoin(); |
480 |
< |
shouldThrow(); |
481 |
< |
} catch (CancellationException success) { |
482 |
< |
} |
483 |
< |
} |
484 |
< |
}; |
485 |
< |
mainPool.invoke(a); |
486 |
< |
} |
487 |
< |
|
488 |
< |
/** |
489 |
< |
* quietlyHelpJoin of a forked task returns when task cancelled. |
490 |
< |
* getException of cancelled task returns its exception. |
491 |
< |
* isCompletedAbnormally of a cancelled task returns true. |
492 |
< |
* isCancelled of a cancelled task returns true |
493 |
< |
*/ |
494 |
< |
public void testCancelledForkQuietlyHelpJoin() { |
495 |
< |
RecursiveAction a = new RecursiveAction() { |
496 |
< |
public void compute() { |
497 |
< |
FibAction f = new FibAction(8); |
498 |
< |
f.cancel(true); |
499 |
< |
f.fork(); |
500 |
< |
f.quietlyHelpJoin(); |
501 |
< |
threadAssertTrue(f.isDone()); |
502 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
503 |
< |
threadAssertTrue(f.isCancelled()); |
504 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
505 |
< |
} |
506 |
< |
}; |
507 |
< |
mainPool.invoke(a); |
380 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
381 |
> |
public void realCompute() throws Exception { |
382 |
> |
FibAction f = new FibAction(8); |
383 |
> |
assertTrue(f.cancel(true)); |
384 |
> |
assertSame(f, f.fork()); |
385 |
> |
try { |
386 |
> |
f.get(5L, TimeUnit.SECONDS); |
387 |
> |
shouldThrow(); |
388 |
> |
} catch (CancellationException success) {} |
389 |
> |
}}; |
390 |
> |
testInvokeOnPool(mainPool(), a); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
|
* quietlyJoin of a forked task returns when task cancelled |
395 |
|
*/ |
396 |
|
public void testCancelledForkQuietlyJoin() { |
397 |
< |
RecursiveAction a = new RecursiveAction() { |
398 |
< |
public void compute() { |
399 |
< |
FibAction f = new FibAction(8); |
400 |
< |
f.cancel(true); |
401 |
< |
f.fork(); |
402 |
< |
f.quietlyJoin(); |
403 |
< |
threadAssertTrue(f.isDone()); |
404 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
405 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
406 |
< |
} |
407 |
< |
}; |
408 |
< |
mainPool.invoke(a); |
397 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
398 |
> |
public void realCompute() { |
399 |
> |
FibAction f = new FibAction(8); |
400 |
> |
assertTrue(f.cancel(true)); |
401 |
> |
assertSame(f, f.fork()); |
402 |
> |
f.quietlyJoin(); |
403 |
> |
assertTrue(f.isDone()); |
404 |
> |
assertTrue(f.isCompletedAbnormally()); |
405 |
> |
assertTrue(f.isCancelled()); |
406 |
> |
assertTrue(f.getException() instanceof CancellationException); |
407 |
> |
}}; |
408 |
> |
testInvokeOnPool(mainPool(), a); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
|
* getPool of executing task returns its pool |
413 |
|
*/ |
414 |
|
public void testGetPool() { |
415 |
< |
RecursiveAction a = new RecursiveAction() { |
416 |
< |
public void compute() { |
417 |
< |
threadAssertTrue(getPool() == mainPool); |
418 |
< |
} |
419 |
< |
}; |
420 |
< |
mainPool.invoke(a); |
415 |
> |
final ForkJoinPool mainPool = mainPool(); |
416 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
417 |
> |
public void realCompute() { |
418 |
> |
assertSame(mainPool, getPool()); |
419 |
> |
}}; |
420 |
> |
testInvokeOnPool(mainPool, a); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
|
* getPool of non-FJ task returns null |
425 |
|
*/ |
426 |
|
public void testGetPool2() { |
427 |
< |
RecursiveAction a = new RecursiveAction() { |
428 |
< |
public void compute() { |
429 |
< |
threadAssertTrue(getPool() == null); |
430 |
< |
} |
431 |
< |
}; |
549 |
< |
a.invoke(); |
427 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
428 |
> |
public void realCompute() { |
429 |
> |
assertNull(getPool()); |
430 |
> |
}}; |
431 |
> |
assertNull(a.invoke()); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
|
* inForkJoinPool of executing task returns true |
436 |
|
*/ |
437 |
|
public void testInForkJoinPool() { |
438 |
< |
RecursiveAction a = new RecursiveAction() { |
439 |
< |
public void compute() { |
440 |
< |
threadAssertTrue(inForkJoinPool()); |
441 |
< |
} |
442 |
< |
}; |
561 |
< |
mainPool.invoke(a); |
438 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
439 |
> |
public void realCompute() { |
440 |
> |
assertTrue(inForkJoinPool()); |
441 |
> |
}}; |
442 |
> |
testInvokeOnPool(mainPool(), a); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
|
* inForkJoinPool of non-FJ task returns false |
447 |
|
*/ |
448 |
|
public void testInForkJoinPool2() { |
449 |
< |
RecursiveAction a = new RecursiveAction() { |
450 |
< |
public void compute() { |
451 |
< |
threadAssertTrue(!inForkJoinPool()); |
452 |
< |
} |
453 |
< |
}; |
573 |
< |
a.invoke(); |
449 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
450 |
> |
public void realCompute() { |
451 |
> |
assertFalse(inForkJoinPool()); |
452 |
> |
}}; |
453 |
> |
assertNull(a.invoke()); |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
|
* getPool of current thread in pool returns its pool |
458 |
|
*/ |
459 |
|
public void testWorkerGetPool() { |
460 |
< |
RecursiveAction a = new RecursiveAction() { |
461 |
< |
public void compute() { |
462 |
< |
ForkJoinWorkerThread w = |
463 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
464 |
< |
threadAssertTrue(w.getPool() == mainPool); |
465 |
< |
} |
466 |
< |
}; |
467 |
< |
mainPool.invoke(a); |
460 |
> |
final ForkJoinPool mainPool = mainPool(); |
461 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
462 |
> |
public void realCompute() { |
463 |
> |
ForkJoinWorkerThread w = |
464 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
465 |
> |
assertSame(mainPool, w.getPool()); |
466 |
> |
}}; |
467 |
> |
testInvokeOnPool(mainPool, a); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
592 |
– |
* |
472 |
|
*/ |
473 |
|
public void testWorkerGetPoolIndex() { |
474 |
< |
RecursiveAction a = new RecursiveAction() { |
475 |
< |
public void compute() { |
476 |
< |
ForkJoinWorkerThread w = |
477 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
478 |
< |
int idx = w.getPoolIndex(); |
479 |
< |
threadAssertTrue(idx >= 0); |
480 |
< |
threadAssertTrue(idx < mainPool.getPoolSize()); |
481 |
< |
} |
482 |
< |
}; |
483 |
< |
mainPool.invoke(a); |
474 |
> |
final ForkJoinPool mainPool = mainPool(); |
475 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
476 |
> |
public void realCompute() { |
477 |
> |
ForkJoinWorkerThread w = |
478 |
> |
(ForkJoinWorkerThread)(Thread.currentThread()); |
479 |
> |
int idx = w.getPoolIndex(); |
480 |
> |
assertTrue(idx >= 0); |
481 |
> |
assertTrue(idx < mainPool.getPoolSize()); |
482 |
> |
}}; |
483 |
> |
testInvokeOnPool(mainPool, a); |
484 |
|
} |
485 |
|
|
486 |
|
|
488 |
|
* setRawResult(null) succeeds |
489 |
|
*/ |
490 |
|
public void testSetRawResult() { |
491 |
< |
RecursiveAction a = new RecursiveAction() { |
492 |
< |
public void compute() { |
493 |
< |
setRawResult(null); |
494 |
< |
} |
495 |
< |
}; |
617 |
< |
a.invoke(); |
491 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
492 |
> |
public void realCompute() { |
493 |
> |
setRawResult(null); |
494 |
> |
}}; |
495 |
> |
assertNull(a.invoke()); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
|
* A reinitialized task may be re-invoked |
500 |
|
*/ |
501 |
|
public void testReinitialize() { |
502 |
< |
RecursiveAction a = new RecursiveAction() { |
503 |
< |
public void compute() { |
504 |
< |
FibAction f = new FibAction(8); |
505 |
< |
f.invoke(); |
506 |
< |
threadAssertTrue(f.result == 21); |
507 |
< |
threadAssertTrue(f.isDone()); |
508 |
< |
threadAssertFalse(f.isCancelled()); |
509 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
510 |
< |
f.reinitialize(); |
511 |
< |
f.invoke(); |
512 |
< |
threadAssertTrue(f.result == 21); |
513 |
< |
} |
514 |
< |
}; |
637 |
< |
mainPool.invoke(a); |
502 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
503 |
> |
public void realCompute() { |
504 |
> |
FibAction f = new FibAction(8); |
505 |
> |
assertNull(f.invoke()); |
506 |
> |
assertEquals(21, f.result); |
507 |
> |
assertTrue(f.isDone()); |
508 |
> |
assertFalse(f.isCancelled()); |
509 |
> |
assertFalse(f.isCompletedAbnormally()); |
510 |
> |
f.reinitialize(); |
511 |
> |
assertNull(f.invoke()); |
512 |
> |
assertEquals(21, f.result); |
513 |
> |
}}; |
514 |
> |
testInvokeOnPool(mainPool(), a); |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
|
* invoke task throws exception after invoking completeExceptionally |
519 |
|
*/ |
520 |
|
public void testCompleteExceptionally() { |
521 |
< |
RecursiveAction a = new RecursiveAction() { |
522 |
< |
public void compute() { |
523 |
< |
try { |
524 |
< |
FibAction f = new FibAction(8); |
525 |
< |
f.completeExceptionally(new FJException()); |
526 |
< |
f.invoke(); |
527 |
< |
shouldThrow(); |
528 |
< |
} catch (FJException success) { |
529 |
< |
} |
530 |
< |
} |
654 |
< |
}; |
655 |
< |
mainPool.invoke(a); |
521 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
522 |
> |
public void realCompute() { |
523 |
> |
FibAction f = new FibAction(8); |
524 |
> |
f.completeExceptionally(new FJException()); |
525 |
> |
try { |
526 |
> |
f.invoke(); |
527 |
> |
shouldThrow(); |
528 |
> |
} catch (FJException success) {} |
529 |
> |
}}; |
530 |
> |
testInvokeOnPool(mainPool(), a); |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
|
* invoke task suppresses execution invoking complete |
535 |
|
*/ |
536 |
|
public void testComplete() { |
537 |
< |
RecursiveAction a = new RecursiveAction() { |
538 |
< |
public void compute() { |
539 |
< |
FibAction f = new FibAction(8); |
540 |
< |
f.complete(null); |
541 |
< |
f.invoke(); |
542 |
< |
threadAssertTrue(f.isDone()); |
543 |
< |
threadAssertTrue(f.result == 0); |
544 |
< |
} |
545 |
< |
}; |
671 |
< |
mainPool.invoke(a); |
537 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
538 |
> |
public void realCompute() { |
539 |
> |
FibAction f = new FibAction(8); |
540 |
> |
f.complete(null); |
541 |
> |
assertNull(f.invoke()); |
542 |
> |
assertTrue(f.isDone()); |
543 |
> |
assertEquals(0, f.result); |
544 |
> |
}}; |
545 |
> |
testInvokeOnPool(mainPool(), a); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
|
* invokeAll(t1, t2) invokes all task arguments |
550 |
|
*/ |
551 |
|
public void testInvokeAll2() { |
552 |
< |
RecursiveAction a = new RecursiveAction() { |
553 |
< |
public void compute() { |
554 |
< |
FibAction f = new FibAction(8); |
555 |
< |
FibAction g = new FibAction(9); |
556 |
< |
invokeAll(f, g); |
557 |
< |
threadAssertTrue(f.isDone()); |
558 |
< |
threadAssertTrue(f.result == 21); |
559 |
< |
threadAssertTrue(g.isDone()); |
560 |
< |
threadAssertTrue(g.result == 34); |
561 |
< |
} |
562 |
< |
}; |
689 |
< |
mainPool.invoke(a); |
552 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
553 |
> |
public void realCompute() { |
554 |
> |
FibAction f = new FibAction(8); |
555 |
> |
FibAction g = new FibAction(9); |
556 |
> |
invokeAll(f, g); |
557 |
> |
assertTrue(f.isDone()); |
558 |
> |
assertEquals(21, f.result); |
559 |
> |
assertTrue(g.isDone()); |
560 |
> |
assertEquals(34, g.result); |
561 |
> |
}}; |
562 |
> |
testInvokeOnPool(mainPool(), a); |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
|
* invokeAll(tasks) with 1 argument invokes task |
567 |
|
*/ |
568 |
|
public void testInvokeAll1() { |
569 |
< |
RecursiveAction a = new RecursiveAction() { |
570 |
< |
public void compute() { |
571 |
< |
FibAction f = new FibAction(8); |
572 |
< |
invokeAll(f); |
573 |
< |
threadAssertTrue(f.isDone()); |
574 |
< |
threadAssertTrue(f.result == 21); |
575 |
< |
} |
576 |
< |
}; |
704 |
< |
mainPool.invoke(a); |
569 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
570 |
> |
public void realCompute() { |
571 |
> |
FibAction f = new FibAction(8); |
572 |
> |
invokeAll(f); |
573 |
> |
assertTrue(f.isDone()); |
574 |
> |
assertEquals(21, f.result); |
575 |
> |
}}; |
576 |
> |
testInvokeOnPool(mainPool(), a); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
580 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
581 |
|
*/ |
582 |
|
public void testInvokeAll3() { |
583 |
< |
RecursiveAction a = new RecursiveAction() { |
584 |
< |
public void compute() { |
585 |
< |
FibAction f = new FibAction(8); |
586 |
< |
FibAction g = new FibAction(9); |
587 |
< |
FibAction h = new FibAction(7); |
588 |
< |
invokeAll(f, g, h); |
589 |
< |
threadAssertTrue(f.isDone()); |
590 |
< |
threadAssertTrue(f.result == 21); |
591 |
< |
threadAssertTrue(g.isDone()); |
592 |
< |
threadAssertTrue(g.result == 34); |
593 |
< |
threadAssertTrue(h.isDone()); |
594 |
< |
threadAssertTrue(h.result == 13); |
595 |
< |
} |
596 |
< |
}; |
725 |
< |
mainPool.invoke(a); |
583 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
584 |
> |
public void realCompute() { |
585 |
> |
FibAction f = new FibAction(8); |
586 |
> |
FibAction g = new FibAction(9); |
587 |
> |
FibAction h = new FibAction(7); |
588 |
> |
invokeAll(f, g, h); |
589 |
> |
assertTrue(f.isDone()); |
590 |
> |
assertEquals(21, f.result); |
591 |
> |
assertTrue(g.isDone()); |
592 |
> |
assertEquals(34, g.result); |
593 |
> |
assertTrue(h.isDone()); |
594 |
> |
assertEquals(13, h.result); |
595 |
> |
}}; |
596 |
> |
testInvokeOnPool(mainPool(), a); |
597 |
|
} |
598 |
|
|
599 |
|
/** |
600 |
|
* invokeAll(collection) invokes all tasks in the collection |
601 |
|
*/ |
602 |
|
public void testInvokeAllCollection() { |
603 |
< |
RecursiveAction a = new RecursiveAction() { |
604 |
< |
public void compute() { |
605 |
< |
FibAction f = new FibAction(8); |
606 |
< |
FibAction g = new FibAction(9); |
607 |
< |
FibAction h = new FibAction(7); |
608 |
< |
HashSet set = new HashSet(); |
609 |
< |
set.add(f); |
610 |
< |
set.add(g); |
611 |
< |
set.add(h); |
612 |
< |
invokeAll(set); |
613 |
< |
threadAssertTrue(f.isDone()); |
614 |
< |
threadAssertTrue(f.result == 21); |
615 |
< |
threadAssertTrue(g.isDone()); |
616 |
< |
threadAssertTrue(g.result == 34); |
617 |
< |
threadAssertTrue(h.isDone()); |
618 |
< |
threadAssertTrue(h.result == 13); |
619 |
< |
} |
620 |
< |
}; |
750 |
< |
mainPool.invoke(a); |
603 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
604 |
> |
public void realCompute() { |
605 |
> |
FibAction f = new FibAction(8); |
606 |
> |
FibAction g = new FibAction(9); |
607 |
> |
FibAction h = new FibAction(7); |
608 |
> |
HashSet set = new HashSet(); |
609 |
> |
set.add(f); |
610 |
> |
set.add(g); |
611 |
> |
set.add(h); |
612 |
> |
invokeAll(set); |
613 |
> |
assertTrue(f.isDone()); |
614 |
> |
assertEquals(21, f.result); |
615 |
> |
assertTrue(g.isDone()); |
616 |
> |
assertEquals(34, g.result); |
617 |
> |
assertTrue(h.isDone()); |
618 |
> |
assertEquals(13, h.result); |
619 |
> |
}}; |
620 |
> |
testInvokeOnPool(mainPool(), a); |
621 |
|
} |
622 |
|
|
623 |
|
|
625 |
|
* invokeAll(tasks) with any null task throws NPE |
626 |
|
*/ |
627 |
|
public void testInvokeAllNPE() { |
628 |
< |
RecursiveAction a = new RecursiveAction() { |
629 |
< |
public void compute() { |
630 |
< |
try { |
631 |
< |
FibAction f = new FibAction(8); |
632 |
< |
FibAction g = new FibAction(9); |
633 |
< |
FibAction h = null; |
634 |
< |
invokeAll(f, g, h); |
635 |
< |
shouldThrow(); |
636 |
< |
} catch (NullPointerException success) { |
637 |
< |
} |
638 |
< |
} |
769 |
< |
}; |
770 |
< |
mainPool.invoke(a); |
628 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
629 |
> |
public void realCompute() { |
630 |
> |
FibAction f = new FibAction(8); |
631 |
> |
FibAction g = new FibAction(9); |
632 |
> |
FibAction h = null; |
633 |
> |
try { |
634 |
> |
invokeAll(f, g, h); |
635 |
> |
shouldThrow(); |
636 |
> |
} catch (NullPointerException success) {} |
637 |
> |
}}; |
638 |
> |
testInvokeOnPool(mainPool(), a); |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
|
* invokeAll(t1, t2) throw exception if any task does |
643 |
|
*/ |
644 |
|
public void testAbnormalInvokeAll2() { |
645 |
< |
RecursiveAction a = new RecursiveAction() { |
646 |
< |
public void compute() { |
647 |
< |
try { |
648 |
< |
FibAction f = new FibAction(8); |
649 |
< |
FailingFibAction g = new FailingFibAction(9); |
650 |
< |
invokeAll(f, g); |
651 |
< |
shouldThrow(); |
652 |
< |
} catch (FJException success) { |
653 |
< |
} |
654 |
< |
} |
787 |
< |
}; |
788 |
< |
mainPool.invoke(a); |
645 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
646 |
> |
public void realCompute() { |
647 |
> |
FibAction f = new FibAction(8); |
648 |
> |
FailingFibAction g = new FailingFibAction(9); |
649 |
> |
try { |
650 |
> |
invokeAll(f, g); |
651 |
> |
shouldThrow(); |
652 |
> |
} catch (FJException success) {} |
653 |
> |
}}; |
654 |
> |
testInvokeOnPool(mainPool(), a); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
659 |
|
*/ |
660 |
|
public void testAbnormalInvokeAll1() { |
661 |
< |
RecursiveAction a = new RecursiveAction() { |
662 |
< |
public void compute() { |
663 |
< |
try { |
664 |
< |
FailingFibAction g = new FailingFibAction(9); |
665 |
< |
invokeAll(g); |
666 |
< |
shouldThrow(); |
667 |
< |
} catch (FJException success) { |
668 |
< |
} |
669 |
< |
} |
804 |
< |
}; |
805 |
< |
mainPool.invoke(a); |
661 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
662 |
> |
public void realCompute() { |
663 |
> |
FailingFibAction g = new FailingFibAction(9); |
664 |
> |
try { |
665 |
> |
invokeAll(g); |
666 |
> |
shouldThrow(); |
667 |
> |
} catch (FJException success) {} |
668 |
> |
}}; |
669 |
> |
testInvokeOnPool(mainPool(), a); |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
674 |
|
*/ |
675 |
|
public void testAbnormalInvokeAll3() { |
676 |
< |
RecursiveAction a = new RecursiveAction() { |
677 |
< |
public void compute() { |
678 |
< |
try { |
679 |
< |
FibAction f = new FibAction(8); |
680 |
< |
FailingFibAction g = new FailingFibAction(9); |
681 |
< |
FibAction h = new FibAction(7); |
682 |
< |
invokeAll(f, g, h); |
683 |
< |
shouldThrow(); |
684 |
< |
} catch (FJException success) { |
685 |
< |
} |
686 |
< |
} |
823 |
< |
}; |
824 |
< |
mainPool.invoke(a); |
676 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
677 |
> |
public void realCompute() { |
678 |
> |
FibAction f = new FibAction(8); |
679 |
> |
FailingFibAction g = new FailingFibAction(9); |
680 |
> |
FibAction h = new FibAction(7); |
681 |
> |
try { |
682 |
> |
invokeAll(f, g, h); |
683 |
> |
shouldThrow(); |
684 |
> |
} catch (FJException success) {} |
685 |
> |
}}; |
686 |
> |
testInvokeOnPool(mainPool(), a); |
687 |
|
} |
688 |
|
|
689 |
|
/** |
690 |
|
* invokeAll(collection) throws exception if any task does |
691 |
|
*/ |
692 |
|
public void testAbnormalInvokeAllCollection() { |
693 |
< |
RecursiveAction a = new RecursiveAction() { |
694 |
< |
public void compute() { |
695 |
< |
try { |
696 |
< |
FailingFibAction f = new FailingFibAction(8); |
697 |
< |
FibAction g = new FibAction(9); |
698 |
< |
FibAction h = new FibAction(7); |
699 |
< |
HashSet set = new HashSet(); |
700 |
< |
set.add(f); |
701 |
< |
set.add(g); |
702 |
< |
set.add(h); |
703 |
< |
invokeAll(set); |
704 |
< |
shouldThrow(); |
705 |
< |
} catch (FJException success) { |
706 |
< |
} |
707 |
< |
} |
846 |
< |
}; |
847 |
< |
mainPool.invoke(a); |
693 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
694 |
> |
public void realCompute() { |
695 |
> |
FailingFibAction f = new FailingFibAction(8); |
696 |
> |
FibAction g = new FibAction(9); |
697 |
> |
FibAction h = new FibAction(7); |
698 |
> |
HashSet set = new HashSet(); |
699 |
> |
set.add(f); |
700 |
> |
set.add(g); |
701 |
> |
set.add(h); |
702 |
> |
try { |
703 |
> |
invokeAll(set); |
704 |
> |
shouldThrow(); |
705 |
> |
} catch (FJException success) {} |
706 |
> |
}}; |
707 |
> |
testInvokeOnPool(mainPool(), a); |
708 |
|
} |
709 |
|
|
710 |
|
/** |
712 |
|
* and suppresses execution |
713 |
|
*/ |
714 |
|
public void testTryUnfork() { |
715 |
< |
RecursiveAction a = new RecursiveAction() { |
716 |
< |
public void compute() { |
717 |
< |
FibAction g = new FibAction(9); |
718 |
< |
g.fork(); |
719 |
< |
FibAction f = new FibAction(8); |
720 |
< |
f.fork(); |
721 |
< |
threadAssertTrue(f.tryUnfork()); |
722 |
< |
helpQuiesce(); |
723 |
< |
threadAssertFalse(f.isDone()); |
724 |
< |
threadAssertTrue(g.isDone()); |
725 |
< |
} |
726 |
< |
}; |
867 |
< |
singletonPool.invoke(a); |
715 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
716 |
> |
public void realCompute() { |
717 |
> |
FibAction g = new FibAction(9); |
718 |
> |
assertSame(g, g.fork()); |
719 |
> |
FibAction f = new FibAction(8); |
720 |
> |
assertSame(f, f.fork()); |
721 |
> |
assertTrue(f.tryUnfork()); |
722 |
> |
helpQuiesce(); |
723 |
> |
assertFalse(f.isDone()); |
724 |
> |
assertTrue(g.isDone()); |
725 |
> |
}}; |
726 |
> |
testInvokeOnPool(singletonPool(), a); |
727 |
|
} |
728 |
|
|
729 |
|
/** |
731 |
|
* there are more tasks than threads |
732 |
|
*/ |
733 |
|
public void testGetSurplusQueuedTaskCount() { |
734 |
< |
RecursiveAction a = new RecursiveAction() { |
735 |
< |
public void compute() { |
736 |
< |
FibAction h = new FibAction(7); |
737 |
< |
h.fork(); |
738 |
< |
FibAction g = new FibAction(9); |
739 |
< |
g.fork(); |
740 |
< |
FibAction f = new FibAction(8); |
741 |
< |
f.fork(); |
742 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
743 |
< |
helpQuiesce(); |
744 |
< |
} |
745 |
< |
}; |
887 |
< |
singletonPool.invoke(a); |
734 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
735 |
> |
public void realCompute() { |
736 |
> |
FibAction h = new FibAction(7); |
737 |
> |
assertSame(h, h.fork()); |
738 |
> |
FibAction g = new FibAction(9); |
739 |
> |
assertSame(g, g.fork()); |
740 |
> |
FibAction f = new FibAction(8); |
741 |
> |
assertSame(f, f.fork()); |
742 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
743 |
> |
helpQuiesce(); |
744 |
> |
}}; |
745 |
> |
testInvokeOnPool(singletonPool(), a); |
746 |
|
} |
747 |
|
|
748 |
|
/** |
749 |
|
* peekNextLocalTask returns most recent unexecuted task. |
750 |
|
*/ |
751 |
|
public void testPeekNextLocalTask() { |
752 |
< |
RecursiveAction a = new RecursiveAction() { |
753 |
< |
public void compute() { |
754 |
< |
FibAction g = new FibAction(9); |
755 |
< |
g.fork(); |
756 |
< |
FibAction f = new FibAction(8); |
757 |
< |
f.fork(); |
758 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
759 |
< |
f.join(); |
760 |
< |
threadAssertTrue(f.isDone()); |
761 |
< |
helpQuiesce(); |
762 |
< |
} |
763 |
< |
}; |
906 |
< |
singletonPool.invoke(a); |
752 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
753 |
> |
public void realCompute() { |
754 |
> |
FibAction g = new FibAction(9); |
755 |
> |
assertSame(g, g.fork()); |
756 |
> |
FibAction f = new FibAction(8); |
757 |
> |
assertSame(f, f.fork()); |
758 |
> |
assertSame(f, peekNextLocalTask()); |
759 |
> |
assertNull(f.join()); |
760 |
> |
assertTrue(f.isDone()); |
761 |
> |
helpQuiesce(); |
762 |
> |
}}; |
763 |
> |
testInvokeOnPool(singletonPool(), a); |
764 |
|
} |
765 |
|
|
766 |
|
/** |
768 |
|
* without executing it |
769 |
|
*/ |
770 |
|
public void testPollNextLocalTask() { |
771 |
< |
RecursiveAction a = new RecursiveAction() { |
772 |
< |
public void compute() { |
773 |
< |
FibAction g = new FibAction(9); |
774 |
< |
g.fork(); |
775 |
< |
FibAction f = new FibAction(8); |
776 |
< |
f.fork(); |
777 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
778 |
< |
helpQuiesce(); |
779 |
< |
threadAssertFalse(f.isDone()); |
780 |
< |
} |
781 |
< |
}; |
925 |
< |
singletonPool.invoke(a); |
771 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
772 |
> |
public void realCompute() { |
773 |
> |
FibAction g = new FibAction(9); |
774 |
> |
assertSame(g, g.fork()); |
775 |
> |
FibAction f = new FibAction(8); |
776 |
> |
assertSame(f, f.fork()); |
777 |
> |
assertSame(f, pollNextLocalTask()); |
778 |
> |
helpQuiesce(); |
779 |
> |
assertFalse(f.isDone()); |
780 |
> |
}}; |
781 |
> |
testInvokeOnPool(singletonPool(), a); |
782 |
|
} |
783 |
|
|
784 |
|
/** |
786 |
|
* without executing it |
787 |
|
*/ |
788 |
|
public void testPollTask() { |
789 |
< |
RecursiveAction a = new RecursiveAction() { |
790 |
< |
public void compute() { |
791 |
< |
FibAction g = new FibAction(9); |
792 |
< |
g.fork(); |
793 |
< |
FibAction f = new FibAction(8); |
794 |
< |
f.fork(); |
795 |
< |
threadAssertTrue(pollTask() == f); |
796 |
< |
helpQuiesce(); |
797 |
< |
threadAssertFalse(f.isDone()); |
798 |
< |
threadAssertTrue(g.isDone()); |
799 |
< |
} |
800 |
< |
}; |
945 |
< |
singletonPool.invoke(a); |
789 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
790 |
> |
public void realCompute() { |
791 |
> |
FibAction g = new FibAction(9); |
792 |
> |
assertSame(g, g.fork()); |
793 |
> |
FibAction f = new FibAction(8); |
794 |
> |
assertSame(f, f.fork()); |
795 |
> |
assertSame(f, pollTask()); |
796 |
> |
helpQuiesce(); |
797 |
> |
assertFalse(f.isDone()); |
798 |
> |
assertTrue(g.isDone()); |
799 |
> |
}}; |
800 |
> |
testInvokeOnPool(singletonPool(), a); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
805 |
|
*/ |
806 |
|
public void testPeekNextLocalTaskAsync() { |
807 |
< |
RecursiveAction a = new RecursiveAction() { |
808 |
< |
public void compute() { |
809 |
< |
FibAction g = new FibAction(9); |
810 |
< |
g.fork(); |
811 |
< |
FibAction f = new FibAction(8); |
812 |
< |
f.fork(); |
813 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
814 |
< |
f.join(); |
815 |
< |
helpQuiesce(); |
816 |
< |
threadAssertTrue(f.isDone()); |
817 |
< |
} |
818 |
< |
}; |
964 |
< |
asyncSingletonPool.invoke(a); |
807 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
808 |
> |
public void realCompute() { |
809 |
> |
FibAction g = new FibAction(9); |
810 |
> |
assertSame(g, g.fork()); |
811 |
> |
FibAction f = new FibAction(8); |
812 |
> |
assertSame(f, f.fork()); |
813 |
> |
assertSame(g, peekNextLocalTask()); |
814 |
> |
assertNull(f.join()); |
815 |
> |
helpQuiesce(); |
816 |
> |
assertTrue(f.isDone()); |
817 |
> |
}}; |
818 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
819 |
|
} |
820 |
|
|
821 |
|
/** |
823 |
|
* without executing it, in async mode |
824 |
|
*/ |
825 |
|
public void testPollNextLocalTaskAsync() { |
826 |
< |
RecursiveAction a = new RecursiveAction() { |
827 |
< |
public void compute() { |
828 |
< |
FibAction g = new FibAction(9); |
829 |
< |
g.fork(); |
830 |
< |
FibAction f = new FibAction(8); |
831 |
< |
f.fork(); |
832 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
833 |
< |
helpQuiesce(); |
834 |
< |
threadAssertTrue(f.isDone()); |
835 |
< |
threadAssertFalse(g.isDone()); |
836 |
< |
} |
837 |
< |
}; |
984 |
< |
asyncSingletonPool.invoke(a); |
826 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
827 |
> |
public void realCompute() { |
828 |
> |
FibAction g = new FibAction(9); |
829 |
> |
assertSame(g, g.fork()); |
830 |
> |
FibAction f = new FibAction(8); |
831 |
> |
assertSame(f, f.fork()); |
832 |
> |
assertSame(g, pollNextLocalTask()); |
833 |
> |
helpQuiesce(); |
834 |
> |
assertTrue(f.isDone()); |
835 |
> |
assertFalse(g.isDone()); |
836 |
> |
}}; |
837 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
838 |
|
} |
839 |
|
|
840 |
|
/** |
842 |
|
* without executing it, in async mode |
843 |
|
*/ |
844 |
|
public void testPollTaskAsync() { |
845 |
< |
RecursiveAction a = new RecursiveAction() { |
846 |
< |
public void compute() { |
847 |
< |
FibAction g = new FibAction(9); |
848 |
< |
g.fork(); |
849 |
< |
FibAction f = new FibAction(8); |
850 |
< |
f.fork(); |
851 |
< |
threadAssertTrue(pollTask() == g); |
852 |
< |
helpQuiesce(); |
853 |
< |
threadAssertTrue(f.isDone()); |
854 |
< |
threadAssertFalse(g.isDone()); |
855 |
< |
} |
856 |
< |
}; |
1004 |
< |
asyncSingletonPool.invoke(a); |
845 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
846 |
> |
public void realCompute() { |
847 |
> |
FibAction g = new FibAction(9); |
848 |
> |
assertSame(g, g.fork()); |
849 |
> |
FibAction f = new FibAction(8); |
850 |
> |
assertSame(f, f.fork()); |
851 |
> |
assertSame(g, pollTask()); |
852 |
> |
helpQuiesce(); |
853 |
> |
assertTrue(f.isDone()); |
854 |
> |
assertFalse(g.isDone()); |
855 |
> |
}}; |
856 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
857 |
|
} |
858 |
|
|
859 |
|
} |