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.RecursiveTask; |
12 |
+ |
import java.util.concurrent.TimeUnit; |
13 |
+ |
import java.util.HashSet; |
14 |
|
|
15 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(RecursiveTaskTest.class); |
21 |
> |
return new TestSuite(RecursiveTaskTest.class); |
22 |
> |
} |
23 |
> |
|
24 |
> |
private static ForkJoinPool mainPool() { |
25 |
> |
return new ForkJoinPool(); |
26 |
|
} |
27 |
|
|
28 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
29 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
30 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
31 |
< |
static { |
32 |
< |
asyncSingletonPool.setAsyncMode(true); |
28 |
> |
private static ForkJoinPool singletonPool() { |
29 |
> |
return new ForkJoinPool(1); |
30 |
> |
} |
31 |
> |
|
32 |
> |
private static ForkJoinPool asyncSingletonPool() { |
33 |
> |
return new ForkJoinPool(1, |
34 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
35 |
> |
null, true); |
36 |
> |
} |
37 |
> |
|
38 |
> |
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
39 |
> |
try { |
40 |
> |
return pool.invoke(a); |
41 |
> |
} finally { |
42 |
> |
joinPool(pool); |
43 |
> |
} |
44 |
|
} |
45 |
|
|
46 |
|
static final class FJException extends RuntimeException { |
84 |
|
* isCompletedAbnormally and isCancelled return false for normally |
85 |
|
* completed tasks. getRawResult of a completed non-null task |
86 |
|
* returns value; |
68 |
– |
* |
87 |
|
*/ |
88 |
|
public void testInvoke() { |
89 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
90 |
< |
public Integer compute() { |
91 |
< |
FibTask f = new FibTask(8); |
92 |
< |
Integer r = f.invoke(); |
93 |
< |
threadAssertTrue(r == 21); |
94 |
< |
threadAssertTrue(f.isDone()); |
95 |
< |
threadAssertFalse(f.isCancelled()); |
96 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
97 |
< |
threadAssertTrue(f.getRawResult() == 21); |
98 |
< |
return r; |
99 |
< |
} |
100 |
< |
}; |
101 |
< |
assertTrue(mainPool.invoke(a) == 21); |
90 |
> |
public Integer compute() { |
91 |
> |
FibTask f = new FibTask(8); |
92 |
> |
Integer r = f.invoke(); |
93 |
> |
threadAssertTrue(r == 21); |
94 |
> |
threadAssertTrue(f.isDone()); |
95 |
> |
threadAssertFalse(f.isCancelled()); |
96 |
> |
threadAssertFalse(f.isCompletedAbnormally()); |
97 |
> |
threadAssertTrue(f.getRawResult() == 21); |
98 |
> |
return r; |
99 |
> |
} |
100 |
> |
}; |
101 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
108 |
|
*/ |
109 |
|
public void testQuietlyInvoke() { |
110 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
111 |
< |
public Integer compute() { |
112 |
< |
FibTask f = new FibTask(8); |
113 |
< |
f.quietlyInvoke(); |
114 |
< |
Integer r = f.getRawResult(); |
115 |
< |
threadAssertTrue(r == 21); |
116 |
< |
threadAssertTrue(f.isDone()); |
117 |
< |
threadAssertFalse(f.isCancelled()); |
118 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
119 |
< |
return r; |
120 |
< |
} |
121 |
< |
}; |
122 |
< |
assertTrue(mainPool.invoke(a) == 21); |
111 |
> |
public Integer compute() { |
112 |
> |
FibTask f = new FibTask(8); |
113 |
> |
f.quietlyInvoke(); |
114 |
> |
Integer r = f.getRawResult(); |
115 |
> |
threadAssertTrue(r == 21); |
116 |
> |
threadAssertTrue(f.isDone()); |
117 |
> |
threadAssertFalse(f.isCancelled()); |
118 |
> |
threadAssertFalse(f.isCompletedAbnormally()); |
119 |
> |
return r; |
120 |
> |
} |
121 |
> |
}; |
122 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
127 |
|
*/ |
128 |
|
public void testForkJoin() { |
129 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
130 |
< |
public Integer compute() { |
131 |
< |
FibTask f = new FibTask(8); |
132 |
< |
f.fork(); |
133 |
< |
Integer r = f.join(); |
134 |
< |
threadAssertTrue(r == 21); |
135 |
< |
threadAssertTrue(f.isDone()); |
136 |
< |
return r; |
137 |
< |
} |
138 |
< |
}; |
139 |
< |
assertTrue(mainPool.invoke(a) == 21); |
130 |
> |
public Integer compute() { |
131 |
> |
FibTask f = new FibTask(8); |
132 |
> |
threadAssertSame(f, f.fork()); |
133 |
> |
Integer r = f.join(); |
134 |
> |
threadAssertTrue(r == 21); |
135 |
> |
threadAssertTrue(f.isDone()); |
136 |
> |
return r; |
137 |
> |
} |
138 |
> |
}; |
139 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
144 |
|
*/ |
145 |
|
public void testForkGet() { |
146 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
147 |
< |
public Integer compute() { |
148 |
< |
try { |
131 |
< |
FibTask f = new FibTask(8); |
132 |
< |
f.fork(); |
133 |
< |
Integer r = f.get(); |
134 |
< |
threadAssertTrue(r == 21); |
135 |
< |
threadAssertTrue(f.isDone()); |
136 |
< |
return r; |
137 |
< |
} catch (Exception ex) { |
138 |
< |
unexpectedException(); |
139 |
< |
} |
140 |
< |
return NoResult; |
141 |
< |
} |
142 |
< |
}; |
143 |
< |
assertTrue(mainPool.invoke(a) == 21); |
144 |
< |
} |
145 |
< |
|
146 |
< |
/** |
147 |
< |
* timed get of a forked task returns when task completes |
148 |
< |
*/ |
149 |
< |
public void testForkTimedGet() { |
150 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
151 |
< |
public Integer compute() { |
152 |
< |
try { |
153 |
< |
FibTask f = new FibTask(8); |
154 |
< |
f.fork(); |
155 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
156 |
< |
threadAssertTrue(r == 21); |
157 |
< |
threadAssertTrue(f.isDone()); |
158 |
< |
return r; |
159 |
< |
} catch (Exception ex) { |
160 |
< |
unexpectedException(); |
161 |
< |
} |
162 |
< |
return NoResult; |
163 |
< |
} |
164 |
< |
}; |
165 |
< |
assertTrue(mainPool.invoke(a) == 21); |
166 |
< |
} |
167 |
< |
|
168 |
< |
/** |
169 |
< |
* helpJoin of a forked task returns when task completes |
170 |
< |
*/ |
171 |
< |
public void testForkHelpJoin() { |
172 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
173 |
< |
public Integer compute() { |
147 |
> |
public Integer compute() { |
148 |
> |
try { |
149 |
|
FibTask f = new FibTask(8); |
150 |
< |
f.fork(); |
151 |
< |
Integer r = f.helpJoin(); |
150 |
> |
threadAssertSame(f, f.fork()); |
151 |
> |
Integer r = f.get(); |
152 |
|
threadAssertTrue(r == 21); |
153 |
|
threadAssertTrue(f.isDone()); |
154 |
|
return r; |
155 |
+ |
} catch (Exception ex) { |
156 |
+ |
unexpectedException(ex); |
157 |
|
} |
158 |
< |
}; |
159 |
< |
assertTrue(mainPool.invoke(a) == 21); |
158 |
> |
return NoResult; |
159 |
> |
} |
160 |
> |
}; |
161 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* quietlyJoin of a forked task returns when task completes |
165 |
> |
* timed get of a forked task returns when task completes |
166 |
|
*/ |
167 |
< |
public void testForkQuietlyJoin() { |
167 |
> |
public void testForkTimedGet() { |
168 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
169 |
< |
public Integer compute() { |
169 |
> |
public Integer compute() { |
170 |
> |
try { |
171 |
|
FibTask f = new FibTask(8); |
172 |
< |
f.fork(); |
173 |
< |
f.quietlyJoin(); |
194 |
< |
Integer r = f.getRawResult(); |
172 |
> |
threadAssertSame(f, f.fork()); |
173 |
> |
Integer r = f.get(5L, TimeUnit.SECONDS); |
174 |
|
threadAssertTrue(r == 21); |
175 |
|
threadAssertTrue(f.isDone()); |
176 |
|
return r; |
177 |
+ |
} catch (Exception ex) { |
178 |
+ |
unexpectedException(ex); |
179 |
|
} |
180 |
< |
}; |
181 |
< |
assertTrue(mainPool.invoke(a) == 21); |
180 |
> |
return NoResult; |
181 |
> |
} |
182 |
> |
}; |
183 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
184 |
|
} |
185 |
|
|
203 |
– |
|
186 |
|
/** |
187 |
< |
* quietlyHelpJoin of a forked task returns when task completes |
187 |
> |
* quietlyJoin of a forked task returns when task completes |
188 |
|
*/ |
189 |
< |
public void testForkQuietlyHelpJoin() { |
189 |
> |
public void testForkQuietlyJoin() { |
190 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
191 |
< |
public Integer compute() { |
192 |
< |
FibTask f = new FibTask(8); |
193 |
< |
f.fork(); |
194 |
< |
f.quietlyHelpJoin(); |
195 |
< |
Integer r = f.getRawResult(); |
196 |
< |
threadAssertTrue(r == 21); |
197 |
< |
threadAssertTrue(f.isDone()); |
198 |
< |
return r; |
199 |
< |
} |
200 |
< |
}; |
201 |
< |
assertTrue(mainPool.invoke(a) == 21); |
191 |
> |
public Integer compute() { |
192 |
> |
FibTask f = new FibTask(8); |
193 |
> |
threadAssertSame(f, f.fork()); |
194 |
> |
f.quietlyJoin(); |
195 |
> |
Integer r = f.getRawResult(); |
196 |
> |
threadAssertTrue(r == 21); |
197 |
> |
threadAssertTrue(f.isDone()); |
198 |
> |
return r; |
199 |
> |
} |
200 |
> |
}; |
201 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
202 |
|
} |
203 |
|
|
204 |
|
|
208 |
|
*/ |
209 |
|
public void testForkHelpQuiesce() { |
210 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
211 |
< |
public Integer compute() { |
212 |
< |
FibTask f = new FibTask(8); |
213 |
< |
f.fork(); |
214 |
< |
f.helpQuiesce(); |
215 |
< |
Integer r = f.getRawResult(); |
216 |
< |
threadAssertTrue(r == 21); |
217 |
< |
threadAssertTrue(f.isDone()); |
218 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
219 |
< |
return r; |
220 |
< |
} |
221 |
< |
}; |
222 |
< |
assertTrue(mainPool.invoke(a) == 21); |
211 |
> |
public Integer compute() { |
212 |
> |
FibTask f = new FibTask(8); |
213 |
> |
threadAssertSame(f, f.fork()); |
214 |
> |
f.helpQuiesce(); |
215 |
> |
Integer r = f.getRawResult(); |
216 |
> |
threadAssertTrue(r == 21); |
217 |
> |
threadAssertTrue(f.isDone()); |
218 |
> |
threadAssertTrue(getQueuedTaskCount() == 0); |
219 |
> |
return r; |
220 |
> |
} |
221 |
> |
}; |
222 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
223 |
|
} |
224 |
|
|
225 |
|
|
228 |
|
*/ |
229 |
|
public void testAbnormalInvoke() { |
230 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
231 |
< |
public Integer compute() { |
232 |
< |
try { |
233 |
< |
FailingFibTask f = new FailingFibTask(8); |
234 |
< |
f.invoke(); |
235 |
< |
shouldThrow(); |
254 |
< |
return NoResult; |
255 |
< |
} catch (FJException success) { |
256 |
< |
} |
231 |
> |
public Integer compute() { |
232 |
> |
try { |
233 |
> |
FailingFibTask f = new FailingFibTask(8); |
234 |
> |
f.invoke(); |
235 |
> |
shouldThrow(); |
236 |
|
return NoResult; |
237 |
+ |
} catch (FJException success) { |
238 |
|
} |
239 |
< |
}; |
240 |
< |
mainPool.invoke(a); |
239 |
> |
return NoResult; |
240 |
> |
} |
241 |
> |
}; |
242 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
243 |
|
} |
244 |
|
|
245 |
|
/** |
247 |
|
*/ |
248 |
|
public void testAbnormalQuietlyInvoke() { |
249 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
250 |
< |
public Integer compute() { |
251 |
< |
FailingFibTask f = new FailingFibTask(8); |
252 |
< |
f.quietlyInvoke(); |
253 |
< |
threadAssertTrue(f.isDone()); |
254 |
< |
return NoResult; |
255 |
< |
} |
256 |
< |
}; |
257 |
< |
mainPool.invoke(a); |
250 |
> |
public Integer compute() { |
251 |
> |
FailingFibTask f = new FailingFibTask(8); |
252 |
> |
f.quietlyInvoke(); |
253 |
> |
threadAssertTrue(f.isDone()); |
254 |
> |
return NoResult; |
255 |
> |
} |
256 |
> |
}; |
257 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
258 |
|
} |
259 |
|
|
260 |
|
/** |
262 |
|
*/ |
263 |
|
public void testAbnormalForkJoin() { |
264 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
265 |
< |
public Integer compute() { |
266 |
< |
try { |
267 |
< |
FailingFibTask f = new FailingFibTask(8); |
268 |
< |
f.fork(); |
269 |
< |
Integer r = f.join(); |
270 |
< |
shouldThrow(); |
271 |
< |
return r; |
272 |
< |
} catch (FJException success) { |
291 |
< |
} |
292 |
< |
return NoResult; |
265 |
> |
public Integer compute() { |
266 |
> |
try { |
267 |
> |
FailingFibTask f = new FailingFibTask(8); |
268 |
> |
threadAssertSame(f, f.fork()); |
269 |
> |
Integer r = f.join(); |
270 |
> |
shouldThrow(); |
271 |
> |
return r; |
272 |
> |
} catch (FJException success) { |
273 |
|
} |
274 |
< |
}; |
275 |
< |
mainPool.invoke(a); |
274 |
> |
return NoResult; |
275 |
> |
} |
276 |
> |
}; |
277 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
282 |
|
*/ |
283 |
|
public void testAbnormalForkGet() { |
284 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
285 |
< |
public Integer compute() { |
286 |
< |
try { |
287 |
< |
FailingFibTask f = new FailingFibTask(8); |
288 |
< |
f.fork(); |
289 |
< |
Integer r = f.get(); |
290 |
< |
shouldThrow(); |
291 |
< |
return r; |
292 |
< |
} catch (ExecutionException success) { |
293 |
< |
} catch (Exception ex) { |
294 |
< |
unexpectedException(ex); |
313 |
< |
} |
314 |
< |
return NoResult; |
285 |
> |
public Integer compute() { |
286 |
> |
try { |
287 |
> |
FailingFibTask f = new FailingFibTask(8); |
288 |
> |
threadAssertSame(f, f.fork()); |
289 |
> |
Integer r = f.get(); |
290 |
> |
shouldThrow(); |
291 |
> |
return r; |
292 |
> |
} catch (ExecutionException success) { |
293 |
> |
} catch (Exception ex) { |
294 |
> |
unexpectedException(ex); |
295 |
|
} |
296 |
< |
}; |
297 |
< |
mainPool.invoke(a); |
296 |
> |
return NoResult; |
297 |
> |
} |
298 |
> |
}; |
299 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
304 |
|
*/ |
305 |
|
public void testAbnormalForkTimedGet() { |
306 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
307 |
< |
public Integer compute() { |
308 |
< |
try { |
327 |
< |
FailingFibTask f = new FailingFibTask(8); |
328 |
< |
f.fork(); |
329 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
330 |
< |
shouldThrow(); |
331 |
< |
return r; |
332 |
< |
} catch (ExecutionException success) { |
333 |
< |
} catch (Exception ex) { |
334 |
< |
unexpectedException(ex); |
335 |
< |
} |
336 |
< |
return NoResult; |
337 |
< |
} |
338 |
< |
}; |
339 |
< |
mainPool.invoke(a); |
340 |
< |
} |
341 |
< |
|
342 |
< |
/** |
343 |
< |
* join of a forked task throws exception when task completes abnormally |
344 |
< |
*/ |
345 |
< |
public void testAbnormalForkHelpJoin() { |
346 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
347 |
< |
public Integer compute() { |
348 |
< |
try { |
349 |
< |
FailingFibTask f = new FailingFibTask(8); |
350 |
< |
f.fork(); |
351 |
< |
Integer r = f.helpJoin(); |
352 |
< |
shouldThrow(); |
353 |
< |
return r; |
354 |
< |
} catch (FJException success) { |
355 |
< |
} |
356 |
< |
return NoResult; |
357 |
< |
} |
358 |
< |
}; |
359 |
< |
mainPool.invoke(a); |
360 |
< |
} |
361 |
< |
|
362 |
< |
/** |
363 |
< |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
364 |
< |
* getException of failed task returns its exception. |
365 |
< |
* isCompletedAbnormally of a failed task returns true. |
366 |
< |
* isCancelled of a failed uncancelled task returns false |
367 |
< |
*/ |
368 |
< |
public void testAbnormalForkQuietlyHelpJoin() { |
369 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
370 |
< |
public Integer compute() { |
307 |
> |
public Integer compute() { |
308 |
> |
try { |
309 |
|
FailingFibTask f = new FailingFibTask(8); |
310 |
< |
f.fork(); |
311 |
< |
f.quietlyHelpJoin(); |
312 |
< |
threadAssertTrue(f.isDone()); |
313 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
314 |
< |
threadAssertFalse(f.isCancelled()); |
315 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
316 |
< |
return NoResult; |
310 |
> |
threadAssertSame(f, f.fork()); |
311 |
> |
Integer r = f.get(5L, TimeUnit.SECONDS); |
312 |
> |
shouldThrow(); |
313 |
> |
return r; |
314 |
> |
} catch (ExecutionException success) { |
315 |
> |
} catch (Exception ex) { |
316 |
> |
unexpectedException(ex); |
317 |
|
} |
318 |
< |
}; |
319 |
< |
mainPool.invoke(a); |
318 |
> |
return NoResult; |
319 |
> |
} |
320 |
> |
}; |
321 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
326 |
|
*/ |
327 |
|
public void testAbnormalForkQuietlyJoin() { |
328 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
329 |
< |
public Integer compute() { |
330 |
< |
FailingFibTask f = new FailingFibTask(8); |
331 |
< |
f.fork(); |
332 |
< |
f.quietlyJoin(); |
333 |
< |
threadAssertTrue(f.isDone()); |
334 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
335 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
336 |
< |
return NoResult; |
337 |
< |
} |
338 |
< |
}; |
339 |
< |
mainPool.invoke(a); |
329 |
> |
public Integer compute() { |
330 |
> |
FailingFibTask f = new FailingFibTask(8); |
331 |
> |
threadAssertSame(f, f.fork()); |
332 |
> |
f.quietlyJoin(); |
333 |
> |
threadAssertTrue(f.isDone()); |
334 |
> |
threadAssertTrue(f.isCompletedAbnormally()); |
335 |
> |
threadAssertTrue(f.getException() instanceof FJException); |
336 |
> |
return NoResult; |
337 |
> |
} |
338 |
> |
}; |
339 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
344 |
|
*/ |
345 |
|
public void testCancelledInvoke() { |
346 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
347 |
< |
public Integer compute() { |
348 |
< |
try { |
349 |
< |
FibTask f = new FibTask(8); |
350 |
< |
f.cancel(true); |
351 |
< |
Integer r = f.invoke(); |
352 |
< |
shouldThrow(); |
353 |
< |
return r; |
354 |
< |
} catch (CancellationException success) { |
415 |
< |
} |
416 |
< |
return NoResult; |
347 |
> |
public Integer compute() { |
348 |
> |
try { |
349 |
> |
FibTask f = new FibTask(8); |
350 |
> |
threadAssertTrue(f.cancel(true)); |
351 |
> |
Integer r = f.invoke(); |
352 |
> |
shouldThrow(); |
353 |
> |
return r; |
354 |
> |
} catch (CancellationException success) { |
355 |
|
} |
356 |
< |
}; |
357 |
< |
mainPool.invoke(a); |
356 |
> |
return NoResult; |
357 |
> |
} |
358 |
> |
}; |
359 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
364 |
|
*/ |
365 |
|
public void testCancelledForkJoin() { |
366 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
367 |
< |
public Integer compute() { |
368 |
< |
try { |
369 |
< |
FibTask f = new FibTask(8); |
370 |
< |
f.cancel(true); |
371 |
< |
f.fork(); |
372 |
< |
Integer r = f.join(); |
373 |
< |
shouldThrow(); |
374 |
< |
return r; |
375 |
< |
} catch (CancellationException success) { |
436 |
< |
} |
437 |
< |
return NoResult; |
367 |
> |
public Integer compute() { |
368 |
> |
try { |
369 |
> |
FibTask f = new FibTask(8); |
370 |
> |
threadAssertTrue(f.cancel(true)); |
371 |
> |
threadAssertSame(f, f.fork()); |
372 |
> |
Integer r = f.join(); |
373 |
> |
shouldThrow(); |
374 |
> |
return r; |
375 |
> |
} catch (CancellationException success) { |
376 |
|
} |
377 |
< |
}; |
378 |
< |
mainPool.invoke(a); |
377 |
> |
return NoResult; |
378 |
> |
} |
379 |
> |
}; |
380 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
381 |
|
} |
382 |
|
|
383 |
|
/** |
385 |
|
*/ |
386 |
|
public void testCancelledForkGet() { |
387 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
388 |
< |
public Integer compute() { |
389 |
< |
try { |
390 |
< |
FibTask f = new FibTask(8); |
391 |
< |
f.cancel(true); |
392 |
< |
f.fork(); |
393 |
< |
Integer r = f.get(); |
394 |
< |
shouldThrow(); |
395 |
< |
return r; |
396 |
< |
} catch (CancellationException success) { |
397 |
< |
} catch (Exception ex) { |
398 |
< |
unexpectedException(ex); |
459 |
< |
} |
460 |
< |
return NoResult; |
388 |
> |
public Integer compute() { |
389 |
> |
try { |
390 |
> |
FibTask f = new FibTask(8); |
391 |
> |
threadAssertTrue(f.cancel(true)); |
392 |
> |
threadAssertSame(f, f.fork()); |
393 |
> |
Integer r = f.get(); |
394 |
> |
shouldThrow(); |
395 |
> |
return r; |
396 |
> |
} catch (CancellationException success) { |
397 |
> |
} catch (Exception ex) { |
398 |
> |
unexpectedException(ex); |
399 |
|
} |
400 |
< |
}; |
401 |
< |
mainPool.invoke(a); |
400 |
> |
return NoResult; |
401 |
> |
} |
402 |
> |
}; |
403 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
408 |
|
*/ |
409 |
|
public void testCancelledForkTimedGet() { |
410 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
411 |
< |
public Integer compute() { |
412 |
< |
try { |
473 |
< |
FibTask f = new FibTask(8); |
474 |
< |
f.cancel(true); |
475 |
< |
f.fork(); |
476 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
477 |
< |
shouldThrow(); |
478 |
< |
return r; |
479 |
< |
} catch (CancellationException success) { |
480 |
< |
} catch (Exception ex) { |
481 |
< |
unexpectedException(ex); |
482 |
< |
} |
483 |
< |
return NoResult; |
484 |
< |
} |
485 |
< |
}; |
486 |
< |
mainPool.invoke(a); |
487 |
< |
} |
488 |
< |
|
489 |
< |
/** |
490 |
< |
* join of a forked task throws exception when task cancelled |
491 |
< |
*/ |
492 |
< |
public void testCancelledForkHelpJoin() { |
493 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
494 |
< |
public Integer compute() { |
495 |
< |
try { |
496 |
< |
FibTask f = new FibTask(8); |
497 |
< |
f.cancel(true); |
498 |
< |
f.fork(); |
499 |
< |
Integer r = f.helpJoin(); |
500 |
< |
shouldThrow(); |
501 |
< |
return r; |
502 |
< |
} catch (CancellationException success) { |
503 |
< |
} |
504 |
< |
return NoResult; |
505 |
< |
} |
506 |
< |
}; |
507 |
< |
mainPool.invoke(a); |
508 |
< |
} |
509 |
< |
|
510 |
< |
/** |
511 |
< |
* quietlyHelpJoin of a forked task returns when task cancelled. |
512 |
< |
* getException of cancelled task returns its exception |
513 |
< |
* isCompletedAbnormally of a cancelled task returns true. |
514 |
< |
* isCancelled of a cancelled task returns true |
515 |
< |
*/ |
516 |
< |
public void testCancelledForkQuietlyHelpJoin() { |
517 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
518 |
< |
public Integer compute() { |
411 |
> |
public Integer compute() { |
412 |
> |
try { |
413 |
|
FibTask f = new FibTask(8); |
414 |
< |
f.cancel(true); |
415 |
< |
f.fork(); |
416 |
< |
f.quietlyHelpJoin(); |
417 |
< |
threadAssertTrue(f.isDone()); |
418 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
419 |
< |
threadAssertTrue(f.isCancelled()); |
420 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
421 |
< |
return NoResult; |
414 |
> |
threadAssertTrue(f.cancel(true)); |
415 |
> |
threadAssertSame(f, f.fork()); |
416 |
> |
Integer r = f.get(5L, TimeUnit.SECONDS); |
417 |
> |
shouldThrow(); |
418 |
> |
return r; |
419 |
> |
} catch (CancellationException success) { |
420 |
> |
} catch (Exception ex) { |
421 |
> |
unexpectedException(ex); |
422 |
|
} |
423 |
< |
}; |
424 |
< |
mainPool.invoke(a); |
423 |
> |
return NoResult; |
424 |
> |
} |
425 |
> |
}; |
426 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
431 |
|
*/ |
432 |
|
public void testCancelledForkQuietlyJoin() { |
433 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
434 |
< |
public Integer compute() { |
435 |
< |
FibTask f = new FibTask(8); |
436 |
< |
f.cancel(true); |
437 |
< |
f.fork(); |
438 |
< |
f.quietlyJoin(); |
439 |
< |
threadAssertTrue(f.isDone()); |
440 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
441 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
442 |
< |
return NoResult; |
443 |
< |
} |
444 |
< |
}; |
445 |
< |
mainPool.invoke(a); |
434 |
> |
public Integer compute() { |
435 |
> |
FibTask f = new FibTask(8); |
436 |
> |
threadAssertTrue(f.cancel(true)); |
437 |
> |
threadAssertSame(f, f.fork()); |
438 |
> |
f.quietlyJoin(); |
439 |
> |
threadAssertTrue(f.isDone()); |
440 |
> |
threadAssertTrue(f.isCompletedAbnormally()); |
441 |
> |
threadAssertTrue(f.getException() instanceof CancellationException); |
442 |
> |
return NoResult; |
443 |
> |
} |
444 |
> |
}; |
445 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
|
* getPool of executing task returns its pool |
450 |
|
*/ |
451 |
|
public void testGetPool() { |
452 |
+ |
final ForkJoinPool mainPool = mainPool(); |
453 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
454 |
< |
public Integer compute() { |
455 |
< |
threadAssertTrue(getPool() == mainPool); |
456 |
< |
return NoResult; |
457 |
< |
} |
458 |
< |
}; |
459 |
< |
mainPool.invoke(a); |
454 |
> |
public Integer compute() { |
455 |
> |
threadAssertTrue(getPool() == mainPool); |
456 |
> |
return NoResult; |
457 |
> |
} |
458 |
> |
}; |
459 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool, a)); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
469 |
|
return NoResult; |
470 |
|
} |
471 |
|
}; |
472 |
< |
a.invoke(); |
472 |
> |
assertSame(NoResult, a.invoke()); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
477 |
|
*/ |
478 |
|
public void testInForkJoinPool() { |
479 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
480 |
< |
public Integer compute() { |
481 |
< |
threadAssertTrue(inForkJoinPool()); |
482 |
< |
return NoResult; |
483 |
< |
} |
484 |
< |
}; |
485 |
< |
mainPool.invoke(a); |
480 |
> |
public Integer compute() { |
481 |
> |
threadAssertTrue(inForkJoinPool()); |
482 |
> |
return NoResult; |
483 |
> |
} |
484 |
> |
}; |
485 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
490 |
|
*/ |
491 |
|
public void testInForkJoinPool2() { |
492 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
493 |
< |
public Integer compute() { |
494 |
< |
threadAssertTrue(!inForkJoinPool()); |
495 |
< |
return NoResult; |
496 |
< |
} |
497 |
< |
}; |
498 |
< |
a.invoke(); |
493 |
> |
public Integer compute() { |
494 |
> |
threadAssertTrue(!inForkJoinPool()); |
495 |
> |
return NoResult; |
496 |
> |
} |
497 |
> |
}; |
498 |
> |
assertSame(NoResult, a.invoke()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
< |
* setRawResult(null) succeeds |
502 |
> |
* The value set by setRawResult is returned by invoke |
503 |
|
*/ |
504 |
|
public void testSetRawResult() { |
505 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
506 |
< |
public Integer compute() { |
507 |
< |
setRawResult(NoResult); |
508 |
< |
return NoResult; |
509 |
< |
} |
510 |
< |
}; |
511 |
< |
assertEquals(a.invoke(), NoResult); |
506 |
> |
public Integer compute() { |
507 |
> |
setRawResult(NoResult); |
508 |
> |
return NoResult; |
509 |
> |
} |
510 |
> |
}; |
511 |
> |
assertSame(NoResult, a.invoke()); |
512 |
|
} |
513 |
|
|
514 |
|
/** |
516 |
|
*/ |
517 |
|
public void testReinitialize() { |
518 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
519 |
< |
public Integer compute() { |
520 |
< |
FibTask f = new FibTask(8); |
521 |
< |
Integer r = f.invoke(); |
522 |
< |
threadAssertTrue(r == 21); |
523 |
< |
threadAssertTrue(f.isDone()); |
524 |
< |
threadAssertFalse(f.isCancelled()); |
525 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
526 |
< |
f.reinitialize(); |
527 |
< |
r = f.invoke(); |
528 |
< |
threadAssertTrue(r == 21); |
529 |
< |
return NoResult; |
530 |
< |
} |
531 |
< |
}; |
532 |
< |
mainPool.invoke(a); |
519 |
> |
public Integer compute() { |
520 |
> |
FibTask f = new FibTask(8); |
521 |
> |
Integer r = f.invoke(); |
522 |
> |
threadAssertTrue(r == 21); |
523 |
> |
threadAssertTrue(f.isDone()); |
524 |
> |
threadAssertFalse(f.isCancelled()); |
525 |
> |
threadAssertFalse(f.isCompletedAbnormally()); |
526 |
> |
f.reinitialize(); |
527 |
> |
r = f.invoke(); |
528 |
> |
threadAssertTrue(r == 21); |
529 |
> |
return NoResult; |
530 |
> |
} |
531 |
> |
}; |
532 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
537 |
|
*/ |
538 |
|
public void testCompleteExceptionally() { |
539 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
540 |
< |
public Integer compute() { |
541 |
< |
try { |
542 |
< |
FibTask f = new FibTask(8); |
543 |
< |
f.completeExceptionally(new FJException()); |
544 |
< |
Integer r = f.invoke(); |
545 |
< |
shouldThrow(); |
546 |
< |
return r; |
547 |
< |
} catch (FJException success) { |
651 |
< |
} |
652 |
< |
return NoResult; |
540 |
> |
public Integer compute() { |
541 |
> |
try { |
542 |
> |
FibTask f = new FibTask(8); |
543 |
> |
f.completeExceptionally(new FJException()); |
544 |
> |
Integer r = f.invoke(); |
545 |
> |
shouldThrow(); |
546 |
> |
return r; |
547 |
> |
} catch (FJException success) { |
548 |
|
} |
549 |
< |
}; |
550 |
< |
mainPool.invoke(a); |
549 |
> |
return NoResult; |
550 |
> |
} |
551 |
> |
}; |
552 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
553 |
|
} |
554 |
|
|
555 |
|
/** |
557 |
|
*/ |
558 |
|
public void testComplete() { |
559 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
560 |
< |
public Integer compute() { |
561 |
< |
FibTask f = new FibTask(8); |
562 |
< |
f.complete(NoResult); |
563 |
< |
Integer r = f.invoke(); |
564 |
< |
threadAssertTrue(f.isDone()); |
565 |
< |
threadAssertTrue(r == NoResult); |
566 |
< |
return r; |
567 |
< |
} |
568 |
< |
}; |
569 |
< |
mainPool.invoke(a); |
560 |
> |
public Integer compute() { |
561 |
> |
FibTask f = new FibTask(8); |
562 |
> |
f.complete(NoResult); |
563 |
> |
Integer r = f.invoke(); |
564 |
> |
threadAssertTrue(f.isDone()); |
565 |
> |
threadAssertTrue(r == NoResult); |
566 |
> |
return r; |
567 |
> |
} |
568 |
> |
}; |
569 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
574 |
|
*/ |
575 |
|
public void testInvokeAll2() { |
576 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
577 |
< |
public Integer compute() { |
578 |
< |
FibTask f = new FibTask(8); |
579 |
< |
FibTask g = new FibTask(9); |
580 |
< |
invokeAll(f, g); |
581 |
< |
threadAssertTrue(f.isDone()); |
582 |
< |
threadAssertTrue(f.join() == 21); |
583 |
< |
threadAssertTrue(g.isDone()); |
584 |
< |
threadAssertTrue(g.join() == 34); |
585 |
< |
return NoResult; |
586 |
< |
} |
587 |
< |
}; |
588 |
< |
mainPool.invoke(a); |
577 |
> |
public Integer compute() { |
578 |
> |
FibTask f = new FibTask(8); |
579 |
> |
FibTask g = new FibTask(9); |
580 |
> |
invokeAll(f, g); |
581 |
> |
threadAssertTrue(f.isDone()); |
582 |
> |
threadAssertTrue(f.join() == 21); |
583 |
> |
threadAssertTrue(g.isDone()); |
584 |
> |
threadAssertTrue(g.join() == 34); |
585 |
> |
return NoResult; |
586 |
> |
} |
587 |
> |
}; |
588 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
593 |
|
*/ |
594 |
|
public void testInvokeAll1() { |
595 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
596 |
< |
public Integer compute() { |
597 |
< |
FibTask f = new FibTask(8); |
598 |
< |
invokeAll(f); |
599 |
< |
threadAssertTrue(f.isDone()); |
600 |
< |
threadAssertTrue(f.join() == 21); |
601 |
< |
return NoResult; |
602 |
< |
} |
603 |
< |
}; |
604 |
< |
mainPool.invoke(a); |
596 |
> |
public Integer compute() { |
597 |
> |
FibTask f = new FibTask(8); |
598 |
> |
invokeAll(f); |
599 |
> |
threadAssertTrue(f.isDone()); |
600 |
> |
threadAssertTrue(f.join() == 21); |
601 |
> |
return NoResult; |
602 |
> |
} |
603 |
> |
}; |
604 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
605 |
|
} |
606 |
|
|
607 |
|
/** |
609 |
|
*/ |
610 |
|
public void testInvokeAll3() { |
611 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
612 |
< |
public Integer compute() { |
613 |
< |
FibTask f = new FibTask(8); |
614 |
< |
FibTask g = new FibTask(9); |
615 |
< |
FibTask h = new FibTask(7); |
616 |
< |
invokeAll(f, g, h); |
617 |
< |
threadAssertTrue(f.isDone()); |
618 |
< |
threadAssertTrue(f.join() == 21); |
619 |
< |
threadAssertTrue(g.isDone()); |
620 |
< |
threadAssertTrue(g.join() == 34); |
621 |
< |
threadAssertTrue(h.isDone()); |
622 |
< |
threadAssertTrue(h.join() == 13); |
623 |
< |
return NoResult; |
624 |
< |
} |
625 |
< |
}; |
626 |
< |
mainPool.invoke(a); |
612 |
> |
public Integer compute() { |
613 |
> |
FibTask f = new FibTask(8); |
614 |
> |
FibTask g = new FibTask(9); |
615 |
> |
FibTask h = new FibTask(7); |
616 |
> |
invokeAll(f, g, h); |
617 |
> |
threadAssertTrue(f.isDone()); |
618 |
> |
threadAssertTrue(f.join() == 21); |
619 |
> |
threadAssertTrue(g.isDone()); |
620 |
> |
threadAssertTrue(g.join() == 34); |
621 |
> |
threadAssertTrue(h.isDone()); |
622 |
> |
threadAssertTrue(h.join() == 13); |
623 |
> |
return NoResult; |
624 |
> |
} |
625 |
> |
}; |
626 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
627 |
|
} |
628 |
|
|
629 |
|
/** |
631 |
|
*/ |
632 |
|
public void testInvokeAllCollection() { |
633 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
634 |
< |
public Integer compute() { |
635 |
< |
FibTask f = new FibTask(8); |
636 |
< |
FibTask g = new FibTask(9); |
637 |
< |
FibTask h = new FibTask(7); |
638 |
< |
HashSet set = new HashSet(); |
639 |
< |
set.add(f); |
640 |
< |
set.add(g); |
641 |
< |
set.add(h); |
642 |
< |
invokeAll(set); |
643 |
< |
threadAssertTrue(f.isDone()); |
644 |
< |
threadAssertTrue(f.join() == 21); |
645 |
< |
threadAssertTrue(g.isDone()); |
646 |
< |
threadAssertTrue(g.join() == 34); |
647 |
< |
threadAssertTrue(h.isDone()); |
648 |
< |
threadAssertTrue(h.join() == 13); |
649 |
< |
return NoResult; |
650 |
< |
} |
651 |
< |
}; |
652 |
< |
mainPool.invoke(a); |
634 |
> |
public Integer compute() { |
635 |
> |
FibTask f = new FibTask(8); |
636 |
> |
FibTask g = new FibTask(9); |
637 |
> |
FibTask h = new FibTask(7); |
638 |
> |
HashSet set = new HashSet(); |
639 |
> |
set.add(f); |
640 |
> |
set.add(g); |
641 |
> |
set.add(h); |
642 |
> |
invokeAll(set); |
643 |
> |
threadAssertTrue(f.isDone()); |
644 |
> |
threadAssertTrue(f.join() == 21); |
645 |
> |
threadAssertTrue(g.isDone()); |
646 |
> |
threadAssertTrue(g.join() == 34); |
647 |
> |
threadAssertTrue(h.isDone()); |
648 |
> |
threadAssertTrue(h.join() == 13); |
649 |
> |
return NoResult; |
650 |
> |
} |
651 |
> |
}; |
652 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
653 |
|
} |
654 |
|
|
655 |
|
|
658 |
|
*/ |
659 |
|
public void testAbnormalInvokeAll2() { |
660 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
661 |
< |
public Integer compute() { |
662 |
< |
try { |
663 |
< |
FibTask f = new FibTask(8); |
664 |
< |
FailingFibTask g = new FailingFibTask(9); |
665 |
< |
invokeAll(f, g); |
666 |
< |
shouldThrow(); |
770 |
< |
return NoResult; |
771 |
< |
} catch (FJException success) { |
772 |
< |
} |
661 |
> |
public Integer compute() { |
662 |
> |
try { |
663 |
> |
FibTask f = new FibTask(8); |
664 |
> |
FailingFibTask g = new FailingFibTask(9); |
665 |
> |
invokeAll(f, g); |
666 |
> |
shouldThrow(); |
667 |
|
return NoResult; |
668 |
+ |
} catch (FJException success) { |
669 |
|
} |
670 |
< |
}; |
671 |
< |
mainPool.invoke(a); |
670 |
> |
return NoResult; |
671 |
> |
} |
672 |
> |
}; |
673 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
678 |
|
*/ |
679 |
|
public void testAbnormalInvokeAll1() { |
680 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
681 |
< |
public Integer compute() { |
682 |
< |
try { |
683 |
< |
FailingFibTask g = new FailingFibTask(9); |
684 |
< |
invokeAll(g); |
685 |
< |
shouldThrow(); |
789 |
< |
return NoResult; |
790 |
< |
} catch (FJException success) { |
791 |
< |
} |
681 |
> |
public Integer compute() { |
682 |
> |
try { |
683 |
> |
FailingFibTask g = new FailingFibTask(9); |
684 |
> |
invokeAll(g); |
685 |
> |
shouldThrow(); |
686 |
|
return NoResult; |
687 |
+ |
} catch (FJException success) { |
688 |
|
} |
689 |
< |
}; |
690 |
< |
mainPool.invoke(a); |
689 |
> |
return NoResult; |
690 |
> |
} |
691 |
> |
}; |
692 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
693 |
|
} |
694 |
|
|
695 |
|
/** |
697 |
|
*/ |
698 |
|
public void testAbnormalInvokeAll3() { |
699 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
700 |
< |
public Integer compute() { |
701 |
< |
try { |
702 |
< |
FibTask f = new FibTask(8); |
703 |
< |
FailingFibTask g = new FailingFibTask(9); |
704 |
< |
FibTask h = new FibTask(7); |
705 |
< |
invokeAll(f, g, h); |
706 |
< |
shouldThrow(); |
810 |
< |
return NoResult; |
811 |
< |
} catch (FJException success) { |
812 |
< |
} |
700 |
> |
public Integer compute() { |
701 |
> |
try { |
702 |
> |
FibTask f = new FibTask(8); |
703 |
> |
FailingFibTask g = new FailingFibTask(9); |
704 |
> |
FibTask h = new FibTask(7); |
705 |
> |
invokeAll(f, g, h); |
706 |
> |
shouldThrow(); |
707 |
|
return NoResult; |
708 |
+ |
} catch (FJException success) { |
709 |
|
} |
710 |
< |
}; |
711 |
< |
mainPool.invoke(a); |
710 |
> |
return NoResult; |
711 |
> |
} |
712 |
> |
}; |
713 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
714 |
|
} |
715 |
|
|
716 |
|
/** |
718 |
|
*/ |
719 |
|
public void testAbnormalInvokeAllCollection() { |
720 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
721 |
< |
public Integer compute() { |
722 |
< |
try { |
723 |
< |
FailingFibTask f = new FailingFibTask(8); |
724 |
< |
FibTask g = new FibTask(9); |
725 |
< |
FibTask h = new FibTask(7); |
726 |
< |
HashSet set = new HashSet(); |
727 |
< |
set.add(f); |
728 |
< |
set.add(g); |
729 |
< |
set.add(h); |
730 |
< |
invokeAll(set); |
731 |
< |
shouldThrow(); |
835 |
< |
return NoResult; |
836 |
< |
} catch (FJException success) { |
837 |
< |
} |
721 |
> |
public Integer compute() { |
722 |
> |
try { |
723 |
> |
FailingFibTask f = new FailingFibTask(8); |
724 |
> |
FibTask g = new FibTask(9); |
725 |
> |
FibTask h = new FibTask(7); |
726 |
> |
HashSet set = new HashSet(); |
727 |
> |
set.add(f); |
728 |
> |
set.add(g); |
729 |
> |
set.add(h); |
730 |
> |
invokeAll(set); |
731 |
> |
shouldThrow(); |
732 |
|
return NoResult; |
733 |
+ |
} catch (FJException success) { |
734 |
|
} |
735 |
< |
}; |
736 |
< |
mainPool.invoke(a); |
735 |
> |
return NoResult; |
736 |
> |
} |
737 |
> |
}; |
738 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
739 |
|
} |
740 |
|
|
741 |
|
/** |
744 |
|
*/ |
745 |
|
public void testTryUnfork() { |
746 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
747 |
< |
public Integer compute() { |
748 |
< |
FibTask g = new FibTask(9); |
749 |
< |
g.fork(); |
750 |
< |
FibTask f = new FibTask(8); |
751 |
< |
f.fork(); |
752 |
< |
threadAssertTrue(f.tryUnfork()); |
753 |
< |
helpQuiesce(); |
754 |
< |
threadAssertFalse(f.isDone()); |
755 |
< |
threadAssertTrue(g.isDone()); |
756 |
< |
return NoResult; |
757 |
< |
} |
758 |
< |
}; |
759 |
< |
singletonPool.invoke(a); |
747 |
> |
public Integer compute() { |
748 |
> |
FibTask g = new FibTask(9); |
749 |
> |
threadAssertSame(g, g.fork()); |
750 |
> |
FibTask f = new FibTask(8); |
751 |
> |
threadAssertSame(f, f.fork()); |
752 |
> |
threadAssertTrue(f.tryUnfork()); |
753 |
> |
helpQuiesce(); |
754 |
> |
threadAssertFalse(f.isDone()); |
755 |
> |
threadAssertTrue(g.isDone()); |
756 |
> |
return NoResult; |
757 |
> |
} |
758 |
> |
}; |
759 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
760 |
|
} |
761 |
|
|
762 |
|
/** |
765 |
|
*/ |
766 |
|
public void testGetSurplusQueuedTaskCount() { |
767 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
768 |
< |
public Integer compute() { |
769 |
< |
FibTask h = new FibTask(7); |
770 |
< |
h.fork(); |
771 |
< |
FibTask g = new FibTask(9); |
772 |
< |
g.fork(); |
773 |
< |
FibTask f = new FibTask(8); |
774 |
< |
f.fork(); |
775 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
776 |
< |
helpQuiesce(); |
777 |
< |
return NoResult; |
778 |
< |
} |
779 |
< |
}; |
780 |
< |
singletonPool.invoke(a); |
768 |
> |
public Integer compute() { |
769 |
> |
FibTask h = new FibTask(7); |
770 |
> |
threadAssertSame(h, h.fork()); |
771 |
> |
FibTask g = new FibTask(9); |
772 |
> |
threadAssertSame(g, g.fork()); |
773 |
> |
FibTask f = new FibTask(8); |
774 |
> |
threadAssertSame(f, f.fork()); |
775 |
> |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
776 |
> |
helpQuiesce(); |
777 |
> |
return NoResult; |
778 |
> |
} |
779 |
> |
}; |
780 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
781 |
|
} |
782 |
|
|
783 |
|
/** |
785 |
|
*/ |
786 |
|
public void testPeekNextLocalTask() { |
787 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
788 |
< |
public Integer compute() { |
789 |
< |
FibTask g = new FibTask(9); |
790 |
< |
g.fork(); |
791 |
< |
FibTask f = new FibTask(8); |
792 |
< |
f.fork(); |
793 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
794 |
< |
f.join(); |
795 |
< |
threadAssertTrue(f.isDone()); |
796 |
< |
helpQuiesce(); |
797 |
< |
return NoResult; |
798 |
< |
} |
799 |
< |
}; |
800 |
< |
singletonPool.invoke(a); |
788 |
> |
public Integer compute() { |
789 |
> |
FibTask g = new FibTask(9); |
790 |
> |
threadAssertSame(g, g.fork()); |
791 |
> |
FibTask f = new FibTask(8); |
792 |
> |
threadAssertSame(f, f.fork()); |
793 |
> |
threadAssertTrue(peekNextLocalTask() == f); |
794 |
> |
threadAssertTrue(f.join() == 21); |
795 |
> |
threadAssertTrue(f.isDone()); |
796 |
> |
helpQuiesce(); |
797 |
> |
return NoResult; |
798 |
> |
} |
799 |
> |
}; |
800 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
806 |
|
*/ |
807 |
|
public void testPollNextLocalTask() { |
808 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
809 |
< |
public Integer compute() { |
810 |
< |
FibTask g = new FibTask(9); |
811 |
< |
g.fork(); |
812 |
< |
FibTask f = new FibTask(8); |
813 |
< |
f.fork(); |
814 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
815 |
< |
helpQuiesce(); |
816 |
< |
threadAssertFalse(f.isDone()); |
817 |
< |
return NoResult; |
818 |
< |
} |
819 |
< |
}; |
820 |
< |
singletonPool.invoke(a); |
809 |
> |
public Integer compute() { |
810 |
> |
FibTask g = new FibTask(9); |
811 |
> |
threadAssertSame(g, g.fork()); |
812 |
> |
FibTask f = new FibTask(8); |
813 |
> |
threadAssertSame(f, f.fork()); |
814 |
> |
threadAssertTrue(pollNextLocalTask() == f); |
815 |
> |
helpQuiesce(); |
816 |
> |
threadAssertFalse(f.isDone()); |
817 |
> |
return NoResult; |
818 |
> |
} |
819 |
> |
}; |
820 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
821 |
|
} |
822 |
|
|
823 |
|
/** |
824 |
< |
* pollTask returns an unexecuted task |
928 |
< |
* without executing it |
824 |
> |
* pollTask returns an unexecuted task without executing it |
825 |
|
*/ |
826 |
|
public void testPollTask() { |
827 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
828 |
< |
public Integer compute() { |
829 |
< |
FibTask g = new FibTask(9); |
830 |
< |
g.fork(); |
831 |
< |
FibTask f = new FibTask(8); |
832 |
< |
f.fork(); |
833 |
< |
threadAssertTrue(pollTask() == f); |
834 |
< |
helpQuiesce(); |
835 |
< |
threadAssertFalse(f.isDone()); |
836 |
< |
threadAssertTrue(g.isDone()); |
837 |
< |
return NoResult; |
838 |
< |
} |
839 |
< |
}; |
840 |
< |
singletonPool.invoke(a); |
828 |
> |
public Integer compute() { |
829 |
> |
FibTask g = new FibTask(9); |
830 |
> |
threadAssertSame(g, g.fork()); |
831 |
> |
FibTask f = new FibTask(8); |
832 |
> |
threadAssertSame(f, f.fork()); |
833 |
> |
threadAssertTrue(pollTask() == f); |
834 |
> |
helpQuiesce(); |
835 |
> |
threadAssertFalse(f.isDone()); |
836 |
> |
threadAssertTrue(g.isDone()); |
837 |
> |
return NoResult; |
838 |
> |
} |
839 |
> |
}; |
840 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
841 |
|
} |
842 |
|
|
843 |
|
/** |
845 |
|
*/ |
846 |
|
public void testPeekNextLocalTaskAsync() { |
847 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
848 |
< |
public Integer compute() { |
849 |
< |
FibTask g = new FibTask(9); |
850 |
< |
g.fork(); |
851 |
< |
FibTask f = new FibTask(8); |
852 |
< |
f.fork(); |
853 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
854 |
< |
f.join(); |
855 |
< |
helpQuiesce(); |
856 |
< |
threadAssertTrue(f.isDone()); |
857 |
< |
return NoResult; |
858 |
< |
} |
859 |
< |
}; |
860 |
< |
asyncSingletonPool.invoke(a); |
848 |
> |
public Integer compute() { |
849 |
> |
FibTask g = new FibTask(9); |
850 |
> |
threadAssertSame(g, g.fork()); |
851 |
> |
FibTask f = new FibTask(8); |
852 |
> |
threadAssertSame(f, f.fork()); |
853 |
> |
threadAssertTrue(peekNextLocalTask() == g); |
854 |
> |
threadAssertEquals(21, (int) f.join()); |
855 |
> |
helpQuiesce(); |
856 |
> |
threadAssertTrue(f.isDone()); |
857 |
> |
return NoResult; |
858 |
> |
} |
859 |
> |
}; |
860 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
866 |
|
*/ |
867 |
|
public void testPollNextLocalTaskAsync() { |
868 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
869 |
< |
public Integer compute() { |
870 |
< |
FibTask g = new FibTask(9); |
871 |
< |
g.fork(); |
872 |
< |
FibTask f = new FibTask(8); |
873 |
< |
f.fork(); |
874 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
875 |
< |
helpQuiesce(); |
876 |
< |
threadAssertTrue(f.isDone()); |
877 |
< |
threadAssertFalse(g.isDone()); |
878 |
< |
return NoResult; |
879 |
< |
} |
880 |
< |
}; |
881 |
< |
asyncSingletonPool.invoke(a); |
869 |
> |
public Integer compute() { |
870 |
> |
FibTask g = new FibTask(9); |
871 |
> |
threadAssertSame(g, g.fork()); |
872 |
> |
FibTask f = new FibTask(8); |
873 |
> |
threadAssertSame(f, f.fork()); |
874 |
> |
threadAssertTrue(pollNextLocalTask() == g); |
875 |
> |
helpQuiesce(); |
876 |
> |
threadAssertTrue(f.isDone()); |
877 |
> |
threadAssertFalse(g.isDone()); |
878 |
> |
return NoResult; |
879 |
> |
} |
880 |
> |
}; |
881 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
882 |
|
} |
883 |
|
|
884 |
|
/** |
887 |
|
*/ |
888 |
|
public void testPollTaskAsync() { |
889 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
890 |
< |
public Integer compute() { |
891 |
< |
FibTask g = new FibTask(9); |
892 |
< |
g.fork(); |
893 |
< |
FibTask f = new FibTask(8); |
894 |
< |
f.fork(); |
895 |
< |
threadAssertTrue(pollTask() == g); |
896 |
< |
helpQuiesce(); |
897 |
< |
threadAssertTrue(f.isDone()); |
898 |
< |
threadAssertFalse(g.isDone()); |
899 |
< |
return NoResult; |
900 |
< |
} |
901 |
< |
}; |
902 |
< |
asyncSingletonPool.invoke(a); |
890 |
> |
public Integer compute() { |
891 |
> |
FibTask g = new FibTask(9); |
892 |
> |
threadAssertSame(g, g.fork()); |
893 |
> |
FibTask f = new FibTask(8); |
894 |
> |
threadAssertSame(f, f.fork()); |
895 |
> |
threadAssertTrue(pollTask() == g); |
896 |
> |
helpQuiesce(); |
897 |
> |
threadAssertTrue(f.isDone()); |
898 |
> |
threadAssertFalse(g.isDone()); |
899 |
> |
return NoResult; |
900 |
> |
} |
901 |
> |
}; |
902 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
903 |
|
} |
904 |
|
|
905 |
|
} |