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