com.google.common.util.concurrent.SettableFuture#cancel ( )源码实例Demo

下面列出了com.google.common.util.concurrent.SettableFuture#cancel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: presto   文件: TestStateMachine.java
private static void assertNoStateChange(StateMachine<State> stateMachine, StateChanger stateChange)
{
    State initialState = stateMachine.get();
    ListenableFuture<State> futureChange = stateMachine.getStateChange(initialState);

    SettableFuture<State> listenerChange = addTestListener(stateMachine);

    // listeners should not be added if we are in a terminal state, but listener should fire
    boolean isTerminalState = stateMachine.isTerminalState(initialState);
    if (isTerminalState) {
        assertEquals(stateMachine.getStateChangeListeners(), ImmutableSet.of());
    }

    stateChange.run();

    assertEquals(stateMachine.get(), initialState);

    // the future change will trigger if the state machine is in a terminal state
    // this is to prevent waiting for state changes that will never occur
    assertEquals(futureChange.isDone(), isTerminalState);
    futureChange.cancel(true);

    // test listener future only completes if the state actually changed
    assertFalse(listenerChange.isDone());
    listenerChange.cancel(true);
}
 
public void schedule(ScheduledCommand command) {
   logger.trace("Scheduling command [{}]", command);
   final SettableFuture<ScheduledTask> ref = SettableFuture.create();
   pendingRequests.put(command.getSchedulerAddress(), ref);
   try {
      ScheduledTask task = scheduler.scheduleAt(() -> dispatch(command, ref), command.getScheduledTime());
      ref.set(task);
      metrics.onCommandScheduled();
   }
   finally {
      // should never happen, but...
      // if anything goes wrong, clear it out
      if(!ref.isDone()) {
         pendingRequests.remove(command.getSchedulerAddress(), ref);
         ref.cancel(true);
      }
   }
}
 
@Test
public void testCancelledMismatchFormatting() throws Exception {
  final SettableFuture<Void> future = SettableFuture.create();

  try {
    // Cancel the future
    future.cancel(true);
    final StringDescription description = new StringDescription();
    SUT.describeMismatch(future, description);

    assertThat(description.toString(), is("a future that was cancelled"));
  } finally {
    // This will cause the future's thread to throw InterruptedException and make it return
    future.cancel(true);
  }
}
 
@Test
public void testCancelledMismatchFormatting() throws Exception {
  final SettableFuture<Void> future = SettableFuture.create();

  try {
    // Cancel the future
    future.cancel(true);
    final StringDescription description = new StringDescription();
    SUT.describeMismatch(future, description);

    assertThat(description.toString(), is("a future that was cancelled"));
  } finally {
    // Clear the interrupted flag to avoid interference between tests
    Thread.interrupted();
  }
}
 
源代码5 项目: buck   文件: MoreFuturesTest.java
@Test
public void isSuccessReturnsTrueOnlyWhenFutureSuccessful() throws InterruptedException {
  SettableFuture<Object> unresolvedFuture = SettableFuture.create();
  assertFalse(MoreFutures.isSuccess(unresolvedFuture));

  SettableFuture<Object> failedFuture = SettableFuture.create();
  failedFuture.setException(new RuntimeException());
  assertFalse(MoreFutures.isSuccess(failedFuture));

  SettableFuture<Object> cancelledFuture = SettableFuture.create();
  cancelledFuture.cancel(/* mayInterruptIfRunning */ true);
  assertFalse(MoreFutures.isSuccess(cancelledFuture));

  SettableFuture<Object> resolvedFuture = SettableFuture.create();
  resolvedFuture.set(new Object());
  assertTrue(MoreFutures.isSuccess(resolvedFuture));
}
 
@Test
public void testCancelledMismatchFormatting() throws Exception {
  final SettableFuture<Void> future = SettableFuture.create();

  // Cancel the future
  future.cancel(true);
  final StringDescription description = new StringDescription();
  SUT.describeMismatch(future, description);

  assertThat(description.toString(), is("a future that was cancelled"));
}
 
