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