@@ -1238,53 +1238,30 @@ func SleepHour(ctx Context) error {
12381238}
12391239
12401240func 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
12781261func 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