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