Sure thing!

package org.apache.geode.examples;

import static org.apache.geode.test.dunit.VM.getVM;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.Serializable;
import java.util.concurrent.Future;

import org.junit.Rule;
import org.junit.Test;

import org.apache.geode.test.dunit.AsyncInvocation;
import org.apache.geode.test.dunit.rules.DistributedRule;

public class ExampleDistributedTest implements Serializable {

  @Rule
  public DistributedRule distributedRule = new DistributedRule(1);

  /**
   * Awaits completion of AsyncInvocation with Void type.
   */
  @Test
  public void awaitCompletionOfAsyncInvocation() throws Exception {
    AsyncInvocation<Void> asyncActionInVM0 = getVM(0).invokeAsync(() ->
doAsyncAction());

    // do other actions in parallel

    asyncActionInVM0.await();
  }

  /**
   * Awaits completion of AsyncInvocation and then returns its result.
   */
  @Test
  public void getResultOfAsyncInvocation() throws Exception {
    AsyncInvocation<Serializable> asyncActionInVM0 =
        getVM(0).invokeAsync(() -> doAsyncActionWithReturnValue());

    // do other actions in parallel

    Serializable result = asyncActionInVM0.get();
    assertThat(result).isNotNull();
  }

  /**
   * Awaits completion of AsyncInvocation as a Future with Void type.
   */
  @Test
  public void awaitCompletionOfFuture() throws Exception {
    Future<Void> asyncActionInVM0 = getVM(0).invokeAsync(() ->
doAsyncAction());

    // do other actions in parallel

    asyncActionInVM0.get();
  }

  /**
   * Awaits completion of AsyncInvocation as a Future and then returns its
result.
   */
  @Test
  public void getResultOfFuture() throws Exception {
    Future<Serializable> asyncActionInVM0 =
        getVM(0).invokeAsync(() -> doAsyncActionWithReturnValue());

    // do other actions in parallel

    Serializable result = asyncActionInVM0.get();
    assertThat(result).isNotNull();
  }

  private void doAsyncAction() {
    // do some work that will need to execute asynchronously
  }

  private Serializable doAsyncActionWithReturnValue() {
    // do some work that will need to execute asynchronously
    return "Result";
  }
}

On Wed, Mar 18, 2020 at 3:17 PM Anilkumar Gingade <aging...@pivotal.io>
wrote:

> Thanks Kirk. Can you add an example here...
>
> On Wed, Mar 18, 2020 at 11:12 AM Kirk Lund <kl...@apache.org> wrote:
>
> > Tips on using AsyncInvocation:
> >
> > * Always use await() or get()
> > * Both check and throw any remote exceptions
> > * Both use GeodeAwaitility Timeout and will throw TimeoutException if
> it’s
> > exceeded
> > * Use await() for Void types and get() when expecting a non-null value
> >
> > Recent improvements:
> >
> > Timeout now gets a remote stack trace to use as the cause and dumps stack
> > traces for that JVM’s threads.
> >
> > You can also declare your instance of AsyncInvocation as a Future and
> > simply use the standard Java API for Futures. This basically means the
> test
> > will invoke get() for both Void and non-Void types.
> >
> > AsyncInvocation handles everything for you when you invoke await() or
> get()
> > -- there is no need to invoke any of the deprecated APIs on
> > AsyncInvocation:
> > * Both use the GeodeAwaitility Timeout and throw TImeoutException
> > * If Timeout occurs, AsyncInvocation will use the remote stack trace of
> the
> > stuck thread as the cause and it will also print all threads stacks for
> > that DUnit VM to facilitate debugging
> > * Both will check for a remote failure and rethrow it
> >
>

Reply via email to