@Override public String get(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { delegateForDelayedRuntimeException.get(timeout, unit); throw new RuntimeException(); } };
@Override public String get() throws ExecutionException, InterruptedException { delegateForDelayedRuntimeException.get(); throw new RuntimeException(); }
public void await() { try { future.get(); } catch (Throwable e) { throw new RuntimeException(e); } } }
public void testDefaultState() throws Exception { try { future.get(5, TimeUnit.MILLISECONDS); fail(); } catch (TimeoutException expected) { } }
@GwtIncompatible // Mockito public void testOnSuccessThrowsRuntimeException() throws Exception { RuntimeException exception = new RuntimeException(); String result = "result"; SettableFuture<String> future = SettableFuture.create(); @SuppressWarnings("unchecked") // Safe for a mock FutureCallback<String> callback = Mockito.mock(FutureCallback.class); addCallback(future, callback, directExecutor()); Mockito.doThrow(exception).when(callback).onSuccess(result); future.set(result); assertEquals(result, future.get()); Mockito.verify(callback).onSuccess(result); Mockito.verifyNoMoreInteractions(callback); }
@GwtIncompatible // Mockito public void testOnSuccessThrowsError() throws Exception { class TestError extends Error {} TestError error = new TestError(); String result = "result"; SettableFuture<String> future = SettableFuture.create(); @SuppressWarnings("unchecked") // Safe for a mock FutureCallback<String> callback = Mockito.mock(FutureCallback.class); addCallback(future, callback, directExecutor()); Mockito.doThrow(error).when(callback).onSuccess(result); try { future.set(result); fail("Should have thrown"); } catch (TestError e) { assertSame(error, e); } assertEquals(result, future.get()); Mockito.verify(callback).onSuccess(result); Mockito.verifyNoMoreInteractions(callback); }
resultFuture.get(); fail(); } catch (CancellationException expected) {
resultFuture.get(); fail(); } catch (CancellationException expected) {
public void testCancel_innerCancelsAsync() throws Exception { SettableFuture<Object> async = SettableFuture.create(); SettableFuture<Object> inner = SettableFuture.create(); async.setFuture(inner); inner.cancel(true); assertTrue(async.isCancelled()); try { async.get(); fail("Expected CancellationException"); } catch (CancellationException expected) { /* expected */ } }
resultFuture.get(); fail(); } catch (CancellationException expected) {
resultFuture.get(); fail(); } catch (CancellationException expected) {
public void testSetValue_simpleThreaded() throws Exception { SettableFuture<Integer> future = SettableFuture.create(); assertTrue(future.set(42)); // Later attempts to set the future should return false. assertFalse(future.set(23)); assertFalse(future.setException(new Exception("bar"))); assertFalse(future.setFuture(SettableFuture.<Integer>create())); // Check that the future has been set properly. assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertEquals(42, (int) future.get()); }
public void testSetException() throws Exception { SettableFuture<Object> future = SettableFuture.create(); Exception e = new Exception("foobarbaz"); assertTrue(future.setException(e)); // Later attempts to set the future should return false. assertFalse(future.set(23)); assertFalse(future.setException(new Exception("quux"))); assertFalse(future.setFuture(SettableFuture.create())); // Check that the future has been set properly. assertTrue(future.isDone()); assertFalse(future.isCancelled()); try { future.get(); fail("Expected ExecutionException"); } catch (ExecutionException ee) { assertThat(ee).hasCauseThat().isSameAs(e); } }
future.get(10, TimeUnit.SECONDS); try { executor.execute(Runnables.doNothing());
private static void assertStateChange(StateMachine<State> stateMachine, StateChanger stateChange, State expectedState) throws Exception { State initialState = stateMachine.get(); ListenableFuture<State> futureChange = stateMachine.getStateChange(initialState); SettableFuture<State> listenerChange = addTestListener(stateMachine); stateChange.run(); assertEquals(stateMachine.get(), expectedState); assertEquals(futureChange.get(10, SECONDS), expectedState); assertEquals(listenerChange.get(10, SECONDS), expectedState); // listeners should not be retained if we are in a terminal state boolean isTerminalState = stateMachine.isTerminalState(expectedState); if (isTerminalState) { assertEquals(stateMachine.getStateChangeListeners(), ImmutableSet.of()); } }
public void testCancel_resultCancelsInner() throws Exception { SettableFuture<Object> async = SettableFuture.create(); SettableFuture<Object> inner = SettableFuture.create(); async.setFuture(inner); async.cancel(false); assertTrue(inner.isCancelled()); assertFalse(inner.wasInterrupted()); try { inner.get(); fail("Expected CancellationException"); } catch (CancellationException expected) { /* expected */ } }
public void testCancel_resultCancelsInner_interrupted() throws Exception { SettableFuture<Object> async = SettableFuture.create(); SettableFuture<Object> inner = SettableFuture.create(); async.setFuture(inner); async.cancel(true); assertTrue(inner.isCancelled()); assertTrue(inner.wasInterrupted()); try { inner.get(); fail("Expected CancellationException"); } catch (CancellationException expected) { /* expected */ } }
public void testSetFuture() throws Exception { SettableFuture<String> future = SettableFuture.create(); SettableFuture<String> nested = SettableFuture.create(); assertTrue(future.setFuture(nested)); // Later attempts to set the future should return false. assertFalse(future.set("x")); assertFalse(future.setException(new Exception("bar"))); assertFalse(future.setFuture(SettableFuture.<String>create())); // Check that the future has been set properly. assertFalse(future.isDone()); assertFalse(future.isCancelled()); try { future.get(0, TimeUnit.MILLISECONDS); fail("Expected TimeoutException"); } catch (TimeoutException expected) { /* expected */ } nested.set("foo"); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertEquals("foo", future.get()); }
public void testSetFuture_genericsHierarchy() throws Exception { SettableFuture<Foo> future = SettableFuture.create(); SettableFuture<FooChild> nested = SettableFuture.create(); assertTrue(future.setFuture(nested)); // Later attempts to set the future should return false. assertFalse(future.set(new Foo())); assertFalse(future.setException(new Exception("bar"))); assertFalse(future.setFuture(SettableFuture.<Foo>create())); // Check that the future has been set properly. assertFalse(future.isDone()); assertFalse(future.isCancelled()); try { future.get(0, TimeUnit.MILLISECONDS); fail("Expected TimeoutException"); } catch (TimeoutException expected) { /* expected */ } FooChild value = new FooChild(); nested.set(value); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertSame(value, future.get()); }
public void testFutureCancellableBeforeFunctionCompletion() throws Exception { // Set the result in a separate thread since this test runs the function // (which will block) in the same thread. new Thread() { @Override public void run() { inputFuture.set(SLOW_FUNC_VALID_INPUT_DATA); } }.start(); funcIsWaitingLatch.await(); assertTrue(resultFuture.cancel(true)); assertTrue(resultFuture.isCancelled()); assertFalse(inputFuture.isCancelled()); assertFalse(outputFuture.isCancelled()); try { resultFuture.get(); fail("Result future is cancelled and should have thrown a" + " CancellationException"); } catch (CancellationException expected) { } funcCompletionLatch.countDown(); // allow the function to complete try { outputFuture.get(); fail( "The function output future is cancelled and should have thrown a" + " CancellationException"); } catch (CancellationException expected) { } }