From 17ab801a9bd22e22842d053f52c3aebf45bcc53b Mon Sep 17 00:00:00 2001 From: Maxim Fateev Date: Tue, 6 Jan 2026 21:06:54 -0800 Subject: [PATCH] Fix test server to return INVALID_ARGUMENT for UNHANDLED_COMMAND The real Temporal server returns INVALID_ARGUMENT error when a workflow task has unhandled commands. The test server was silently recording the failure in history but returning success to the caller, causing the SDK to apply completion metrics before learning the task was rejected. This fix makes the test server throw INVALID_ARGUMENT: UnhandledCommand to match real server behavior, preventing double-counted metrics. Also adds registry.clear() in MetricsTest setUp for clean state. --- .../java/io/temporal/client/functional/MetricsTest.java | 1 + .../internal/testservice/TestWorkflowMutableStateImpl.java | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/temporal-sdk/src/test/java/io/temporal/client/functional/MetricsTest.java b/temporal-sdk/src/test/java/io/temporal/client/functional/MetricsTest.java index 98fddf17c..ce353e940 100644 --- a/temporal-sdk/src/test/java/io/temporal/client/functional/MetricsTest.java +++ b/temporal-sdk/src/test/java/io/temporal/client/functional/MetricsTest.java @@ -63,6 +63,7 @@ public class MetricsTest { @Before public void setUp() { + registry.clear(); tagsNamespaceQueue = replaceTags(TAGS_NAMESPACE, MetricsTag.TASK_QUEUE, testWorkflowRule.getTaskQueue()); } diff --git a/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java b/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java index 61d29ece6..c667c47a2 100644 --- a/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java +++ b/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java @@ -488,14 +488,16 @@ public void completeWorkflowTask( if (unhandledCommand(request) || unhandledMessages(request)) { // Fail the workflow task if there are new events or messages and a command tries to - // complete the - // workflow + // complete the workflow. Record the failure in history, then throw an error to the + // caller (matching real server behavior). failWorkflowTaskWithAReason( WorkflowTaskFailedCause.WORKFLOW_TASK_FAILED_CAUSE_UNHANDLED_COMMAND, null, ctx, request, false); + ctx.setExceptionIfEmpty( + Status.INVALID_ARGUMENT.withDescription("UnhandledCommand").asRuntimeException()); return; }