Java 类com.google.common.util.concurrent.FuturesTest.SingleCallListener 实例源码

项目:guava-mock    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadUsesGivenExecutor() throws Exception {
  ExecutorService executorService = newCachedThreadPool(
      new ThreadFactoryBuilder().setDaemon(true).build());
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  ExecutorSpy spy = new ExecutorSpy(executorService);
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, spy);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(spy.wasExecuted);
  assertFalse(singleCallListener.wasCalled());
  assertFalse(listenableFuture.isDone());

  listenableFuture.addListener(singleCallListener, executorService);
  abstractFuture.set(DATA1);
  assertEquals(DATA1, listenableFuture.get());
  singleCallListener.waitForCall();

  assertTrue(spy.wasExecuted);
  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:googles-monorepo-demo    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadUsesGivenExecutor() throws Exception {
  ExecutorService executorService = newCachedThreadPool(
      new ThreadFactoryBuilder().setDaemon(true).build());
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  ExecutorSpy spy = new ExecutorSpy(executorService);
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, spy);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(spy.wasExecuted);
  assertFalse(singleCallListener.wasCalled());
  assertFalse(listenableFuture.isDone());

  listenableFuture.addListener(singleCallListener, executorService);
  abstractFuture.set(DATA1);
  assertEquals(DATA1, listenableFuture.get());
  singleCallListener.waitForCall();

  assertTrue(spy.wasExecuted);
  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:guava-libraries    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadUsesGivenExecutor() throws Exception {
  ExecutorService executorService = newCachedThreadPool(
      new ThreadFactoryBuilder().setDaemon(true).build());
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  ExecutorSpy spy = new ExecutorSpy(executorService);
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, spy);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(spy.wasExecuted);
  assertFalse(singleCallListener.wasCalled());
  assertFalse(listenableFuture.isDone());

  listenableFuture.addListener(singleCallListener, executorService);
  abstractFuture.set(DATA1);
  assertEquals(DATA1, listenableFuture.get());
  singleCallListener.waitForCall();

  assertTrue(spy.wasExecuted);
  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:guava-mock    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadIgnoresExecutorWhenDelegateIsDone()
    throws Exception {
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  abstractFuture.set(DATA1);
  ExecutorSpy spy = new ExecutorSpy(directExecutor());
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, spy);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(spy.wasExecuted);
  assertFalse(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone()); // We call AbstractFuture#set above.

  // #addListener() will run the listener immediately because the Future is
  // already finished (we explicitly set the result of it above).
  listenableFuture.addListener(singleCallListener, directExecutor());
  assertEquals(DATA1, listenableFuture.get());

  // 'spy' should have been ignored since 'abstractFuture' was done before
  // a listener was added.
  assertFalse(spy.wasExecuted);
  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:guava-mock    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadCustomExecutorInterrupted()
    throws Exception {
  final CountDownLatch submitSuccessful = new CountDownLatch(1);
  ExecutorService executorService = new ThreadPoolExecutor(
      0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
      new SynchronousQueue<Runnable>(),
      new ThreadFactoryBuilder().setDaemon(true).build()) {
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
      submitSuccessful.countDown();
    }
  };
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, executorService);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(singleCallListener.wasCalled());
  assertFalse(listenableFuture.isDone());

  listenableFuture.addListener(singleCallListener, directExecutor());
  /*
   * Don't shut down until the listenInPoolThread task has been accepted to
   * run. We want to see what happens when it's interrupted, not when it's
   * rejected.
   */
  submitSuccessful.await();
  executorService.shutdownNow();
  abstractFuture.set(DATA1);
  assertEquals(DATA1, listenableFuture.get());
  singleCallListener.waitForCall();

  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:googles-monorepo-demo    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadIgnoresExecutorWhenDelegateIsDone()
    throws Exception {
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  abstractFuture.set(DATA1);
  ExecutorSpy spy = new ExecutorSpy(directExecutor());
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, spy);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(spy.wasExecuted);
  assertFalse(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone()); // We call AbstractFuture#set above.

  // #addListener() will run the listener immediately because the Future is
  // already finished (we explicitly set the result of it above).
  listenableFuture.addListener(singleCallListener, directExecutor());
  assertEquals(DATA1, listenableFuture.get());

  // 'spy' should have been ignored since 'abstractFuture' was done before
  // a listener was added.
  assertFalse(spy.wasExecuted);
  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:googles-monorepo-demo    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadCustomExecutorInterrupted()
    throws Exception {
  final CountDownLatch submitSuccessful = new CountDownLatch(1);
  ExecutorService executorService = new ThreadPoolExecutor(
      0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
      new SynchronousQueue<Runnable>(),
      new ThreadFactoryBuilder().setDaemon(true).build()) {
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
      submitSuccessful.countDown();
    }
  };
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, executorService);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(singleCallListener.wasCalled());
  assertFalse(listenableFuture.isDone());

  listenableFuture.addListener(singleCallListener, directExecutor());
  /*
   * Don't shut down until the listenInPoolThread task has been accepted to
   * run. We want to see what happens when it's interrupted, not when it's
   * rejected.
   */
  submitSuccessful.await();
  executorService.shutdownNow();
  abstractFuture.set(DATA1);
  assertEquals(DATA1, listenableFuture.get());
  singleCallListener.waitForCall();

  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:guava-libraries    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadIgnoresExecutorWhenDelegateIsDone()
    throws Exception {
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  abstractFuture.set(DATA1);
  ExecutorSpy spy = new ExecutorSpy(directExecutor());
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, spy);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(spy.wasExecuted);
  assertFalse(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone()); // We call AbstractFuture#set above.

  // #addListener() will run the listener immediately because the Future is
  // already finished (we explicitly set the result of it above).
  listenableFuture.addListener(singleCallListener, directExecutor());
  assertEquals(DATA1, listenableFuture.get());

  // 'spy' should have been ignored since 'abstractFuture' was done before
  // a listener was added.
  assertFalse(spy.wasExecuted);
  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}
项目:guava-libraries    文件:JdkFutureAdaptersTest.java   
public void testListenInPoolThreadCustomExecutorInterrupted()
    throws Exception {
  final CountDownLatch submitSuccessful = new CountDownLatch(1);
  ExecutorService executorService = new ThreadPoolExecutor(
      0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
      new SynchronousQueue<Runnable>(),
      new ThreadFactoryBuilder().setDaemon(true).build()) {
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
      submitSuccessful.countDown();
    }
  };
  NonListenableSettableFuture<String> abstractFuture =
      NonListenableSettableFuture.create();
  ListenableFuture<String> listenableFuture =
      listenInPoolThread(abstractFuture, executorService);

  SingleCallListener singleCallListener = new SingleCallListener();
  singleCallListener.expectCall();

  assertFalse(singleCallListener.wasCalled());
  assertFalse(listenableFuture.isDone());

  listenableFuture.addListener(singleCallListener, directExecutor());
  /*
   * Don't shut down until the listenInPoolThread task has been accepted to
   * run. We want to see what happens when it's interrupted, not when it's
   * rejected.
   */
  submitSuccessful.await();
  executorService.shutdownNow();
  abstractFuture.set(DATA1);
  assertEquals(DATA1, listenableFuture.get());
  singleCallListener.waitForCall();

  assertTrue(singleCallListener.wasCalled());
  assertTrue(listenableFuture.isDone());
}