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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8