Skip to content

Commit 7525c65

Browse files
authoredSep 17, 2024
Test Server: Fix Nexus operation cancel before start (#2223)
* Test Server: Fix Nexus operation cancel before start * test
1 parent 6f0cf07 commit 7525c65

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed
 

‎temporal-test-server/src/main/java/io/temporal/internal/testservice/StateMachines.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,12 @@ public static StateMachine<NexusOperationData> newNexusOperation(Endpoint endpoi
608608
.add(NONE, INITIATE, INITIATED, StateMachines::scheduleNexusOperation)
609609
.add(INITIATED, START, STARTED, StateMachines::startNexusOperation)
610610
.add(INITIATED, TIME_OUT, TIMED_OUT, StateMachines::timeoutNexusOperation)
611-
// Transitions directly to CANCELED if operation has not been started
612611
// TODO: properly support cancel before start
613-
.add(
614-
INITIATED,
615-
REQUEST_CANCELLATION,
616-
CANCELED,
617-
StateMachines::reportNexusOperationCancellation)
612+
// .add(
613+
// INITIATED,
614+
// REQUEST_CANCELLATION,
615+
// INITIATED,
616+
// StateMachines::requestCancelNexusOperation)
618617
.add(INITIATED, CANCEL, CANCELED, StateMachines::reportNexusOperationCancellation)
619618
// Transitions directly from INITIATED to COMPLETE for sync completions
620619
.add(INITIATED, COMPLETE, COMPLETED, StateMachines::completeNexusOperation)

‎temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.temporal.api.enums.v1.*;
4242
import io.temporal.api.errordetails.v1.QueryFailedFailure;
4343
import io.temporal.api.failure.v1.ApplicationFailureInfo;
44+
import io.temporal.api.failure.v1.CanceledFailureInfo;
4445
import io.temporal.api.failure.v1.Failure;
4546
import io.temporal.api.history.v1.*;
4647
import io.temporal.api.nexus.v1.Endpoint;
@@ -753,14 +754,26 @@ private void processRequestCancelNexusOperation(
753754
.asRuntimeException();
754755
}
755756

756-
operation.action(Action.REQUEST_CANCELLATION, ctx, null, workflowTaskCompletedId);
757-
if (isTerminalState(operation.getState())) {
758-
// Operation canceled before started, so immediately remove operation since no new
759-
// cancellation task will be generated.
757+
if (operation.getState() == State.INITIATED) {
760758
// TODO: properly support cancel before start once server does
759+
ctx.addEvent(
760+
HistoryEvent.newBuilder()
761+
.setEventType(EventType.EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED)
762+
.setNexusOperationCancelRequestedEventAttributes(
763+
NexusOperationCancelRequestedEventAttributes.newBuilder()
764+
.setScheduledEventId(attr.getScheduledEventId())
765+
.setWorkflowTaskCompletedEventId(workflowTaskCompletedId))
766+
.build());
767+
Failure canceled =
768+
Failure.newBuilder()
769+
.setMessage("operation canceled before it was started")
770+
.setCanceledFailureInfo(CanceledFailureInfo.getDefaultInstance())
771+
.build();
772+
operation.action(Action.CANCEL, ctx, canceled, workflowTaskCompletedId);
761773
nexusOperations.remove(scheduleEventId);
762774
ctx.setNeedWorkflowTask(true);
763775
} else {
776+
operation.action(Action.REQUEST_CANCELLATION, ctx, null, workflowTaskCompletedId);
764777
ctx.addTimer(
765778
ProtobufTimeUtils.toJavaDuration(operation.getData().requestTimeout),
766779
() ->

‎temporal-test-server/src/test/java/io/temporal/testserver/functional/NexusWorkflowTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,9 @@ public void testNexusOperationCancelBeforeStart() {
576576
events.get(0).getNexusOperationCanceledEventAttributes().getFailure();
577577
assertOperationFailureInfo(failure.getNexusOperationExecutionFailureInfo());
578578
Assert.assertEquals("nexus operation completed unsuccessfully", failure.getMessage());
579-
Assert.assertFalse(failure.hasCause());
579+
io.temporal.api.failure.v1.Failure cause = failure.getCause();
580+
Assert.assertEquals("operation canceled before it was started", cause.getMessage());
581+
Assert.assertNotNull(cause.getCanceledFailureInfo());
580582
}
581583

582584
@Test(timeout = 15000)

0 commit comments

Comments
 (0)