com.google.common.util.concurrent.SettableFuture.get()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中com.google.common.util.concurrent.SettableFuture.get()方法的一些代码示例,展示了SettableFuture.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SettableFuture.get()方法的具体详情如下:
包路径:com.google.common.util.concurrent.SettableFuture
类名称:SettableFuture
方法名:get

SettableFuture.get介绍

暂无

代码示例

代码示例来源:origin: google/guava

@Override
 public String get(long timeout, TimeUnit unit)
   throws ExecutionException, InterruptedException, TimeoutException {
  delegateForDelayedRuntimeException.get(timeout, unit);
  throw new RuntimeException();
 }
};

代码示例来源:origin: google/guava

@Override
public String get() throws ExecutionException, InterruptedException {
 delegateForDelayedRuntimeException.get();
 throw new RuntimeException();
}

代码示例来源:origin: prestodb/presto

public void await()
  {
    try {
      future.get();
    }
    catch (Throwable e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: google/guava

public void testDefaultState() throws Exception {
 try {
  future.get(5, TimeUnit.MILLISECONDS);
  fail();
 } catch (TimeoutException 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

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

代码示例来源:origin: google/guava

resultFuture.get();
 fail();
} catch (CancellationException expected) {

代码示例来源:origin: google/guava

resultFuture.get();
 fail();
} catch (CancellationException expected) {

代码示例来源:origin: google/guava

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 */
 }
}

代码示例来源:origin: google/guava

resultFuture.get();
 fail();
} catch (CancellationException expected) {

代码示例来源:origin: google/guava

resultFuture.get();
 fail();
} catch (CancellationException expected) {

代码示例来源: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 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

future.get(10, TimeUnit.SECONDS);
try {
 executor.execute(Runnables.doNothing());

代码示例来源:origin: prestodb/presto

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

代码示例来源:origin: google/guava

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 */
 }
}

代码示例来源:origin: google/guava

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 */
 }
}

代码示例来源:origin: google/guava

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

代码示例来源:origin: google/guava

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

代码示例来源:origin: google/guava

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) {
 }
}

相关文章