Skip to content

Commit 5c9599d

Browse files
Account for dynamic workflows when setting WorkflowImplementationOptions (#3042)
1 parent 6d6b743 commit 5c9599d

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎temporal-sdk/src/main/java/io/temporal/internal/sync/POJOWorkflowImplementationFactory.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public final class POJOWorkflowImplementationFactory implements ReplayWorkflowFa
7777
private Functions.Func1<EncodedValues, ? extends DynamicWorkflow>
7878
dynamicWorkflowImplementationFactory;
7979

80+
@Nullable private WorkflowImplementationOptions dynamicWorkflowImplementationOptions;
81+
8082
private final Map<String, WorkflowImplementationOptions> implementationOptions =
8183
Collections.synchronizedMap(new HashMap<>());
8284

@@ -136,6 +138,7 @@ public <R> void addWorkflowImplementationFactory(
136138
}
137139
dynamicWorkflowImplementationFactory =
138140
(Functions.Func1<EncodedValues, ? extends DynamicWorkflow>) factory;
141+
dynamicWorkflowImplementationOptions = options;
139142
return;
140143
}
141144
workflowInstanceFactories.put(clazz, factory);
@@ -213,6 +216,7 @@ private <T> void registerWorkflowImplementationType(
213216
}
214217
}
215218
};
219+
dynamicWorkflowImplementationOptions = options;
216220
return;
217221
} catch (NoSuchMethodException e) {
218222
throw new IllegalArgumentException(
@@ -285,8 +289,11 @@ private SyncWorkflowDefinition getWorkflowDefinition(
285289
public ReplayWorkflow getWorkflow(
286290
WorkflowType workflowType, WorkflowExecution workflowExecution) {
287291
SyncWorkflowDefinition workflow = getWorkflowDefinition(workflowType, workflowExecution);
292+
boolean isDynamicWorkflow = !workflowDefinitions.containsKey(workflowType.getName());
288293
WorkflowImplementationOptions workflowImplementationOptions =
289-
implementationOptions.get(workflowType.getName());
294+
isDynamicWorkflow
295+
? dynamicWorkflowImplementationOptions
296+
: implementationOptions.get(workflowType.getName());
290297
DataConverter dataConverterWithWorkflowContext =
291298
dataConverter.withContext(
292299
new WorkflowSerializationContext(namespace, workflowExecution.getWorkflowId()));

‎temporal-sdk/src/test/java/io/temporal/workflow/DynamicWorkflowTest.java‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.temporal.workflow;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.fail;
54

65
import io.temporal.activity.Activity;
76
import io.temporal.activity.ActivityOptions;
@@ -14,6 +13,7 @@
1413
import io.temporal.failure.ApplicationFailure;
1514
import io.temporal.testing.TestWorkflowEnvironment;
1615
import io.temporal.testing.internal.SDKTestWorkflowRule;
16+
import io.temporal.worker.WorkflowImplementationOptions;
1717
import java.time.Duration;
1818
import java.util.ArrayList;
1919
import java.util.List;
@@ -74,6 +74,13 @@ public Object execute(EncodedValues args) {
7474
}
7575
}
7676

77+
public static class FailingDynamicWorkflowImpl implements DynamicWorkflow {
78+
@Override
79+
public Object execute(EncodedValues args) {
80+
throw new NullPointerException("simulated");
81+
}
82+
}
83+
7784
@Test
7885
public void testDynamicWorkflow() {
7986
TestWorkflowEnvironment testEnvironment = testWorkflowRule.getTestEnvironment();
@@ -124,4 +131,41 @@ public void testDynamicWorkflowFailure() {
124131
workflow.start("startArg0", true /* fail */);
125132
workflow.getResult(String.class);
126133
}
134+
135+
@Test(expected = WorkflowFailedException.class)
136+
public void testDynamicWorkflowFailureTypes() {
137+
TestWorkflowEnvironment testEnvironment = testWorkflowRule.getTestEnvironment();
138+
testEnvironment
139+
.getWorkerFactory()
140+
.getWorker(testWorkflowRule.getTaskQueue())
141+
.registerWorkflowImplementationTypes(
142+
WorkflowImplementationOptions.newBuilder()
143+
.setFailWorkflowExceptionTypes(NullPointerException.class)
144+
.build(),
145+
FailingDynamicWorkflowImpl.class);
146+
testEnvironment.start();
147+
148+
WorkflowStub workflow = testWorkflowRule.newUntypedWorkflowStub("workflowFoo");
149+
workflow.start();
150+
workflow.getResult(String.class);
151+
}
152+
153+
@Test(expected = WorkflowFailedException.class)
154+
public void testDynamicWorkflowFactoryFailureTypes() {
155+
TestWorkflowEnvironment testEnvironment = testWorkflowRule.getTestEnvironment();
156+
testEnvironment
157+
.getWorkerFactory()
158+
.getWorker(testWorkflowRule.getTaskQueue())
159+
.registerWorkflowImplementationFactory(
160+
FailingDynamicWorkflowImpl.class,
161+
FailingDynamicWorkflowImpl::new,
162+
WorkflowImplementationOptions.newBuilder()
163+
.setFailWorkflowExceptionTypes(NullPointerException.class)
164+
.build());
165+
testEnvironment.start();
166+
167+
WorkflowStub workflow = testWorkflowRule.newUntypedWorkflowStub("workflowFoo");
168+
workflow.start();
169+
workflow.getResult(String.class);
170+
}
127171
}

0 commit comments

Comments
 (0)