com.google.common.util.concurrent.FuturesTest.ExecutorSpy

Here are the examples of the java api class com.google.common.util.concurrent.FuturesTest.ExecutorSpy taken from open source projects.

1. JdkFutureAdaptersTest#testListenInPoolThreadUsesGivenExecutor()

Project: guava
Source File: JdkFutureAdaptersTest.java
View license
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());
}

2. JdkFutureAdaptersTest#testListenInPoolThreadIgnoresExecutorWhenDelegateIsDone()

Project: guava
Source File: JdkFutureAdaptersTest.java
View license
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());
    // We call AbstractFuture#set above.
    assertTrue(listenableFuture.isDone());
    // #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());
}