1 |
dl |
1.1 |
/* |
2 |
dl |
1.10 |
* 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 |
jsr166 |
1.24 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.12 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
jsr166 |
1.26 |
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 |
jsr166 |
1.29 |
import java.util.concurrent.atomic.AtomicInteger; |
18 |
jsr166 |
1.15 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
jsr166 |
1.22 |
import static java.util.concurrent.TimeUnit.SECONDS; |
20 |
dl |
1.4 |
import java.util.*; |
21 |
dl |
1.1 |
|
22 |
dl |
1.4 |
public class FutureTaskTest extends JSR166TestCase { |
23 |
dl |
1.1 |
|
24 |
|
|
public static void main(String[] args) { |
25 |
jsr166 |
1.19 |
junit.textui.TestRunner.run(suite()); |
26 |
dl |
1.1 |
} |
27 |
|
|
public static Test suite() { |
28 |
jsr166 |
1.16 |
return new TestSuite(FutureTaskTest.class); |
29 |
dl |
1.1 |
} |
30 |
|
|
|
31 |
jsr166 |
1.29 |
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 |
jsr166 |
1.22 |
void checkNotDone(Future<?> f) { |
55 |
|
|
assertFalse(f.isDone()); |
56 |
|
|
assertFalse(f.isCancelled()); |
57 |
jsr166 |
1.29 |
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 |
jsr166 |
1.22 |
} |
76 |
|
|
|
77 |
|
|
<T> void checkCompletedNormally(Future<T> f, T expected) { |
78 |
jsr166 |
1.29 |
checkIsDone(f); |
79 |
jsr166 |
1.22 |
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 |
jsr166 |
1.29 |
checkIsDone(f); |
91 |
jsr166 |
1.22 |
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 |
jsr166 |
1.29 |
} |
105 |
jsr166 |
1.22 |
|
106 |
jsr166 |
1.29 |
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 |
jsr166 |
1.22 |
} |
113 |
|
|
|
114 |
|
|
void checkCompletedAbnormally(Future<?> f, Throwable t) { |
115 |
jsr166 |
1.29 |
checkIsDone(f); |
116 |
jsr166 |
1.22 |
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 |
dl |
1.4 |
/** |
134 |
|
|
* Subclass to expose protected methods |
135 |
|
|
*/ |
136 |
dl |
1.7 |
static class PublicFutureTask extends FutureTask { |
137 |
jsr166 |
1.29 |
private final AtomicInteger runCount; |
138 |
|
|
private final AtomicInteger doneCount = new AtomicInteger(0); |
139 |
|
|
private final AtomicInteger runAndResetCount = new AtomicInteger(0); |
140 |
|
|
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 |
jsr166 |
1.31 |
public void run() { |
158 |
|
|
runCount.getAndIncrement(); |
159 |
|
|
runnable.run(); |
160 |
|
|
}}, result); |
161 |
jsr166 |
1.29 |
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 |
jsr166 |
1.30 |
@Override public void done() { |
176 |
jsr166 |
1.29 |
assertTrue(isDone()); |
177 |
|
|
doneCount.incrementAndGet(); |
178 |
|
|
super.done(); |
179 |
|
|
} |
180 |
jsr166 |
1.30 |
@Override public boolean runAndReset() { |
181 |
jsr166 |
1.29 |
runAndResetCount.incrementAndGet(); |
182 |
|
|
return super.runAndReset(); |
183 |
|
|
} |
184 |
jsr166 |
1.30 |
@Override public void set(Object x) { |
185 |
jsr166 |
1.29 |
setCount.incrementAndGet(); |
186 |
|
|
super.set(x); |
187 |
|
|
} |
188 |
jsr166 |
1.30 |
@Override public void setException(Throwable t) { |
189 |
jsr166 |
1.29 |
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 |
dl |
1.4 |
} |
201 |
dl |
1.1 |
|
202 |
dl |
1.5 |
/** |
203 |
jsr166 |
1.29 |
* creating a future with a null callable throws NullPointerException |
204 |
dl |
1.5 |
*/ |
205 |
|
|
public void testConstructor() { |
206 |
dl |
1.3 |
try { |
207 |
jsr166 |
1.29 |
new FutureTask(null); |
208 |
jsr166 |
1.17 |
shouldThrow(); |
209 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
210 |
dl |
1.3 |
} |
211 |
|
|
|
212 |
dl |
1.5 |
/** |
213 |
jsr166 |
1.29 |
* creating a future with null runnable throws NullPointerException |
214 |
dl |
1.5 |
*/ |
215 |
|
|
public void testConstructor2() { |
216 |
dl |
1.3 |
try { |
217 |
jsr166 |
1.29 |
new FutureTask(null, Boolean.TRUE); |
218 |
jsr166 |
1.17 |
shouldThrow(); |
219 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
220 |
dl |
1.3 |
} |
221 |
|
|
|
222 |
dl |
1.5 |
/** |
223 |
dl |
1.6 |
* isDone is true when a task completes |
224 |
dl |
1.5 |
*/ |
225 |
|
|
public void testIsDone() { |
226 |
jsr166 |
1.29 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
227 |
|
|
assertFalse(task.isDone()); |
228 |
jsr166 |
1.16 |
task.run(); |
229 |
|
|
assertTrue(task.isDone()); |
230 |
jsr166 |
1.22 |
checkCompletedNormally(task, Boolean.TRUE); |
231 |
jsr166 |
1.29 |
assertEquals(1, task.runCount()); |
232 |
dl |
1.4 |
} |
233 |
|
|
|
234 |
dl |
1.5 |
/** |
235 |
dl |
1.9 |
* runAndReset of a non-cancelled task succeeds |
236 |
dl |
1.5 |
*/ |
237 |
dl |
1.9 |
public void testRunAndReset() { |
238 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
239 |
jsr166 |
1.29 |
for (int i = 0; i < 3; i++) { |
240 |
|
|
assertTrue(task.runAndReset()); |
241 |
|
|
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 |
dl |
1.4 |
} |
248 |
|
|
|
249 |
dl |
1.5 |
/** |
250 |
dl |
1.9 |
* runAndReset after cancellation fails |
251 |
dl |
1.5 |
*/ |
252 |
jsr166 |
1.29 |
public void testRunAndResetAfterCancel() { |
253 |
|
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
254 |
|
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
255 |
|
|
assertTrue(task.cancel(mayInterruptIfRunning)); |
256 |
|
|
for (int i = 0; i < 3; i++) { |
257 |
|
|
assertFalse(task.runAndReset()); |
258 |
|
|
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 |
dl |
1.4 |
} |
267 |
|
|
|
268 |
dl |
1.5 |
/** |
269 |
dl |
1.11 |
* setting value causes get to return it |
270 |
dl |
1.5 |
*/ |
271 |
jsr166 |
1.15 |
public void testSet() throws Exception { |
272 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
273 |
dl |
1.4 |
task.set(one); |
274 |
jsr166 |
1.29 |
for (int i = 0; i < 3; i++) { |
275 |
|
|
assertSame(one, task.get()); |
276 |
|
|
assertSame(one, task.get(LONG_DELAY_MS, MILLISECONDS)); |
277 |
|
|
assertEquals(1, task.setCount()); |
278 |
|
|
} |
279 |
|
|
tryToConfuseDoneTask(task); |
280 |
jsr166 |
1.22 |
checkCompletedNormally(task, one); |
281 |
jsr166 |
1.29 |
assertEquals(0, task.runCount()); |
282 |
dl |
1.4 |
} |
283 |
|
|
|
284 |
dl |
1.5 |
/** |
285 |
dl |
1.6 |
* setException causes get to throw ExecutionException |
286 |
dl |
1.5 |
*/ |
287 |
jsr166 |
1.29 |
public void testSetException_get() throws Exception { |
288 |
dl |
1.4 |
Exception nse = new NoSuchElementException(); |
289 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
290 |
dl |
1.4 |
task.setException(nse); |
291 |
jsr166 |
1.29 |
|
292 |
|
|
try { |
293 |
|
|
task.get(); |
294 |
|
|
shouldThrow(); |
295 |
|
|
} catch (ExecutionException success) { |
296 |
|
|
assertSame(nse, success.getCause()); |
297 |
|
|
checkCompletedAbnormally(task, nse); |
298 |
|
|
} |
299 |
|
|
|
300 |
dl |
1.4 |
try { |
301 |
jsr166 |
1.29 |
task.get(LONG_DELAY_MS, MILLISECONDS); |
302 |
jsr166 |
1.17 |
shouldThrow(); |
303 |
jsr166 |
1.15 |
} catch (ExecutionException success) { |
304 |
jsr166 |
1.29 |
assertSame(nse, success.getCause()); |
305 |
jsr166 |
1.22 |
checkCompletedAbnormally(task, nse); |
306 |
dl |
1.4 |
} |
307 |
jsr166 |
1.29 |
|
308 |
|
|
assertEquals(1, task.setExceptionCount()); |
309 |
|
|
assertEquals(0, task.setCount()); |
310 |
|
|
tryToConfuseDoneTask(task); |
311 |
|
|
checkCompletedAbnormally(task, nse); |
312 |
|
|
assertEquals(0, task.runCount()); |
313 |
dl |
1.4 |
} |
314 |
|
|
|
315 |
dl |
1.5 |
/** |
316 |
jsr166 |
1.29 |
* cancel(false) before run succeeds |
317 |
dl |
1.5 |
*/ |
318 |
dl |
1.1 |
public void testCancelBeforeRun() { |
319 |
jsr166 |
1.29 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
320 |
dl |
1.1 |
assertTrue(task.cancel(false)); |
321 |
jsr166 |
1.16 |
task.run(); |
322 |
jsr166 |
1.32 |
assertEquals(0, task.runCount()); |
323 |
jsr166 |
1.29 |
assertEquals(0, task.setCount()); |
324 |
|
|
assertEquals(0, task.setExceptionCount()); |
325 |
jsr166 |
1.32 |
assertTrue(task.isCancelled()); |
326 |
|
|
assertTrue(task.isDone()); |
327 |
jsr166 |
1.29 |
tryToConfuseDoneTask(task); |
328 |
jsr166 |
1.32 |
assertEquals(0, task.runCount()); |
329 |
jsr166 |
1.22 |
checkCancelled(task); |
330 |
dl |
1.1 |
} |
331 |
|
|
|
332 |
dl |
1.5 |
/** |
333 |
jsr166 |
1.29 |
* cancel(true) before run succeeds |
334 |
dl |
1.5 |
*/ |
335 |
dl |
1.1 |
public void testCancelBeforeRun2() { |
336 |
jsr166 |
1.29 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
337 |
dl |
1.1 |
assertTrue(task.cancel(true)); |
338 |
jsr166 |
1.16 |
task.run(); |
339 |
jsr166 |
1.32 |
assertEquals(0, task.runCount()); |
340 |
jsr166 |
1.29 |
assertEquals(0, task.setCount()); |
341 |
|
|
assertEquals(0, task.setExceptionCount()); |
342 |
jsr166 |
1.32 |
assertTrue(task.isCancelled()); |
343 |
|
|
assertTrue(task.isDone()); |
344 |
jsr166 |
1.29 |
tryToConfuseDoneTask(task); |
345 |
jsr166 |
1.32 |
assertEquals(0, task.runCount()); |
346 |
jsr166 |
1.22 |
checkCancelled(task); |
347 |
dl |
1.1 |
} |
348 |
|
|
|
349 |
dl |
1.5 |
/** |
350 |
jsr166 |
1.29 |
* cancel(false) of a completed task fails |
351 |
dl |
1.5 |
*/ |
352 |
dl |
1.1 |
public void testCancelAfterRun() { |
353 |
jsr166 |
1.29 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
354 |
jsr166 |
1.16 |
task.run(); |
355 |
dl |
1.1 |
assertFalse(task.cancel(false)); |
356 |
jsr166 |
1.32 |
assertEquals(1, task.runCount()); |
357 |
jsr166 |
1.29 |
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) 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 |
jsr166 |
1.32 |
assertEquals(1, task.runCount()); |
372 |
jsr166 |
1.29 |
assertEquals(1, task.setCount()); |
373 |
|
|
assertEquals(0, task.setExceptionCount()); |
374 |
|
|
tryToConfuseDoneTask(task); |
375 |
jsr166 |
1.22 |
checkCompletedNormally(task, Boolean.TRUE); |
376 |
jsr166 |
1.29 |
assertEquals(1, task.runCount()); |
377 |
dl |
1.1 |
} |
378 |
|
|
|
379 |
dl |
1.5 |
/** |
380 |
jsr166 |
1.29 |
* cancel(true) interrupts a running task that subsequently succeeds |
381 |
dl |
1.5 |
*/ |
382 |
jsr166 |
1.29 |
public void testCancelInterrupt() { |
383 |
|
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
384 |
|
|
final PublicFutureTask task = |
385 |
|
|
new PublicFutureTask(new CheckedRunnable() { |
386 |
|
|
public void realRun() { |
387 |
|
|
pleaseCancel.countDown(); |
388 |
|
|
try { |
389 |
|
|
delay(LONG_DELAY_MS); |
390 |
|
|
shouldThrow(); |
391 |
|
|
} catch (InterruptedException success) {} |
392 |
jsr166 |
1.15 |
}}); |
393 |
|
|
|
394 |
jsr166 |
1.22 |
Thread t = newStartedThread(task); |
395 |
jsr166 |
1.29 |
await(pleaseCancel); |
396 |
jsr166 |
1.15 |
assertTrue(task.cancel(true)); |
397 |
jsr166 |
1.29 |
assertTrue(task.isCancelled()); |
398 |
jsr166 |
1.32 |
assertTrue(task.isDone()); |
399 |
jsr166 |
1.29 |
awaitTermination(t); |
400 |
|
|
assertEquals(1, task.runCount()); |
401 |
|
|
assertEquals(1, task.setCount()); |
402 |
|
|
assertEquals(0, task.setExceptionCount()); |
403 |
|
|
tryToConfuseDoneTask(task); |
404 |
jsr166 |
1.22 |
checkCancelled(task); |
405 |
jsr166 |
1.29 |
} |
406 |
|
|
|
407 |
|
|
/** |
408 |
jsr166 |
1.33 |
* cancel(true) tries to interrupt a running task, but |
409 |
|
|
* Thread.interrupt throws (simulating a restrictive security |
410 |
|
|
* manager) |
411 |
jsr166 |
1.32 |
*/ |
412 |
|
|
public void testCancelInterrupt_ThrowsSecurityException() { |
413 |
|
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
414 |
|
|
final CountDownLatch cancelled = new CountDownLatch(1); |
415 |
|
|
final PublicFutureTask task = |
416 |
|
|
new PublicFutureTask(new CheckedRunnable() { |
417 |
|
|
public void realRun() { |
418 |
|
|
pleaseCancel.countDown(); |
419 |
|
|
await(cancelled); |
420 |
|
|
assertFalse(Thread.interrupted()); |
421 |
|
|
}}); |
422 |
|
|
|
423 |
jsr166 |
1.33 |
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(); |
430 |
|
|
|
431 |
jsr166 |
1.32 |
await(pleaseCancel); |
432 |
|
|
try { |
433 |
jsr166 |
1.33 |
task.cancel(true); |
434 |
|
|
shouldThrow(); |
435 |
|
|
} catch (SecurityException expected) {} |
436 |
|
|
|
437 |
|
|
// We failed to deliver the interrupt, but the world retains |
438 |
|
|
// its sanity, as if we had done task.cancel(false) |
439 |
jsr166 |
1.32 |
assertTrue(task.isCancelled()); |
440 |
|
|
assertTrue(task.isDone()); |
441 |
|
|
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 |
jsr166 |
1.29 |
* cancel(true) interrupts a running task that subsequently throws |
455 |
|
|
*/ |
456 |
|
|
public void testCancelInterrupt_taskFails() { |
457 |
|
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
458 |
|
|
final PublicFutureTask task = |
459 |
|
|
new PublicFutureTask(new Runnable() { |
460 |
|
|
public void run() { |
461 |
|
|
try { |
462 |
|
|
pleaseCancel.countDown(); |
463 |
|
|
delay(LONG_DELAY_MS); |
464 |
jsr166 |
1.35 |
shouldThrow(); |
465 |
|
|
} catch (Throwable t) { |
466 |
|
|
assertTrue(t instanceof InterruptedException); |
467 |
|
|
} |
468 |
|
|
throw new RuntimeException(); |
469 |
jsr166 |
1.29 |
}}); |
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 |
jsr166 |
1.22 |
checkCancelled(task); |
481 |
dl |
1.1 |
} |
482 |
|
|
|
483 |
dl |
1.5 |
/** |
484 |
dl |
1.6 |
* cancel(false) does not interrupt a running task |
485 |
dl |
1.5 |
*/ |
486 |
jsr166 |
1.29 |
public void testCancelNoInterrupt() { |
487 |
|
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
488 |
jsr166 |
1.22 |
final CountDownLatch cancelled = new CountDownLatch(1); |
489 |
jsr166 |
1.29 |
final PublicFutureTask task = |
490 |
|
|
new PublicFutureTask(new CheckedCallable<Boolean>() { |
491 |
|
|
public Boolean realCall() { |
492 |
|
|
pleaseCancel.countDown(); |
493 |
|
|
await(cancelled); |
494 |
jsr166 |
1.22 |
assertFalse(Thread.interrupted()); |
495 |
dl |
1.1 |
return Boolean.TRUE; |
496 |
jsr166 |
1.15 |
}}); |
497 |
|
|
|
498 |
jsr166 |
1.22 |
Thread t = newStartedThread(task); |
499 |
jsr166 |
1.29 |
await(pleaseCancel); |
500 |
jsr166 |
1.15 |
assertTrue(task.cancel(false)); |
501 |
jsr166 |
1.29 |
assertTrue(task.isCancelled()); |
502 |
jsr166 |
1.22 |
cancelled.countDown(); |
503 |
jsr166 |
1.29 |
awaitTermination(t); |
504 |
|
|
assertEquals(1, task.runCount()); |
505 |
|
|
assertEquals(1, task.setCount()); |
506 |
|
|
assertEquals(0, task.setExceptionCount()); |
507 |
|
|
tryToConfuseDoneTask(task); |
508 |
jsr166 |
1.22 |
checkCancelled(task); |
509 |
dl |
1.1 |
} |
510 |
|
|
|
511 |
dl |
1.5 |
/** |
512 |
jsr166 |
1.23 |
* run in one thread causes get in another thread to retrieve value |
513 |
dl |
1.5 |
*/ |
514 |
jsr166 |
1.29 |
public void testGetRun() { |
515 |
|
|
final CountDownLatch pleaseRun = new CountDownLatch(2); |
516 |
jsr166 |
1.23 |
|
517 |
|
|
final PublicFutureTask task = |
518 |
|
|
new PublicFutureTask(new CheckedCallable<Object>() { |
519 |
jsr166 |
1.29 |
public Object realCall() { |
520 |
|
|
return two; |
521 |
jsr166 |
1.23 |
}}); |
522 |
|
|
|
523 |
jsr166 |
1.29 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
524 |
jsr166 |
1.23 |
public void realRun() throws Exception { |
525 |
jsr166 |
1.29 |
pleaseRun.countDown(); |
526 |
|
|
assertSame(two, task.get()); |
527 |
jsr166 |
1.23 |
}}); |
528 |
|
|
|
529 |
jsr166 |
1.29 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
530 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
531 |
jsr166 |
1.29 |
pleaseRun.countDown(); |
532 |
|
|
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
533 |
jsr166 |
1.15 |
}}); |
534 |
jsr166 |
1.12 |
|
535 |
jsr166 |
1.29 |
await(pleaseRun); |
536 |
jsr166 |
1.23 |
checkNotDone(task); |
537 |
jsr166 |
1.29 |
assertTrue(t1.isAlive()); |
538 |
|
|
assertTrue(t2.isAlive()); |
539 |
jsr166 |
1.22 |
task.run(); |
540 |
jsr166 |
1.29 |
checkCompletedNormally(task, two); |
541 |
|
|
assertEquals(1, task.runCount()); |
542 |
|
|
assertEquals(1, task.setCount()); |
543 |
|
|
assertEquals(0, task.setExceptionCount()); |
544 |
|
|
awaitTermination(t1); |
545 |
|
|
awaitTermination(t2); |
546 |
|
|
tryToConfuseDoneTask(task); |
547 |
|
|
checkCompletedNormally(task, two); |
548 |
dl |
1.1 |
} |
549 |
|
|
|
550 |
dl |
1.5 |
/** |
551 |
jsr166 |
1.29 |
* set in one thread causes get in another thread to retrieve value |
552 |
jsr166 |
1.23 |
*/ |
553 |
jsr166 |
1.29 |
public void testGetSet() { |
554 |
|
|
final CountDownLatch pleaseSet = new CountDownLatch(2); |
555 |
jsr166 |
1.23 |
|
556 |
|
|
final PublicFutureTask task = |
557 |
|
|
new PublicFutureTask(new CheckedCallable<Object>() { |
558 |
|
|
public Object realCall() throws InterruptedException { |
559 |
jsr166 |
1.29 |
return two; |
560 |
jsr166 |
1.23 |
}}); |
561 |
|
|
|
562 |
jsr166 |
1.29 |
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 |
jsr166 |
1.23 |
public void realRun() throws Exception { |
570 |
jsr166 |
1.29 |
pleaseSet.countDown(); |
571 |
|
|
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
572 |
jsr166 |
1.23 |
}}); |
573 |
|
|
|
574 |
jsr166 |
1.29 |
await(pleaseSet); |
575 |
jsr166 |
1.23 |
checkNotDone(task); |
576 |
jsr166 |
1.29 |
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 |
jsr166 |
1.23 |
} |
587 |
|
|
|
588 |
|
|
/** |
589 |
jsr166 |
1.21 |
* Cancelling a task causes timed get in another thread to throw |
590 |
|
|
* CancellationException |
591 |
dl |
1.5 |
*/ |
592 |
jsr166 |
1.29 |
public void testTimedGet_Cancellation() { |
593 |
|
|
for (final boolean mayInterruptIfRunning : |
594 |
|
|
new boolean[] { true, false }) { |
595 |
|
|
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
596 |
|
|
final CountDownLatch cancelled = new CountDownLatch(1); |
597 |
|
|
final PublicFutureTask task = |
598 |
|
|
new PublicFutureTask(new CheckedCallable<Object>() { |
599 |
|
|
public Object realCall() throws InterruptedException { |
600 |
|
|
pleaseCancel.countDown(); |
601 |
|
|
if (mayInterruptIfRunning) { |
602 |
|
|
try { |
603 |
|
|
delay(2*LONG_DELAY_MS); |
604 |
|
|
} catch (InterruptedException success) {} |
605 |
|
|
} else { |
606 |
|
|
await(cancelled); |
607 |
|
|
} |
608 |
|
|
return two; |
609 |
|
|
}}); |
610 |
|
|
|
611 |
|
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
612 |
|
|
public void realRun() throws Exception { |
613 |
|
|
pleaseCancel.countDown(); |
614 |
|
|
task.get(); |
615 |
|
|
}}; |
616 |
|
|
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
617 |
|
|
public void realRun() throws Exception { |
618 |
|
|
pleaseCancel.countDown(); |
619 |
|
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
620 |
|
|
}}; |
621 |
|
|
t1.start(); |
622 |
|
|
t2.start(); |
623 |
|
|
Thread t3 = newStartedThread(task); |
624 |
|
|
await(pleaseCancel); |
625 |
|
|
checkIsRunning(task); |
626 |
|
|
task.cancel(mayInterruptIfRunning); |
627 |
|
|
checkCancelled(task); |
628 |
|
|
awaitTermination(t1); |
629 |
|
|
awaitTermination(t2); |
630 |
|
|
cancelled.countDown(); |
631 |
|
|
awaitTermination(t3); |
632 |
|
|
assertEquals(1, task.runCount()); |
633 |
|
|
assertEquals(1, task.setCount()); |
634 |
|
|
assertEquals(0, task.setExceptionCount()); |
635 |
|
|
tryToConfuseDoneTask(task); |
636 |
|
|
checkCancelled(task); |
637 |
|
|
} |
638 |
dl |
1.1 |
} |
639 |
jsr166 |
1.12 |
|
640 |
dl |
1.5 |
/** |
641 |
dl |
1.6 |
* A runtime exception in task causes get to throw ExecutionException |
642 |
dl |
1.5 |
*/ |
643 |
jsr166 |
1.15 |
public void testGet_ExecutionException() throws InterruptedException { |
644 |
jsr166 |
1.29 |
final ArithmeticException e = new ArithmeticException(); |
645 |
|
|
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
646 |
jsr166 |
1.16 |
public Object call() { |
647 |
jsr166 |
1.29 |
throw e; |
648 |
jsr166 |
1.15 |
}}); |
649 |
|
|
|
650 |
jsr166 |
1.22 |
task.run(); |
651 |
jsr166 |
1.29 |
assertEquals(1, task.runCount()); |
652 |
|
|
assertEquals(0, task.setCount()); |
653 |
|
|
assertEquals(1, task.setExceptionCount()); |
654 |
jsr166 |
1.15 |
try { |
655 |
jsr166 |
1.22 |
task.get(); |
656 |
jsr166 |
1.17 |
shouldThrow(); |
657 |
jsr166 |
1.16 |
} catch (ExecutionException success) { |
658 |
jsr166 |
1.29 |
assertSame(e, success.getCause()); |
659 |
|
|
tryToConfuseDoneTask(task); |
660 |
jsr166 |
1.22 |
checkCompletedAbnormally(task, success.getCause()); |
661 |
dl |
1.1 |
} |
662 |
|
|
} |
663 |
jsr166 |
1.12 |
|
664 |
dl |
1.5 |
/** |
665 |
jsr166 |
1.20 |
* A runtime exception in task causes timed get to throw ExecutionException |
666 |
dl |
1.5 |
*/ |
667 |
jsr166 |
1.15 |
public void testTimedGet_ExecutionException2() throws Exception { |
668 |
jsr166 |
1.29 |
final ArithmeticException e = new ArithmeticException(); |
669 |
|
|
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
670 |
jsr166 |
1.16 |
public Object call() { |
671 |
jsr166 |
1.29 |
throw e; |
672 |
jsr166 |
1.15 |
}}); |
673 |
|
|
|
674 |
jsr166 |
1.22 |
task.run(); |
675 |
jsr166 |
1.16 |
try { |
676 |
jsr166 |
1.29 |
task.get(LONG_DELAY_MS, MILLISECONDS); |
677 |
jsr166 |
1.17 |
shouldThrow(); |
678 |
jsr166 |
1.16 |
} catch (ExecutionException success) { |
679 |
jsr166 |
1.29 |
assertSame(e, success.getCause()); |
680 |
|
|
tryToConfuseDoneTask(task); |
681 |
jsr166 |
1.22 |
checkCompletedAbnormally(task, success.getCause()); |
682 |
jsr166 |
1.15 |
} |
683 |
dl |
1.1 |
} |
684 |
jsr166 |
1.12 |
|
685 |
dl |
1.5 |
/** |
686 |
jsr166 |
1.29 |
* get is interruptible |
687 |
dl |
1.5 |
*/ |
688 |
jsr166 |
1.29 |
public void testGet_interruptible() { |
689 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
690 |
jsr166 |
1.22 |
final FutureTask task = new FutureTask(new NoOpCallable()); |
691 |
jsr166 |
1.29 |
Thread t = newStartedThread(new CheckedRunnable() { |
692 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
693 |
jsr166 |
1.29 |
Thread.currentThread().interrupt(); |
694 |
|
|
try { |
695 |
|
|
task.get(); |
696 |
|
|
shouldThrow(); |
697 |
|
|
} catch (InterruptedException success) {} |
698 |
|
|
assertFalse(Thread.interrupted()); |
699 |
|
|
|
700 |
|
|
pleaseInterrupt.countDown(); |
701 |
|
|
try { |
702 |
|
|
task.get(); |
703 |
|
|
shouldThrow(); |
704 |
|
|
} catch (InterruptedException success) {} |
705 |
|
|
assertFalse(Thread.interrupted()); |
706 |
jsr166 |
1.15 |
}}); |
707 |
|
|
|
708 |
jsr166 |
1.29 |
await(pleaseInterrupt); |
709 |
jsr166 |
1.15 |
t.interrupt(); |
710 |
jsr166 |
1.28 |
awaitTermination(t); |
711 |
jsr166 |
1.22 |
checkNotDone(task); |
712 |
dl |
1.1 |
} |
713 |
|
|
|
714 |
dl |
1.5 |
/** |
715 |
jsr166 |
1.29 |
* timed get is interruptible |
716 |
dl |
1.5 |
*/ |
717 |
jsr166 |
1.29 |
public void testTimedGet_interruptible() { |
718 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
719 |
jsr166 |
1.22 |
final FutureTask task = new FutureTask(new NoOpCallable()); |
720 |
jsr166 |
1.29 |
Thread t = newStartedThread(new CheckedRunnable() { |
721 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
722 |
jsr166 |
1.29 |
Thread.currentThread().interrupt(); |
723 |
|
|
try { |
724 |
|
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
725 |
|
|
shouldThrow(); |
726 |
|
|
} catch (InterruptedException success) {} |
727 |
|
|
assertFalse(Thread.interrupted()); |
728 |
|
|
|
729 |
|
|
pleaseInterrupt.countDown(); |
730 |
|
|
try { |
731 |
|
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
732 |
|
|
shouldThrow(); |
733 |
|
|
} catch (InterruptedException success) {} |
734 |
|
|
assertFalse(Thread.interrupted()); |
735 |
jsr166 |
1.15 |
}}); |
736 |
|
|
|
737 |
jsr166 |
1.29 |
await(pleaseInterrupt); |
738 |
jsr166 |
1.15 |
t.interrupt(); |
739 |
jsr166 |
1.28 |
awaitTermination(t); |
740 |
jsr166 |
1.22 |
checkNotDone(task); |
741 |
dl |
1.1 |
} |
742 |
jsr166 |
1.12 |
|
743 |
dl |
1.5 |
/** |
744 |
dl |
1.6 |
* A timed out timed get throws TimeoutException |
745 |
dl |
1.5 |
*/ |
746 |
jsr166 |
1.15 |
public void testGet_TimeoutException() throws Exception { |
747 |
jsr166 |
1.29 |
FutureTask task = new FutureTask(new NoOpCallable()); |
748 |
|
|
long startTime = System.nanoTime(); |
749 |
jsr166 |
1.16 |
try { |
750 |
jsr166 |
1.29 |
task.get(timeoutMillis(), MILLISECONDS); |
751 |
jsr166 |
1.17 |
shouldThrow(); |
752 |
jsr166 |
1.29 |
} catch (TimeoutException success) { |
753 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
754 |
|
|
} |
755 |
|
|
} |
756 |
|
|
|
757 |
|
|
/** |
758 |
|
|
* timed get with null TimeUnit throws NullPointerException |
759 |
|
|
*/ |
760 |
|
|
public void testGet_NullTimeUnit() throws Exception { |
761 |
|
|
FutureTask task = new FutureTask(new NoOpCallable()); |
762 |
|
|
long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; |
763 |
|
|
|
764 |
|
|
for (long timeout : timeouts) { |
765 |
|
|
try { |
766 |
|
|
task.get(timeout, null); |
767 |
|
|
shouldThrow(); |
768 |
|
|
} catch (NullPointerException success) {} |
769 |
|
|
} |
770 |
|
|
|
771 |
|
|
task.run(); |
772 |
|
|
|
773 |
|
|
for (long timeout : timeouts) { |
774 |
|
|
try { |
775 |
|
|
task.get(timeout, null); |
776 |
|
|
shouldThrow(); |
777 |
|
|
} catch (NullPointerException success) {} |
778 |
|
|
} |
779 |
dl |
1.1 |
} |
780 |
jsr166 |
1.12 |
|
781 |
dl |
1.1 |
} |