Skip to content

Commit 437c612

Browse files
Fix Workflow.getWorkflowExecution for ExternalWorkflowStub (#2529)
1 parent d01c85b commit 437c612

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowInternal.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,18 @@ public static <T> T newExternalWorkflowStub(
418418

419419
public static Promise<WorkflowExecution> getWorkflowExecution(Object workflowStub) {
420420
if (workflowStub instanceof StubMarker) {
421-
Object stub = ((StubMarker) workflowStub).__getUntypedStub();
422-
return ((ChildWorkflowStub) stub).getExecution();
421+
Object untyped = ((StubMarker) workflowStub).__getUntypedStub();
422+
if (untyped instanceof ChildWorkflowStub) {
423+
return ((ChildWorkflowStub) untyped).getExecution();
424+
}
425+
426+
if (untyped instanceof ExternalWorkflowStub) {
427+
return newPromise(((ExternalWorkflowStub) untyped).getExecution());
428+
}
423429
}
424430
throw new IllegalArgumentException(
425-
"Not a workflow stub created through Workflow.newChildWorkflowStub: " + workflowStub);
431+
"Not a workflow stub created through Workflow.newChildWorkflowStub or Workflow.newExternalWorkflowStub: "
432+
+ workflowStub);
426433
}
427434

428435
public static ChildWorkflowStub newUntypedChildWorkflowStub(

temporal-sdk/src/main/java/io/temporal/workflow/Workflow.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,11 @@ public static <R> R newExternalWorkflowStub(
200200
/**
201201
* Extracts workflow execution from a stub created through {@link #newChildWorkflowStub(Class,
202202
* ChildWorkflowOptions)} or {@link #newExternalWorkflowStub(Class, String)}. Wrapped in a Promise
203-
* as child workflow start is asynchronous.
203+
* as child workflow start is asynchronous. For an external workflow the returned promise is
204+
* already completed.
204205
*/
205-
public static Promise<WorkflowExecution> getWorkflowExecution(Object childWorkflowStub) {
206-
return WorkflowInternal.getWorkflowExecution(childWorkflowStub);
206+
public static Promise<WorkflowExecution> getWorkflowExecution(Object workflowStub) {
207+
return WorkflowInternal.getWorkflowExecution(workflowStub);
207208
}
208209

209210
/**
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.temporal.workflow;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import io.temporal.api.common.v1.WorkflowExecution;
6+
import io.temporal.client.WorkflowClient;
7+
import io.temporal.client.WorkflowStub;
8+
import io.temporal.testing.internal.SDKTestWorkflowRule;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
12+
public class ExternalWorkflowGetExecutionTest {
13+
14+
@Rule
15+
public SDKTestWorkflowRule testWorkflowRule =
16+
SDKTestWorkflowRule.newBuilder()
17+
.setWorkflowTypes(ParentWorkflowImpl.class, ChildWorkflowImpl.class)
18+
.build();
19+
20+
@Test
21+
public void testGetExecutionFromExternalWorkflowStub() {
22+
ParentWorkflow client =
23+
testWorkflowRule.newWorkflowStub200sTimeoutOptions(ParentWorkflow.class);
24+
WorkflowExecution execution = WorkflowClient.start(client::execute);
25+
String result = WorkflowStub.fromTyped(client).getResult(String.class);
26+
assertEquals(execution.getWorkflowId(), result);
27+
}
28+
29+
@WorkflowInterface
30+
public interface ParentWorkflow {
31+
@WorkflowMethod
32+
String execute();
33+
}
34+
35+
@WorkflowInterface
36+
public interface ChildWorkflow {
37+
@WorkflowMethod
38+
String execute();
39+
}
40+
41+
public static class ParentWorkflowImpl implements ParentWorkflow {
42+
@Override
43+
public String execute() {
44+
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class);
45+
return child.execute();
46+
}
47+
}
48+
49+
public static class ChildWorkflowImpl implements ChildWorkflow {
50+
@Override
51+
public String execute() {
52+
String parentId = Workflow.getInfo().getParentWorkflowId().get();
53+
ParentWorkflow parent = Workflow.newExternalWorkflowStub(ParentWorkflow.class, parentId);
54+
return Workflow.getWorkflowExecution(parent).get().getWorkflowId();
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)