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;
 
 
 
 public abstract class AbstractCheckedFutureTest
     extends AbstractListenableFutureTest {

  
More specific type for the create method.
 
   protected abstract <V> CheckedFuture<V, ?> createCheckedFuture(V value,
       Exception exceptCountDownLatch waitOn);

  
Checks that the exception is the correct type of cancellation exception.
 
   protected abstract void checkCancelledException(Exception e);

  
Checks that the exception is the correct type of execution exception.
 
   protected abstract void checkExecutionException(Exception e);

  
Checks that the exception is the correct type of interruption exception.
 
   protected abstract void checkInterruptedException(Exception e);
 
   @Override
   protected <V> ListenableFuture<V> createListenableFuture(V value,
       Exception exceptCountDownLatch waitOn) {
     return createCheckedFuture(valueexceptwaitOn);
   }

  
Tests that the com.google.common.util.concurrent.CheckedFuture.checkedGet() method throws the correct type of cancellation exception when it is cancelled.
 
 
     final CheckedFuture<Boolean, ?> future =
         createCheckedFuture(.null);
 
     assertFalse(future.isDone());
     assertFalse(future.isCancelled());
 
     new Thread(new Runnable() {
       @Override
       public void run() {
         future.cancel(true);
       }
     }).start();
 
     try {
       future.checkedGet();
       fail("RPC Should have been cancelled.");
     } catch (Exception e) {
       checkCancelledException(e);
     }
 
     assertTrue(future.isDone());
     assertTrue(future.isCancelled());
   }
 
       throws InterruptedException {
 
     final CheckedFuture<Boolean, ?> future =
         createCheckedFuture(.null);
 
    final CountDownLatch startingGate = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    assertFalse(future.isDone());
    assertFalse(future.isCancelled());
    Thread getThread = new Thread(new Runnable() {
      @Override
      public void run() {
        startingGate.countDown();
        try {
          future.checkedGet();
        } catch (Exception e) {
          checkInterruptedException(e);
          // This only gets hit if the original call throws an exception and
          // the check call above passes.
          successLatch.countDown();
        }
      }
    });
    getThread.start();
    assertTrue(startingGate.await(500, .));
    getThread.interrupt();
    assertTrue(successLatch.await(500, .));
    assertFalse(future.isDone());
    assertFalse(future.isCancelled());
  }
    final CheckedFuture<Boolean, ?> future =
        createCheckedFuture(.new Exception("Error"), );
    assertFalse(future.isDone());
    assertFalse(future.isCancelled());
    new Thread(new Runnable() {
      @Override
      public void run() {
        .countDown();
      }
    }).start();
    try {
      future.checkedGet();
      fail();
    } catch (Exception e) {
    }
    assertTrue(future.isDone());
    assertFalse(future.isCancelled());
  }
New to GrepCode? Check out our FAQ X