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