1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
13 |
import java.util.ArrayList; |
14 |
import java.util.List; |
15 |
import java.util.NoSuchElementException; |
16 |
import java.util.concurrent.Callable; |
17 |
import java.util.concurrent.CancellationException; |
18 |
import java.util.concurrent.CountDownLatch; |
19 |
import java.util.concurrent.ExecutionException; |
20 |
import java.util.concurrent.Executors; |
21 |
import java.util.concurrent.ExecutorService; |
22 |
import java.util.concurrent.Future; |
23 |
import java.util.concurrent.FutureTask; |
24 |
import java.util.concurrent.TimeoutException; |
25 |
import java.util.concurrent.atomic.AtomicInteger; |
26 |
|
27 |
import junit.framework.Test; |
28 |
import junit.framework.TestSuite; |
29 |
|
30 |
public class FutureTaskTest extends JSR166TestCase { |
31 |
|
32 |
public static void main(String[] args) { |
33 |
main(suite(), args); |
34 |
} |
35 |
public static Test suite() { |
36 |
return new TestSuite(FutureTaskTest.class); |
37 |
} |
38 |
|
39 |
void checkIsDone(Future<?> f) { |
40 |
assertTrue(f.isDone()); |
41 |
assertFalse(f.cancel(false)); |
42 |
assertFalse(f.cancel(true)); |
43 |
if (f instanceof PublicFutureTask) { |
44 |
PublicFutureTask pf = (PublicFutureTask) f; |
45 |
assertEquals(1, pf.doneCount()); |
46 |
assertFalse(pf.runAndReset()); |
47 |
assertEquals(1, pf.doneCount()); |
48 |
Object r = null; Object exInfo = null; |
49 |
try { |
50 |
r = f.get(); |
51 |
} catch (CancellationException t) { |
52 |
exInfo = CancellationException.class; |
53 |
} catch (ExecutionException t) { |
54 |
exInfo = t.getCause(); |
55 |
} catch (Throwable t) { |
56 |
threadUnexpectedException(t); |
57 |
} |
58 |
|
59 |
// Check that run and runAndReset have no effect. |
60 |
int savedRunCount = pf.runCount(); |
61 |
pf.run(); |
62 |
pf.runAndReset(); |
63 |
assertEquals(savedRunCount, pf.runCount()); |
64 |
try { |
65 |
assertSame(r, f.get()); |
66 |
} catch (CancellationException t) { |
67 |
assertSame(exInfo, CancellationException.class); |
68 |
} catch (ExecutionException t) { |
69 |
assertSame(exInfo, t.getCause()); |
70 |
} catch (Throwable t) { |
71 |
threadUnexpectedException(t); |
72 |
} |
73 |
assertTrue(f.isDone()); |
74 |
} |
75 |
} |
76 |
|
77 |
void checkNotDone(Future<?> f) { |
78 |
assertFalse(f.isDone()); |
79 |
assertFalse(f.isCancelled()); |
80 |
if (f instanceof PublicFutureTask) { |
81 |
PublicFutureTask pf = (PublicFutureTask) f; |
82 |
assertEquals(0, pf.doneCount()); |
83 |
assertEquals(0, pf.setCount()); |
84 |
assertEquals(0, pf.setExceptionCount()); |
85 |
} |
86 |
} |
87 |
|
88 |
void checkIsRunning(Future<?> f) { |
89 |
checkNotDone(f); |
90 |
if (f instanceof FutureTask) { |
91 |
FutureTask ft = (FutureTask<?>) f; |
92 |
// Check that run methods do nothing |
93 |
ft.run(); |
94 |
if (f instanceof PublicFutureTask) { |
95 |
PublicFutureTask pf = (PublicFutureTask) f; |
96 |
int savedRunCount = pf.runCount(); |
97 |
pf.run(); |
98 |
assertFalse(pf.runAndReset()); |
99 |
assertEquals(savedRunCount, pf.runCount()); |
100 |
} |
101 |
checkNotDone(f); |
102 |
} |
103 |
} |
104 |
|
105 |
<T> void checkCompletedNormally(Future<T> f, T expected) { |
106 |
checkIsDone(f); |
107 |
assertFalse(f.isCancelled()); |
108 |
|
109 |
try { |
110 |
assertSame(expected, f.get()); |
111 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
112 |
try { |
113 |
assertSame(expected, f.get(5L, SECONDS)); |
114 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
} |
116 |
|
117 |
void checkCancelled(Future<?> f) { |
118 |
checkIsDone(f); |
119 |
assertTrue(f.isCancelled()); |
120 |
|
121 |
try { |
122 |
f.get(); |
123 |
shouldThrow(); |
124 |
} catch (CancellationException success) { |
125 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
126 |
|
127 |
try { |
128 |
f.get(5L, SECONDS); |
129 |
shouldThrow(); |
130 |
} catch (CancellationException success) { |
131 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
132 |
} |
133 |
|
134 |
void tryToConfuseDoneTask(PublicFutureTask pf) { |
135 |
pf.set(new Object()); |
136 |
pf.setException(new Error()); |
137 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
138 |
pf.cancel(mayInterruptIfRunning); |
139 |
} |
140 |
} |
141 |
|
142 |
void checkCompletedAbnormally(Future<?> f, Throwable t) { |
143 |
checkIsDone(f); |
144 |
assertFalse(f.isCancelled()); |
145 |
|
146 |
try { |
147 |
f.get(); |
148 |
shouldThrow(); |
149 |
} catch (ExecutionException success) { |
150 |
assertSame(t, success.getCause()); |
151 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
152 |
|
153 |
try { |
154 |
f.get(5L, SECONDS); |
155 |
shouldThrow(); |
156 |
} catch (ExecutionException success) { |
157 |
assertSame(t, success.getCause()); |
158 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
} |
160 |
|
161 |
/** |
162 |
* Subclass to expose protected methods |
163 |
*/ |
164 |
static class PublicFutureTask extends FutureTask { |
165 |
private final AtomicInteger runCount; |
166 |
private final AtomicInteger doneCount = new AtomicInteger(0); |
167 |
private final AtomicInteger runAndResetCount = new AtomicInteger(0); |
168 |
private final AtomicInteger setCount = new AtomicInteger(0); |
169 |
private final AtomicInteger setExceptionCount = new AtomicInteger(0); |
170 |
public int runCount() { return runCount.get(); } |
171 |
public int doneCount() { return doneCount.get(); } |
172 |
public int runAndResetCount() { return runAndResetCount.get(); } |
173 |
public int setCount() { return setCount.get(); } |
174 |
public int setExceptionCount() { return setExceptionCount.get(); } |
175 |
|
176 |
PublicFutureTask(Runnable runnable) { |
177 |
this(runnable, seven); |
178 |
} |
179 |
PublicFutureTask(Runnable runnable, Object result) { |
180 |
this(runnable, result, new AtomicInteger(0)); |
181 |
} |
182 |
private PublicFutureTask(final Runnable runnable, Object result, |
183 |
final AtomicInteger runCount) { |
184 |
super(new Runnable() { |
185 |
public void run() { |
186 |
runCount.getAndIncrement(); |
187 |
runnable.run(); |
188 |
}}, result); |
189 |
this.runCount = runCount; |
190 |
} |
191 |
PublicFutureTask(Callable callable) { |
192 |
this(callable, new AtomicInteger(0)); |
193 |
} |
194 |
private PublicFutureTask(final Callable callable, |
195 |
final AtomicInteger runCount) { |
196 |
super(new Callable() { |
197 |
public Object call() throws Exception { |
198 |
runCount.getAndIncrement(); |
199 |
return callable.call(); |
200 |
}}); |
201 |
this.runCount = runCount; |
202 |
} |
203 |
@Override public void done() { |
204 |
assertTrue(isDone()); |
205 |
doneCount.incrementAndGet(); |
206 |
super.done(); |
207 |
} |
208 |
@Override public boolean runAndReset() { |
209 |
runAndResetCount.incrementAndGet(); |
210 |
return super.runAndReset(); |
211 |
} |
212 |
@Override public void set(Object x) { |
213 |
setCount.incrementAndGet(); |
214 |
super.set(x); |
215 |
} |
216 |
@Override public void setException(Throwable t) { |
217 |
setExceptionCount.incrementAndGet(); |
218 |
super.setException(t); |
219 |
} |
220 |
} |
221 |
|
222 |
class Counter extends CheckedRunnable { |
223 |
final AtomicInteger count = new AtomicInteger(0); |
224 |
public int get() { return count.get(); } |
225 |
public void realRun() { |
226 |
count.getAndIncrement(); |
227 |
} |
228 |
} |
229 |
|
230 |
/** |
231 |
* creating a future with a null callable throws NullPointerException |
232 |
*/ |
233 |
public void testConstructor() { |
234 |
try { |
235 |
new FutureTask(null); |
236 |
shouldThrow(); |
237 |
} catch (NullPointerException success) {} |
238 |
} |
239 |
|
240 |
/** |
241 |
* creating a future with null runnable throws NullPointerException |
242 |
*/ |
243 |
public void testConstructor2() { |
244 |
try { |
245 |
new FutureTask(null, Boolean.TRUE); |
246 |
shouldThrow(); |
247 |
} catch (NullPointerException success) {} |
248 |
} |
249 |
|
250 |
/** |
251 |
* isDone is true when a task completes |
252 |
*/ |
253 |
public void testIsDone() { |
254 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
255 |
assertFalse(task.isDone()); |
256 |
task.run(); |
257 |
assertTrue(task.isDone()); |
258 |
checkCompletedNormally(task, Boolean.TRUE); |
259 |
assertEquals(1, task.runCount()); |
260 |
} |
261 |
|
262 |
/** |
263 |
* runAndReset of a non-cancelled task succeeds |
264 |
*/ |
265 |
public void testRunAndReset() { |
266 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
267 |
for (int i = 0; i < 3; i++) { |
268 |
assertTrue(task.runAndReset()); |
269 |
checkNotDone(task); |
270 |
assertEquals(i + 1, task.runCount()); |
271 |
assertEquals(i + 1, task.runAndResetCount()); |
272 |
assertEquals(0, task.setCount()); |
273 |
assertEquals(0, task.setExceptionCount()); |
274 |
} |
275 |
} |
276 |
|
277 |
/** |
278 |
* runAndReset after cancellation fails |
279 |
*/ |
280 |
public void testRunAndResetAfterCancel() { |
281 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
282 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
283 |
assertTrue(task.cancel(mayInterruptIfRunning)); |
284 |
for (int i = 0; i < 3; i++) { |
285 |
assertFalse(task.runAndReset()); |
286 |
assertEquals(0, task.runCount()); |
287 |
assertEquals(i + 1, task.runAndResetCount()); |
288 |
assertEquals(0, task.setCount()); |
289 |
assertEquals(0, task.setExceptionCount()); |
290 |
} |
291 |
tryToConfuseDoneTask(task); |
292 |
checkCancelled(task); |
293 |
} |
294 |
} |
295 |
|
296 |
/** |
297 |
* setting value causes get to return it |
298 |
*/ |
299 |
public void testSet() throws Exception { |
300 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
301 |
task.set(one); |
302 |
for (int i = 0; i < 3; i++) { |
303 |
assertSame(one, task.get()); |
304 |
assertSame(one, task.get(LONG_DELAY_MS, MILLISECONDS)); |
305 |
assertEquals(1, task.setCount()); |
306 |
} |
307 |
tryToConfuseDoneTask(task); |
308 |
checkCompletedNormally(task, one); |
309 |
assertEquals(0, task.runCount()); |
310 |
} |
311 |
|
312 |
/** |
313 |
* setException causes get to throw ExecutionException |
314 |
*/ |
315 |
public void testSetException_get() throws Exception { |
316 |
Exception nse = new NoSuchElementException(); |
317 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
318 |
task.setException(nse); |
319 |
|
320 |
try { |
321 |
task.get(); |
322 |
shouldThrow(); |
323 |
} catch (ExecutionException success) { |
324 |
assertSame(nse, success.getCause()); |
325 |
checkCompletedAbnormally(task, nse); |
326 |
} |
327 |
|
328 |
try { |
329 |
task.get(LONG_DELAY_MS, MILLISECONDS); |
330 |
shouldThrow(); |
331 |
} catch (ExecutionException success) { |
332 |
assertSame(nse, success.getCause()); |
333 |
checkCompletedAbnormally(task, nse); |
334 |
} |
335 |
|
336 |
assertEquals(1, task.setExceptionCount()); |
337 |
assertEquals(0, task.setCount()); |
338 |
tryToConfuseDoneTask(task); |
339 |
checkCompletedAbnormally(task, nse); |
340 |
assertEquals(0, task.runCount()); |
341 |
} |
342 |
|
343 |
/** |
344 |
* cancel(false) before run succeeds |
345 |
*/ |
346 |
public void testCancelBeforeRun() { |
347 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
348 |
assertTrue(task.cancel(false)); |
349 |
task.run(); |
350 |
assertEquals(0, task.runCount()); |
351 |
assertEquals(0, task.setCount()); |
352 |
assertEquals(0, task.setExceptionCount()); |
353 |
assertTrue(task.isCancelled()); |
354 |
assertTrue(task.isDone()); |
355 |
tryToConfuseDoneTask(task); |
356 |
assertEquals(0, task.runCount()); |
357 |
checkCancelled(task); |
358 |
} |
359 |
|
360 |
/** |
361 |
* cancel(true) before run succeeds |
362 |
*/ |
363 |
public void testCancelBeforeRun2() { |
364 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
365 |
assertTrue(task.cancel(true)); |
366 |
task.run(); |
367 |
assertEquals(0, task.runCount()); |
368 |
assertEquals(0, task.setCount()); |
369 |
assertEquals(0, task.setExceptionCount()); |
370 |
assertTrue(task.isCancelled()); |
371 |
assertTrue(task.isDone()); |
372 |
tryToConfuseDoneTask(task); |
373 |
assertEquals(0, task.runCount()); |
374 |
checkCancelled(task); |
375 |
} |
376 |
|
377 |
/** |
378 |
* cancel(false) of a completed task fails |
379 |
*/ |
380 |
public void testCancelAfterRun() { |
381 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
382 |
task.run(); |
383 |
assertFalse(task.cancel(false)); |
384 |
assertEquals(1, task.runCount()); |
385 |
assertEquals(1, task.setCount()); |
386 |
assertEquals(0, task.setExceptionCount()); |
387 |
tryToConfuseDoneTask(task); |
388 |
checkCompletedNormally(task, Boolean.TRUE); |
389 |
assertEquals(1, task.runCount()); |
390 |
} |
391 |
|
392 |
/** |
393 |
* cancel(true) of a completed task fails |
394 |
*/ |
395 |
public void testCancelAfterRun2() { |
396 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
397 |
task.run(); |
398 |
assertFalse(task.cancel(true)); |
399 |
assertEquals(1, task.runCount()); |
400 |
assertEquals(1, task.setCount()); |
401 |
assertEquals(0, task.setExceptionCount()); |
402 |
tryToConfuseDoneTask(task); |
403 |
checkCompletedNormally(task, Boolean.TRUE); |
404 |
assertEquals(1, task.runCount()); |
405 |
} |
406 |
|
407 |
/** |
408 |
* cancel(true) interrupts a running task that subsequently succeeds |
409 |
*/ |
410 |
public void testCancelInterrupt() { |
411 |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
412 |
final PublicFutureTask task = |
413 |
new PublicFutureTask(new CheckedRunnable() { |
414 |
public void realRun() { |
415 |
pleaseCancel.countDown(); |
416 |
try { |
417 |
delay(LONG_DELAY_MS); |
418 |
shouldThrow(); |
419 |
} catch (InterruptedException success) {} |
420 |
}}); |
421 |
|
422 |
Thread t = newStartedThread(task); |
423 |
await(pleaseCancel); |
424 |
assertTrue(task.cancel(true)); |
425 |
assertTrue(task.isCancelled()); |
426 |
assertTrue(task.isDone()); |
427 |
awaitTermination(t); |
428 |
assertEquals(1, task.runCount()); |
429 |
assertEquals(1, task.setCount()); |
430 |
assertEquals(0, task.setExceptionCount()); |
431 |
tryToConfuseDoneTask(task); |
432 |
checkCancelled(task); |
433 |
} |
434 |
|
435 |
/** |
436 |
* cancel(true) tries to interrupt a running task, but |
437 |
* Thread.interrupt throws (simulating a restrictive security |
438 |
* manager) |
439 |
*/ |
440 |
public void testCancelInterrupt_ThrowsSecurityException() { |
441 |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
442 |
final CountDownLatch cancelled = new CountDownLatch(1); |
443 |
final PublicFutureTask task = |
444 |
new PublicFutureTask(new CheckedRunnable() { |
445 |
public void realRun() { |
446 |
pleaseCancel.countDown(); |
447 |
await(cancelled); |
448 |
assertFalse(Thread.interrupted()); |
449 |
}}); |
450 |
|
451 |
final Thread t = new Thread(task) { |
452 |
// Simulate a restrictive security manager. |
453 |
@Override public void interrupt() { |
454 |
throw new SecurityException(); |
455 |
}}; |
456 |
t.setDaemon(true); |
457 |
t.start(); |
458 |
|
459 |
await(pleaseCancel); |
460 |
try { |
461 |
task.cancel(true); |
462 |
shouldThrow(); |
463 |
} catch (SecurityException expected) {} |
464 |
|
465 |
// We failed to deliver the interrupt, but the world retains |
466 |
// its sanity, as if we had done task.cancel(false) |
467 |
assertTrue(task.isCancelled()); |
468 |
assertTrue(task.isDone()); |
469 |
assertEquals(1, task.runCount()); |
470 |
assertEquals(1, task.doneCount()); |
471 |
assertEquals(0, task.setCount()); |
472 |
assertEquals(0, task.setExceptionCount()); |
473 |
cancelled.countDown(); |
474 |
awaitTermination(t); |
475 |
assertEquals(1, task.setCount()); |
476 |
assertEquals(0, task.setExceptionCount()); |
477 |
tryToConfuseDoneTask(task); |
478 |
checkCancelled(task); |
479 |
} |
480 |
|
481 |
/** |
482 |
* cancel(true) interrupts a running task that subsequently throws |
483 |
*/ |
484 |
public void testCancelInterrupt_taskFails() { |
485 |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
486 |
final PublicFutureTask task = |
487 |
new PublicFutureTask(new Runnable() { |
488 |
public void run() { |
489 |
pleaseCancel.countDown(); |
490 |
try { |
491 |
delay(LONG_DELAY_MS); |
492 |
threadShouldThrow(); |
493 |
} catch (InterruptedException success) { |
494 |
} catch (Throwable t) { threadUnexpectedException(t); } |
495 |
throw new RuntimeException(); |
496 |
}}); |
497 |
|
498 |
Thread t = newStartedThread(task); |
499 |
await(pleaseCancel); |
500 |
assertTrue(task.cancel(true)); |
501 |
assertTrue(task.isCancelled()); |
502 |
awaitTermination(t); |
503 |
assertEquals(1, task.runCount()); |
504 |
assertEquals(0, task.setCount()); |
505 |
assertEquals(1, task.setExceptionCount()); |
506 |
tryToConfuseDoneTask(task); |
507 |
checkCancelled(task); |
508 |
} |
509 |
|
510 |
/** |
511 |
* cancel(false) does not interrupt a running task |
512 |
*/ |
513 |
public void testCancelNoInterrupt() { |
514 |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
515 |
final CountDownLatch cancelled = new CountDownLatch(1); |
516 |
final PublicFutureTask task = |
517 |
new PublicFutureTask(new CheckedCallable<Boolean>() { |
518 |
public Boolean realCall() { |
519 |
pleaseCancel.countDown(); |
520 |
await(cancelled); |
521 |
assertFalse(Thread.interrupted()); |
522 |
return Boolean.TRUE; |
523 |
}}); |
524 |
|
525 |
Thread t = newStartedThread(task); |
526 |
await(pleaseCancel); |
527 |
assertTrue(task.cancel(false)); |
528 |
assertTrue(task.isCancelled()); |
529 |
cancelled.countDown(); |
530 |
awaitTermination(t); |
531 |
assertEquals(1, task.runCount()); |
532 |
assertEquals(1, task.setCount()); |
533 |
assertEquals(0, task.setExceptionCount()); |
534 |
tryToConfuseDoneTask(task); |
535 |
checkCancelled(task); |
536 |
} |
537 |
|
538 |
/** |
539 |
* run in one thread causes get in another thread to retrieve value |
540 |
*/ |
541 |
public void testGetRun() { |
542 |
final CountDownLatch pleaseRun = new CountDownLatch(2); |
543 |
|
544 |
final PublicFutureTask task = |
545 |
new PublicFutureTask(new CheckedCallable<Object>() { |
546 |
public Object realCall() { |
547 |
return two; |
548 |
}}); |
549 |
|
550 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
551 |
public void realRun() throws Exception { |
552 |
pleaseRun.countDown(); |
553 |
assertSame(two, task.get()); |
554 |
}}); |
555 |
|
556 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
557 |
public void realRun() throws Exception { |
558 |
pleaseRun.countDown(); |
559 |
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
560 |
}}); |
561 |
|
562 |
await(pleaseRun); |
563 |
checkNotDone(task); |
564 |
assertTrue(t1.isAlive()); |
565 |
assertTrue(t2.isAlive()); |
566 |
task.run(); |
567 |
checkCompletedNormally(task, two); |
568 |
assertEquals(1, task.runCount()); |
569 |
assertEquals(1, task.setCount()); |
570 |
assertEquals(0, task.setExceptionCount()); |
571 |
awaitTermination(t1); |
572 |
awaitTermination(t2); |
573 |
tryToConfuseDoneTask(task); |
574 |
checkCompletedNormally(task, two); |
575 |
} |
576 |
|
577 |
/** |
578 |
* set in one thread causes get in another thread to retrieve value |
579 |
*/ |
580 |
public void testGetSet() { |
581 |
final CountDownLatch pleaseSet = new CountDownLatch(2); |
582 |
|
583 |
final PublicFutureTask task = |
584 |
new PublicFutureTask(new CheckedCallable<Object>() { |
585 |
public Object realCall() throws InterruptedException { |
586 |
return two; |
587 |
}}); |
588 |
|
589 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
590 |
public void realRun() throws Exception { |
591 |
pleaseSet.countDown(); |
592 |
assertSame(two, task.get()); |
593 |
}}); |
594 |
|
595 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
596 |
public void realRun() throws Exception { |
597 |
pleaseSet.countDown(); |
598 |
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
599 |
}}); |
600 |
|
601 |
await(pleaseSet); |
602 |
checkNotDone(task); |
603 |
assertTrue(t1.isAlive()); |
604 |
assertTrue(t2.isAlive()); |
605 |
task.set(two); |
606 |
assertEquals(0, task.runCount()); |
607 |
assertEquals(1, task.setCount()); |
608 |
assertEquals(0, task.setExceptionCount()); |
609 |
tryToConfuseDoneTask(task); |
610 |
checkCompletedNormally(task, two); |
611 |
awaitTermination(t1); |
612 |
awaitTermination(t2); |
613 |
} |
614 |
|
615 |
/** |
616 |
* Cancelling a task causes timed get in another thread to throw |
617 |
* CancellationException |
618 |
*/ |
619 |
public void testTimedGet_Cancellation() { |
620 |
testTimedGet_Cancellation(false); |
621 |
} |
622 |
public void testTimedGet_Cancellation_interrupt() { |
623 |
testTimedGet_Cancellation(true); |
624 |
} |
625 |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
626 |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
627 |
final CountDownLatch cancelled = new CountDownLatch(1); |
628 |
final Callable<Object> callable = |
629 |
new CheckedCallable<Object>() { |
630 |
public Object realCall() throws InterruptedException { |
631 |
pleaseCancel.countDown(); |
632 |
if (mayInterruptIfRunning) { |
633 |
try { |
634 |
delay(2*LONG_DELAY_MS); |
635 |
} catch (InterruptedException success) {} |
636 |
} else { |
637 |
await(cancelled); |
638 |
} |
639 |
return two; |
640 |
}}; |
641 |
final PublicFutureTask task = new PublicFutureTask(callable); |
642 |
|
643 |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
644 |
public void realRun() throws Exception { |
645 |
pleaseCancel.countDown(); |
646 |
task.get(); |
647 |
}}; |
648 |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
649 |
public void realRun() throws Exception { |
650 |
pleaseCancel.countDown(); |
651 |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
652 |
}}; |
653 |
t1.start(); |
654 |
t2.start(); |
655 |
Thread t3 = newStartedThread(task); |
656 |
await(pleaseCancel); |
657 |
checkIsRunning(task); |
658 |
task.cancel(mayInterruptIfRunning); |
659 |
checkCancelled(task); |
660 |
awaitTermination(t1); |
661 |
awaitTermination(t2); |
662 |
cancelled.countDown(); |
663 |
awaitTermination(t3); |
664 |
assertEquals(1, task.runCount()); |
665 |
assertEquals(1, task.setCount()); |
666 |
assertEquals(0, task.setExceptionCount()); |
667 |
tryToConfuseDoneTask(task); |
668 |
checkCancelled(task); |
669 |
} |
670 |
|
671 |
/** |
672 |
* A runtime exception in task causes get to throw ExecutionException |
673 |
*/ |
674 |
public void testGet_ExecutionException() throws InterruptedException { |
675 |
final ArithmeticException e = new ArithmeticException(); |
676 |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
677 |
public Object call() { |
678 |
throw e; |
679 |
}}); |
680 |
|
681 |
task.run(); |
682 |
assertEquals(1, task.runCount()); |
683 |
assertEquals(0, task.setCount()); |
684 |
assertEquals(1, task.setExceptionCount()); |
685 |
try { |
686 |
task.get(); |
687 |
shouldThrow(); |
688 |
} catch (ExecutionException success) { |
689 |
assertSame(e, success.getCause()); |
690 |
tryToConfuseDoneTask(task); |
691 |
checkCompletedAbnormally(task, success.getCause()); |
692 |
} |
693 |
} |
694 |
|
695 |
/** |
696 |
* A runtime exception in task causes timed get to throw ExecutionException |
697 |
*/ |
698 |
public void testTimedGet_ExecutionException2() throws Exception { |
699 |
final ArithmeticException e = new ArithmeticException(); |
700 |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
701 |
public Object call() { |
702 |
throw e; |
703 |
}}); |
704 |
|
705 |
task.run(); |
706 |
try { |
707 |
task.get(LONG_DELAY_MS, MILLISECONDS); |
708 |
shouldThrow(); |
709 |
} catch (ExecutionException success) { |
710 |
assertSame(e, success.getCause()); |
711 |
tryToConfuseDoneTask(task); |
712 |
checkCompletedAbnormally(task, success.getCause()); |
713 |
} |
714 |
} |
715 |
|
716 |
/** |
717 |
* get is interruptible |
718 |
*/ |
719 |
public void testGet_interruptible() { |
720 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
721 |
final FutureTask task = new FutureTask(new NoOpCallable()); |
722 |
Thread t = newStartedThread(new CheckedRunnable() { |
723 |
public void realRun() throws Exception { |
724 |
Thread.currentThread().interrupt(); |
725 |
try { |
726 |
task.get(); |
727 |
shouldThrow(); |
728 |
} catch (InterruptedException success) {} |
729 |
assertFalse(Thread.interrupted()); |
730 |
|
731 |
pleaseInterrupt.countDown(); |
732 |
try { |
733 |
task.get(); |
734 |
shouldThrow(); |
735 |
} catch (InterruptedException success) {} |
736 |
assertFalse(Thread.interrupted()); |
737 |
}}); |
738 |
|
739 |
await(pleaseInterrupt); |
740 |
t.interrupt(); |
741 |
awaitTermination(t); |
742 |
checkNotDone(task); |
743 |
} |
744 |
|
745 |
/** |
746 |
* timed get is interruptible |
747 |
*/ |
748 |
public void testTimedGet_interruptible() { |
749 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
750 |
final FutureTask task = new FutureTask(new NoOpCallable()); |
751 |
Thread t = newStartedThread(new CheckedRunnable() { |
752 |
public void realRun() throws Exception { |
753 |
Thread.currentThread().interrupt(); |
754 |
try { |
755 |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
756 |
shouldThrow(); |
757 |
} catch (InterruptedException success) {} |
758 |
assertFalse(Thread.interrupted()); |
759 |
|
760 |
pleaseInterrupt.countDown(); |
761 |
try { |
762 |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
763 |
shouldThrow(); |
764 |
} catch (InterruptedException success) {} |
765 |
assertFalse(Thread.interrupted()); |
766 |
}}); |
767 |
|
768 |
await(pleaseInterrupt); |
769 |
t.interrupt(); |
770 |
awaitTermination(t); |
771 |
checkNotDone(task); |
772 |
} |
773 |
|
774 |
/** |
775 |
* A timed out timed get throws TimeoutException |
776 |
*/ |
777 |
public void testGet_TimeoutException() throws Exception { |
778 |
FutureTask task = new FutureTask(new NoOpCallable()); |
779 |
long startTime = System.nanoTime(); |
780 |
try { |
781 |
task.get(timeoutMillis(), MILLISECONDS); |
782 |
shouldThrow(); |
783 |
} catch (TimeoutException success) { |
784 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
785 |
} |
786 |
} |
787 |
|
788 |
/** |
789 |
* timed get with null TimeUnit throws NullPointerException |
790 |
*/ |
791 |
public void testGet_NullTimeUnit() throws Exception { |
792 |
FutureTask task = new FutureTask(new NoOpCallable()); |
793 |
long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; |
794 |
|
795 |
for (long timeout : timeouts) { |
796 |
try { |
797 |
task.get(timeout, null); |
798 |
shouldThrow(); |
799 |
} catch (NullPointerException success) {} |
800 |
} |
801 |
|
802 |
task.run(); |
803 |
|
804 |
for (long timeout : timeouts) { |
805 |
try { |
806 |
task.get(timeout, null); |
807 |
shouldThrow(); |
808 |
} catch (NullPointerException success) {} |
809 |
} |
810 |
} |
811 |
|
812 |
/** |
813 |
* timed get with most negative timeout works correctly (i.e. no |
814 |
* underflow bug) |
815 |
*/ |
816 |
public void testGet_NegativeInfinityTimeout() throws Exception { |
817 |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
818 |
final Runnable nop = new Runnable() { public void run() {}}; |
819 |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
820 |
final List<Future<?>> futures = new ArrayList<>(); |
821 |
Runnable r = new Runnable() { public void run() { |
822 |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
823 |
try { |
824 |
task.get(timeout, NANOSECONDS); |
825 |
shouldThrow(); |
826 |
} catch (TimeoutException success) { |
827 |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
828 |
for (int i = 0; i < 10; i++) |
829 |
futures.add(pool.submit(r)); |
830 |
try { |
831 |
joinPool(pool); |
832 |
for (Future<?> future : futures) |
833 |
checkCompletedNormally(future, null); |
834 |
} finally { |
835 |
task.run(); // last resort to help terminate |
836 |
} |
837 |
} |
838 |
|
839 |
} |