[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.28, 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   * 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 142  Line 148 
148          checkCancelled(task);          checkCancelled(task);
149      }      }
150    
   
151      /**      /**
152       * setting value causes get to return it       * setting value causes get to return it
153       */       */
# Line 251  Line 256 
256      }      }
257    
258      /**      /**
259       * set in one thread causes get in another thread to retrieve value       * run in one thread causes get in another thread to retrieve value
260       */       */
261      public void testGet1() throws InterruptedException {      public void testGetRun() throws InterruptedException {
262            final CountDownLatch threadStarted = new CountDownLatch(1);
263    
264          final FutureTask task =          final FutureTask task =
265              new FutureTask(new CheckedCallable<Object>() {              new FutureTask(new CheckedCallable<Object>() {
266                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
267                      return Boolean.TRUE;                      return Boolean.TRUE;
268                  }});                  }});
         checkNotDone(task);  
269    
270          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
271              public void realRun() throws Exception {              public void realRun() throws Exception {
272                    threadStarted.countDown();
273                  assertSame(Boolean.TRUE, task.get());                  assertSame(Boolean.TRUE, task.get());
274              }});              }});
275    
276            threadStarted.await();
277            checkNotDone(task);
278            assertTrue(t.isAlive());
279          task.run();          task.run();
280          checkCompletedNormally(task, Boolean.TRUE);          checkCompletedNormally(task, Boolean.TRUE);
281          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t, MEDIUM_DELAY_MS);
282      }      }
283    
284      /**      /**
285       * 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
286       */       */
287      public void testTimedGet1() throws InterruptedException {      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 =          final FutureTask task =
317              new FutureTask(new CheckedCallable<Object>() {              new FutureTask(new CheckedCallable<Object>() {
318                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
319                      return Boolean.TRUE;                      return Boolean.TRUE;
320                  }});                  }});
         checkNotDone(task);  
321    
322          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
323              public void realRun() throws Exception {              public void realRun() throws Exception {
324                  assertSame(Boolean.TRUE, task.get(SMALL_DELAY_MS, MILLISECONDS));                  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();          task.run();
333          checkCompletedNormally(task, Boolean.TRUE);          checkCompletedNormally(task, Boolean.TRUE);
334          awaitTermination(t, MEDIUM_DELAY_MS);          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       * Cancelling a task causes timed get in another thread to throw
366       * CancellationException       * CancellationException
367       */       */
# Line 302  Line 371 
371              new FutureTask(new CheckedInterruptedCallable<Object>() {              new FutureTask(new CheckedInterruptedCallable<Object>() {
372                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
373                      threadStarted.countDown();                      threadStarted.countDown();
374                      Thread.sleep(LONG_DELAY_MS);                      delay(LONG_DELAY_MS);
375                      return Boolean.TRUE;                      return Boolean.TRUE;
376                  }});                  }});
377    
# Line 331  Line 400 
400              new FutureTask(new CheckedInterruptedCallable<Object>() {              new FutureTask(new CheckedInterruptedCallable<Object>() {
401                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
402                      threadStarted.countDown();                      threadStarted.countDown();
403                      Thread.sleep(LONG_DELAY_MS);                      delay(LONG_DELAY_MS);
404                      return Boolean.TRUE;                      return Boolean.TRUE;
405                  }});                  }});
406    
# Line 350  Line 419 
419          checkCancelled(task);          checkCancelled(task);
420      }      }
421    
   
422      /**      /**
423       * A runtime exception in task causes get to throw ExecutionException       * A runtime exception in task causes get to throw ExecutionException
424       */       */
# Line 389  Line 457 
457          }          }
458      }      }
459    
   
460      /**      /**
461       * Interrupting a waiting get causes it to throw InterruptedException       * Interrupting a waiting get causes it to throw InterruptedException
462       */       */
# Line 404  Line 471 
471    
472          threadStarted.await();          threadStarted.await();
473          t.interrupt();          t.interrupt();
474          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t);
475          checkNotDone(task);          checkNotDone(task);
476      }      }
477    
# Line 417  Line 484 
484          Thread t = newStartedThread(new CheckedInterruptedRunnable() {          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
485              public void realRun() throws Exception {              public void realRun() throws Exception {
486                  threadStarted.countDown();                  threadStarted.countDown();
487                  task.get(LONG_DELAY_MS, MILLISECONDS);                  task.get(2*LONG_DELAY_MS, MILLISECONDS);
488              }});              }});
489    
490          threadStarted.await();          threadStarted.await();
491          t.interrupt();          t.interrupt();
492          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t);
493          checkNotDone(task);          checkNotDone(task);
494      }      }
495    

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8