Java Code Examples for com.google.common.util.concurrent.SettableFuture#cancel()

The following examples show how to use com.google.common.util.concurrent.SettableFuture#cancel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStateMachine.java    From presto with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: PlatformEventSchedulerService.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
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);
      }
   }
}
 
Example 3
Source File: ExceptionallyCompletedFutureTest.java    From java-hamcrest with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 4
Source File: SuccessfullyCompletedFutureTest.java    From java-hamcrest with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 5
Source File: MoreFuturesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 6
Source File: ExceptionallyCompletedBlockingFutureTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 7
Source File: ServerContextLookupOperationTest.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: MoreFuturesTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void getFuturesUncheckedInterruptiblyThrowsCancelledExceptionWhenFutureCancelled() {
  SettableFuture<?> future = SettableFuture.create();

  future.cancel(true);

  expectedException.expect(CancellationException.class);
  MoreFutures.getUncheckedInterruptibly(future);
}
 
Example 9
Source File: ServerContextLookupOperationTest.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
@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);
}
 
Example 10
Source File: ServerContextLookupOperationTest.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
@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);
}