[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.51 - (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 : jsr166 1.40 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 :     import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 :    
12 :     import java.util.ArrayList;
13 :     import java.util.List;
14 :     import java.util.NoSuchElementException;
15 : jsr166 1.26 import java.util.concurrent.Callable;
16 :     import java.util.concurrent.CancellationException;
17 :     import java.util.concurrent.CountDownLatch;
18 : jsr166 1.40 import java.util.concurrent.ExecutionException;
19 :     import java.util.concurrent.Executors;
20 : jsr166 1.39 import java.util.concurrent.ExecutorService;
21 : jsr166 1.26 import java.util.concurrent.Future;
22 :     import java.util.concurrent.FutureTask;
23 :     import java.util.concurrent.TimeoutException;
24 : jsr166 1.29 import java.util.concurrent.atomic.AtomicInteger;
25 : jsr166 1.40
26 :     import junit.framework.Test;
27 :     import junit.framework.TestSuite;
28 : dl 1.1
29 : dl 1.4 public class FutureTaskTest extends JSR166TestCase {
30 : dl 1.1
31 :     public static void main(String[] args) {
32 : jsr166 1.43 main(suite(), args);
33 : dl 1.1 }
34 :     public static Test suite() {
35 : jsr166 1.16 return new TestSuite(FutureTaskTest.class);
36 : dl 1.1 }
37 :    
38 : jsr166 1.29 void checkIsDone(Future<?> f) {
39 :     assertTrue(f.isDone());
40 :     assertFalse(f.cancel(false));
41 :     assertFalse(f.cancel(true));
42 :     if (f instanceof PublicFutureTask) {
43 :     PublicFutureTask pf = (PublicFutureTask) f;
44 :     assertEquals(1, pf.doneCount());
45 :     assertFalse(pf.runAndReset());
46 :     assertEquals(1, pf.doneCount());
47 : jsr166 1.37 Object r = null; Object exInfo = null;
48 :     try {
49 :     r = f.get();
50 :     } catch (CancellationException t) {
51 :     exInfo = CancellationException.class;
52 :     } catch (ExecutionException t) {
53 :     exInfo = t.getCause();
54 : jsr166 1.50 } catch (Throwable t) {
55 : jsr166 1.37 threadUnexpectedException(t);
56 :     }
57 : jsr166 1.29
58 :     // Check that run and runAndReset have no effect.
59 :     int savedRunCount = pf.runCount();
60 :     pf.run();
61 :     pf.runAndReset();
62 :     assertEquals(savedRunCount, pf.runCount());
63 : jsr166 1.37 try {
64 :     assertSame(r, f.get());
65 :     } catch (CancellationException t) {
66 :     assertSame(exInfo, CancellationException.class);
67 :     } catch (ExecutionException t) {
68 :     assertSame(exInfo, t.getCause());
69 : jsr166 1.50 } catch (Throwable t) {
70 : jsr166 1.37 threadUnexpectedException(t);
71 :     }
72 : jsr166 1.29 assertTrue(f.isDone());
73 :     }
74 :     }
75 :    
76 : jsr166 1.22 void checkNotDone(Future<?> f) {
77 :     assertFalse(f.isDone());
78 :     assertFalse(f.isCancelled());
79 : jsr166 1.29 if (f instanceof PublicFutureTask) {
80 :     PublicFutureTask pf = (PublicFutureTask) f;
81 :     assertEquals(0, pf.doneCount());
82 :     assertEquals(0, pf.setCount());
83 :     assertEquals(0, pf.setExceptionCount());
84 :     }
85 :     }
86 :    
87 :     void checkIsRunning(Future<?> f) {
88 :     checkNotDone(f);
89 :     if (f instanceof FutureTask) {
90 :     FutureTask ft = (FutureTask<?>) f;
91 :     // Check that run methods do nothing
92 :     ft.run();
93 : jsr166 1.37 if (f instanceof PublicFutureTask) {
94 :     PublicFutureTask pf = (PublicFutureTask) f;
95 :     int savedRunCount = pf.runCount();
96 :     pf.run();
97 :     assertFalse(pf.runAndReset());
98 :     assertEquals(savedRunCount, pf.runCount());
99 :     }
100 : jsr166 1.29 checkNotDone(f);
101 :     }
102 : jsr166 1.22 }
103 :    
104 :     <T> void checkCompletedNormally(Future<T> f, T expected) {
105 : jsr166 1.29 checkIsDone(f);
106 : jsr166 1.22 assertFalse(f.isCancelled());
107 :    
108 :     try {
109 :     assertSame(expected, f.get());
110 : jsr166 1.46 assertSame(expected, f.get(randomTimeout(), randomTimeUnit()));
111 : jsr166 1.50 } catch (Throwable fail) { threadUnexpectedException(fail); }
112 : jsr166 1.22 }
113 :    
114 :     void checkCancelled(Future<?> f) {
115 : jsr166 1.29 checkIsDone(f);
116 : jsr166 1.22 assertTrue(f.isCancelled());
117 :    
118 :     try {
119 :     f.get();
120 :     shouldThrow();
121 :     } catch (CancellationException success) {
122 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
123 :    
124 :     try {
125 : jsr166 1.46 f.get(randomTimeout(), randomTimeUnit());
126 : jsr166 1.22 shouldThrow();
127 :     } catch (CancellationException success) {
128 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
129 : jsr166 1.29 }
130 : jsr166 1.22
131 : jsr166 1.29 void tryToConfuseDoneTask(PublicFutureTask pf) {
132 :     pf.set(new Object());
133 :     pf.setException(new Error());
134 :     for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
135 : jsr166 1.41 pf.cancel(mayInterruptIfRunning);
136 : jsr166 1.29 }
137 : jsr166 1.22 }
138 :    
139 :     void checkCompletedAbnormally(Future<?> f, Throwable t) {
140 : jsr166 1.29 checkIsDone(f);
141 : jsr166 1.22 assertFalse(f.isCancelled());
142 :    
143 :     try {
144 :     f.get();
145 :     shouldThrow();
146 :     } catch (ExecutionException success) {
147 :     assertSame(t, success.getCause());
148 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
149 :    
150 :     try {
151 : jsr166 1.46 f.get(randomTimeout(), randomTimeUnit());
152 : jsr166 1.22 shouldThrow();
153 :     } catch (ExecutionException success) {
154 :     assertSame(t, success.getCause());
155 :     } catch (Throwable fail) { threadUnexpectedException(fail); }
156 :     }
157 :    
158 : dl 1.4 /**
159 :     * Subclass to expose protected methods
160 :     */
161 : dl 1.7 static class PublicFutureTask extends FutureTask {
162 : jsr166 1.29 private final AtomicInteger runCount;
163 :     private final AtomicInteger doneCount = new AtomicInteger(0);
164 :     private final AtomicInteger runAndResetCount = new AtomicInteger(0);
165 :     private final AtomicInteger setCount = new AtomicInteger(0);
166 :     private final AtomicInteger setExceptionCount = new AtomicInteger(0);
167 :     public int runCount() { return runCount.get(); }
168 :     public int doneCount() { return doneCount.get(); }
169 :     public int runAndResetCount() { return runAndResetCount.get(); }
170 :     public int setCount() { return setCount.get(); }
171 :     public int setExceptionCount() { return setExceptionCount.get(); }
172 :    
173 :     PublicFutureTask(Runnable runnable) {
174 :     this(runnable, seven);
175 :     }
176 :     PublicFutureTask(Runnable runnable, Object result) {
177 :     this(runnable, result, new AtomicInteger(0));
178 :     }
179 :     private PublicFutureTask(final Runnable runnable, Object result,
180 :     final AtomicInteger runCount) {
181 :     super(new Runnable() {
182 : jsr166 1.31 public void run() {
183 :     runCount.getAndIncrement();
184 :     runnable.run();
185 :     }}, result);
186 : jsr166 1.29 this.runCount = runCount;
187 :     }
188 :     PublicFutureTask(Callable callable) {
189 :     this(callable, new AtomicInteger(0));
190 :     }
191 :     private PublicFutureTask(final Callable callable,
192 :     final AtomicInteger runCount) {
193 :     super(new Callable() {
194 :     public Object call() throws Exception {
195 :     runCount.getAndIncrement();
196 :     return callable.call();
197 :     }});
198 :     this.runCount = runCount;
199 :     }
200 : jsr166 1.30 @Override public void done() {
201 : jsr166 1.29 assertTrue(isDone());
202 :     doneCount.incrementAndGet();
203 :     super.done();
204 :     }
205 : jsr166 1.30 @Override public boolean runAndReset() {
206 : jsr166 1.29 runAndResetCount.incrementAndGet();
207 :     return super.runAndReset();
208 :     }
209 : jsr166 1.30 @Override public void set(Object x) {
210 : jsr166 1.29 setCount.incrementAndGet();
211 :     super.set(x);
212 :     }
213 : jsr166 1.30 @Override public void setException(Throwable t) {
214 : jsr166 1.29 setExceptionCount.incrementAndGet();
215 :     super.setException(t);
216 :     }
217 :     }
218 :    
219 :     class Counter extends CheckedRunnable {
220 :     final AtomicInteger count = new AtomicInteger(0);
221 :     public int get() { return count.get(); }
222 :     public void realRun() {
223 :     count.getAndIncrement();
224 :     }
225 : dl 1.4 }
226 : dl 1.1
227 : dl 1.5 /**
228 : jsr166 1.29 * creating a future with a null callable throws NullPointerException
229 : dl 1.5 */
230 :     public void testConstructor() {
231 : dl 1.3 try {
232 : jsr166 1.29 new FutureTask(null);
233 : jsr166 1.17 shouldThrow();
234 : jsr166 1.15 } catch (NullPointerException success) {}
235 : dl 1.3 }
236 :    
237 : dl 1.5 /**
238 : jsr166 1.29 * creating a future with null runnable throws NullPointerException
239 : dl 1.5 */
240 :     public void testConstructor2() {
241 : dl 1.3 try {
242 : jsr166 1.29 new FutureTask(null, Boolean.TRUE);
243 : jsr166 1.17 shouldThrow();
244 : jsr166 1.15 } catch (NullPointerException success) {}
245 : dl 1.3 }
246 :    
247 : dl 1.5 /**
248 : dl 1.6 * isDone is true when a task completes
249 : dl 1.5 */
250 :     public void testIsDone() {
251 : jsr166 1.29 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
252 :     assertFalse(task.isDone());
253 : jsr166 1.16 task.run();
254 :     assertTrue(task.isDone());
255 : jsr166 1.22 checkCompletedNormally(task, Boolean.TRUE);
256 : jsr166 1.29 assertEquals(1, task.runCount());
257 : dl 1.4 }
258 :    
259 : dl 1.5 /**
260 : dl 1.9 * runAndReset of a non-cancelled task succeeds
261 : dl 1.5 */
262 : dl 1.9 public void testRunAndReset() {
263 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
264 : jsr166 1.29 for (int i = 0; i < 3; i++) {
265 :     assertTrue(task.runAndReset());
266 :     checkNotDone(task);
267 : jsr166 1.44 assertEquals(i + 1, task.runCount());
268 :     assertEquals(i + 1, task.runAndResetCount());
269 : jsr166 1.29 assertEquals(0, task.setCount());
270 :     assertEquals(0, task.setExceptionCount());
271 :     }
272 : dl 1.4 }
273 :    
274 : dl 1.5 /**
275 : dl 1.9 * runAndReset after cancellation fails
276 : dl 1.5 */
277 : jsr166 1.29 public void testRunAndResetAfterCancel() {
278 :     for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
279 :     PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
280 :     assertTrue(task.cancel(mayInterruptIfRunning));
281 :     for (int i = 0; i < 3; i++) {
282 :     assertFalse(task.runAndReset());
283 :     assertEquals(0, task.runCount());
284 : jsr166 1.44 assertEquals(i + 1, task.runAndResetCount());
285 : jsr166 1.29 assertEquals(0, task.setCount());
286 :     assertEquals(0, task.setExceptionCount());
287 :     }
288 :     tryToConfuseDoneTask(task);
289 :     checkCancelled(task);
290 :     }
291 : dl 1.4 }
292 :    
293 : dl 1.5 /**
294 : dl 1.11 * setting value causes get to return it
295 : dl 1.5 */
296 : jsr166 1.15 public void testSet() throws Exception {
297 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
298 : dl 1.4 task.set(one);
299 : jsr166 1.29 for (int i = 0; i < 3; i++) {
300 :     assertSame(one, task.get());
301 :     assertSame(one, task.get(LONG_DELAY_MS, MILLISECONDS));
302 :     assertEquals(1, task.setCount());
303 :     }
304 :     tryToConfuseDoneTask(task);
305 : jsr166 1.22 checkCompletedNormally(task, one);
306 : jsr166 1.29 assertEquals(0, task.runCount());
307 : dl 1.4 }
308 :    
309 : dl 1.5 /**
310 : dl 1.6 * setException causes get to throw ExecutionException
311 : dl 1.5 */
312 : jsr166 1.29 public void testSetException_get() throws Exception {
313 : dl 1.4 Exception nse = new NoSuchElementException();
314 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
315 : dl 1.4 task.setException(nse);
316 : jsr166 1.29
317 :     try {
318 :     task.get();
319 :     shouldThrow();
320 :     } catch (ExecutionException success) {
321 :     assertSame(nse, success.getCause());
322 :     checkCompletedAbnormally(task, nse);
323 :     }
324 :    
325 : dl 1.4 try {
326 : jsr166 1.29 task.get(LONG_DELAY_MS, MILLISECONDS);
327 : jsr166 1.17 shouldThrow();
328 : jsr166 1.15 } catch (ExecutionException success) {
329 : jsr166 1.29 assertSame(nse, success.getCause());
330 : jsr166 1.22 checkCompletedAbnormally(task, nse);
331 : dl 1.4 }
332 : jsr166 1.29
333 :     assertEquals(1, task.setExceptionCount());
334 :     assertEquals(0, task.setCount());
335 :     tryToConfuseDoneTask(task);
336 :     checkCompletedAbnormally(task, nse);
337 :     assertEquals(0, task.runCount());
338 : dl 1.4 }
339 :    
340 : dl 1.5 /**
341 : jsr166 1.29 * cancel(false) before run succeeds
342 : dl 1.5 */
343 : dl 1.1 public void testCancelBeforeRun() {
344 : jsr166 1.29 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
345 : dl 1.1 assertTrue(task.cancel(false));
346 : jsr166 1.16 task.run();
347 : jsr166 1.32 assertEquals(0, task.runCount());
348 : jsr166 1.29 assertEquals(0, task.setCount());
349 :     assertEquals(0, task.setExceptionCount());
350 : jsr166 1.32 assertTrue(task.isCancelled());
351 :     assertTrue(task.isDone());
352 : jsr166 1.29 tryToConfuseDoneTask(task);
353 : jsr166 1.32 assertEquals(0, task.runCount());
354 : jsr166 1.22 checkCancelled(task);
355 : dl 1.1 }
356 :    
357 : dl 1.5 /**
358 : jsr166 1.29 * cancel(true) before run succeeds
359 : dl 1.5 */
360 : dl 1.1 public void testCancelBeforeRun2() {
361 : jsr166 1.29 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
362 : dl 1.1 assertTrue(task.cancel(true));
363 : jsr166 1.16 task.run();
364 : jsr166 1.32 assertEquals(0, task.runCount());
365 : jsr166 1.29 assertEquals(0, task.setCount());
366 :     assertEquals(0, task.setExceptionCount());
367 : jsr166 1.32 assertTrue(task.isCancelled());
368 :     assertTrue(task.isDone());
369 : jsr166 1.29 tryToConfuseDoneTask(task);
370 : jsr166 1.32 assertEquals(0, task.runCount());
371 : jsr166 1.22 checkCancelled(task);
372 : dl 1.1 }
373 :    
374 : dl 1.5 /**
375 : jsr166 1.29 * cancel(false) of a completed task fails
376 : dl 1.5 */
377 : dl 1.1 public void testCancelAfterRun() {
378 : jsr166 1.29 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
379 : jsr166 1.16 task.run();
380 : dl 1.1 assertFalse(task.cancel(false));
381 : jsr166 1.32 assertEquals(1, task.runCount());
382 : jsr166 1.29 assertEquals(1, task.setCount());
383 :     assertEquals(0, task.setExceptionCount());
384 :     tryToConfuseDoneTask(task);
385 :     checkCompletedNormally(task, Boolean.TRUE);
386 :     assertEquals(1, task.runCount());
387 :     }
388 :    
389 :     /**
390 :     * cancel(true) of a completed task fails
391 :     */
392 :     public void testCancelAfterRun2() {
393 :     PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
394 :     task.run();
395 :     assertFalse(task.cancel(true));
396 : jsr166 1.32 assertEquals(1, task.runCount());
397 : jsr166 1.29 assertEquals(1, task.setCount());
398 :     assertEquals(0, task.setExceptionCount());
399 :     tryToConfuseDoneTask(task);
400 : jsr166 1.22 checkCompletedNormally(task, Boolean.TRUE);
401 : jsr166 1.29 assertEquals(1, task.runCount());
402 : dl 1.1 }
403 :    
404 : dl 1.5 /**
405 : jsr166 1.29 * cancel(true) interrupts a running task that subsequently succeeds
406 : dl 1.5 */
407 : jsr166 1.29 public void testCancelInterrupt() {
408 :     final CountDownLatch pleaseCancel = new CountDownLatch(1);
409 :     final PublicFutureTask task =
410 :     new PublicFutureTask(new CheckedRunnable() {
411 :     public void realRun() {
412 :     pleaseCancel.countDown();
413 :     try {
414 :     delay(LONG_DELAY_MS);
415 :     shouldThrow();
416 :     } catch (InterruptedException success) {}
417 : jsr166 1.45 assertFalse(Thread.interrupted());
418 : jsr166 1.15 }});
419 :    
420 : jsr166 1.22 Thread t = newStartedThread(task);
421 : jsr166 1.29 await(pleaseCancel);
422 : jsr166 1.15 assertTrue(task.cancel(true));
423 : jsr166 1.29 assertTrue(task.isCancelled());
424 : jsr166 1.32 assertTrue(task.isDone());
425 : jsr166 1.29 awaitTermination(t);
426 :     assertEquals(1, task.runCount());
427 :     assertEquals(1, task.setCount());
428 :     assertEquals(0, task.setExceptionCount());
429 :     tryToConfuseDoneTask(task);
430 : jsr166 1.22 checkCancelled(task);
431 : jsr166 1.29 }
432 :    
433 :     /**
434 : jsr166 1.33 * cancel(true) tries to interrupt a running task, but
435 :     * Thread.interrupt throws (simulating a restrictive security
436 :     * manager)
437 : jsr166 1.32 */
438 :     public void testCancelInterrupt_ThrowsSecurityException() {
439 :     final CountDownLatch pleaseCancel = new CountDownLatch(1);
440 :     final CountDownLatch cancelled = new CountDownLatch(1);
441 :     final PublicFutureTask task =
442 :     new PublicFutureTask(new CheckedRunnable() {
443 :     public void realRun() {
444 :     pleaseCancel.countDown();
445 :     await(cancelled);
446 :     assertFalse(Thread.interrupted());
447 :     }});
448 :    
449 : jsr166 1.33 final Thread t = new Thread(task) {
450 :     // Simulate a restrictive security manager.
451 :     @Override public void interrupt() {
452 :     throw new SecurityException();
453 :     }};
454 :     t.setDaemon(true);
455 :     t.start();
456 :    
457 : jsr166 1.32 await(pleaseCancel);
458 :     try {
459 : jsr166 1.33 task.cancel(true);
460 :     shouldThrow();
461 :     } catch (SecurityException expected) {}
462 :    
463 :     // We failed to deliver the interrupt, but the world retains
464 :     // its sanity, as if we had done task.cancel(false)
465 : jsr166 1.32 assertTrue(task.isCancelled());
466 :     assertTrue(task.isDone());
467 :     assertEquals(1, task.runCount());
468 :     assertEquals(1, task.doneCount());
469 :     assertEquals(0, task.setCount());
470 :     assertEquals(0, task.setExceptionCount());
471 :     cancelled.countDown();
472 :     awaitTermination(t);
473 :     assertEquals(1, task.setCount());
474 :     assertEquals(0, task.setExceptionCount());
475 :     tryToConfuseDoneTask(task);
476 :     checkCancelled(task);
477 :     }
478 :    
479 :     /**
480 : jsr166 1.29 * cancel(true) interrupts a running task that subsequently throws
481 :     */
482 :     public void testCancelInterrupt_taskFails() {
483 :     final CountDownLatch pleaseCancel = new CountDownLatch(1);
484 :     final PublicFutureTask task =
485 :     new PublicFutureTask(new Runnable() {
486 :     public void run() {
487 : jsr166 1.42 pleaseCancel.countDown();
488 : jsr166 1.29 try {
489 :     delay(LONG_DELAY_MS);
490 : jsr166 1.38 threadShouldThrow();
491 :     } catch (InterruptedException success) {
492 :     } catch (Throwable t) { threadUnexpectedException(t); }
493 : jsr166 1.35 throw new RuntimeException();
494 : jsr166 1.29 }});
495 :    
496 :     Thread t = newStartedThread(task);
497 :     await(pleaseCancel);
498 :     assertTrue(task.cancel(true));
499 :     assertTrue(task.isCancelled());
500 :     awaitTermination(t);
501 :     assertEquals(1, task.runCount());
502 :     assertEquals(0, task.setCount());
503 :     assertEquals(1, task.setExceptionCount());
504 :     tryToConfuseDoneTask(task);
505 : jsr166 1.22 checkCancelled(task);
506 : dl 1.1 }
507 :    
508 : dl 1.5 /**
509 : dl 1.6 * cancel(false) does not interrupt a running task
510 : dl 1.5 */
511 : jsr166 1.29 public void testCancelNoInterrupt() {
512 :     final CountDownLatch pleaseCancel = new CountDownLatch(1);
513 : jsr166 1.22 final CountDownLatch cancelled = new CountDownLatch(1);
514 : jsr166 1.29 final PublicFutureTask task =
515 :     new PublicFutureTask(new CheckedCallable<Boolean>() {
516 :     public Boolean realCall() {
517 :     pleaseCancel.countDown();
518 :     await(cancelled);
519 : jsr166 1.22 assertFalse(Thread.interrupted());
520 : dl 1.1 return Boolean.TRUE;
521 : jsr166 1.15 }});
522 :    
523 : jsr166 1.22 Thread t = newStartedThread(task);
524 : jsr166 1.29 await(pleaseCancel);
525 : jsr166 1.15 assertTrue(task.cancel(false));
526 : jsr166 1.29 assertTrue(task.isCancelled());
527 : jsr166 1.22 cancelled.countDown();
528 : jsr166 1.29 awaitTermination(t);
529 :     assertEquals(1, task.runCount());
530 :     assertEquals(1, task.setCount());
531 :     assertEquals(0, task.setExceptionCount());
532 :     tryToConfuseDoneTask(task);
533 : jsr166 1.22 checkCancelled(task);
534 : dl 1.1 }
535 :    
536 : dl 1.5 /**
537 : jsr166 1.23 * run in one thread causes get in another thread to retrieve value
538 : dl 1.5 */
539 : jsr166 1.29 public void testGetRun() {
540 :     final CountDownLatch pleaseRun = new CountDownLatch(2);
541 : jsr166 1.23
542 :     final PublicFutureTask task =
543 :     new PublicFutureTask(new CheckedCallable<Object>() {
544 : jsr166 1.29 public Object realCall() {
545 :     return two;
546 : jsr166 1.23 }});
547 :    
548 : jsr166 1.29 Thread t1 = newStartedThread(new CheckedRunnable() {
549 : jsr166 1.23 public void realRun() throws Exception {
550 : jsr166 1.29 pleaseRun.countDown();
551 :     assertSame(two, task.get());
552 : jsr166 1.23 }});
553 :    
554 : jsr166 1.29 Thread t2 = newStartedThread(new CheckedRunnable() {
555 : jsr166 1.15 public void realRun() throws Exception {
556 : jsr166 1.29 pleaseRun.countDown();
557 :     assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS));
558 : jsr166 1.15 }});
559 : jsr166 1.12
560 : jsr166 1.29 await(pleaseRun);
561 : jsr166 1.23 checkNotDone(task);
562 : jsr166 1.29 assertTrue(t1.isAlive());
563 :     assertTrue(t2.isAlive());
564 : jsr166 1.22 task.run();
565 : jsr166 1.29 checkCompletedNormally(task, two);
566 :     assertEquals(1, task.runCount());
567 :     assertEquals(1, task.setCount());
568 :     assertEquals(0, task.setExceptionCount());
569 :     awaitTermination(t1);
570 :     awaitTermination(t2);
571 :     tryToConfuseDoneTask(task);
572 :     checkCompletedNormally(task, two);
573 : dl 1.1 }
574 :    
575 : dl 1.5 /**
576 : jsr166 1.29 * set in one thread causes get in another thread to retrieve value
577 : jsr166 1.23 */
578 : jsr166 1.29 public void testGetSet() {
579 :     final CountDownLatch pleaseSet = new CountDownLatch(2);
580 : jsr166 1.23
581 :     final PublicFutureTask task =
582 :     new PublicFutureTask(new CheckedCallable<Object>() {
583 :     public Object realCall() throws InterruptedException {
584 : jsr166 1.29 return two;
585 : jsr166 1.23 }});
586 :    
587 : jsr166 1.29 Thread t1 = newStartedThread(new CheckedRunnable() {
588 :     public void realRun() throws Exception {
589 :     pleaseSet.countDown();
590 :     assertSame(two, task.get());
591 :     }});
592 :    
593 :     Thread t2 = newStartedThread(new CheckedRunnable() {
594 : jsr166 1.23 public void realRun() throws Exception {
595 : jsr166 1.29 pleaseSet.countDown();
596 :     assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS));
597 : jsr166 1.23 }});
598 :    
599 : jsr166 1.29 await(pleaseSet);
600 : jsr166 1.23 checkNotDone(task);
601 : jsr166 1.29 assertTrue(t1.isAlive());
602 :     assertTrue(t2.isAlive());
603 :     task.set(two);
604 :     assertEquals(0, task.runCount());
605 :     assertEquals(1, task.setCount());
606 :     assertEquals(0, task.setExceptionCount());
607 :     tryToConfuseDoneTask(task);
608 :     checkCompletedNormally(task, two);
609 :     awaitTermination(t1);
610 :     awaitTermination(t2);
611 : jsr166 1.23 }
612 :    
613 :     /**
614 : jsr166 1.21 * Cancelling a task causes timed get in another thread to throw
615 :     * CancellationException
616 : dl 1.5 */
617 : jsr166 1.29 public void testTimedGet_Cancellation() {
618 : jsr166 1.36 testTimedGet_Cancellation(false);
619 :     }
620 :     public void testTimedGet_Cancellation_interrupt() {
621 :     testTimedGet_Cancellation(true);
622 :     }
623 :     public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) {
624 :     final CountDownLatch pleaseCancel = new CountDownLatch(3);
625 :     final CountDownLatch cancelled = new CountDownLatch(1);
626 :     final Callable<Object> callable =
627 :     new CheckedCallable<Object>() {
628 :     public Object realCall() throws InterruptedException {
629 :     pleaseCancel.countDown();
630 :     if (mayInterruptIfRunning) {
631 :     try {
632 :     delay(2*LONG_DELAY_MS);
633 :     } catch (InterruptedException success) {}
634 :     } else {
635 :     await(cancelled);
636 :     }
637 :     return two;
638 :     }};
639 :     final PublicFutureTask task = new PublicFutureTask(callable);
640 : jsr166 1.29
641 : jsr166 1.36 Thread t1 = new ThreadShouldThrow(CancellationException.class) {
642 : jsr166 1.29 public void realRun() throws Exception {
643 :     pleaseCancel.countDown();
644 :     task.get();
645 :     }};
646 : jsr166 1.36 Thread t2 = new ThreadShouldThrow(CancellationException.class) {
647 : jsr166 1.29 public void realRun() throws Exception {
648 :     pleaseCancel.countDown();
649 :     task.get(2*LONG_DELAY_MS, MILLISECONDS);
650 :     }};
651 : jsr166 1.36 t1.start();
652 :     t2.start();
653 :     Thread t3 = newStartedThread(task);
654 :     await(pleaseCancel);
655 :     checkIsRunning(task);
656 :     task.cancel(mayInterruptIfRunning);
657 :     checkCancelled(task);
658 :     awaitTermination(t1);
659 :     awaitTermination(t2);
660 :     cancelled.countDown();
661 :     awaitTermination(t3);
662 :     assertEquals(1, task.runCount());
663 :     assertEquals(1, task.setCount());
664 :     assertEquals(0, task.setExceptionCount());
665 :     tryToConfuseDoneTask(task);
666 :     checkCancelled(task);
667 : dl 1.1 }
668 : jsr166 1.12
669 : dl 1.5 /**
670 : dl 1.6 * A runtime exception in task causes get to throw ExecutionException
671 : dl 1.5 */
672 : jsr166 1.15 public void testGet_ExecutionException() throws InterruptedException {
673 : jsr166 1.29 final ArithmeticException e = new ArithmeticException();
674 :     final PublicFutureTask task = new PublicFutureTask(new Callable() {
675 : jsr166 1.16 public Object call() {
676 : jsr166 1.29 throw e;
677 : jsr166 1.15 }});
678 :    
679 : jsr166 1.22 task.run();
680 : jsr166 1.29 assertEquals(1, task.runCount());
681 :     assertEquals(0, task.setCount());
682 :     assertEquals(1, task.setExceptionCount());
683 : jsr166 1.15 try {
684 : jsr166 1.22 task.get();
685 : jsr166 1.17 shouldThrow();
686 : jsr166 1.16 } catch (ExecutionException success) {
687 : jsr166 1.29 assertSame(e, success.getCause());
688 :     tryToConfuseDoneTask(task);
689 : jsr166 1.22 checkCompletedAbnormally(task, success.getCause());
690 : dl 1.1 }
691 :     }
692 : jsr166 1.12
693 : dl 1.5 /**
694 : jsr166 1.20 * A runtime exception in task causes timed get to throw ExecutionException
695 : dl 1.5 */
696 : jsr166 1.15 public void testTimedGet_ExecutionException2() throws Exception {
697 : jsr166 1.29 final ArithmeticException e = new ArithmeticException();
698 :     final PublicFutureTask task = new PublicFutureTask(new Callable() {
699 : jsr166 1.16 public Object call() {
700 : jsr166 1.29 throw e;
701 : jsr166 1.15 }});
702 :    
703 : jsr166 1.22 task.run();
704 : jsr166 1.16 try {
705 : jsr166 1.29 task.get(LONG_DELAY_MS, MILLISECONDS);
706 : jsr166 1.17 shouldThrow();
707 : jsr166 1.16 } catch (ExecutionException success) {
708 : jsr166 1.29 assertSame(e, success.getCause());
709 :     tryToConfuseDoneTask(task);
710 : jsr166 1.22 checkCompletedAbnormally(task, success.getCause());
711 : jsr166 1.15 }
712 : dl 1.1 }
713 : jsr166 1.12
714 : dl 1.5 /**
715 : jsr166 1.29 * get is interruptible
716 : dl 1.5 */
717 : jsr166 1.29 public void testGet_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();
725 :     shouldThrow();
726 :     } catch (InterruptedException success) {}
727 :     assertFalse(Thread.interrupted());
728 :    
729 :     pleaseInterrupt.countDown();
730 :     try {
731 :     task.get();
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 :    
743 : dl 1.5 /**
744 : jsr166 1.29 * timed get is interruptible
745 : dl 1.5 */
746 : jsr166 1.29 public void testTimedGet_interruptible() {
747 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
748 : jsr166 1.22 final FutureTask task = new FutureTask(new NoOpCallable());
749 : jsr166 1.29 Thread t = newStartedThread(new CheckedRunnable() {
750 : jsr166 1.15 public void realRun() throws Exception {
751 : jsr166 1.29 Thread.currentThread().interrupt();
752 :     try {
753 :     task.get(2*LONG_DELAY_MS, MILLISECONDS);
754 :     shouldThrow();
755 :     } catch (InterruptedException success) {}
756 :     assertFalse(Thread.interrupted());
757 :    
758 :     pleaseInterrupt.countDown();
759 :     try {
760 :     task.get(2*LONG_DELAY_MS, MILLISECONDS);
761 :     shouldThrow();
762 :     } catch (InterruptedException success) {}
763 :     assertFalse(Thread.interrupted());
764 : jsr166 1.15 }});
765 :    
766 : jsr166 1.29 await(pleaseInterrupt);
767 : jsr166 1.15 t.interrupt();
768 : jsr166 1.28 awaitTermination(t);
769 : jsr166 1.22 checkNotDone(task);
770 : dl 1.1 }
771 : jsr166 1.12
772 : dl 1.5 /**
773 : dl 1.6 * A timed out timed get throws TimeoutException
774 : dl 1.5 */
775 : jsr166 1.15 public void testGet_TimeoutException() throws Exception {
776 : jsr166 1.29 FutureTask task = new FutureTask(new NoOpCallable());
777 :     long startTime = System.nanoTime();
778 : jsr166 1.16 try {
779 : jsr166 1.29 task.get(timeoutMillis(), MILLISECONDS);
780 : jsr166 1.17 shouldThrow();
781 : jsr166 1.29 } catch (TimeoutException success) {
782 :     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
783 :     }
784 :     }
785 :    
786 :     /**
787 :     * timed get with null TimeUnit throws NullPointerException
788 :     */
789 :     public void testGet_NullTimeUnit() throws Exception {
790 :     FutureTask task = new FutureTask(new NoOpCallable());
791 :     long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE };
792 :    
793 :     for (long timeout : timeouts) {
794 :     try {
795 :     task.get(timeout, null);
796 :     shouldThrow();
797 :     } catch (NullPointerException success) {}
798 :     }
799 :    
800 :     task.run();
801 :    
802 :     for (long timeout : timeouts) {
803 :     try {
804 :     task.get(timeout, null);
805 :     shouldThrow();
806 :     } catch (NullPointerException success) {}
807 :     }
808 : dl 1.1 }
809 : jsr166 1.12
810 : jsr166 1.39 /**
811 :     * timed get with most negative timeout works correctly (i.e. no
812 :     * underflow bug)
813 :     */
814 :     public void testGet_NegativeInfinityTimeout() throws Exception {
815 :     final ExecutorService pool = Executors.newFixedThreadPool(10);
816 :     final Runnable nop = new Runnable() { public void run() {}};
817 :     final FutureTask<Void> task = new FutureTask<>(nop, null);
818 :     final List<Future<?>> futures = new ArrayList<>();
819 :     Runnable r = new Runnable() { public void run() {
820 :     for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) {
821 :     try {
822 :     task.get(timeout, NANOSECONDS);
823 :     shouldThrow();
824 :     } catch (TimeoutException success) {
825 :     } catch (Throwable fail) {threadUnexpectedException(fail);}}}};
826 :     for (int i = 0; i < 10; i++)
827 :     futures.add(pool.submit(r));
828 :     try {
829 :     joinPool(pool);
830 :     for (Future<?> future : futures)
831 :     checkCompletedNormally(future, null);
832 :     } finally {
833 :     task.run(); // last resort to help terminate
834 :     }
835 :     }
836 :    
837 : jsr166 1.47 /**
838 :     * toString indicates current completion state
839 :     */
840 :     public void testToString_incomplete() {
841 : jsr166 1.51 FutureTask<String> f = new FutureTask<>(() -> "");
842 : jsr166 1.47 assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
843 :     if (testImplementationDetails)
844 :     assertTrue(f.toString().startsWith(
845 :     identityString(f) + "[Not completed, task ="));
846 :     }
847 :    
848 :     public void testToString_normal() {
849 : jsr166 1.51 FutureTask<String> f = new FutureTask<>(() -> "");
850 : jsr166 1.47 f.run();
851 :     assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
852 :     if (testImplementationDetails)
853 :     assertEquals(identityString(f) + "[Completed normally]",
854 :     f.toString());
855 :     }
856 :    
857 :     public void testToString_exception() {
858 : jsr166 1.51 FutureTask<String> f = new FutureTask<>(
859 : jsr166 1.47 () -> { throw new ArithmeticException(); });
860 :     f.run();
861 :     assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
862 :     if (testImplementationDetails)
863 :     assertTrue(f.toString().startsWith(
864 :     identityString(f) + "[Completed exceptionally: "));
865 :     }
866 :    
867 :     public void testToString_cancelled() {
868 :     for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
869 : jsr166 1.51 FutureTask<String> f = new FutureTask<>(() -> "");
870 : jsr166 1.47 assertTrue(f.cancel(mayInterruptIfRunning));
871 :     assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]"));
872 :     if (testImplementationDetails)
873 :     assertEquals(identityString(f) + "[Cancelled]",
874 :     f.toString());
875 :     }
876 :     }
877 :    
878 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8