Refine search
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 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 testSuccessfulAsList_partialFailure() 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.set(DATA2); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(null, DATA2).inOrder(); }
public void testCompletionOrderMixedBagOTypes() throws Exception { SettableFuture<Long> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); SettableFuture<Integer> future3 = SettableFuture.create(); ImmutableList<? extends ListenableFuture<?>> inputs = ImmutableList.<ListenableFuture<?>>of(future1, future2, future3); ImmutableList<ListenableFuture<Object>> futures = inCompletionOrder(inputs); future2.set("1L"); future1.set(2L); future3.set(3); ImmutableList<?> expected = ImmutableList.of("1L", 2L, 3); for (int i = 0; i < expected.size(); i++) { assertEquals(expected.get(i), getDone(futures.get(i))); } }
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); } }
public void testSuccessfulAsList_mixed() throws Exception { SingleCallListener listener = new SingleCallListener(); SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); SettableFuture<String> future3 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = successfulAsList(future1, future2, future3); compound.addListener(listener, directExecutor()); // First is cancelled, second fails, third succeeds assertFalse(compound.isDone()); future1.cancel(true); assertFalse(compound.isDone()); future2.setException(new Throwable("failed2")); assertFalse(compound.isDone()); listener.expectCall(); future3.set(DATA3); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(null, null, DATA3).inOrder(); }
public void testMakeUninterruptible_timedGetZeroTimeoutAttempted() throws TimeoutException, ExecutionException { SettableFuture<String> future = SettableFuture.create(); future.set(RESULT); /* * getUninterruptibly should call the timed get method once with a * wait of 0 seconds (and it should succeed, since the result is already * available). */ assertEquals(RESULT, getUninterruptibly(future, 0, SECONDS)); }
public void testMakeUninterruptible_timedGetNegativeTimeoutAttempted() throws TimeoutException, ExecutionException { SettableFuture<String> future = SettableFuture.create(); future.set(RESULT); /* * The getUninterruptibly should call the timed get method once with a * wait of -1 seconds (and it should succeed, since the result is already * available). */ assertEquals(RESULT, getUninterruptibly(future, -1, SECONDS)); }
public void testTransform_ExceptionAfterCancellation() throws Exception { class Transformer implements Function<Object, Object> { ListenableFuture<Object> output; @Override public Object apply(Object input) { output.cancel(false); throw new MyRuntimeException(); } } 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 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 testToString() { Future<?> first = serializer.submitAsync(firstCallable, directExecutor()); TestCallable secondCallable = new TestCallable(SettableFuture.<Void>create()); Future<?> second = serializer.submitAsync(secondCallable, directExecutor()); assertThat(secondCallable.called).isFalse(); assertThat(second.toString()).contains(secondCallable.toString()); firstFuture.set(null); assertThat(second.toString()).contains(secondCallable.future.toString()); }
public void testSetIndirectSelf_toString() { final SettableFuture<Object> orig = SettableFuture.create(); // unlike the above this indirection defeats the trivial cycle detection and causes a SOE orig.set( new Object() { @Override public String toString() { return orig.toString(); } }); try { orig.toString(); fail(); } catch (StackOverflowError 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); }
public void testTransform_listenerThrowsError() throws Exception { SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = transform(input, identity(), directExecutor()); output.addListener( new Runnable() { @Override public void run() { throw new MyError(); } }, directExecutor()); try { input.set("foo"); fail(); } catch (MyError expected) { } }
@AndroidIncompatible // reference is never cleared under some versions of the emulator @GwtIncompatible public void testInputGCedIfUnreferenced() throws Exception { SettableFuture<Long> future1 = SettableFuture.create(); SettableFuture<Long> future2 = SettableFuture.create(); WeakReference<SettableFuture<Long>> future1Ref = new WeakReference<>(future1); WeakReference<SettableFuture<Long>> future2Ref = new WeakReference<>(future2); ImmutableList<ListenableFuture<Long>> delegates = inCompletionOrder(ImmutableList.<ListenableFuture<Long>>of(future1, future2)); future1.set(1L); future1 = null; // First future is complete, should be unreferenced GcFinalization.awaitClear(future1Ref); ListenableFuture<Long> outputFuture1 = delegates.get(0); delegates = null; future2 = null; // No references to list or other output future, second future should be unreferenced GcFinalization.awaitClear(future2Ref); outputFuture1.get(); }