@@ -1232,3 +1232,61 @@ func TestDynamicWorkflows(t *testing.T) {
12321232 require .NoError (t , err )
12331233 require .Equal (t , "dynamic-activity - grape - cherry" , result )
12341234}
1235+
1236+ func SleepHour (ctx Context ) error {
1237+ Sleep (ctx , time .Hour )
1238+ return nil
1239+ }
1240+
1241+ func SleepThenCancel (ctx Context ) error {
1242+ selector := NewSelector (ctx )
1243+ var activationWorkflow * WorkflowExecution
1244+ selector .AddReceive (GetSignalChannel (ctx , "activate" ), func (c ReceiveChannel , more bool ) {
1245+ c .Receive (ctx , nil )
1246+ GetLogger (ctx ).Info ("Received activation signal" )
1247+ if activationWorkflow != nil {
1248+ RequestCancelExternalWorkflow (ctx , activationWorkflow .ID , activationWorkflow .RunID )
1249+ }
1250+
1251+ cwf := ExecuteChildWorkflow (
1252+ ctx ,
1253+ SleepHour ,
1254+ )
1255+
1256+ var res WorkflowExecution
1257+ if err := cwf .GetChildWorkflowExecution ().Get (ctx , & res ); err != nil {
1258+ GetLogger (ctx ).Error ("Failed to start child workflow" , "error" , err )
1259+ return
1260+ }
1261+ activationWorkflow = & res
1262+
1263+ selector .AddFuture (cwf , func (f Future ) {
1264+ if err := f .Get (ctx , nil ); err != nil {
1265+ GetLogger (ctx ).Error ("Child workflow failed" , "error" , err )
1266+ } else {
1267+ GetLogger (ctx ).Info ("Child workflow completed successfully" )
1268+ }
1269+ activationWorkflow = nil
1270+ })
1271+ })
1272+
1273+ for selector .HasPending () || activationWorkflow != nil {
1274+ selector .Select (ctx )
1275+ }
1276+ return nil
1277+ }
1278+
1279+ func TestRequestCancelExternalWorkflowInSelector (t * testing.T ) {
1280+ testSuite := & WorkflowTestSuite {}
1281+ env := testSuite .NewTestWorkflowEnvironment ()
1282+ env .RegisterWorkflow (SleepHour )
1283+ env .RegisterDelayedCallback (func () {
1284+ env .SignalWorkflow ("activate" , nil )
1285+ }, 0 )
1286+ env .RegisterDelayedCallback (func () {
1287+ env .SignalWorkflow ("activate" , nil )
1288+ }, time .Second )
1289+ env .ExecuteWorkflow (SleepThenCancel )
1290+ require .NoError (t , env .GetWorkflowError ())
1291+ env .IsWorkflowCompleted ()
1292+ }
0 commit comments