本文整理了Java中com.google.common.util.concurrent.SettableFuture.create()
方法的一些代码示例,展示了SettableFuture.create()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SettableFuture.create()
方法的具体详情如下:
包路径:com.google.common.util.concurrent.SettableFuture
类名称:SettableFuture
方法名:create
[英]Creates a new SettableFuture that can be completed or cancelled by a later method call.
[中]创建一个新的SettableFuture,可以通过稍后的方法调用来完成或取消。
代码示例来源:origin: google/guava
public void testSameThreadSuccess() {
SettableFuture<String> f = SettableFuture.create();
MockCallback callback = new MockCallback("foo");
addCallback(f, callback, directExecutor());
f.set("foo");
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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();
}
代码示例来源:origin: google/guava
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)));
}
}
代码示例来源:origin: google/guava
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);
}
}
代码示例来源:origin: google/guava
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();
}
代码示例来源:origin: google/guava
public void testTrustedGetFailure_Completed() {
SettableFuture<String> future = SettableFuture.create();
future.set("261");
assertThat(future.tryInternalFastPathGetFailure()).isNull();
}
代码示例来源:origin: google/guava
public void testSetSelf_toString() {
SettableFuture<Object> orig = SettableFuture.create();
orig.set(orig);
assertThat(orig.toString()).contains("[status=SUCCESS, result=[this future]]");
}
代码示例来源:origin: google/guava
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));
}
代码示例来源:origin: google/guava
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));
}
代码示例来源:origin: google/guava
public void testCancel_beforeSet() throws Exception {
SettableFuture<Object> async = SettableFuture.create();
async.cancel(true);
assertFalse(async.set(42));
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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) {
}
}
代码示例来源:origin: google/guava
@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);
}
代码示例来源:origin: google/guava
public void testExecutorSuccess() {
CountingSameThreadExecutor ex = new CountingSameThreadExecutor();
SettableFuture<String> f = SettableFuture.create();
MockCallback callback = new MockCallback("foo");
Futures.addCallback(f, callback, ex);
f.set("foo");
assertEquals(1, ex.runCount);
}
代码示例来源:origin: google/guava
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) {
}
}
代码示例来源:origin: google/guava
@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();
}
内容来源于网络,如有侵权,请联系作者删除!