@Test
public void getRepositoriesCancellation() throws ExecutionException, InterruptedException, TimeoutException {
    // Create context
    URI serverUri = URI.create("http://server");
    AuthenticationInfo info = new AuthenticationInfo("", "", "", "");
    TeamProjectCollectionReference collection = new TeamProjectCollectionReference();
    ServerContext context = new ServerContextBuilder().type(ServerContext.Type.TFS).authentication(info).uri(serverUri).collection(collection).build();
    MockServerContextLookupOperation operation = new MockServerContextLookupOperation(Collections.singletonList(context), ServerContextLookupOperation.ContextScope.REPOSITORY);
    operation.cancelWhenStarted();

    // set up listener
    final SettableFuture<Boolean> startedCalled = SettableFuture.create();
    final SettableFuture<Boolean> completedCalled = SettableFuture.create();
    final SettableFuture<Boolean> canceledCalled = SettableFuture.create();
    final SettableFuture<List<ServerContext>> results = SettableFuture.create();
    setupListener(operation, startedCalled, completedCalled, canceledCalled, results);

    // do lookup
    operation.doWorkAsync(Operation.EMPTY_INPUTS);

    // Verify results
    Assert.assertTrue(startedCalled.get(30, TimeUnit.SECONDS));
    Assert.assertTrue(canceledCalled.get(30, TimeUnit.SECONDS));
    Assert.assertTrue(completedCalled.get(30, TimeUnit.SECONDS));
    //TODO: Assert.assertFalse(results.isDone()); - causing build failure on Mac from command line, so commenting out

    completedCalled.cancel(true);
    results.cancel(true);
}
 
源代码8 项目: buck   文件: MoreFuturesTest.java
@Test
public void getFuturesUncheckedInterruptiblyThrowsCancelledExceptionWhenFutureCancelled() {
  SettableFuture<?> future = SettableFuture.create();

  future.cancel(true);

  expectedException.expect(CancellationException.class);
  MoreFutures.getUncheckedInterruptibly(future);
}
 
@Test
public void getRepositoriesSync() throws ExecutionException, InterruptedException {
    // Create context
    URI serverUri = URI.create("http://server");
    AuthenticationInfo info = new AuthenticationInfo("", "", "", "");
    TeamProjectCollectionReference collection = new TeamProjectCollectionReference();
    ServerContext context = new ServerContextBuilder().type(ServerContext.Type.TFS).authentication(info).uri(serverUri).collection(collection).build();
    MockServerContextLookupOperation operation = new MockServerContextLookupOperation(Collections.singletonList(context), ServerContextLookupOperation.ContextScope.REPOSITORY);

    // create 3 repos 2 from same project
    String repoName1 = "repo1";
    String repoName2 = "repo2";
    String repoName3 = "repo3";
    String projectName1 = "project1";
    String projectName2 = "project2";
    TeamProjectReference project1 = new TeamProjectReference();
    project1.setName(projectName1);
    TeamProjectReference project2 = new TeamProjectReference();
    project2.setName(projectName2);
    GitRepository repo1 = new GitRepository();
    repo1.setName(repoName1);
    repo1.setProjectReference(project1);
    repo1.setRemoteUrl("http://server/_git/repo1");
    GitRepository repo2 = new GitRepository();
    repo2.setName(repoName2);
    repo2.setProjectReference(project2);
    repo2.setRemoteUrl("http://server/_git/repo2");
    GitRepository repo3 = new GitRepository();
    repo3.setName(repoName3);
    repo3.setProjectReference(project1);
    repo3.setRemoteUrl("http://server/_git/repo3");

    // add these repos to the Mock operation as our results
    operation.addRepository(repo1);
    operation.addRepository(repo2);
    operation.addRepository(repo3);

    // set up listener
    final SettableFuture<Boolean> startedCalled = SettableFuture.create();
    final SettableFuture<Boolean> completedCalled = SettableFuture.create();
    final SettableFuture<Boolean> canceledCalled = SettableFuture.create();
    final SettableFuture<List<ServerContext>> results = SettableFuture.create();
    setupListener(operation, startedCalled, completedCalled, canceledCalled, results);

    // do lookup
    operation.doWork(Operation.EMPTY_INPUTS);

    // Verify results
    List<ServerContext> newContexts = results.get();
    Assert.assertEquals(3, newContexts.size());
    Assert.assertEquals(repo1, newContexts.get(0).getGitRepository());
    Assert.assertEquals(repo2, newContexts.get(1).getGitRepository());
    Assert.assertEquals(repo3, newContexts.get(2).getGitRepository());
    Assert.assertTrue(startedCalled.get());
    Assert.assertTrue(completedCalled.get());
    Assert.assertFalse(canceledCalled.isDone());

    // cancel remaining futures
    canceledCalled.cancel(true);
}
 
