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.3 by dl, Fri Sep 12 15:40:25 2003 UTC vs.
Revision 1.23 by jsr166, Fri Jan 7 07:46:26 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines