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 testSetFuture_stackOverflow() { SettableFuture<String> orig = SettableFuture.create(); SettableFuture<String> prev = orig; for (int i = 0; i < 100000; i++) { SettableFuture<String> curr = SettableFuture.create(); prev.setFuture(curr); prev = curr; } // prev represents the 'innermost' future prev.set("done"); assertTrue(orig.isDone()); }
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 */ } }
public void testSuccessfulAsList_totalFailure() throws Exception { SingleCallListener listener = new SingleCallListener(); SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = successfulAsList(future1, future2); compound.addListener(listener, directExecutor()); assertFalse(compound.isDone()); future1.setException(new Throwable("failed1")); assertFalse(compound.isDone()); listener.expectCall(); future2.setException(new Throwable("failed2")); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(null, null).inOrder(); }
/** * Test the case where the futures are fulfilled prior to constructing the ListFuture. There was a * bug where the loop that connects a Listener to each of the futures would die on the last * loop-check as done() on ListFuture nulled out the variable being looped over (the list of * futures). */ public void testAllAsList_doneFutures() throws Exception { // Create input and output SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); SettableFuture<String> future3 = SettableFuture.create(); // Satisfy each input prior to creating compound and check the output future1.set(DATA1); future2.set(DATA2); future3.set(DATA3); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2, future3); // Attach a listener SingleCallListener listener = new SingleCallListener(); listener.expectCall(); compound.addListener(listener, directExecutor()); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(DATA1, DATA2, DATA3).inOrder(); }
public void testTransformAsync_cancelPropagatesToInput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); AsyncFunction<Foo, Bar> function = new AsyncFunction<Foo, Bar>() { @Override public ListenableFuture<Bar> apply(Foo unused) { throw new AssertionFailedError("Unexpeted call to apply."); } }; assertTrue(transformAsync(input, function, directExecutor()).cancel(false)); assertTrue(input.isCancelled()); assertFalse(input.wasInterrupted()); }
@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); }
public void testCallableStartsAfterFirstFutureCompletes() { @SuppressWarnings({"unused", "nullness"}) Future<?> possiblyIgnoredError = serializer.submitAsync(firstCallable, directExecutor()); TestCallable secondCallable = new TestCallable(Futures.<Void>immediateFuture(null)); @SuppressWarnings({"unused", "nullness"}) Future<?> possiblyIgnoredError1 = serializer.submitAsync(secondCallable, directExecutor()); assertThat(firstCallable.called).isTrue(); assertThat(secondCallable.called).isFalse(); firstFuture.set(null); assertThat(secondCallable.called).isTrue(); }
@GwtIncompatible // threads final CountDownLatch inFunction = new CountDownLatch(1); final CountDownLatch functionDone = new CountDownLatch(1); final SettableFuture<Integer> resultFuture = SettableFuture.create(); AsyncFunction<String, Integer> function = new AsyncFunction<String, Integer>() { SettableFuture<String> inputFuture = SettableFuture.create(); ListenableFuture<Integer> future = transformAsync(inputFuture, function, newSingleThreadExecutor()); inputFuture.set("value"); inFunction.await(); future.cancel(false); functionDone.countDown(); try { future.get(); fail(); } catch (CancellationException expected) { resultFuture.get(); fail(); } catch (CancellationException expected) {
@GwtIncompatible // get() timeout public void testTransformAsync_asyncFunction_timeout() throws InterruptedException, ExecutionException { AsyncFunction<String, Integer> function = constantAsyncFunction(immediateFuture(1)); ListenableFuture<Integer> future = transformAsync(SettableFuture.<String>create(), function, directExecutor()); try { future.get(1, MILLISECONDS); fail(); } catch (TimeoutException expected) { } }
@GwtIncompatible // threads SettableFuture<String> stringFuture = SettableFuture.create(); SettableFuture<Boolean> booleanFuture = SettableFuture.create(); final CountDownLatch inFunction = new CountDownLatch(1); final CountDownLatch shouldCompleteFunction = new CountDownLatch(1); final SettableFuture<String> resultFuture = SettableFuture.create(); AsyncCallable<String> combiner = new AsyncCallable<String>() { whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor()); stringFuture.set("value"); booleanFuture.set(true); inFunction.await(); futureResult.cancel(false); shouldCompleteFunction.countDown(); try { futureResult.get(); fail(); } catch (CancellationException expected) { resultFuture.get(); fail(); } catch (CancellationException expected) {
@GwtIncompatible // threads final CountDownLatch inFunction = new CountDownLatch(1); final CountDownLatch callableDone = new CountDownLatch(1); final SettableFuture<Integer> resultFuture = SettableFuture.create(); AsyncCallable<Integer> callable = new AsyncCallable<Integer>() { SettableFuture<String> inputFuture = SettableFuture.create(); ListenableFuture<Integer> future = submitAsync(callable, newSingleThreadExecutor()); inputFuture.set("value"); inFunction.await(); future.cancel(false); callableDone.countDown(); try { future.get(); fail(); } catch (CancellationException expected) { resultFuture.get(); fail(); } catch (CancellationException expected) {
public void testAllAsList_resultCancelled() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(false)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); }
public void testSameThreadSuccess() { SettableFuture<String> f = SettableFuture.create(); MockCallback callback = new MockCallback("foo"); addCallback(f, callback, directExecutor()); f.set("foo"); }
public void testAllAsList_failure() throws Exception { SingleCallListener listener = new SingleCallListener(); SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); compound.addListener(listener, directExecutor()); listener.expectCall(); Throwable exception = new Throwable("failed1"); future1.setException(exception); assertTrue(compound.isDone()); assertTrue(listener.wasCalled()); assertFalse(future2.isDone()); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
public void testSuccessfulAsList_resultCancelled() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = successfulAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(false)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); }
public void testSameThreadExecutionException() { SettableFuture<String> f = SettableFuture.create(); Exception e = new IllegalArgumentException("foo not found"); MockCallback callback = new MockCallback(e); addCallback(f, callback, directExecutor()); f.setException(e); }
public void testTransform_ErrorAfterCancellation() throws Exception { class Transformer implements Function<Object, Object> { ListenableFuture<Object> output; @Override public Object apply(Object input) { output.cancel(false); throw new MyError(); } } Transformer transformer = new Transformer(); SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = transform(input, transformer, directExecutor()); transformer.output = output; input.set("foo"); assertTrue(output.isCancelled()); }
public void testCatchingAsync_rejectionPropagatesToOutput() throws Exception { SettableFuture<String> input = SettableFuture.create(); ListenableFuture<String> transformed = catchingAsync( input, Throwable.class, constantAsyncFunction(immediateFuture("foo")), REJECTING_EXECUTOR); input.setException(new Exception()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }