|
| 1 | +/* |
| 2 | + * Copyright 2025 - 2026 Zigflow authors <https://github.com/zigflow/zigflow/graphs/contributors> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Package cancelfork exercises cancelling a workflow while a non-competing |
| 18 | +// fork is running. The workflow is `set` -> fork of two long `wait` |
| 19 | +// branches -> `set`. Cancelling it while both branches wait must cancel |
| 20 | +// each branch, must not run the trailing set task, and must close the |
| 21 | +// execution and both branches as CANCELED. |
| 22 | +// |
| 23 | +// Regression test for the bug where the fork closed in the same workflow |
| 24 | +// task that requested the branch cancellations, so the parent close |
| 25 | +// policy terminated the branches before they received the cancellation. |
| 26 | +package cancelfork |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "testing" |
| 31 | + "time" |
| 32 | + |
| 33 | + zlog "github.com/rs/zerolog/log" |
| 34 | + "github.com/stretchr/testify/assert" |
| 35 | + "github.com/stretchr/testify/require" |
| 36 | + temporal "github.com/zigflow/helpers" |
| 37 | + "github.com/zigflow/zigflow/tests/e2e/utils" |
| 38 | + enums "go.temporal.io/api/enums/v1" |
| 39 | + "go.temporal.io/sdk/client" |
| 40 | + sdktemporal "go.temporal.io/sdk/temporal" |
| 41 | +) |
| 42 | + |
| 43 | +// workflowID is fixed so the test can address the execution and derive |
| 44 | +// the branch workflow IDs the fork assigns. |
| 45 | +const workflowID = "cancel-fork-cancels-branches" |
| 46 | + |
| 47 | +// startupTimeout bounds the wait for the worker to come up and both |
| 48 | +// branches to reach their timers. |
| 49 | +const startupTimeout = 2 * time.Minute |
| 50 | + |
| 51 | +// cancelTimeout bounds the wait for the execution to close after |
| 52 | +// cancellation is requested. The branch waits are ten minutes, so closing |
| 53 | +// inside this window proves the cancellation interrupted them. |
| 54 | +const cancelTimeout = 30 * time.Second |
| 55 | + |
| 56 | +// trailingStage is the value the trailing set task would record. It must |
| 57 | +// never appear anywhere in the execution history. |
| 58 | +const trailingStage = "after_fork_ran" |
| 59 | + |
| 60 | +// branches are the fork's branch keys, from which the fork derives each |
| 61 | +// branch's child workflow ID. |
| 62 | +var branches = []string{"left", "right"} |
| 63 | + |
| 64 | +var testCase = utils.TestCase{ |
| 65 | + Name: "cancel-fork", |
| 66 | + WorkflowPath: "workflow.yaml", |
| 67 | + Test: func(t *testing.T, test *utils.TestCase) { |
| 68 | + c, err := temporal.NewConnectionWithEnvvars( |
| 69 | + temporal.WithZerolog(&zlog.Logger), |
| 70 | + ) |
| 71 | + require.NoError(t, err) |
| 72 | + defer c.Close() |
| 73 | + |
| 74 | + wCtx := context.Background() |
| 75 | + |
| 76 | + we, err := c.ExecuteWorkflow(wCtx, client.StartWorkflowOptions{ |
| 77 | + ID: workflowID, |
| 78 | + TaskQueue: test.Workflow.Document.Namespace, |
| 79 | + }, test.Workflow.Document.Name) |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + runID := we.GetRunID() |
| 83 | + |
| 84 | + // Terminate on the way out so a failing assertion cannot leave |
| 85 | + // ten minute timers running against the shared Temporal server. |
| 86 | + // Terminating the parent terminates any branch still open. |
| 87 | + t.Cleanup(func() { |
| 88 | + _ = c.TerminateWorkflow(context.Background(), workflowID, runID, "cancel-fork test cleanup") |
| 89 | + }) |
| 90 | + |
| 91 | + // Cancel only once both branches are genuinely blocked on their |
| 92 | + // timers, so the test does not depend on an arbitrary sleep. |
| 93 | + for _, branch := range branches { |
| 94 | + waitForBranchTimer(t, c, wCtx, branchWorkflowID(branch)) |
| 95 | + } |
| 96 | + |
| 97 | + require.NoError(t, c.CancelWorkflow(wCtx, workflowID, runID)) |
| 98 | + |
| 99 | + closeCtx, closeCancel := context.WithTimeout(wCtx, cancelTimeout) |
| 100 | + defer closeCancel() |
| 101 | + |
| 102 | + var result any |
| 103 | + err = we.Get(closeCtx, &result) |
| 104 | + require.Error(t, err, "a cancelled workflow must not return a result") |
| 105 | + assert.True(t, sdktemporal.IsCanceledError(err), |
| 106 | + "the client must observe the execution as cancelled, got: %v", err) |
| 107 | + |
| 108 | + assertCanceled(t, c, wCtx, workflowID, runID) |
| 109 | + |
| 110 | + // The reported bug: the branches closed as TERMINATED by the |
| 111 | + // parent close policy instead of receiving the cancellation. |
| 112 | + for _, branch := range branches { |
| 113 | + assertCanceled(t, c, wCtx, branchWorkflowID(branch), "") |
| 114 | + } |
| 115 | + |
| 116 | + assertTrailingTaskDidNotRun(t, c, wCtx, runID) |
| 117 | + }, |
| 118 | +} |
| 119 | + |
| 120 | +// branchWorkflowID returns the child workflow ID the fork assigns to a |
| 121 | +// branch. |
| 122 | +func branchWorkflowID(branch string) string { |
| 123 | + return workflowID + "_fork_" + branch |
| 124 | +} |
| 125 | + |
| 126 | +// waitForBranchTimer polls a branch's history until its wait timer has |
| 127 | +// started. The branch may not exist yet when polling begins. |
| 128 | +func waitForBranchTimer(t *testing.T, c client.Client, ctx context.Context, id string) { |
| 129 | + t.Helper() |
| 130 | + |
| 131 | + deadline := time.Now().Add(startupTimeout) |
| 132 | + for time.Now().Before(deadline) { |
| 133 | + iter := c.GetWorkflowHistory(ctx, id, "", false, enums.HISTORY_EVENT_FILTER_TYPE_ALL_EVENT) |
| 134 | + for iter.HasNext() { |
| 135 | + event, err := iter.Next() |
| 136 | + if err != nil { |
| 137 | + break |
| 138 | + } |
| 139 | + if event.GetEventType() == enums.EVENT_TYPE_TIMER_STARTED { |
| 140 | + return |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + time.Sleep(500 * time.Millisecond) |
| 145 | + } |
| 146 | + |
| 147 | + t.Fatalf("branch %q did not reach its wait within %s", id, startupTimeout) |
| 148 | +} |
| 149 | + |
| 150 | +// assertCanceled checks that Temporal recorded the execution as CANCELED. |
| 151 | +func assertCanceled(t *testing.T, c client.Client, ctx context.Context, id, runID string) { |
| 152 | + t.Helper() |
| 153 | + |
| 154 | + desc, err := c.DescribeWorkflowExecution(ctx, id, runID) |
| 155 | + require.NoError(t, err) |
| 156 | + assert.Equal(t, enums.WORKFLOW_EXECUTION_STATUS_CANCELED, desc.GetWorkflowExecutionInfo().GetStatus(), |
| 157 | + "Temporal must record %q as CANCELED", id) |
| 158 | +} |
| 159 | + |
| 160 | +// assertTrailingTaskDidNotRun checks the closed parent's history for any |
| 161 | +// sign that the task after the fork ran. |
| 162 | +func assertTrailingTaskDidNotRun(t *testing.T, c client.Client, ctx context.Context, runID string) { |
| 163 | + t.Helper() |
| 164 | + |
| 165 | + iter := c.GetWorkflowHistory(ctx, workflowID, runID, false, enums.HISTORY_EVENT_FILTER_TYPE_ALL_EVENT) |
| 166 | + for iter.HasNext() { |
| 167 | + event, err := iter.Next() |
| 168 | + require.NoError(t, err, "read workflow history") |
| 169 | + |
| 170 | + assert.NotEqual(t, enums.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED, event.GetEventType(), |
| 171 | + "a cancelled workflow must not complete") |
| 172 | + assert.NotContains(t, event.String(), trailingStage, |
| 173 | + "the task after the cancelled fork must not have run") |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func init() { |
| 178 | + utils.AddTestCase(&testCase) |
| 179 | +} |
0 commit comments