Skip to content

Commit ae6d39a

Browse files
committed
Simplify test to min-repro, add comment explaining solution
1 parent 572386f commit ae6d39a

File tree

2 files changed

+28
-54
lines changed

2 files changed

+28
-54
lines changed

internal/internal_workflow_testsuite.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,23 +2281,20 @@ func (env *testWorkflowEnvironmentImpl) RequestCancelChildWorkflow(_, workflowID
22812281

22822282
func (env *testWorkflowEnvironmentImpl) RequestCancelExternalWorkflow(namespace, workflowID, runID string, callback ResultHandler) {
22832283
if env.workflowInfo.WorkflowExecution.ID == workflowID {
2284-
// cancel current workflow from within workflow context
2285-
if sd, ok := env.workflowDef.(*syncWorkflowDefinition); ok {
2286-
env.postCallback(func() {
2287-
sd.dispatcher.NewCoroutine(sd.rootCtx, "cancel-self", true, func(ctx Context) {
2288-
env.workflowCancelHandler()
2289-
})
2290-
if env.isChildWorkflow() && env.onChildWorkflowCanceledListener != nil {
2291-
env.onChildWorkflowCanceledListener(env.workflowInfo)
2292-
}
2293-
}, true)
2284+
// The way testWorkflowEnvironment is setup today, we close the child workflow dispatcher before calling
2285+
// the workflowCancelHandler. A larger refactor would be needed to handle this similar to non-test code.
2286+
// Maybe worth doing when https://github.com/temporalio/go-sdk/issues/50 is tackled.
2287+
if sd, ok := env.workflowDef.(*syncWorkflowDefinition); ok && env.isChildWorkflow() {
2288+
sd.dispatcher.NewCoroutine(sd.rootCtx, "cancel-self", true, func(ctx Context) {
2289+
env.workflowCancelHandler()
2290+
})
22942291
} else {
22952292
env.workflowCancelHandler()
2296-
if env.isChildWorkflow() && env.onChildWorkflowCanceledListener != nil {
2297-
env.postCallback(func() {
2298-
env.onChildWorkflowCanceledListener(env.workflowInfo)
2299-
}, false)
2300-
}
2293+
}
2294+
if env.isChildWorkflow() && env.onChildWorkflowCanceledListener != nil {
2295+
env.postCallback(func() {
2296+
env.onChildWorkflowCanceledListener(env.workflowInfo)
2297+
}, false)
23012298
}
23022299
return
23032300
} else if childHandle, ok := env.runningWorkflows[workflowID]; ok && !childHandle.handled {

internal/workflow_testsuite_test.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,53 +1238,30 @@ func SleepHour(ctx Context) error {
12381238
}
12391239

12401240
func SleepThenCancel(ctx Context) error {
1241-
selector := NewSelector(ctx)
1242-
var activationWorkflow *WorkflowExecution
1243-
selector.AddReceive(GetSignalChannel(ctx, "activate"), func(c ReceiveChannel, more bool) {
1244-
c.Receive(ctx, nil)
1245-
GetLogger(ctx).Info("Received activation signal")
1246-
if activationWorkflow != nil {
1247-
RequestCancelExternalWorkflow(ctx, activationWorkflow.ID, activationWorkflow.RunID)
1248-
}
1249-
1250-
cwf := ExecuteChildWorkflow(
1251-
ctx,
1252-
SleepHour,
1253-
)
1254-
1255-
var res WorkflowExecution
1256-
if err := cwf.GetChildWorkflowExecution().Get(ctx, &res); err != nil {
1257-
GetLogger(ctx).Error("Failed to start child workflow", "error", err)
1258-
return
1259-
}
1260-
activationWorkflow = &res
1261-
1262-
selector.AddFuture(cwf, func(f Future) {
1263-
if err := f.Get(ctx, nil); err != nil {
1264-
GetLogger(ctx).Error("Child workflow failed", "error", err)
1265-
} else {
1266-
GetLogger(ctx).Info("Child workflow completed successfully")
1267-
}
1268-
activationWorkflow = nil
1269-
})
1270-
})
1241+
cwf := ExecuteChildWorkflow(
1242+
ctx,
1243+
SleepHour,
1244+
)
1245+
var res WorkflowExecution
1246+
if err := cwf.GetChildWorkflowExecution().Get(ctx, &res); err != nil {
1247+
return err
1248+
}
12711249

1272-
for selector.HasPending() || activationWorkflow != nil {
1273-
selector.Select(ctx)
1250+
// Canceling an external workflow that causes a timer to cancel used to fail due to
1251+
// "illegal access from outside of workflow context"
1252+
err := RequestCancelExternalWorkflow(ctx, res.ID, res.RunID).Get(ctx, nil)
1253+
if err != nil {
1254+
return err
12741255
}
1275-
return nil
1256+
1257+
// Give the workflow time to finish canceling the child workflow
1258+
return Sleep(ctx, 1*time.Second)
12761259
}
12771260

12781261
func TestRequestCancelExternalWorkflowInSelector(t *testing.T) {
12791262
testSuite := &WorkflowTestSuite{}
12801263
env := testSuite.NewTestWorkflowEnvironment()
12811264
env.RegisterWorkflow(SleepHour)
1282-
env.RegisterDelayedCallback(func() {
1283-
env.SignalWorkflow("activate", nil)
1284-
}, 0)
1285-
env.RegisterDelayedCallback(func() {
1286-
env.SignalWorkflow("activate", nil)
1287-
}, time.Second)
12881265
env.ExecuteWorkflow(SleepThenCancel)
12891266
require.NoError(t, env.GetWorkflowError())
12901267
env.IsWorkflowCompleted()

0 commit comments

Comments
 (0)