[cvs] / jsr166 / src / test / tck / FutureTaskTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/FutureTaskTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.31 - (view) (download)

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.29 assertEquals(0, task.setCount());
323 :     assertEquals(0, task.setExceptionCount());
324 :     tryToConfuseDoneTask(task);
325 : jsr166 1.22 checkCancelled(task);
326 : jsr166 1.29 assertEquals(0, task.runCount());
327 : dl 1.1 }
328 :    
329 : dl 1.5 /**
330 : jsr166 1.29 * cancel(true) before run succeeds
331 : dl 1.5 */
332 : dl 1.1 public void testCancelBeforeRun2() {
333 : jsr166 1.29 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
334 : dl 1.1 assertTrue(task.cancel(true));
335 : jsr166 1.16 task.run();
336 : jsr166 1.29 assertEquals(0, task.setCount());
337 :     assertEquals(0, task.setExceptionCount());
338 :     tryToConfuseDoneTask(task);
339 : jsr166 1.22 checkCancelled(task);
340 : jsr166 1.29 assertEquals(0, task.runCount());
341 : dl 1.1 }
342 :    
343 : dl 1.5 /**
344 : jsr166 1.29 * cancel(false) of a completed task fails
345 : dl 1.5 */
346 : dl 1.1 public void testCancelAfterRun() {
347 : jsr166 1.29 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
348 : jsr166 1.16 task.run();
349 : dl 1.1 assertFalse(task.cancel(false));
350 : jsr166 1.29 assertEquals(1, task.setCount());
351 :     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 : jsr166 1.22 checkCompletedNormally(task, Boolean.TRUE);
368 : jsr166 1.29 assertEquals(1, task.runCount());
369 : dl 1.1 }
370 :    
371 : dl 1.5 /**
372 : jsr166 1.29 * cancel(true) interrupts a running task that subsequently succeeds
373 : dl 1.5 */
374 : jsr166 1.29 public void testCancelInterrupt() {
375 :     final CountDownLatch pleaseCancel = new CountDownLatch(1);
376 :     final PublicFutureTask task =
377 :     new PublicFutureTask(new CheckedRunnable() {
378 :     public void realRun() {
379 :     pleaseCancel.countDown();
380 :     try {
381 :     delay(LONG_DELAY_MS);
382 :     shouldThrow();
383 :     } catch (InterruptedException success) {}
384 : jsr166 1.15 }});
385 :    
386 : jsr166 1.22 Thread t = newStartedThread(task);
387 : jsr166 1.29 await(pleaseCancel);
388 : jsr166 1.15 assertTrue(task.cancel(true));
389 : jsr166 1.29 assertTrue(task.isCancelled());
390 :     awaitTermination(t);
391 :     assertEquals(1, task.runCount());
392 :     assertEquals(1, task.setCount());
393 :     assertEquals(0, task.setExceptionCount());
394 :     tryToConfuseDoneTask(task);
395 : jsr166 1.22 checkCancelled(task);
396 : jsr166 1.29 }
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 : jsr166 1.22 checkCancelled(task);
422 : dl 1.1 }
423 :    
424 : dl 1.5 /**
425 : dl 1.6 * cancel(false) does not interrupt a running task
426 : dl 1.5 */
427 : jsr166 1.29 public void testCancelNoInterrupt() {
428 :     final CountDownLatch pleaseCancel = new CountDownLatch(1);
429 : jsr166 1.22 final CountDownLatch cancelled = new CountDownLatch(1);
430 : jsr166 1.29 final PublicFutureTask task =
431 :     new PublicFutureTask(new CheckedCallable<Boolean>() {
432 :     public Boolean realCall() {
433 :     pleaseCancel.countDown();
434 :     await(cancelled);
435 : jsr166 1.22 assertFalse(Thread.interrupted());
436 : dl 1.1 return Boolean.TRUE;
437 : jsr166 1.15 }});
438 :    
439 : jsr166 1.22 Thread t = newStartedThread(task);
440 : jsr166 1.29 await(pleaseCancel);
441 : jsr166 1.15 assertTrue(task.cancel(false));
442 : jsr166 1.29 assertTrue(task.isCancelled());
443 : jsr166 1.22 cancelled.countDown();
444 : jsr166 1.29 awaitTermination(t);
445 :     assertEquals(1, task.runCount());
446 :     assertEquals(1, task.setCount());
447 :     assertEquals(0, task.setExceptionCount());
448 :     tryToConfuseDoneTask(task);
449 : jsr166 1.22 checkCancelled(task);
450 : dl 1.1 }
451 :    
452 : dl 1.5 /**
453 : jsr166 1.23 * run in one thread causes get in another thread to retrieve value
454 : dl 1.5 */
455 : jsr166 1.29 public void testGetRun() {
456 :     final CountDownLatch pleaseRun = new CountDownLatch(2);
457 : jsr166 1.23
458 :     final PublicFutureTask task =
459 :     new PublicFutureTask(new CheckedCallable<Object>() {
460 : jsr166 1.29 public Object realCall() {
461 :     return two;
462 : jsr166 1.23 }});
463 :    
464 : jsr166 1.29 Thread t1 = newStartedThread(new CheckedRunnable() {
465 : jsr166 1.23 public void realRun() throws Exception {
466 : jsr166 1.29 pleaseRun.countDown();
467 :     assertSame(two, task.get());
468 : jsr166 1.23 }});
469 :    
470 : jsr166 1.29 Thread t2 = newStartedThread(new CheckedRunnable() {
471 : jsr166 1.15 public void realRun() throws Exception {
472 : jsr166 1.29 pleaseRun.countDown();
473 :     assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS));
474 : jsr166 1.15 }});
475 : jsr166 1.12
476 : jsr166 1.29 await(pleaseRun);
477 : jsr166 1.23 checkNotDone(task);
478 : jsr166 1.29 assertTrue(t1.isAlive());
479 :     assertTrue(t2.isAlive());
480 : jsr166 1.22 task.run();
481 : jsr166 1.29 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 : dl 1.1 }
490 :    
491 : dl 1.5 /**
492 : jsr166 1.29 * set in one thread causes get in another thread to retrieve value
493 : jsr166 1.23 */
494 : jsr166 1.29 public void testGetSet() {
495 :     final CountDownLatch pleaseSet = new CountDownLatch(2);
496 : jsr166 1.23
497 :     final PublicFutureTask task =
498 :     new PublicFutureTask(new CheckedCallable<Object>() {
499 :     public Object realCall() throws InterruptedException {
500 : jsr166 1.29 return two;
501 : jsr166 1.23 }});
502 :    
503 : jsr166 1.29 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 : jsr166 1.23 public void realRun() throws Exception {
511 : jsr166 1.29 pleaseSet.countDown();
512 :     assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS));
513 : jsr166 1.23 }});
514 :    
515 : jsr166 1.29 await(pleaseSet);
516 : jsr166 1.23 checkNotDone(task);
517 : jsr166 1.29 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 : jsr166 1.23 }
528 :    
529 :     /**
530 : jsr166 1.21 * Cancelling a task causes timed get in another thread to throw
531 :     * CancellationException
532 : dl 1.5 */
533 : jsr166 1.29 public void testTimedGet_Cancellation() {
534 :     for (final boolean mayInterruptIfRunning :
535 :     new boolean[] { true, false }) {
536 :     final CountDownLatch pleaseCancel = new CountDownLatch(3);
537 :     final CountDownLatch cancelled = new CountDownLatch(1);
538 :     final PublicFutureTask task =
539 :     new PublicFutureTask(new CheckedCallable<Object>() {
540 :     public Object realCall() throws InterruptedException {
541 :     pleaseCancel.countDown();
542 :     if (mayInterruptIfRunning) {
543 :     try {
544 :     delay(2*LONG_DELAY_MS);
545 :     } catch (InterruptedException success) {}
546 :     } else {
547 :     await(cancelled);
548 :     }
549 :     return two;
550 :     }});
551 :    
552 :     Thread t1 = new ThreadShouldThrow(CancellationException.class) {
553 :     public void realRun() throws Exception {
554 :     pleaseCancel.countDown();
555 :     task.get();
556 :     }};
557 :     Thread t2 = new ThreadShouldThrow(CancellationException.class) {
558 :     public void realRun() throws Exception {
559 :     pleaseCancel.countDown();
560 :     task.get(2*LONG_DELAY_MS, MILLISECONDS);
561 :     }};
562 :     t1.start();
563 :     t2.start();
564 :     Thread t3 = newStartedThread(task);
565 :     await(pleaseCancel);
566 :     checkIsRunning(task);
567 :     task.cancel(mayInterruptIfRunning);
568 :     checkCancelled(task);
569 :     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 : dl 1.1 }
580 : jsr166 1.12
581 : dl 1.5 /**
582 : dl 1.6 * A runtime exception in task causes get to throw ExecutionException
583 : dl 1.5 */
584 : jsr166 1.15 public void testGet_ExecutionException() throws InterruptedException {
585 : jsr166 1.29 final ArithmeticException e = new ArithmeticException();
586 :     final PublicFutureTask task = new PublicFutureTask(new Callable() {
587 : jsr166 1.16 public Object call() {
588 : jsr166 1.29 throw e;
589 : jsr166 1.15 }});
590 :    
591 : jsr166 1.22 task.run();
592 : jsr166 1.29 assertEquals(1, task.runCount());
593 :     assertEquals(0, task.setCount());
594 :     assertEquals(1, task.setExceptionCount());
595 : jsr166 1.15 try {
596 : jsr166 1.22 task.get();
597 : jsr166 1.17 shouldThrow();
598 : jsr166 1.16 } catch (ExecutionException success) {
599 : jsr166 1.29 assertSame(e, success.getCause());
600 :     tryToConfuseDoneTask(task);
601 : jsr166 1.22 checkCompletedAbnormally(task, success.getCause());
602 : dl 1.1 }
603 :     }
604 : jsr166 1.12
605 : dl 1.5 /**
606 : jsr166 1.20 * A runtime exception in task causes timed get to throw ExecutionException
607 : dl 1.5 */
608 : jsr166 1.15 public void testTimedGet_ExecutionException2() throws Exception {
609 : jsr166 1.29 final ArithmeticException e = new ArithmeticException();
610 :     final PublicFutureTask task = new PublicFutureTask(new Callable() {
611 : jsr166 1.16 public Object call() {
612 : jsr166 1.29 throw e;
613 : jsr166 1.15 }});
614 :    
615 : jsr166 1.22 task.run();
616 : jsr166 1.16 try {
617 : jsr166 1.29 task.get(LONG_DELAY_MS, MILLISECONDS);
618 : jsr166 1.17 shouldThrow();
619 : jsr166 1.16 } catch (ExecutionException success) {
620 : jsr166 1.29 assertSame(e, success.getCause());
621 :     tryToConfuseDoneTask(task);
622 : jsr166 1.22 checkCompletedAbnormally(task, success.getCause());
623 : jsr166 1.15 }
624 : dl 1.1 }
625 : jsr166 1.12
626 : dl 1.5 /**
627 : jsr166 1.29 * get is interruptible
628 : dl 1.5 */
629 : jsr166 1.29 public void testGet_interruptible() {
630 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
631 : jsr166 1.22 final FutureTask task = new FutureTask(new NoOpCallable());
632 : jsr166 1.29 Thread t = newStartedThread(new CheckedRunnable() {
633 : jsr166 1.15 public void realRun() throws Exception {
634 : jsr166 1.29 Thread.currentThread().interrupt();
635 :     try {
636 :     task.get();
637 :     shouldThrow();
638 :     } catch (InterruptedException success) {}
639 :     assertFalse(Thread.interrupted());
640 :    
641 :     pleaseInterrupt.countDown();
642 :     try {
643 :     task.get();
644 :     shouldThrow();
645 :     } catch (InterruptedException success) {}
646 :     assertFalse(Thread.interrupted());
647 : jsr166 1.15 }});
648 :    
649 : jsr166 1.29 await(pleaseInterrupt);
650 : jsr166 1.15 t.interrupt();
651 : jsr166 1.28 awaitTermination(t);
652 : jsr166 1.22 checkNotDone(task);
653 : dl 1.1 }
654 :    
655 : dl 1.5 /**
656 : jsr166 1.29 * timed get is interruptible
657 : dl 1.5 */
658 : jsr166 1.29 public void testTimedGet_interruptible() {
659 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
660 : jsr166 1.22 final FutureTask task = new FutureTask(new NoOpCallable());
661 : jsr166 1.29 Thread t = newStartedThread(new CheckedRunnable() {
662 : jsr166 1.15 public void realRun() throws Exception {
663 : jsr166 1.29 Thread.currentThread().interrupt();
664 :     try {
665 :     task.get(2*LONG_DELAY_MS, MILLISECONDS);
666 :     shouldThrow();
667 :     } catch (InterruptedException success) {}
668 :     assertFalse(Thread.interrupted());
669 :    
670 :     pleaseInterrupt.countDown();
671 :     try {
672 :     task.get(2*LONG_DELAY_MS, MILLISECONDS);
673 :     shouldThrow();
674 :     } catch (InterruptedException success) {}
675 :     assertFalse(Thread.interrupted());
676 : jsr166 1.15 }});
677 :    
678 : jsr166 1.29 await(pleaseInterrupt);
679 : jsr166 1.15 t.interrupt();
680 : jsr166 1.28 awaitTermination(t);
681 : jsr166 1.22 checkNotDone(task);
682 : dl 1.1 }
683 : jsr166 1.12
684 : dl 1.5 /**
685 : dl 1.6 * A timed out timed get throws TimeoutException
686 : dl 1.5 */
687 : jsr166 1.15 public void testGet_TimeoutException() throws Exception {
688 : jsr166 1.29 FutureTask task = new FutureTask(new NoOpCallable());
689 :     long startTime = System.nanoTime();
690 : jsr166 1.16 try {
691 : jsr166 1.29 task.get(timeoutMillis(), MILLISECONDS);
692 : jsr166 1.17 shouldThrow();
693 : jsr166 1.29 } 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 {
716 :     task.get(timeout, null);
717 :     shouldThrow();
718 :     } catch (NullPointerException success) {}
719 :     }
720 : dl 1.1 }
721 : jsr166 1.12
722 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8