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.26 by jsr166, Tue May 31 16:16:23 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 251 | Line 257 | public class FutureTaskTest extends JSR1
257      }
258  
259      /**
260 <     * set in one thread causes get in another thread to retrieve value
260 >     * run in one thread causes get in another thread to retrieve value
261       */
262 <    public void testGet1() throws InterruptedException {
262 >    public void testGetRun() throws InterruptedException {
263 >        final CountDownLatch threadStarted = new CountDownLatch(1);
264 >
265          final FutureTask task =
266              new FutureTask(new CheckedCallable<Object>() {
267                  public Object realCall() throws InterruptedException {
268                      return Boolean.TRUE;
269                  }});
262        checkNotDone(task);
270  
271          Thread t = newStartedThread(new CheckedRunnable() {
272              public void realRun() throws Exception {
273 +                threadStarted.countDown();
274                  assertSame(Boolean.TRUE, task.get());
275              }});
276  
277 +        threadStarted.await();
278 +        checkNotDone(task);
279 +        assertTrue(t.isAlive());
280          task.run();
281          checkCompletedNormally(task, Boolean.TRUE);
282          awaitTermination(t, MEDIUM_DELAY_MS);
283      }
284  
285      /**
286 <     * set in one thread causes timed get in another thread to retrieve value
286 >     * set in one thread causes get in another thread to retrieve value
287 >     */
288 >    public void testGetSet() throws InterruptedException {
289 >        final CountDownLatch threadStarted = new CountDownLatch(1);
290 >
291 >        final PublicFutureTask task =
292 >            new PublicFutureTask(new CheckedCallable<Object>() {
293 >                public Object realCall() throws InterruptedException {
294 >                    return Boolean.TRUE;
295 >                }});
296 >
297 >        Thread t = newStartedThread(new CheckedRunnable() {
298 >            public void realRun() throws Exception {
299 >                threadStarted.countDown();
300 >                assertSame(Boolean.FALSE, task.get());
301 >            }});
302 >
303 >        threadStarted.await();
304 >        checkNotDone(task);
305 >        assertTrue(t.isAlive());
306 >        task.set(Boolean.FALSE);
307 >        checkCompletedNormally(task, Boolean.FALSE);
308 >        awaitTermination(t, MEDIUM_DELAY_MS);
309 >    }
310 >
311 >    /**
312 >     * run in one thread causes timed get in another thread to retrieve value
313       */
314 <    public void testTimedGet1() throws InterruptedException {
314 >    public void testTimedGetRun() throws InterruptedException {
315 >        final CountDownLatch threadStarted = new CountDownLatch(1);
316 >
317          final FutureTask task =
318              new FutureTask(new CheckedCallable<Object>() {
319                  public Object realCall() throws InterruptedException {
320                      return Boolean.TRUE;
321                  }});
283        checkNotDone(task);
322  
323          Thread t = newStartedThread(new CheckedRunnable() {
324              public void realRun() throws Exception {
325 <                assertSame(Boolean.TRUE, task.get(SMALL_DELAY_MS, MILLISECONDS));
325 >                threadStarted.countDown();
326 >                assertSame(Boolean.TRUE,
327 >                           task.get(MEDIUM_DELAY_MS, MILLISECONDS));
328              }});
329  
330 +        threadStarted.await();
331 +        checkNotDone(task);
332 +        assertTrue(t.isAlive());
333          task.run();
334          checkCompletedNormally(task, Boolean.TRUE);
335          awaitTermination(t, MEDIUM_DELAY_MS);
336      }
337  
338      /**
339 +     * set in one thread causes timed get in another thread to retrieve value
340 +     */
341 +    public void testTimedGetSet() throws InterruptedException {
342 +        final CountDownLatch threadStarted = new CountDownLatch(1);
343 +
344 +        final PublicFutureTask task =
345 +            new PublicFutureTask(new CheckedCallable<Object>() {
346 +                public Object realCall() throws InterruptedException {
347 +                    return Boolean.TRUE;
348 +                }});
349 +
350 +        Thread t = newStartedThread(new CheckedRunnable() {
351 +            public void realRun() throws Exception {
352 +                threadStarted.countDown();
353 +                assertSame(Boolean.FALSE,
354 +                           task.get(MEDIUM_DELAY_MS, MILLISECONDS));
355 +            }});
356 +
357 +        threadStarted.await();
358 +        checkNotDone(task);
359 +        assertTrue(t.isAlive());
360 +        task.set(Boolean.FALSE);
361 +        checkCompletedNormally(task, Boolean.FALSE);
362 +        awaitTermination(t, MEDIUM_DELAY_MS);
363 +    }
364 +
365 +    /**
366       * Cancelling a task causes timed get in another thread to throw
367       * CancellationException
368       */
# Line 302 | Line 372 | public class FutureTaskTest extends JSR1
372              new FutureTask(new CheckedInterruptedCallable<Object>() {
373                  public Object realCall() throws InterruptedException {
374                      threadStarted.countDown();
375 <                    Thread.sleep(LONG_DELAY_MS);
375 >                    delay(LONG_DELAY_MS);
376                      return Boolean.TRUE;
377                  }});
378  
# Line 331 | Line 401 | public class FutureTaskTest extends JSR1
401              new FutureTask(new CheckedInterruptedCallable<Object>() {
402                  public Object realCall() throws InterruptedException {
403                      threadStarted.countDown();
404 <                    Thread.sleep(LONG_DELAY_MS);
404 >                    delay(LONG_DELAY_MS);
405                      return Boolean.TRUE;
406                  }});
407  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines