ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
(Generate patch)

Comparing jsr166/src/test/tck/FutureTaskTest.java (file contents):
Revision 1.25 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.44 by jsr166, Sun May 24 01:42:14 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines