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.22 by jsr166, Sun Nov 28 20:20:00 2010 UTC vs.
Revision 1.28 by jsr166, Sat Jun 18 14:31:51 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 < import java.util.concurrent.*;
10 > import java.util.concurrent.Callable;
11 > import java.util.concurrent.CancellationException;
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.ExecutionException;
14 > import java.util.concurrent.Future;
15 > import java.util.concurrent.FutureTask;
16 > import java.util.concurrent.TimeoutException;
17   import static java.util.concurrent.TimeUnit.MILLISECONDS;
18   import static java.util.concurrent.TimeUnit.SECONDS;
19   import java.util.*;
# Line 142 | Line 148 | public class FutureTaskTest extends JSR1
148          checkCancelled(task);
149      }
150  
145
151      /**
152       * setting value causes get to return it
153       */
# Line 251 | Line 256 | public class FutureTaskTest extends JSR1
256      }
257  
258      /**
259 <     * set in one thread causes get in another thread to retrieve value
259 >     * run in one thread causes get in another thread to retrieve value
260       */
261 <    public void testGet1() throws InterruptedException {
261 >    public void testGetRun() throws InterruptedException {
262 >        final CountDownLatch threadStarted = new CountDownLatch(1);
263 >
264          final FutureTask task =
265              new FutureTask(new CheckedCallable<Object>() {
266                  public Object realCall() throws InterruptedException {
267                      return Boolean.TRUE;
268                  }});
262        checkNotDone(task);
269  
270          Thread t = newStartedThread(new CheckedRunnable() {
271              public void realRun() throws Exception {
272 +                threadStarted.countDown();
273                  assertSame(Boolean.TRUE, task.get());
274              }});
275  
276 +        threadStarted.await();
277 +        checkNotDone(task);
278 +        assertTrue(t.isAlive());
279          task.run();
280          checkCompletedNormally(task, Boolean.TRUE);
281          awaitTermination(t, MEDIUM_DELAY_MS);
282      }
283  
284      /**
285 <     * set in one thread causes timed get in another thread to retrieve value
285 >     * set in one thread causes get in another thread to retrieve value
286       */
287 <    public void testTimedGet1() throws InterruptedException {
287 >    public void testGetSet() throws InterruptedException {
288 >        final CountDownLatch threadStarted = new CountDownLatch(1);
289 >
290 >        final PublicFutureTask task =
291 >            new PublicFutureTask(new CheckedCallable<Object>() {
292 >                public Object realCall() throws InterruptedException {
293 >                    return Boolean.TRUE;
294 >                }});
295 >
296 >        Thread t = newStartedThread(new CheckedRunnable() {
297 >            public void realRun() throws Exception {
298 >                threadStarted.countDown();
299 >                assertSame(Boolean.FALSE, task.get());
300 >            }});
301 >
302 >        threadStarted.await();
303 >        checkNotDone(task);
304 >        assertTrue(t.isAlive());
305 >        task.set(Boolean.FALSE);
306 >        checkCompletedNormally(task, Boolean.FALSE);
307 >        awaitTermination(t, MEDIUM_DELAY_MS);
308 >    }
309 >
310 >    /**
311 >     * run in one thread causes timed get in another thread to retrieve value
312 >     */
313 >    public void testTimedGetRun() throws InterruptedException {
314 >        final CountDownLatch threadStarted = new CountDownLatch(1);
315 >
316          final FutureTask task =
317              new FutureTask(new CheckedCallable<Object>() {
318                  public Object realCall() throws InterruptedException {
319                      return Boolean.TRUE;
320                  }});
283        checkNotDone(task);
321  
322          Thread t = newStartedThread(new CheckedRunnable() {
323              public void realRun() throws Exception {
324 <                assertSame(Boolean.TRUE, task.get(SMALL_DELAY_MS, MILLISECONDS));
324 >                threadStarted.countDown();
325 >                assertSame(Boolean.TRUE,
326 >                           task.get(MEDIUM_DELAY_MS, MILLISECONDS));
327              }});
328  
329 +        threadStarted.await();
330 +        checkNotDone(task);
331 +        assertTrue(t.isAlive());
332          task.run();
333          checkCompletedNormally(task, Boolean.TRUE);
334          awaitTermination(t, MEDIUM_DELAY_MS);
335      }
336  
337      /**
338 +     * set in one thread causes timed get in another thread to retrieve value
339 +     */
340 +    public void testTimedGetSet() throws InterruptedException {
341 +        final CountDownLatch threadStarted = new CountDownLatch(1);
342 +
343 +        final PublicFutureTask task =
344 +            new PublicFutureTask(new CheckedCallable<Object>() {
345 +                public Object realCall() throws InterruptedException {
346 +                    return Boolean.TRUE;
347 +                }});
348 +
349 +        Thread t = newStartedThread(new CheckedRunnable() {
350 +            public void realRun() throws Exception {
351 +                threadStarted.countDown();
352 +                assertSame(Boolean.FALSE,
353 +                           task.get(MEDIUM_DELAY_MS, MILLISECONDS));
354 +            }});
355 +
356 +        threadStarted.await();
357 +        checkNotDone(task);
358 +        assertTrue(t.isAlive());
359 +        task.set(Boolean.FALSE);
360 +        checkCompletedNormally(task, Boolean.FALSE);
361 +        awaitTermination(t, MEDIUM_DELAY_MS);
362 +    }
363 +
364 +    /**
365       * Cancelling a task causes timed get in another thread to throw
366       * CancellationException
367       */
# Line 302 | Line 371 | public class FutureTaskTest extends JSR1
371              new FutureTask(new CheckedInterruptedCallable<Object>() {
372                  public Object realCall() throws InterruptedException {
373                      threadStarted.countDown();
374 <                    Thread.sleep(LONG_DELAY_MS);
374 >                    delay(LONG_DELAY_MS);
375                      return Boolean.TRUE;
376                  }});
377  
# Line 331 | Line 400 | public class FutureTaskTest extends JSR1
400              new FutureTask(new CheckedInterruptedCallable<Object>() {
401                  public Object realCall() throws InterruptedException {
402                      threadStarted.countDown();
403 <                    Thread.sleep(LONG_DELAY_MS);
403 >                    delay(LONG_DELAY_MS);
404                      return Boolean.TRUE;
405                  }});
406  
# Line 350 | Line 419 | public class FutureTaskTest extends JSR1
419          checkCancelled(task);
420      }
421  
353
422      /**
423       * A runtime exception in task causes get to throw ExecutionException
424       */
# Line 389 | Line 457 | public class FutureTaskTest extends JSR1
457          }
458      }
459  
392
460      /**
461       * Interrupting a waiting get causes it to throw InterruptedException
462       */
# Line 404 | Line 471 | public class FutureTaskTest extends JSR1
471  
472          threadStarted.await();
473          t.interrupt();
474 <        awaitTermination(t, MEDIUM_DELAY_MS);
474 >        awaitTermination(t);
475          checkNotDone(task);
476      }
477  
# Line 417 | Line 484 | public class FutureTaskTest extends JSR1
484          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
485              public void realRun() throws Exception {
486                  threadStarted.countDown();
487 <                task.get(LONG_DELAY_MS, MILLISECONDS);
487 >                task.get(2*LONG_DELAY_MS, MILLISECONDS);
488              }});
489  
490          threadStarted.await();
491          t.interrupt();
492 <        awaitTermination(t, MEDIUM_DELAY_MS);
492 >        awaitTermination(t);
493          checkNotDone(task);
494      }
495  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines