[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.23 - (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 :     * http://creativecommons.org/licenses/publicdomain
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 :     import java.util.concurrent.*;
11 : jsr166 1.15 import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 : jsr166 1.22 import static java.util.concurrent.TimeUnit.SECONDS;
13 : dl 1.4 import java.util.*;
14 : dl 1.1
15 : dl 1.4 public class FutureTaskTest extends JSR166TestCase {
16 : dl 1.1
17 :     public static void main(String[] args) {
18 : jsr166 1.19 junit.textui.TestRunner.run(suite());
19 : dl 1.1 }
20 :     public static Test suite() {
21 : jsr166 1.16 return new TestSuite(FutureTaskTest.class);
22 : dl 1.1 }
23 :    
24 : jsr166 1.22 void checkNotDone(Future<?> f) {
25 :     assertFalse(f.isDone());
26 :     assertFalse(f.isCancelled());
27 :     }
28 :    
29 :     <T> void checkCompletedNormally(Future<T> f, T expected) {
30 :     assertTrue(f.isDone());
31 :     assertFalse(f.isCancelled());
32 :    
33 :     try {
34 :     assertSame(expected, f.get());
35 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
36 :     try {
37 :     assertSame(expected, f.get(5L, SECONDS));
38 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
39 :    
40 :     assertFalse(f.cancel(false));
41 :     assertFalse(f.cancel(true));
42 :     }
43 :    
44 :     void checkCancelled(Future<?> f) {
45 :     assertTrue(f.isDone());
46 :     assertTrue(f.isCancelled());
47 :    
48 :     try {
49 :     f.get();
50 :     shouldThrow();
51 :     } catch (CancellationException success) {
52 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
53 :    
54 :     try {
55 :     f.get(5L, SECONDS);
56 :     shouldThrow();
57 :     } catch (CancellationException success) {
58 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
59 :    
60 :     assertFalse(f.cancel(false));
61 :     assertFalse(f.cancel(true));
62 :     }
63 :    
64 :     void checkCompletedAbnormally(Future<?> f, Throwable t) {
65 :     assertTrue(f.isDone());
66 :     assertFalse(f.isCancelled());
67 :    
68 :     try {
69 :     f.get();
70 :     shouldThrow();
71 :     } catch (ExecutionException success) {
72 :     assertSame(t, success.getCause());
73 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
74 :    
75 :     try {
76 :     f.get(5L, SECONDS);
77 :     shouldThrow();
78 :     } catch (ExecutionException success) {
79 :     assertSame(t, success.getCause());
80 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
81 :    
82 :     assertFalse(f.cancel(false));
83 :     assertFalse(f.cancel(true));
84 :     }
85 :    
86 : dl 1.4 /**
87 :     * Subclass to expose protected methods
88 :     */
89 : dl 1.7 static class PublicFutureTask extends FutureTask {
90 :     public PublicFutureTask(Callable r) { super(r); }
91 : dl 1.9 public boolean runAndReset() { return super.runAndReset(); }
92 : dl 1.4 public void set(Object x) { super.set(x); }
93 :     public void setException(Throwable t) { super.setException(t); }
94 :     }
95 : dl 1.1
96 : dl 1.5 /**
97 : dl 1.6 * Creating a future with a null callable throws NPE
98 : dl 1.5 */
99 :     public void testConstructor() {
100 : dl 1.3 try {
101 :     FutureTask task = new FutureTask(null);
102 : jsr166 1.17 shouldThrow();
103 : jsr166 1.15 } catch (NullPointerException success) {}
104 : dl 1.3 }
105 :    
106 : dl 1.5 /**
107 : dl 1.6 * creating a future with null runnable fails
108 : dl 1.5 */
109 :     public void testConstructor2() {
110 : dl 1.3 try {
111 :     FutureTask task = new FutureTask(null, Boolean.TRUE);
112 : jsr166 1.17 shouldThrow();
113 : jsr166 1.15 } catch (NullPointerException success) {}
114 : dl 1.3 }
115 :    
116 : dl 1.5 /**
117 : dl 1.6 * isDone is true when a task completes
118 : dl 1.5 */
119 :     public void testIsDone() {
120 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
121 : jsr166 1.16 task.run();
122 :     assertTrue(task.isDone());
123 : jsr166 1.22 checkCompletedNormally(task, Boolean.TRUE);
124 : dl 1.4 }
125 :    
126 : dl 1.5 /**
127 : dl 1.9 * runAndReset of a non-cancelled task succeeds
128 : dl 1.5 */
129 : dl 1.9 public void testRunAndReset() {
130 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
131 : jsr166 1.16 assertTrue(task.runAndReset());
132 : jsr166 1.22 checkNotDone(task);
133 : dl 1.4 }
134 :    
135 : dl 1.5 /**
136 : dl 1.9 * runAndReset after cancellation fails
137 : dl 1.5 */
138 : dl 1.4 public void testResetAfterCancel() {
139 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
140 : dl 1.4 assertTrue(task.cancel(false));
141 : jsr166 1.16 assertFalse(task.runAndReset());
142 : jsr166 1.22 checkCancelled(task);
143 : dl 1.4 }
144 :    
145 : dl 1.1
146 : dl 1.5 /**
147 : dl 1.11 * setting value causes get to return it
148 : dl 1.5 */
149 : jsr166 1.15 public void testSet() throws Exception {
150 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
151 : dl 1.4 task.set(one);
152 : jsr166 1.18 assertSame(task.get(), one);
153 : jsr166 1.22 checkCompletedNormally(task, one);
154 : dl 1.4 }
155 :    
156 : dl 1.5 /**
157 : dl 1.6 * setException causes get to throw ExecutionException
158 : dl 1.5 */
159 : jsr166 1.15 public void testSetException() throws Exception {
160 : dl 1.4 Exception nse = new NoSuchElementException();
161 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
162 : dl 1.4 task.setException(nse);
163 :     try {
164 :     Object x = task.get();
165 : jsr166 1.17 shouldThrow();
166 : jsr166 1.15 } catch (ExecutionException success) {
167 :     assertSame(success.getCause(), nse);
168 : jsr166 1.22 checkCompletedAbnormally(task, nse);
169 : dl 1.4 }
170 :     }
171 :    
172 : dl 1.5 /**
173 : jsr166 1.21 * Cancelling before running succeeds
174 : dl 1.5 */
175 : dl 1.1 public void testCancelBeforeRun() {
176 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
177 : dl 1.1 assertTrue(task.cancel(false));
178 : jsr166 1.16 task.run();
179 : jsr166 1.22 checkCancelled(task);
180 : dl 1.1 }
181 :    
182 : dl 1.5 /**
183 : dl 1.6 * Cancel(true) before run succeeds
184 : dl 1.5 */
185 : dl 1.1 public void testCancelBeforeRun2() {
186 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
187 : dl 1.1 assertTrue(task.cancel(true));
188 : jsr166 1.16 task.run();
189 : jsr166 1.22 checkCancelled(task);
190 : dl 1.1 }
191 :    
192 : dl 1.5 /**
193 : dl 1.6 * cancel of a completed task fails
194 : dl 1.5 */
195 : dl 1.1 public void testCancelAfterRun() {
196 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
197 : jsr166 1.16 task.run();
198 : dl 1.1 assertFalse(task.cancel(false));
199 : jsr166 1.22 checkCompletedNormally(task, Boolean.TRUE);
200 : dl 1.1 }
201 :    
202 : dl 1.5 /**
203 : dl 1.6 * cancel(true) interrupts a running task
204 : dl 1.5 */
205 : jsr166 1.15 public void testCancelInterrupt() throws InterruptedException {
206 : jsr166 1.22 final CountDownLatch threadStarted = new CountDownLatch(1);
207 : jsr166 1.16 final FutureTask task =
208 : jsr166 1.22 new FutureTask(new CheckedCallable<Object>() {
209 :     public Object realCall() {
210 :     threadStarted.countDown();
211 :     long t0 = System.nanoTime();
212 :     for (;;) {
213 :     if (Thread.interrupted())
214 :     return Boolean.TRUE;
215 :     if (millisElapsedSince(t0) > MEDIUM_DELAY_MS)
216 :     fail("interrupt not delivered");
217 :     Thread.yield();
218 :     }
219 : jsr166 1.15 }});
220 :    
221 : jsr166 1.22 Thread t = newStartedThread(task);
222 :     threadStarted.await();
223 : jsr166 1.15 assertTrue(task.cancel(true));
224 : jsr166 1.22 checkCancelled(task);
225 :     awaitTermination(t, MEDIUM_DELAY_MS);
226 :     checkCancelled(task);
227 : dl 1.1 }
228 :    
229 : dl 1.5 /**
230 : dl 1.6 * cancel(false) does not interrupt a running task
231 : dl 1.5 */
232 : jsr166 1.15 public void testCancelNoInterrupt() throws InterruptedException {
233 : jsr166 1.22 final CountDownLatch threadStarted = new CountDownLatch(1);
234 :     final CountDownLatch cancelled = new CountDownLatch(1);
235 :     final FutureTask<Boolean> task =
236 :     new FutureTask<Boolean>(new CheckedCallable<Boolean>() {
237 :     public Boolean realCall() throws InterruptedException {
238 :     threadStarted.countDown();
239 :     cancelled.await(MEDIUM_DELAY_MS, MILLISECONDS);
240 :     assertFalse(Thread.interrupted());
241 : dl 1.1 return Boolean.TRUE;
242 : jsr166 1.15 }});
243 :    
244 : jsr166 1.22 Thread t = newStartedThread(task);
245 :     threadStarted.await();
246 : jsr166 1.15 assertTrue(task.cancel(false));
247 : jsr166 1.22 checkCancelled(task);
248 :     cancelled.countDown();
249 :     awaitTermination(t, MEDIUM_DELAY_MS);
250 :     checkCancelled(task);
251 : dl 1.1 }
252 :    
253 : dl 1.5 /**
254 : jsr166 1.23 * run in one thread causes get in another thread to retrieve value
255 : dl 1.5 */
256 : jsr166 1.23 public void testGetRun() throws InterruptedException {
257 :     final CountDownLatch threadStarted = new CountDownLatch(1);
258 :    
259 : jsr166 1.22 final FutureTask task =
260 : jsr166 1.15 new FutureTask(new CheckedCallable<Object>() {
261 : jsr166 1.16 public Object realCall() throws InterruptedException {
262 : dl 1.1 return Boolean.TRUE;
263 : jsr166 1.15 }});
264 : jsr166 1.22
265 :     Thread t = newStartedThread(new CheckedRunnable() {
266 : jsr166 1.15 public void realRun() throws Exception {
267 : jsr166 1.23 threadStarted.countDown();
268 : jsr166 1.22 assertSame(Boolean.TRUE, task.get());
269 : jsr166 1.15 }});
270 : dl 1.1
271 : jsr166 1.23 threadStarted.await();
272 :     checkNotDone(task);
273 :     assertTrue(t.isAlive());
274 : jsr166 1.22 task.run();
275 :     checkCompletedNormally(task, Boolean.TRUE);
276 :     awaitTermination(t, MEDIUM_DELAY_MS);
277 : dl 1.1 }
278 :    
279 : dl 1.5 /**
280 : jsr166 1.23 * set in one thread causes get in another thread to retrieve value
281 :     */
282 :     public void testGetSet() throws InterruptedException {
283 :     final CountDownLatch threadStarted = new CountDownLatch(1);
284 :    
285 :     final PublicFutureTask task =
286 :     new PublicFutureTask(new CheckedCallable<Object>() {
287 :     public Object realCall() throws InterruptedException {
288 :     return Boolean.TRUE;
289 :     }});
290 :    
291 :     Thread t = newStartedThread(new CheckedRunnable() {
292 :     public void realRun() throws Exception {
293 :     threadStarted.countDown();
294 :     assertSame(Boolean.FALSE, task.get());
295 :     }});
296 :    
297 :     threadStarted.await();
298 :     checkNotDone(task);
299 :     assertTrue(t.isAlive());
300 :     task.set(Boolean.FALSE);
301 :     checkCompletedNormally(task, Boolean.FALSE);
302 :     awaitTermination(t, MEDIUM_DELAY_MS);
303 :     }
304 :    
305 :     /**
306 :     * run in one thread causes timed get in another thread to retrieve value
307 : dl 1.5 */
308 : jsr166 1.23 public void testTimedGetRun() throws InterruptedException {
309 :     final CountDownLatch threadStarted = new CountDownLatch(1);
310 :    
311 : jsr166 1.22 final FutureTask task =
312 : jsr166 1.15 new FutureTask(new CheckedCallable<Object>() {
313 : jsr166 1.16 public Object realCall() throws InterruptedException {
314 : dl 1.1 return Boolean.TRUE;
315 : jsr166 1.15 }});
316 : jsr166 1.22
317 :     Thread t = newStartedThread(new CheckedRunnable() {
318 : jsr166 1.15 public void realRun() throws Exception {
319 : jsr166 1.23 threadStarted.countDown();
320 :     assertSame(Boolean.TRUE,
321 :     task.get(MEDIUM_DELAY_MS, MILLISECONDS));
322 : jsr166 1.15 }});
323 : jsr166 1.12
324 : jsr166 1.23 threadStarted.await();
325 :     checkNotDone(task);
326 :     assertTrue(t.isAlive());
327 : jsr166 1.22 task.run();
328 :     checkCompletedNormally(task, Boolean.TRUE);
329 :     awaitTermination(t, MEDIUM_DELAY_MS);
330 : dl 1.1 }
331 :    
332 : dl 1.5 /**
333 : jsr166 1.23 * set in one thread causes timed get in another thread to retrieve value
334 :     */
335 :     public void testTimedGetSet() throws InterruptedException {
336 :     final CountDownLatch threadStarted = new CountDownLatch(1);
337 :    
338 :     final PublicFutureTask task =
339 :     new PublicFutureTask(new CheckedCallable<Object>() {
340 :     public Object realCall() throws InterruptedException {
341 :     return Boolean.TRUE;
342 :     }});
343 :    
344 :     Thread t = newStartedThread(new CheckedRunnable() {
345 :     public void realRun() throws Exception {
346 :     threadStarted.countDown();
347 :     assertSame(Boolean.FALSE,
348 :     task.get(MEDIUM_DELAY_MS, MILLISECONDS));
349 :     }});
350 :    
351 :     threadStarted.await();
352 :     checkNotDone(task);
353 :     assertTrue(t.isAlive());
354 :     task.set(Boolean.FALSE);
355 :     checkCompletedNormally(task, Boolean.FALSE);
356 :     awaitTermination(t, MEDIUM_DELAY_MS);
357 :     }
358 :    
359 :     /**
360 : jsr166 1.21 * Cancelling a task causes timed get in another thread to throw
361 :     * CancellationException
362 : dl 1.5 */
363 : jsr166 1.15 public void testTimedGet_Cancellation() throws InterruptedException {
364 : jsr166 1.22 final CountDownLatch threadStarted = new CountDownLatch(2);
365 :     final FutureTask task =
366 : jsr166 1.15 new FutureTask(new CheckedInterruptedCallable<Object>() {
367 : jsr166 1.16 public Object realCall() throws InterruptedException {
368 : jsr166 1.22 threadStarted.countDown();
369 :     Thread.sleep(LONG_DELAY_MS);
370 : jsr166 1.16 return Boolean.TRUE;
371 : jsr166 1.15 }});
372 :    
373 :     Thread t1 = new ThreadShouldThrow(CancellationException.class) {
374 :     public void realRun() throws Exception {
375 : jsr166 1.22 threadStarted.countDown();
376 :     task.get(MEDIUM_DELAY_MS, MILLISECONDS);
377 : jsr166 1.15 }};
378 : jsr166 1.22 Thread t2 = new Thread(task);
379 : jsr166 1.15 t1.start();
380 :     t2.start();
381 : jsr166 1.22 threadStarted.await();
382 :     task.cancel(true);
383 :     awaitTermination(t1, MEDIUM_DELAY_MS);
384 :     awaitTermination(t2, MEDIUM_DELAY_MS);
385 :     checkCancelled(task);
386 : dl 1.1 }
387 : dl 1.6
388 : dl 1.5 /**
389 : jsr166 1.20 * Cancelling a task causes get in another thread to throw
390 :     * CancellationException
391 : dl 1.5 */
392 : jsr166 1.15 public void testGet_Cancellation() throws InterruptedException {
393 : jsr166 1.22 final CountDownLatch threadStarted = new CountDownLatch(2);
394 :     final FutureTask task =
395 : jsr166 1.15 new FutureTask(new CheckedInterruptedCallable<Object>() {
396 : jsr166 1.16 public Object realCall() throws InterruptedException {
397 : jsr166 1.22 threadStarted.countDown();
398 :     Thread.sleep(LONG_DELAY_MS);
399 : jsr166 1.16 return Boolean.TRUE;
400 : jsr166 1.15 }});
401 : jsr166 1.22
402 : jsr166 1.15 Thread t1 = new ThreadShouldThrow(CancellationException.class) {
403 :     public void realRun() throws Exception {
404 : jsr166 1.22 threadStarted.countDown();
405 :     task.get();
406 : jsr166 1.15 }};
407 : jsr166 1.22 Thread t2 = new Thread(task);
408 : jsr166 1.15 t1.start();
409 :     t2.start();
410 : jsr166 1.22 threadStarted.await();
411 :     task.cancel(true);
412 :     awaitTermination(t1, MEDIUM_DELAY_MS);
413 :     awaitTermination(t2, MEDIUM_DELAY_MS);
414 :     checkCancelled(task);
415 : dl 1.1 }
416 : jsr166 1.12
417 : dl 1.1
418 : dl 1.5 /**
419 : dl 1.6 * A runtime exception in task causes get to throw ExecutionException
420 : dl 1.5 */
421 : jsr166 1.15 public void testGet_ExecutionException() throws InterruptedException {
422 : jsr166 1.22 final FutureTask task = new FutureTask(new Callable() {
423 : jsr166 1.16 public Object call() {
424 : jsr166 1.15 return 5/0;
425 :     }});
426 :    
427 : jsr166 1.22 task.run();
428 : jsr166 1.15 try {
429 : jsr166 1.22 task.get();
430 : jsr166 1.17 shouldThrow();
431 : jsr166 1.16 } catch (ExecutionException success) {
432 : jsr166 1.15 assertTrue(success.getCause() instanceof ArithmeticException);
433 : jsr166 1.22 checkCompletedAbnormally(task, success.getCause());
434 : dl 1.1 }
435 :     }
436 : jsr166 1.12
437 : dl 1.5 /**
438 : jsr166 1.20 * A runtime exception in task causes timed get to throw ExecutionException
439 : dl 1.5 */
440 : jsr166 1.15 public void testTimedGet_ExecutionException2() throws Exception {
441 : jsr166 1.22 final FutureTask task = new FutureTask(new Callable() {
442 : jsr166 1.16 public Object call() {
443 : jsr166 1.15 return 5/0;
444 :     }});
445 :    
446 : jsr166 1.22 task.run();
447 : jsr166 1.16 try {
448 : jsr166 1.22 task.get(SHORT_DELAY_MS, MILLISECONDS);
449 : jsr166 1.17 shouldThrow();
450 : jsr166 1.16 } catch (ExecutionException success) {
451 : jsr166 1.15 assertTrue(success.getCause() instanceof ArithmeticException);
452 : jsr166 1.22 checkCompletedAbnormally(task, success.getCause());
453 : jsr166 1.15 }
454 : dl 1.1 }
455 : jsr166 1.12
456 : dl 1.1
457 : dl 1.5 /**
458 : dl 1.6 * Interrupting a waiting get causes it to throw InterruptedException
459 : dl 1.5 */
460 : jsr166 1.15 public void testGet_InterruptedException() throws InterruptedException {
461 : jsr166 1.22 final CountDownLatch threadStarted = new CountDownLatch(1);
462 :     final FutureTask task = new FutureTask(new NoOpCallable());
463 :     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
464 : jsr166 1.15 public void realRun() throws Exception {
465 : jsr166 1.22 threadStarted.countDown();
466 :     task.get();
467 : jsr166 1.15 }});
468 :    
469 : jsr166 1.22 threadStarted.await();
470 : jsr166 1.15 t.interrupt();
471 : jsr166 1.22 awaitTermination(t, MEDIUM_DELAY_MS);
472 :     checkNotDone(task);
473 : dl 1.1 }
474 :    
475 : dl 1.5 /**
476 : jsr166 1.20 * Interrupting a waiting timed get causes it to throw InterruptedException
477 : dl 1.5 */
478 : jsr166 1.15 public void testTimedGet_InterruptedException2() throws InterruptedException {
479 : jsr166 1.22 final CountDownLatch threadStarted = new CountDownLatch(1);
480 :     final FutureTask task = new FutureTask(new NoOpCallable());
481 :     Thread t = newStartedThread(new CheckedInterruptedRunnable() {
482 : jsr166 1.15 public void realRun() throws Exception {
483 : jsr166 1.22 threadStarted.countDown();
484 :     task.get(LONG_DELAY_MS, MILLISECONDS);
485 : jsr166 1.15 }});
486 :    
487 : jsr166 1.22 threadStarted.await();
488 : jsr166 1.15 t.interrupt();
489 : jsr166 1.22 awaitTermination(t, MEDIUM_DELAY_MS);
490 :     checkNotDone(task);
491 : dl 1.1 }
492 : jsr166 1.12
493 : dl 1.5 /**
494 : dl 1.6 * A timed out timed get throws TimeoutException
495 : dl 1.5 */
496 : jsr166 1.15 public void testGet_TimeoutException() throws Exception {
497 : jsr166 1.16 try {
498 : jsr166 1.22 FutureTask task = new FutureTask(new NoOpCallable());
499 :     task.get(1, MILLISECONDS);
500 : jsr166 1.17 shouldThrow();
501 : jsr166 1.16 } catch (TimeoutException success) {}
502 : dl 1.1 }
503 : jsr166 1.12
504 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8