@Test
public void getRepositoriesAsync() throws ExecutionException, InterruptedException {
    // Create context
    URI serverUri = URI.create("http://server");
    AuthenticationInfo info = new AuthenticationInfo("", "", "", "");
    TeamProjectCollectionReference collection = new TeamProjectCollectionReference();
    ServerContext context = new ServerContextBuilder().type(ServerContext.Type.TFS).authentication(info).uri(serverUri).collection(collection).build();
    MockServerContextLookupOperation operation = new MockServerContextLookupOperation(Collections.singletonList(context), ServerContextLookupOperation.ContextScope.REPOSITORY);

    // create 3 repos 2 from same project
    String repoName1 = "repo1";
    String repoName2 = "repo2";
    String repoName3 = "repo3";
    String projectName1 = "project1";
    String projectName2 = "project2";
    TeamProjectReference project1 = new TeamProjectReference();
    project1.setName(projectName1);
    TeamProjectReference project2 = new TeamProjectReference();
    project2.setName(projectName2);
    GitRepository repo1 = new GitRepository();
    repo1.setName(repoName1);
    repo1.setProjectReference(project1);
    repo1.setRemoteUrl("http://server/_git/repo1");
    GitRepository repo2 = new GitRepository();
    repo2.setName(repoName2);
    repo2.setProjectReference(project2);
    repo2.setRemoteUrl("http://server/_git/repo2");
    GitRepository repo3 = new GitRepository();
    repo3.setName(repoName3);
    repo3.setProjectReference(project1);
    repo3.setRemoteUrl("http://server/_git/repo3");

    // add these repos to the Mock operation as our results
    operation.addRepository(repo1);
    operation.addRepository(repo2);
    operation.addRepository(repo3);

    // set up listener
    final SettableFuture<Boolean> startedCalled = SettableFuture.create();
    final SettableFuture<Boolean> completedCalled = SettableFuture.create();
    final SettableFuture<Boolean> canceledCalled = SettableFuture.create();
    final SettableFuture<List<ServerContext>> results = SettableFuture.create();
    setupListener(operation, startedCalled, completedCalled, canceledCalled, results);

    // do lookup
    operation.doWorkAsync(Operation.EMPTY_INPUTS);

    // Verify results
    List<ServerContext> newContexts = results.get();
    Assert.assertEquals(3, newContexts.size());
    Assert.assertEquals(repo1, newContexts.get(0).getGitRepository());
    Assert.assertEquals(repo2, newContexts.get(1).getGitRepository());
    Assert.assertEquals(repo3, newContexts.get(2).getGitRepository());
    Assert.assertTrue(startedCalled.get());
    Assert.assertTrue(completedCalled.get());
    Assert.assertFalse(canceledCalled.isDone());

    // cancel remaining futures
    canceledCalled.cancel(true);
}