Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.client.WorkflowStub;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CompletableFuture;

public class Starter {

/**
* Show the use and difference between getResult and getResultAsync for waiting on workflow
* results.
*/
@SuppressWarnings("FutureReturnValueIgnored")
public static void main(String[] args) {
MyWorkflow workflowStub1 =
Worker.client.newWorkflowStub(
Expand All @@ -37,29 +36,16 @@ public static void main(String[] args) {

// getResultAsync returns a CompletableFuture
// It is not a blocking call like getResult(...)
untypedStub2
.getResultAsync(String.class)
.thenApply(
result2 -> {
System.out.println("Result2: " + result2);
return result2;
});

System.out.println("Waiting on result2...");
// Our workflow sleeps for 5 seconds (async)
// Here we block the thread (Thread.sleep) for 7 (2 more than the workflow exec time)
// To show that getResultsAsync completion happens during this time (async)
sleep(7);
printAsyncResult(untypedStub2.getResultAsync(String.class));
System.out.println("Done waiting on result2...");
System.exit(0);
}

private static void sleep(int seconds) {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(seconds));
} catch (InterruptedException e) {
System.out.println("Exception: " + e.getMessage());
System.exit(0);
}
static void printAsyncResult(CompletableFuture<String> result) {
CompletableFuture<Void> printedResult =
result.thenAccept(result2 -> System.out.println("Result2: " + result2));

System.out.println("Waiting on result2...");
printedResult.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.temporal.samples.getresultsasync;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.junit.Test;

public class StarterTest {

@Test
public void propagatesAsyncFailure() {
IllegalStateException failure = new IllegalStateException("workflow failed");

CompletionException thrown =
assertThrows(
CompletionException.class,
() -> Starter.printAsyncResult(CompletableFuture.failedFuture(failure)));

assertSame(failure, thrown.getCause());
}

@Test
public void waitsForTheResultPrintingStage() {
CompletableFuture<String> result =
new CompletableFuture<String>() {
@Override
public <U> CompletableFuture<U> newIncompleteFuture() {
return new CompletableFuture<U>() {
@Override
public U join() {
completeResult();
return super.join();
}
};
}

private void completeResult() {
complete("workflow result");
}
};

Starter.printAsyncResult(result);

assertTrue("The pending result should complete during the wait", result.isDone());
}
}