5 |
|
*/ |
6 |
|
|
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
|
|
9 |
|
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
83 |
|
|
84 |
|
Thread.currentThread().interrupt(); |
85 |
|
try { |
86 |
< |
a.get(5L, SECONDS); |
86 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
87 |
|
shouldThrow(); |
88 |
|
} catch (InterruptedException success) { |
89 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
90 |
|
} |
91 |
|
|
92 |
|
try { |
93 |
< |
a.get(0L, SECONDS); |
93 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
94 |
|
shouldThrow(); |
95 |
|
} catch (TimeoutException success) { |
96 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
108 |
|
assertFalse(a.cancel(true)); |
109 |
|
try { |
110 |
|
assertNull(a.get()); |
111 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
113 |
< |
try { |
114 |
< |
assertNull(a.get(5L, SECONDS)); |
111 |
> |
assertNull(a.get(randomTimeout(), randomTimeUnit())); |
112 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
113 |
|
} |
114 |
|
|
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
|
|
135 |
|
try { |
136 |
< |
a.get(5L, SECONDS); |
136 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
137 |
|
shouldThrow(); |
138 |
|
} catch (CancellationException success) { |
139 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
165 |
|
|
166 |
|
try { |
167 |
< |
a.get(5L, SECONDS); |
167 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
168 |
|
shouldThrow(); |
169 |
|
} catch (ExecutionException success) { |
170 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
176 |
|
public FJException(Throwable cause) { super(cause); } |
177 |
|
} |
178 |
|
|
179 |
< |
// A simple recursive action for testing |
179 |
> |
/** A simple recursive action for testing. */ |
180 |
|
final class FibAction extends CheckedRecursiveAction { |
181 |
|
final int number; |
182 |
|
int result; |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
< |
// A recursive action failing in base case |
197 |
> |
/** A recursive action failing in base case. */ |
198 |
|
static final class FailingFibAction extends RecursiveAction { |
199 |
|
final int number; |
200 |
|
int result; |
266 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
267 |
|
protected void realCompute() { |
268 |
|
FibAction f = new FibAction(8); |
269 |
< |
final Thread myself = Thread.currentThread(); |
269 |
> |
final Thread currentThread = Thread.currentThread(); |
270 |
|
|
271 |
|
// test join() |
272 |
|
assertSame(f, f.fork()); |
273 |
< |
myself.interrupt(); |
277 |
< |
assertTrue(myself.isInterrupted()); |
273 |
> |
currentThread.interrupt(); |
274 |
|
assertNull(f.join()); |
275 |
|
Thread.interrupted(); |
276 |
|
assertEquals(21, f.result); |
279 |
|
f = new FibAction(8); |
280 |
|
f.cancel(true); |
281 |
|
assertSame(f, f.fork()); |
282 |
< |
myself.interrupt(); |
287 |
< |
assertTrue(myself.isInterrupted()); |
282 |
> |
currentThread.interrupt(); |
283 |
|
try { |
284 |
|
f.join(); |
285 |
|
shouldThrow(); |
291 |
|
f = new FibAction(8); |
292 |
|
f.completeExceptionally(new FJException()); |
293 |
|
assertSame(f, f.fork()); |
294 |
< |
myself.interrupt(); |
300 |
< |
assertTrue(myself.isInterrupted()); |
294 |
> |
currentThread.interrupt(); |
295 |
|
try { |
296 |
|
f.join(); |
297 |
|
shouldThrow(); |
303 |
|
// test quietlyJoin() |
304 |
|
f = new FibAction(8); |
305 |
|
assertSame(f, f.fork()); |
306 |
< |
myself.interrupt(); |
313 |
< |
assertTrue(myself.isInterrupted()); |
306 |
> |
currentThread.interrupt(); |
307 |
|
f.quietlyJoin(); |
308 |
|
Thread.interrupted(); |
309 |
|
assertEquals(21, f.result); |
312 |
|
f = new FibAction(8); |
313 |
|
f.cancel(true); |
314 |
|
assertSame(f, f.fork()); |
315 |
< |
myself.interrupt(); |
323 |
< |
assertTrue(myself.isInterrupted()); |
315 |
> |
currentThread.interrupt(); |
316 |
|
f.quietlyJoin(); |
317 |
|
Thread.interrupted(); |
318 |
|
checkCancelled(f); |
320 |
|
f = new FibAction(8); |
321 |
|
f.completeExceptionally(new FJException()); |
322 |
|
assertSame(f, f.fork()); |
323 |
< |
myself.interrupt(); |
332 |
< |
assertTrue(myself.isInterrupted()); |
323 |
> |
currentThread.interrupt(); |
324 |
|
f.quietlyJoin(); |
325 |
|
Thread.interrupted(); |
326 |
|
checkCompletedAbnormally(f, f.getException()); |
353 |
|
protected void realCompute() throws Exception { |
354 |
|
FibAction f = new FibAction(8); |
355 |
|
assertSame(f, f.fork()); |
356 |
< |
assertNull(f.get(5L, SECONDS)); |
356 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
357 |
|
assertEquals(21, f.result); |
358 |
|
checkCompletedNormally(f); |
359 |
|
}}; |
369 |
|
FibAction f = new FibAction(8); |
370 |
|
assertSame(f, f.fork()); |
371 |
|
try { |
372 |
< |
f.get(5L, null); |
372 |
> |
f.get(randomTimeout(), null); |
373 |
|
shouldThrow(); |
374 |
|
} catch (NullPointerException success) {} |
375 |
|
}}; |
469 |
|
FailingFibAction f = new FailingFibAction(8); |
470 |
|
assertSame(f, f.fork()); |
471 |
|
try { |
472 |
< |
f.get(5L, SECONDS); |
472 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
473 |
|
shouldThrow(); |
474 |
|
} catch (ExecutionException success) { |
475 |
|
Throwable cause = success.getCause(); |
561 |
|
assertTrue(f.cancel(true)); |
562 |
|
assertSame(f, f.fork()); |
563 |
|
try { |
564 |
< |
f.get(5L, SECONDS); |
564 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
565 |
|
shouldThrow(); |
566 |
|
} catch (CancellationException success) { |
567 |
|
checkCancelled(f); |
902 |
|
} |
903 |
|
} |
904 |
|
|
905 |
< |
// Version of CCF with forced failure in left completions |
905 |
> |
/** Version of CCF with forced failure in left completions. */ |
906 |
|
abstract static class FailingCCF extends CountedCompleter { |
907 |
|
int number; |
908 |
|
int rnumber; |
1037 |
|
CCF f = new LCCF(null, 8); |
1038 |
|
assertSame(f, f.fork()); |
1039 |
|
try { |
1040 |
< |
f.get(5L, null); |
1040 |
> |
f.get(randomTimeout(), null); |
1041 |
|
shouldThrow(); |
1042 |
|
} catch (NullPointerException success) {} |
1043 |
|
}}; |