Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License.  You may obtain a copy
   * of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  * License for the specific language governing permissions and limitations under
  * the License.
  */
 
 package com.google.common.util.concurrent.testing;
 
 
 
Abstract test case parent for anything implementing com.google.common.util.concurrent.ListenableFuture. Tests the two get methods and the addListener method.

Author(s):
Sven Mawson
Since:
10.0
 
 public abstract class AbstractListenableFutureTest extends TestCase {
 
   protected CountDownLatch latch;
   protected ListenableFuture<Booleanfuture;
 
   @Override
   protected void setUp() throws Exception {
 
     // Create a latch and a future that waits on the latch.
      = new CountDownLatch(1);
   }
 
   @Override
   protected void tearDown() throws Exception {
 
     // Make sure we have no waiting threads.
     .countDown();
   }

  
Constructs a listenable future with a value available after the latch has counted down.
 
   protected abstract <V> ListenableFuture<V> createListenableFuture(
       V valueException exceptCountDownLatch waitOn);

  
Tests that the java.util.concurrent.Future.get() method blocks until a value is available.
 
   public void testGetBlocksUntilValueAvailable() throws Throwable {
 
 
     final CountDownLatch successLatch = new CountDownLatch(1);
     final Throwable[] badness = new Throwable[1];
 
     // Wait on the future in a separate thread.
     new Thread(new Runnable() {
       @Override
       public void run() {
         try {
           assertSame(..get());
           successLatch.countDown();
         } catch (Throwable t) {
           t.printStackTrace();
           badness[0] = t;
         }
       }}).start();
 
     // Release the future value.
     .countDown();
 
     assertTrue(successLatch.await(10, .));
 
     if (badness[0] != null) {
       throw badness[0];
    }
  }

  
Tests that the java.util.concurrent.Future.get(long,java.util.concurrent.TimeUnit) method times out correctly.
      ExecutionException {
    // The task thread waits for the latch, so we expect a timeout here.
    try {
      .get(20, .);
      fail("Should have timed out trying to get the value.");
    } catch (TimeoutException expected) {
      // Expected.
    } finally {
      .countDown();
    }
  }

  
Tests that a canceled future throws a cancellation exception. This method checks the cancel, isCancelled, and isDone methods.
  public void testCanceledFutureThrowsCancellation() throws Exception {
    final CountDownLatch successLatch = new CountDownLatch(1);
    // Run cancellation in a separate thread as an extra thread-safety test.
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          .get();
        } catch (CancellationException expected) {
          successLatch.countDown();
        } catch (Exception ignored) {
          // All other errors are ignored, we expect a cancellation.
        }
      }
    }).start();
    .cancel(true);
    assertTrue(successLatch.await(200, .));
    .countDown();
  }
  public void testListenersNotifiedOnError() throws Exception {
    final CountDownLatch successLatch = new CountDownLatch(1);
    final CountDownLatch listenerLatch = new CountDownLatch(1);
    ExecutorService exec = Executors.newCachedThreadPool();
    .addListener(new Runnable() {
      @Override
      public void run() {
        listenerLatch.countDown();
      }
    }, exec);
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          .get();
        } catch (CancellationException expected) {
          successLatch.countDown();
        } catch (Exception ignored) {
          // No success latch count down.
        }
      }
    }).start();
    .cancel(true);
    assertTrue(successLatch.await(200, .));
    assertTrue(listenerLatch.await(200, .));
    .countDown();
    exec.shutdown();
  }

  
Tests that all listeners complete, even if they were added before or after the future was finishing. Also acts as a concurrency test to make sure the locking is done correctly when a future is finishing so that no listeners can be lost.
    ExecutorService exec = Executors.newCachedThreadPool();
    int listenerCount = 20;
    final CountDownLatch listenerLatch = new CountDownLatch(listenerCount);
    // Test that listeners added both before and after the value is available
    // get called correctly.
    for (int i = 0; i < 20; i++) {
      // Right in the middle start up a thread to close the latch.
      if (i == 10) {
        new Thread(new Runnable() {
          @Override
          public void run() {
            .countDown();
          }
        }).start();
      }
      .addListener(new Runnable() {
        @Override
        public void run() {
          listenerLatch.countDown();
        }
      }, exec);
    }
    // Wait for the listener latch to complete.
    listenerLatch.await(500, .);
    exec.shutdown();
  }
New to GrepCode? Check out our FAQ X