|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 9 | + "github.com/mendixlabs/mxcli/model" |
| 10 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 11 | +) |
| 12 | + |
| 13 | +// Retry-loop error handler: a call activity with a custom error handler |
| 14 | +// whose body ends with an IF where the ELSE raises and the THEN performs |
| 15 | +// retry actions must produce a merge placed before the source activity, |
| 16 | +// with the handler tail looping back to the merge. Without this topology |
| 17 | +// the merge falls after the source and subsequent activities referencing |
| 18 | +// the call's output variable trigger CE0108 "variable not in scope". |
| 19 | +func TestRetryLoopErrorHandlerLoopsBackToSource(t *testing.T) { |
| 20 | + fb := &flowBuilder{ |
| 21 | + spacing: HorizontalSpacing, |
| 22 | + measurer: &layoutMeasurer{}, |
| 23 | + declaredVars: map[string]string{"R": "String", "RetryCount": "Integer"}, |
| 24 | + } |
| 25 | + |
| 26 | + oc := fb.buildFlowGraph([]ast.MicroflowStatement{ |
| 27 | + &ast.DeclareStmt{ |
| 28 | + Variable: "RetryCount", |
| 29 | + Type: ast.DataType{Kind: ast.TypeInteger}, |
| 30 | + InitialValue: &ast.LiteralExpr{Kind: ast.LiteralInteger, Value: "0"}, |
| 31 | + }, |
| 32 | + &ast.CallMicroflowStmt{ |
| 33 | + MicroflowName: ast.QualifiedName{Module: "M", Name: "Fetch"}, |
| 34 | + OutputVariable: "R", |
| 35 | + ErrorHandling: &ast.ErrorHandlingClause{ |
| 36 | + Type: ast.ErrorHandlingCustomWithoutRollback, |
| 37 | + Body: []ast.MicroflowStatement{ |
| 38 | + &ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "fetch failed"}}, |
| 39 | + &ast.IfStmt{ |
| 40 | + Condition: &ast.BinaryExpr{ |
| 41 | + Operator: "<", |
| 42 | + Left: &ast.VariableExpr{Name: "RetryCount"}, |
| 43 | + Right: &ast.LiteralExpr{Kind: ast.LiteralInteger, Value: "3"}, |
| 44 | + }, |
| 45 | + ThenBody: []ast.MicroflowStatement{ |
| 46 | + &ast.MfSetStmt{ |
| 47 | + Target: "RetryCount", |
| 48 | + Value: &ast.BinaryExpr{ |
| 49 | + Operator: "+", |
| 50 | + Left: &ast.VariableExpr{Name: "RetryCount"}, |
| 51 | + Right: &ast.LiteralExpr{Kind: ast.LiteralInteger, Value: "1"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + ElseBody: []ast.MicroflowStatement{ |
| 56 | + &ast.RaiseErrorStmt{}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "ok"}}, |
| 63 | + &ast.ReturnStmt{}, |
| 64 | + }, nil) |
| 65 | + |
| 66 | + // Find the call activity, the retry-body log/set activities, and the merge. |
| 67 | + var callID, logAfterID model.ID |
| 68 | + var mergeIDs []model.ID |
| 69 | + logSeen := 0 |
| 70 | + for _, obj := range oc.Objects { |
| 71 | + switch o := obj.(type) { |
| 72 | + case *microflows.ActionActivity: |
| 73 | + if _, ok := o.Action.(*microflows.MicroflowCallAction); ok { |
| 74 | + callID = o.ID |
| 75 | + } |
| 76 | + if _, ok := o.Action.(*microflows.LogMessageAction); ok { |
| 77 | + logSeen++ |
| 78 | + // logSeen==1 is the error-handler log inside the handler body. |
| 79 | + // logSeen==2 is the main-path log after the call. |
| 80 | + if logSeen == 2 { |
| 81 | + logAfterID = o.ID |
| 82 | + } |
| 83 | + } |
| 84 | + case *microflows.ExclusiveMerge: |
| 85 | + mergeIDs = append(mergeIDs, o.ID) |
| 86 | + } |
| 87 | + } |
| 88 | + if callID == "" { |
| 89 | + t.Fatal("no call activity found") |
| 90 | + } |
| 91 | + if logAfterID == "" { |
| 92 | + t.Fatal("no post-call log activity found") |
| 93 | + } |
| 94 | + if len(mergeIDs) == 0 { |
| 95 | + t.Fatal("no merge node found — retry-loop topology was not built") |
| 96 | + } |
| 97 | + |
| 98 | + // The merge must sit on the call's inbound path: there must be an edge |
| 99 | + // prev->merge (not prev->call) and an edge merge->call. The handler |
| 100 | + // tail loops back to the merge as a plain SequenceFlow (not marked |
| 101 | + // IsErrorHandler — the error marker applies only to the source→first |
| 102 | + // handler activity edge, which addErrorHandlerFlow emits separately). |
| 103 | + var mergeToCallFound bool |
| 104 | + var prevToMergeFound bool |
| 105 | + var tailToMergeFound bool |
| 106 | + // Find the retry-branch tail activity (the ChangeVariable that |
| 107 | + // increments the retry counter in the test fixture). |
| 108 | + var tailID model.ID |
| 109 | + for _, obj := range oc.Objects { |
| 110 | + if a, ok := obj.(*microflows.ActionActivity); ok { |
| 111 | + if _, isChange := a.Action.(*microflows.ChangeVariableAction); isChange { |
| 112 | + tailID = a.ID |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + for _, f := range oc.Flows { |
| 117 | + for _, mID := range mergeIDs { |
| 118 | + if f.DestinationID == callID && f.OriginID == mID { |
| 119 | + mergeToCallFound = true |
| 120 | + } |
| 121 | + if f.DestinationID == mID && f.OriginID == tailID && !f.IsErrorHandler { |
| 122 | + tailToMergeFound = true |
| 123 | + } |
| 124 | + if f.DestinationID == mID && !f.IsErrorHandler && f.OriginID != mID && f.OriginID != tailID { |
| 125 | + prevToMergeFound = true |
| 126 | + } |
| 127 | + } |
| 128 | + // No flow should terminate directly at the call from a non-merge origin. |
| 129 | + if f.DestinationID == callID && !f.IsErrorHandler { |
| 130 | + originIsMerge := false |
| 131 | + for _, mID := range mergeIDs { |
| 132 | + if f.OriginID == mID { |
| 133 | + originIsMerge = true |
| 134 | + } |
| 135 | + } |
| 136 | + if !originIsMerge { |
| 137 | + t.Errorf("normal inbound flow to call %s came from non-merge origin %s; retry-loop merge was not inserted", |
| 138 | + shortID(callID), shortID(f.OriginID)) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + if !mergeToCallFound { |
| 143 | + t.Error("missing merge -> call flow") |
| 144 | + } |
| 145 | + if !prevToMergeFound { |
| 146 | + t.Error("missing prev -> merge normal flow") |
| 147 | + } |
| 148 | + if !tailToMergeFound { |
| 149 | + t.Error("missing handler-tail -> merge (loop-back) flow") |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Non-retry error handlers (both branches return, neither branch raises, |
| 154 | +// or no trailing IF) must still use the forward-merge path. |
| 155 | +func TestNonRetryErrorHandlerUsesForwardMerge(t *testing.T) { |
| 156 | + fb := &flowBuilder{ |
| 157 | + spacing: HorizontalSpacing, |
| 158 | + measurer: &layoutMeasurer{}, |
| 159 | + declaredVars: map[string]string{"R": "String"}, |
| 160 | + } |
| 161 | + |
| 162 | + oc := fb.buildFlowGraph([]ast.MicroflowStatement{ |
| 163 | + &ast.CallMicroflowStmt{ |
| 164 | + MicroflowName: ast.QualifiedName{Module: "M", Name: "Fetch"}, |
| 165 | + OutputVariable: "R", |
| 166 | + ErrorHandling: &ast.ErrorHandlingClause{ |
| 167 | + Type: ast.ErrorHandlingCustomWithoutRollback, |
| 168 | + Body: []ast.MicroflowStatement{ |
| 169 | + // Single log — no trailing IF, no retry shape. |
| 170 | + &ast.LogStmt{Level: ast.LogError, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "failed"}}, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "ok"}}, |
| 175 | + &ast.ReturnStmt{}, |
| 176 | + }, nil) |
| 177 | + |
| 178 | + // The call should have an inbound flow from the previous activity (the |
| 179 | + // StartEvent or a fb-generated start), NOT via a merge. If a merge were |
| 180 | + // incorrectly inserted, every such non-retry microflow would gain an |
| 181 | + // extra unnecessary node. |
| 182 | + var callID model.ID |
| 183 | + for _, obj := range oc.Objects { |
| 184 | + if a, ok := obj.(*microflows.ActionActivity); ok { |
| 185 | + if _, isCall := a.Action.(*microflows.MicroflowCallAction); isCall { |
| 186 | + callID = a.ID |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + if callID == "" { |
| 191 | + t.Fatal("no call activity") |
| 192 | + } |
| 193 | + for _, f := range oc.Flows { |
| 194 | + if f.DestinationID == callID && !f.IsErrorHandler { |
| 195 | + // Origin should be a non-merge (StartEvent or a plain activity). |
| 196 | + for _, obj := range oc.Objects { |
| 197 | + if obj.GetID() == f.OriginID { |
| 198 | + if _, isMerge := obj.(*microflows.ExclusiveMerge); isMerge { |
| 199 | + t.Errorf("non-retry handler unexpectedly inserted a merge before call") |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func shortID(id model.ID) string { |
| 208 | + s := string(id) |
| 209 | + if len(s) > 8 { |
| 210 | + return s[:8] |
| 211 | + } |
| 212 | + return s |
| 213 | +} |
0 commit comments