Skip to content

Commit f0613c6

Browse files
emrahsmeiliang86
andauthored
Support update search attribute inside workflow (#360) (#452)
Updated the TestWorkflowMutableStateImpl to ensure the UpsertWorkflowSearchAttributes is recorded to the history in the unit tests. Prior to this fix, tests that involve search attributes would hit NonDeterminisicWorkflowError. Co-authored-by: Liang Mei <[email protected]>
1 parent 8714d94 commit f0613c6

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowMutableStateImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ private void processDecision(
420420
ctx, d.getRequestCancelExternalWorkflowExecutionDecisionAttributes());
421421
break;
422422
case UpsertWorkflowSearchAttributes:
423-
// TODO: https://github.com/uber/cadence-java-client/issues/360
423+
processUpsertWorkflowSearchAttributes(
424+
ctx, d.getUpsertWorkflowSearchAttributesDecisionAttributes(), decisionTaskCompletedId);
424425
break;
425426
}
426427
}
@@ -1050,6 +1051,22 @@ private void processContinueAsNewWorkflowExecution(
10501051
event.getWorkflowExecutionContinuedAsNewEventAttributes().setNewExecutionRunId(runId);
10511052
}
10521053

1054+
private void processUpsertWorkflowSearchAttributes(
1055+
RequestContext ctx,
1056+
UpsertWorkflowSearchAttributesDecisionAttributes attr,
1057+
long decisionTaskCompletedId)
1058+
throws BadRequestError, InternalServiceError {
1059+
UpsertWorkflowSearchAttributesEventAttributes upsertEventAttr =
1060+
new UpsertWorkflowSearchAttributesEventAttributes()
1061+
.setSearchAttributes(attr.getSearchAttributes())
1062+
.setDecisionTaskCompletedEventId(decisionTaskCompletedId);
1063+
HistoryEvent event =
1064+
new HistoryEvent()
1065+
.setEventType(EventType.UpsertWorkflowSearchAttributes)
1066+
.setUpsertWorkflowSearchAttributesEventAttributes(upsertEventAttr);
1067+
ctx.addEvent(event);
1068+
}
1069+
10531070
@Override
10541071
public void startWorkflow(
10551072
boolean continuedAsNew, Optional<SignalWorkflowExecutionRequest> signalWithStartSignal)

src/test/java/com/uber/cadence/workflow/WorkflowTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5323,13 +5323,13 @@ public void testExceptionInSignal() throws InterruptedException {
53235323

53245324
public interface TestUpsertSearchAttributes {
53255325
@WorkflowMethod
5326-
String execute(String keyword);
5326+
String execute(String taskList, String keyword);
53275327
}
53285328

53295329
public static class TestUpsertSearchAttributesImpl implements TestUpsertSearchAttributes {
53305330

53315331
@Override
5332-
public String execute(String keyword) {
5332+
public String execute(String taskList, String keyword) {
53335333
SearchAttributes searchAttributes = Workflow.getWorkflowInfo().getSearchAttributes();
53345334
assertNull(searchAttributes);
53355335

@@ -5343,6 +5343,14 @@ public String execute(String keyword) {
53435343
WorkflowUtils.getValueFromSearchAttributes(
53445344
searchAttributes, "CustomKeywordField", String.class));
53455345

5346+
// Running the activity below ensures that we have one more decision task to be executed after
5347+
// adding the search attributes. This helps with replaying the history one more time to check
5348+
// against a possible NonDeterminisicWorkflowError which could be caused by missing
5349+
// UpsertWorkflowSearchAttributes event in history.
5350+
TestActivities activities =
5351+
Workflow.newActivityStub(TestActivities.class, newActivityOptions1(taskList));
5352+
activities.activity();
5353+
53465354
return "done";
53475355
}
53485356
}
@@ -5353,9 +5361,9 @@ public void testUpsertSearchAttributes() {
53535361
TestUpsertSearchAttributes testWorkflow =
53545362
workflowClient.newWorkflowStub(
53555363
TestUpsertSearchAttributes.class, newWorkflowOptionsBuilder(taskList).build());
5356-
String result = testWorkflow.execute("testKey");
5364+
String result = testWorkflow.execute(taskList, "testKey");
53575365
assertEquals("done", result);
5358-
tracer.setExpected("upsertSearchAttributes");
5366+
tracer.setExpected("upsertSearchAttributes", "executeActivity TestActivities::activity");
53595367
}
53605368

53615369
private static class TracingWorkflowInterceptor implements WorkflowInterceptor {

0 commit comments

Comments
 (0)