From 201240a4f1075a48e23a3af17fd11a6d1c89bcd6 Mon Sep 17 00:00:00 2001 From: Quinn Klassen Date: Fri, 12 Jan 2024 16:51:06 -0800 Subject: [PATCH] Allow creating a stub of an update only interface (#1967) Allow creating a stub of an update only interface. --- .../client/WorkflowClientInternalImpl.java | 12 ++- .../updateTest/UpdateAnnotationTest.java | 92 +++++++++++++++++++ 2 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateAnnotationTest.java diff --git a/temporal-sdk/src/main/java/io/temporal/client/WorkflowClientInternalImpl.java b/temporal-sdk/src/main/java/io/temporal/client/WorkflowClientInternalImpl.java index a73e29894..0e02603f0 100644 --- a/temporal-sdk/src/main/java/io/temporal/client/WorkflowClientInternalImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/client/WorkflowClientInternalImpl.java @@ -47,10 +47,7 @@ import io.temporal.serviceclient.MetricsTag; import io.temporal.serviceclient.WorkflowServiceStubs; import io.temporal.worker.WorkerFactory; -import io.temporal.workflow.Functions; -import io.temporal.workflow.QueryMethod; -import io.temporal.workflow.SignalMethod; -import io.temporal.workflow.WorkflowMethod; +import io.temporal.workflow.*; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -175,7 +172,12 @@ public T newWorkflowStub(Class workflowInterface, String workflowId) { @Override public T newWorkflowStub( Class workflowInterface, String workflowId, Optional runId) { - checkAnnotation(workflowInterface, WorkflowMethod.class, QueryMethod.class, SignalMethod.class); + checkAnnotation( + workflowInterface, + WorkflowMethod.class, + QueryMethod.class, + SignalMethod.class, + UpdateMethod.class); if (Strings.isNullOrEmpty(workflowId)) { throw new IllegalArgumentException("workflowId is null or empty"); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateAnnotationTest.java new file mode 100644 index 000000000..c8e37d3a9 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateAnnotationTest.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. + * + * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this material except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.temporal.workflow.updateTest; + +import static org.junit.Assert.assertEquals; + +import io.temporal.api.common.v1.WorkflowExecution; +import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowOptions; +import io.temporal.testing.internal.SDKTestOptions; +import io.temporal.testing.internal.SDKTestWorkflowRule; +import io.temporal.workflow.UpdateMethod; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.WorkflowInterface; +import io.temporal.workflow.WorkflowMethod; +import java.util.Optional; +import org.junit.Rule; +import org.junit.Test; + +public class UpdateAnnotationTest { + @Rule + public SDKTestWorkflowRule testWorkflowRule = + SDKTestWorkflowRule.newBuilder().setWorkflowTypes(WorkflowImpl.class).build(); + + @Test + public void testUpdateOnlyInterface() { + // Verify a stub to an interface with only @UpdateMethod can be created. + WorkflowClient workflowClient = testWorkflowRule.getWorkflowClient(); + WorkflowOptions options = + SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()).toBuilder() + .build(); + WorkflowTestInterface workflow = + workflowClient.newWorkflowStub(WorkflowTestInterface.class, options); + + WorkflowExecution execution = WorkflowClient.start(workflow::execute); + + UpdateWorkflowInterface updateOnlyWorkflow = + workflowClient.newWorkflowStub(UpdateWorkflowInterface.class, execution.getWorkflowId()); + updateOnlyWorkflow.update(); + + String result = + testWorkflowRule + .getWorkflowClient() + .newUntypedWorkflowStub(execution, Optional.empty()) + .getResult(String.class); + assertEquals("success", result); + } + + public interface UpdateWorkflowInterface { + @UpdateMethod + void update(); + } + + @WorkflowInterface + public interface WorkflowTestInterface extends UpdateWorkflowInterface { + @WorkflowMethod + String execute(); + } + + public static class WorkflowImpl implements WorkflowTestInterface { + boolean complete = false; + + @Override + public String execute() { + Workflow.await(() -> complete); + return "success"; + } + + @Override + public void update() { + complete = true; + } + } +}