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()); }
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()); }
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()); }