[cvs] / jsr166 / src / test / tck / FutureTaskTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/FutureTaskTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.22, Sun Nov 28 20:20:00 2010 UTC revision 1.26, 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   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain   * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.   * Pat Fisher, Mike Judd.
7   */   */
8    
9  import junit.framework.*;  import junit.framework.*;
10  import java.util.concurrent.*;  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;  import static java.util.concurrent.TimeUnit.MILLISECONDS;
18  import static java.util.concurrent.TimeUnit.SECONDS;  import static java.util.concurrent.TimeUnit.SECONDS;
19  import java.util.*;  import java.util.*;
# Line 251  Line 257 
257      }      }
258    
259      /**      /**
260       * set in one thread causes get in another thread to retrieve value       * run in one thread causes get in another thread to retrieve value
261       */       */
262      public void testGet1() throws InterruptedException {      public void testGetRun() throws InterruptedException {
263            final CountDownLatch threadStarted = new CountDownLatch(1);
264    
265          final FutureTask task =          final FutureTask task =
266              new FutureTask(new CheckedCallable<Object>() {              new FutureTask(new CheckedCallable<Object>() {
267                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
268                      return Boolean.TRUE;                      return Boolean.TRUE;
269                  }});                  }});
         checkNotDone(task);  
270    
271          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
272              public void realRun() throws Exception {              public void realRun() throws Exception {
273                    threadStarted.countDown();
274                  assertSame(Boolean.TRUE, task.get());                  assertSame(Boolean.TRUE, task.get());
275              }});              }});
276    
277            threadStarted.await();
278            checkNotDone(task);
279            assertTrue(t.isAlive());
280          task.run();          task.run();
281          checkCompletedNormally(task, Boolean.TRUE);          checkCompletedNormally(task, Boolean.TRUE);
282          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t, MEDIUM_DELAY_MS);
283      }      }
284    
285      /**      /**
286       * set in one thread causes timed get in another thread to retrieve value       * 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 {      public void testTimedGetRun() throws InterruptedException {
315            final CountDownLatch threadStarted = new CountDownLatch(1);
316    
317          final FutureTask task =          final FutureTask task =
318              new FutureTask(new CheckedCallable<Object>() {              new FutureTask(new CheckedCallable<Object>() {
319                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
320                      return Boolean.TRUE;                      return Boolean.TRUE;
321                  }});                  }});
         checkNotDone(task);  
322    
323          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
324              public void realRun() throws Exception {              public void realRun() throws Exception {
325                  assertSame(Boolean.TRUE, task.get(SMALL_DELAY_MS, MILLISECONDS));                  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();          task.run();
334          checkCompletedNormally(task, Boolean.TRUE);          checkCompletedNormally(task, Boolean.TRUE);
335          awaitTermination(t, MEDIUM_DELAY_MS);          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       * Cancelling a task causes timed get in another thread to throw
367       * CancellationException       * CancellationException
368       */       */
# Line 302  Line 372 
372              new FutureTask(new CheckedInterruptedCallable<Object>() {              new FutureTask(new CheckedInterruptedCallable<Object>() {
373                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
374                      threadStarted.countDown();                      threadStarted.countDown();
375                      Thread.sleep(LONG_DELAY_MS);                      delay(LONG_DELAY_MS);
376                      return Boolean.TRUE;                      return Boolean.TRUE;
377                  }});                  }});
378    
# Line 331  Line 401 
401              new FutureTask(new CheckedInterruptedCallable<Object>() {              new FutureTask(new CheckedInterruptedCallable<Object>() {
402                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
403                      threadStarted.countDown();                      threadStarted.countDown();
404                      Thread.sleep(LONG_DELAY_MS);                      delay(LONG_DELAY_MS);
405                      return Boolean.TRUE;                      return Boolean.TRUE;
406                  }});                  }});
407    

Legend:
Removed from v.1.22  
changed lines
  Added in v.1.26

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8