1 |
/* |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* public domain. Use, modify, and redistribute this code in any way |
* Expert Group and released to the public domain, as explained at |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* 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 reset() { return super.reset(); } |
private final AtomicInteger doneCount = new AtomicInteger(0); |
139 |
public void setCancelled() { super.setCancelled(); } |
private final AtomicInteger runAndResetCount = new AtomicInteger(0); |
140 |
public void set(Object x) { super.set(x); } |
private final AtomicInteger setCount = new AtomicInteger(0); |
141 |
public void setException(Throwable t) { super.setException(t); } |
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 |
|
public void done() { |
176 |
|
assertTrue(isDone()); |
177 |
|
doneCount.incrementAndGet(); |
178 |
|
super.done(); |
179 |
|
} |
180 |
|
public boolean runAndReset() { |
181 |
|
runAndResetCount.incrementAndGet(); |
182 |
|
return super.runAndReset(); |
183 |
|
} |
184 |
|
public void set(Object x) { |
185 |
|
setCount.incrementAndGet(); |
186 |
|
super.set(x); |
187 |
|
} |
188 |
|
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() { |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
|
|
task.run(); |
|
|
assertTrue(task.isDone()); |
|
|
assertFalse(task.isCancelled()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* reset of a done task succeeds and changes status to not done |
|
|
*/ |
|
|
public void testReset() { |
|
226 |
PublicFutureTask task = new PublicFutureTask(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 |
assertTrue(task.reset()); |
checkCompletedNormally(task, Boolean.TRUE); |
231 |
assertFalse(task.isDone()); |
assertEquals(1, task.runCount()); |
232 |
} |
} |
233 |
|
|
234 |
/** |
/** |
235 |
* Resetting after cancellation fails |
* runAndReset of a non-cancelled task succeeds |
236 |
*/ |
*/ |
237 |
public void testResetAfterCancel() { |
public void testRunAndReset() { |
238 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
239 |
assertTrue(task.cancel(false)); |
for (int i = 0; i < 3; i++) { |
240 |
task.run(); |
assertTrue(task.runAndReset()); |
241 |
assertTrue(task.isDone()); |
checkNotDone(task); |
242 |
assertTrue(task.isCancelled()); |
assertEquals(i+1, task.runCount()); |
243 |
assertFalse(task.reset()); |
assertEquals(i+1, task.runAndResetCount()); |
244 |
|
assertEquals(0, task.setCount()); |
245 |
|
assertEquals(0, task.setExceptionCount()); |
246 |
|
} |
247 |
} |
} |
|
|
|
248 |
|
|
249 |
/** |
/** |
250 |
* setCancelled of a new task causes isCancelled to be true |
* runAndReset after cancellation fails |
251 |
*/ |
*/ |
252 |
public void testSetCancelled() { |
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 |
task.setCancelled(); |
for (int i = 0; i < 3; i++) { |
257 |
assertTrue(task.isDone()); |
assertFalse(task.runAndReset()); |
258 |
assertTrue(task.isCancelled()); |
assertEquals(0, task.runCount()); |
259 |
|
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 gauses 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.setCount()); |
323 |
assertTrue(task.isCancelled()); |
assertEquals(0, task.setExceptionCount()); |
324 |
|
tryToConfuseDoneTask(task); |
325 |
|
checkCancelled(task); |
326 |
|
assertEquals(0, task.runCount()); |
327 |
} |
} |
328 |
|
|
329 |
/** |
/** |
330 |
* Cancel(true) before run succeeds |
* cancel(true) before run succeeds |
331 |
*/ |
*/ |
332 |
public void testCancelBeforeRun2() { |
public void testCancelBeforeRun2() { |
333 |
FutureTask task = new FutureTask( new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
334 |
assertTrue(task.cancel(true)); |
assertTrue(task.cancel(true)); |
335 |
task.run(); |
task.run(); |
336 |
assertTrue(task.isDone()); |
assertEquals(0, task.setCount()); |
337 |
assertTrue(task.isCancelled()); |
assertEquals(0, task.setExceptionCount()); |
338 |
|
tryToConfuseDoneTask(task); |
339 |
|
checkCancelled(task); |
340 |
|
assertEquals(0, task.runCount()); |
341 |
} |
} |
342 |
|
|
343 |
/** |
/** |
344 |
* cancel of a completed task fails |
* cancel(false) of a completed task fails |
345 |
*/ |
*/ |
346 |
public void testCancelAfterRun() { |
public void testCancelAfterRun() { |
347 |
FutureTask task = new FutureTask( new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
348 |
task.run(); |
task.run(); |
349 |
assertFalse(task.cancel(false)); |
assertFalse(task.cancel(false)); |
350 |
assertTrue(task.isDone()); |
assertEquals(1, task.setCount()); |
351 |
assertFalse(task.isCancelled()); |
assertEquals(0, task.setExceptionCount()); |
352 |
|
tryToConfuseDoneTask(task); |
353 |
|
checkCompletedNormally(task, Boolean.TRUE); |
354 |
|
assertEquals(1, task.runCount()); |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
|
* cancel(true) of a completed task fails |
359 |
|
*/ |
360 |
|
public void testCancelAfterRun2() { |
361 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
362 |
|
task.run(); |
363 |
|
assertFalse(task.cancel(true)); |
364 |
|
assertEquals(1, task.setCount()); |
365 |
|
assertEquals(0, task.setExceptionCount()); |
366 |
|
tryToConfuseDoneTask(task); |
367 |
|
checkCompletedNormally(task, Boolean.TRUE); |
368 |
|
assertEquals(1, task.runCount()); |
369 |
} |
} |
370 |
|
|
371 |
/** |
/** |
372 |
* cancel(true) interrupts a running task |
* cancel(true) interrupts a running task that subsequently succeeds |
373 |
*/ |
*/ |
374 |
public void testCancelInterrupt() { |
public void testCancelInterrupt() { |
375 |
FutureTask task = new FutureTask( new Callable() { |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
376 |
public Object call() { |
final PublicFutureTask task = |
377 |
|
new PublicFutureTask(new CheckedRunnable() { |
378 |
|
public void realRun() { |
379 |
|
pleaseCancel.countDown(); |
380 |
try { |
try { |
381 |
Thread.sleep(MEDIUM_DELAY_MS); |
delay(LONG_DELAY_MS); |
382 |
threadShouldThrow(); |
shouldThrow(); |
383 |
} |
} catch (InterruptedException success) {} |
|
catch (InterruptedException success) {} |
|
|
return Boolean.TRUE; |
|
384 |
} }); |
} }); |
|
Thread t = new Thread(task); |
|
|
t.start(); |
|
385 |
|
|
386 |
try { |
Thread t = newStartedThread(task); |
387 |
Thread.sleep(SHORT_DELAY_MS); |
await(pleaseCancel); |
388 |
assertTrue(task.cancel(true)); |
assertTrue(task.cancel(true)); |
|
t.join(); |
|
|
assertTrue(task.isDone()); |
|
389 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
390 |
} catch(InterruptedException e){ |
awaitTermination(t); |
391 |
unexpectedException(); |
assertEquals(1, task.runCount()); |
392 |
} |
assertEquals(1, task.setCount()); |
393 |
|
assertEquals(0, task.setExceptionCount()); |
394 |
|
tryToConfuseDoneTask(task); |
395 |
|
checkCancelled(task); |
396 |
} |
} |
397 |
|
|
398 |
|
/** |
399 |
|
* cancel(true) interrupts a running task that subsequently throws |
400 |
|
*/ |
401 |
|
public void testCancelInterrupt_taskFails() { |
402 |
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
403 |
|
final PublicFutureTask task = |
404 |
|
new PublicFutureTask(new Runnable() { |
405 |
|
public void run() { |
406 |
|
try { |
407 |
|
pleaseCancel.countDown(); |
408 |
|
delay(LONG_DELAY_MS); |
409 |
|
} finally { throw new RuntimeException(); } |
410 |
|
}}); |
411 |
|
|
412 |
|
Thread t = newStartedThread(task); |
413 |
|
await(pleaseCancel); |
414 |
|
assertTrue(task.cancel(true)); |
415 |
|
assertTrue(task.isCancelled()); |
416 |
|
awaitTermination(t); |
417 |
|
assertEquals(1, task.runCount()); |
418 |
|
assertEquals(0, task.setCount()); |
419 |
|
assertEquals(1, task.setExceptionCount()); |
420 |
|
tryToConfuseDoneTask(task); |
421 |
|
checkCancelled(task); |
422 |
|
} |
423 |
|
|
424 |
/** |
/** |
425 |
* cancel(false) does not interrupt a running task |
* cancel(false) does not interrupt a running task |
426 |
*/ |
*/ |
427 |
public void testCancelNoInterrupt() { |
public void testCancelNoInterrupt() { |
428 |
FutureTask task = new FutureTask( new Callable() { |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
429 |
public Object call() { |
final CountDownLatch cancelled = new CountDownLatch(1); |
430 |
try { |
final PublicFutureTask task = |
431 |
Thread.sleep(MEDIUM_DELAY_MS); |
new PublicFutureTask(new CheckedCallable<Boolean>() { |
432 |
} |
public Boolean realCall() { |
433 |
catch (InterruptedException success) { |
pleaseCancel.countDown(); |
434 |
threadFail("should not interrupt"); |
await(cancelled); |
435 |
} |
assertFalse(Thread.interrupted()); |
436 |
return Boolean.TRUE; |
return Boolean.TRUE; |
437 |
} }); |
} }); |
|
Thread t = new Thread(task); |
|
|
t.start(); |
|
438 |
|
|
439 |
try { |
Thread t = newStartedThread(task); |
440 |
Thread.sleep(SHORT_DELAY_MS); |
await(pleaseCancel); |
441 |
assertTrue(task.cancel(false)); |
assertTrue(task.cancel(false)); |
|
t.join(); |
|
|
assertTrue(task.isDone()); |
|
442 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
443 |
} catch(InterruptedException e){ |
cancelled.countDown(); |
444 |
unexpectedException(); |
awaitTermination(t); |
445 |
} |
assertEquals(1, task.runCount()); |
446 |
|
assertEquals(1, task.setCount()); |
447 |
|
assertEquals(0, task.setExceptionCount()); |
448 |
|
tryToConfuseDoneTask(task); |
449 |
|
checkCancelled(task); |
450 |
} |
} |
451 |
|
|
452 |
/** |
/** |
453 |
* set in one thread causes get in another thread to retrieve value |
* run in one thread causes get in another thread to retrieve value |
454 |
*/ |
*/ |
455 |
public void testGet1() { |
public void testGetRun() { |
456 |
final FutureTask ft = new FutureTask(new Callable() { |
final CountDownLatch pleaseRun = new CountDownLatch(2); |
|
public Object call() { |
|
|
try { |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
|
|
} catch(InterruptedException e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
return Boolean.TRUE; |
|
|
} |
|
|
}); |
|
|
Thread t = new Thread(new Runnable() { |
|
|
public void run() { |
|
|
try { |
|
|
ft.get(); |
|
|
} catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
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(); |
|
457 |
|
|
458 |
} |
final PublicFutureTask task = |
459 |
|
new PublicFutureTask(new CheckedCallable<Object>() { |
460 |
|
public Object realCall() { |
461 |
|
return two; |
462 |
|
}}); |
463 |
|
|
464 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
465 |
|
public void realRun() throws Exception { |
466 |
|
pleaseRun.countDown(); |
467 |
|
assertSame(two, task.get()); |
468 |
|
}}); |
469 |
|
|
470 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
471 |
|
public void realRun() throws Exception { |
472 |
|
pleaseRun.countDown(); |
473 |
|
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
474 |
|
}}); |
475 |
|
|
476 |
|
await(pleaseRun); |
477 |
|
checkNotDone(task); |
478 |
|
assertTrue(t1.isAlive()); |
479 |
|
assertTrue(t2.isAlive()); |
480 |
|
task.run(); |
481 |
|
checkCompletedNormally(task, two); |
482 |
|
assertEquals(1, task.runCount()); |
483 |
|
assertEquals(1, task.setCount()); |
484 |
|
assertEquals(0, task.setExceptionCount()); |
485 |
|
awaitTermination(t1); |
486 |
|
awaitTermination(t2); |
487 |
|
tryToConfuseDoneTask(task); |
488 |
|
checkCompletedNormally(task, two); |
489 |
} |
} |
490 |
|
|
491 |
/** |
/** |
492 |
* set in one thread causes timed get in another thread to retrieve value |
* set in one thread causes get in another thread to retrieve value |
493 |
*/ |
*/ |
494 |
public void testTimedGet1() { |
public void testGetSet() { |
495 |
final FutureTask ft = new FutureTask(new Callable() { |
final CountDownLatch pleaseSet = new CountDownLatch(2); |
|
public Object call() { |
|
|
try { |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
|
|
} catch(InterruptedException e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
return Boolean.TRUE; |
|
|
} |
|
|
}); |
|
|
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(); |
|
496 |
|
|
497 |
} |
final PublicFutureTask task = |
498 |
|
new PublicFutureTask(new CheckedCallable<Object>() { |
499 |
|
public Object realCall() throws InterruptedException { |
500 |
|
return two; |
501 |
|
}}); |
502 |
|
|
503 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
504 |
|
public void realRun() throws Exception { |
505 |
|
pleaseSet.countDown(); |
506 |
|
assertSame(two, task.get()); |
507 |
|
}}); |
508 |
|
|
509 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
510 |
|
public void realRun() throws Exception { |
511 |
|
pleaseSet.countDown(); |
512 |
|
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
513 |
|
}}); |
514 |
|
|
515 |
|
await(pleaseSet); |
516 |
|
checkNotDone(task); |
517 |
|
assertTrue(t1.isAlive()); |
518 |
|
assertTrue(t2.isAlive()); |
519 |
|
task.set(two); |
520 |
|
assertEquals(0, task.runCount()); |
521 |
|
assertEquals(1, task.setCount()); |
522 |
|
assertEquals(0, task.setExceptionCount()); |
523 |
|
tryToConfuseDoneTask(task); |
524 |
|
checkCompletedNormally(task, two); |
525 |
|
awaitTermination(t1); |
526 |
|
awaitTermination(t2); |
527 |
} |
} |
528 |
|
|
529 |
/** |
/** |
530 |
* Cancelling a task causes timed get in another thread to throw CancellationException |
* Cancelling a task causes timed get in another thread to throw |
531 |
|
* CancellationException |
532 |
*/ |
*/ |
533 |
public void testTimedGet_Cancellation() { |
public void testTimedGet_Cancellation() { |
534 |
final FutureTask ft = new FutureTask(new Callable() { |
for (final boolean mayInterruptIfRunning : |
535 |
public Object call() { |
new boolean[] { true, false }) { |
536 |
try { |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
537 |
Thread.sleep(SMALL_DELAY_MS); |
final CountDownLatch cancelled = new CountDownLatch(1); |
538 |
threadShouldThrow(); |
final PublicFutureTask task = |
539 |
} catch(InterruptedException e) { |
new PublicFutureTask(new CheckedCallable<Object>() { |
540 |
} |
public Object realCall() throws InterruptedException { |
541 |
return Boolean.TRUE; |
pleaseCancel.countDown(); |
542 |
} |
if (mayInterruptIfRunning) { |
|
}); |
|
543 |
try { |
try { |
544 |
Thread t1 = new Thread(new Runnable() { |
delay(2*LONG_DELAY_MS); |
545 |
public void run() { |
} catch (InterruptedException success) {} |
546 |
try { |
} else { |
547 |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
await(cancelled); |
|
threadShouldThrow(); |
|
|
} catch(CancellationException success) {} |
|
|
catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
Thread t2 = new Thread(ft); |
|
|
t1.start(); |
|
|
t2.start(); |
|
|
Thread.sleep(SHORT_DELAY_MS); |
|
|
ft.cancel(true); |
|
|
t1.join(); |
|
|
t2.join(); |
|
|
} catch(InterruptedException ie){ |
|
|
unexpectedException(); |
|
|
} |
|
548 |
} |
} |
549 |
|
return two; |
550 |
|
}}); |
551 |
|
|
552 |
/** |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
553 |
* Cancelling a task causes get in another thread to throw CancellationException |
public void realRun() throws Exception { |
554 |
*/ |
pleaseCancel.countDown(); |
555 |
public void testGet_Cancellation() { |
task.get(); |
556 |
final FutureTask ft = new FutureTask(new Callable() { |
}}; |
557 |
public Object call() { |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
558 |
try { |
public void realRun() throws Exception { |
559 |
Thread.sleep(MEDIUM_DELAY_MS); |
pleaseCancel.countDown(); |
560 |
threadShouldThrow(); |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
561 |
} catch(InterruptedException e){ |
}}; |
|
} |
|
|
return Boolean.TRUE; |
|
|
} |
|
|
}); |
|
|
try { |
|
|
Thread t1 = new Thread(new Runnable() { |
|
|
public void run() { |
|
|
try { |
|
|
ft.get(); |
|
|
threadShouldThrow(); |
|
|
} catch(CancellationException success){ |
|
|
} |
|
|
catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
Thread t2 = new Thread(ft); |
|
562 |
t1.start(); |
t1.start(); |
563 |
t2.start(); |
t2.start(); |
564 |
Thread.sleep(SHORT_DELAY_MS); |
Thread t3 = newStartedThread(task); |
565 |
ft.cancel(true); |
await(pleaseCancel); |
566 |
t1.join(); |
checkIsRunning(task); |
567 |
t2.join(); |
task.cancel(mayInterruptIfRunning); |
568 |
} catch(InterruptedException success){ |
checkCancelled(task); |
569 |
unexpectedException(); |
awaitTermination(t1); |
570 |
|
awaitTermination(t2); |
571 |
|
cancelled.countDown(); |
572 |
|
awaitTermination(t3); |
573 |
|
assertEquals(1, task.runCount()); |
574 |
|
assertEquals(1, task.setCount()); |
575 |
|
assertEquals(0, task.setExceptionCount()); |
576 |
|
tryToConfuseDoneTask(task); |
577 |
|
checkCancelled(task); |
578 |
} |
} |
579 |
} |
} |
580 |
|
|
|
|
|
581 |
/** |
/** |
582 |
* A runtime exception in task causes get to throw ExecutionException |
* A runtime exception in task causes get to throw ExecutionException |
583 |
*/ |
*/ |
584 |
public void testGet_ExecutionException() { |
public void testGet_ExecutionException() throws InterruptedException { |
585 |
final FutureTask ft = new FutureTask(new Callable() { |
final ArithmeticException e = new ArithmeticException(); |
586 |
|
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
587 |
public Object call() { |
public Object call() { |
588 |
int i = 5/0; |
throw e; |
589 |
return Boolean.TRUE; |
}}); |
590 |
} |
|
591 |
}); |
task.run(); |
592 |
|
assertEquals(1, task.runCount()); |
593 |
|
assertEquals(0, task.setCount()); |
594 |
|
assertEquals(1, task.setExceptionCount()); |
595 |
try { |
try { |
596 |
ft.run(); |
task.get(); |
|
ft.get(); |
|
597 |
shouldThrow(); |
shouldThrow(); |
598 |
} catch(ExecutionException success){ |
} catch(ExecutionException success){ |
599 |
} |
assertSame(e, success.getCause()); |
600 |
catch(Exception e){ |
tryToConfuseDoneTask(task); |
601 |
unexpectedException(); |
checkCompletedAbnormally(task, success.getCause()); |
602 |
} |
} |
603 |
} |
} |
604 |
|
|
605 |
/** |
/** |
606 |
* A runtime exception in task causes timed get to throw ExecutionException |
* A runtime exception in task causes timed get to throw ExecutionException |
607 |
*/ |
*/ |
608 |
public void testTimedGet_ExecutionException2() { |
public void testTimedGet_ExecutionException2() throws Exception { |
609 |
final FutureTask ft = new FutureTask(new Callable() { |
final ArithmeticException e = new ArithmeticException(); |
610 |
|
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
611 |
public Object call() { |
public Object call() { |
612 |
int i = 5/0; |
throw e; |
613 |
return Boolean.TRUE; |
}}); |
614 |
} |
|
615 |
}); |
task.run(); |
616 |
try { |
try { |
617 |
ft.run(); |
task.get(LONG_DELAY_MS, MILLISECONDS); |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
|
618 |
shouldThrow(); |
shouldThrow(); |
619 |
} catch(ExecutionException success) { |
} catch(ExecutionException success) { |
620 |
} catch(TimeoutException success) { } // unlikely but OK |
assertSame(e, success.getCause()); |
621 |
catch(Exception e){ |
tryToConfuseDoneTask(task); |
622 |
unexpectedException(); |
checkCompletedAbnormally(task, success.getCause()); |
623 |
} |
} |
624 |
} |
} |
625 |
|
|
|
|
|
626 |
/** |
/** |
627 |
* Interrupting a waiting get causes it to throw InterruptedException |
* get is interruptible |
628 |
*/ |
*/ |
629 |
public void testGet_InterruptedException() { |
public void testGet_interruptible() { |
630 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
631 |
Thread t = new Thread(new Runnable() { |
final FutureTask task = new FutureTask(new NoOpCallable()); |
632 |
public void run() { |
Thread t = newStartedThread(new CheckedRunnable() { |
633 |
|
public void realRun() throws Exception { |
634 |
|
Thread.currentThread().interrupt(); |
635 |
try { |
try { |
636 |
ft.get(); |
task.get(); |
637 |
threadShouldThrow(); |
shouldThrow(); |
638 |
} catch(InterruptedException success){ |
} catch (InterruptedException success) {} |
639 |
} catch(Exception e){ |
assertFalse(Thread.interrupted()); |
640 |
threadUnexpectedException(); |
|
641 |
} |
pleaseInterrupt.countDown(); |
|
} |
|
|
}); |
|
642 |
try { |
try { |
643 |
t.start(); |
task.get(); |
644 |
Thread.sleep(SHORT_DELAY_MS); |
shouldThrow(); |
645 |
|
} catch (InterruptedException success) {} |
646 |
|
assertFalse(Thread.interrupted()); |
647 |
|
}}); |
648 |
|
|
649 |
|
await(pleaseInterrupt); |
650 |
t.interrupt(); |
t.interrupt(); |
651 |
t.join(); |
awaitTermination(t); |
652 |
} catch(Exception e){ |
checkNotDone(task); |
|
unexpectedException(); |
|
|
} |
|
653 |
} |
} |
654 |
|
|
655 |
/** |
/** |
656 |
* Interrupting a waiting timed get causes it to throw InterruptedException |
* timed get is interruptible |
657 |
*/ |
*/ |
658 |
public void testTimedGet_InterruptedException2() { |
public void testTimedGet_interruptible() { |
659 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
660 |
Thread t = new Thread(new Runnable() { |
final FutureTask task = new FutureTask(new NoOpCallable()); |
661 |
public void run() { |
Thread t = newStartedThread(new CheckedRunnable() { |
662 |
|
public void realRun() throws Exception { |
663 |
|
Thread.currentThread().interrupt(); |
664 |
try { |
try { |
665 |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
666 |
threadShouldThrow(); |
shouldThrow(); |
667 |
} catch(InterruptedException success){} |
} catch(InterruptedException success){} |
668 |
catch(Exception e){ |
assertFalse(Thread.interrupted()); |
669 |
threadUnexpectedException(); |
|
670 |
} |
pleaseInterrupt.countDown(); |
|
} |
|
|
}); |
|
671 |
try { |
try { |
672 |
t.start(); |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
673 |
Thread.sleep(SHORT_DELAY_MS); |
shouldThrow(); |
674 |
|
} catch (InterruptedException success) {} |
675 |
|
assertFalse(Thread.interrupted()); |
676 |
|
}}); |
677 |
|
|
678 |
|
await(pleaseInterrupt); |
679 |
t.interrupt(); |
t.interrupt(); |
680 |
t.join(); |
awaitTermination(t); |
681 |
} catch(Exception e){ |
checkNotDone(task); |
|
unexpectedException(); |
|
|
} |
|
682 |
} |
} |
683 |
|
|
684 |
/** |
/** |
685 |
* A timed out timed get throws TimeoutException |
* A timed out timed get throws TimeoutException |
686 |
*/ |
*/ |
687 |
public void testGet_TimeoutException() { |
public void testGet_TimeoutException() throws Exception { |
688 |
|
FutureTask task = new FutureTask(new NoOpCallable()); |
689 |
|
long startTime = System.nanoTime(); |
690 |
|
try { |
691 |
|
task.get(timeoutMillis(), MILLISECONDS); |
692 |
|
shouldThrow(); |
693 |
|
} catch (TimeoutException success) { |
694 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
695 |
|
} |
696 |
|
} |
697 |
|
|
698 |
|
/** |
699 |
|
* timed get with null TimeUnit throws NullPointerException |
700 |
|
*/ |
701 |
|
public void testGet_NullTimeUnit() throws Exception { |
702 |
|
FutureTask task = new FutureTask(new NoOpCallable()); |
703 |
|
long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; |
704 |
|
|
705 |
|
for (long timeout : timeouts) { |
706 |
|
try { |
707 |
|
task.get(timeout, null); |
708 |
|
shouldThrow(); |
709 |
|
} catch (NullPointerException success) {} |
710 |
|
} |
711 |
|
|
712 |
|
task.run(); |
713 |
|
|
714 |
|
for (long timeout : timeouts) { |
715 |
try { |
try { |
716 |
FutureTask ft = new FutureTask(new NoOpCallable()); |
task.get(timeout, null); |
|
ft.get(1,TimeUnit.MILLISECONDS); |
|
717 |
shouldThrow(); |
shouldThrow(); |
718 |
} catch(TimeoutException success){} |
} catch (NullPointerException success) {} |
|
catch(Exception success){ |
|
|
unexpectedException(); |
|
719 |
} |
} |
720 |
} |
} |
721 |
|
|