[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.25, Fri May 6 11:22:07 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   */   */
# Line 251  Line 251 
251      }      }
252    
253      /**      /**
254       * set in one thread causes get in another thread to retrieve value       * run in one thread causes get in another thread to retrieve value
255       */       */
256      public void testGet1() throws InterruptedException {      public void testGetRun() throws InterruptedException {
257            final CountDownLatch threadStarted = new CountDownLatch(1);
258    
259          final FutureTask task =          final FutureTask task =
260              new FutureTask(new CheckedCallable<Object>() {              new FutureTask(new CheckedCallable<Object>() {
261                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
262                      return Boolean.TRUE;                      return Boolean.TRUE;
263                  }});                  }});
         checkNotDone(task);  
264    
265          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
266              public void realRun() throws Exception {              public void realRun() throws Exception {
267                    threadStarted.countDown();
268                  assertSame(Boolean.TRUE, task.get());                  assertSame(Boolean.TRUE, task.get());
269              }});              }});
270    
271            threadStarted.await();
272            checkNotDone(task);
273            assertTrue(t.isAlive());
274          task.run();          task.run();
275          checkCompletedNormally(task, Boolean.TRUE);          checkCompletedNormally(task, Boolean.TRUE);
276          awaitTermination(t, MEDIUM_DELAY_MS);          awaitTermination(t, MEDIUM_DELAY_MS);
277      }      }
278    
279      /**      /**
280       * 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
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                    }});
290    
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 testTimedGet1() throws InterruptedException {      public void testTimedGetRun() throws InterruptedException {
309            final CountDownLatch threadStarted = new CountDownLatch(1);
310    
311          final FutureTask task =          final FutureTask task =
312              new FutureTask(new CheckedCallable<Object>() {              new FutureTask(new CheckedCallable<Object>() {
313                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
314                      return Boolean.TRUE;                      return Boolean.TRUE;
315                  }});                  }});
         checkNotDone(task);  
316    
317          Thread t = newStartedThread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
318              public void realRun() throws Exception {              public void realRun() throws Exception {
319                  assertSame(Boolean.TRUE, task.get(SMALL_DELAY_MS, MILLISECONDS));                  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();          task.run();
328          checkCompletedNormally(task, Boolean.TRUE);          checkCompletedNormally(task, Boolean.TRUE);
329          awaitTermination(t, MEDIUM_DELAY_MS);          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                    }});
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       * Cancelling a task causes timed get in another thread to throw
361       * CancellationException       * CancellationException
362       */       */
# Line 302  Line 366 
366              new FutureTask(new CheckedInterruptedCallable<Object>() {              new FutureTask(new CheckedInterruptedCallable<Object>() {
367                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
368                      threadStarted.countDown();                      threadStarted.countDown();
369                      Thread.sleep(LONG_DELAY_MS);                      delay(LONG_DELAY_MS);
370                      return Boolean.TRUE;                      return Boolean.TRUE;
371                  }});                  }});
372    
# Line 331  Line 395 
395              new FutureTask(new CheckedInterruptedCallable<Object>() {              new FutureTask(new CheckedInterruptedCallable<Object>() {
396                  public Object realCall() throws InterruptedException {                  public Object realCall() throws InterruptedException {
397                      threadStarted.countDown();                      threadStarted.countDown();
398                      Thread.sleep(LONG_DELAY_MS);                      delay(LONG_DELAY_MS);
399                      return Boolean.TRUE;                      return Boolean.TRUE;
400                  }});                  }});
401    

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8