@@ -459,8 +459,10 @@ func TestNexusWorkflowRunOperation(t *testing.T) {
459459 event , err := iter .Next ()
460460 require .NoError (t , err )
461461 if event .GetEventType () == enumspb .EVENT_TYPE_WORKFLOW_EXECUTION_STARTED {
462- require .Len (t , event .GetLinks (), 1 )
463- require .True (t , proto .Equal (link , event .GetLinks ()[0 ].GetWorkflowEvent ()))
462+ completionCallbacks := event .GetWorkflowExecutionStartedEventAttributes ().GetCompletionCallbacks ()
463+ require .Len (t , completionCallbacks , 1 )
464+ require .Len (t , completionCallbacks [0 ].GetLinks (), 1 )
465+ require .True (t , proto .Equal (link , completionCallbacks [0 ].GetLinks ()[0 ].GetWorkflowEvent ()))
464466 break
465467 }
466468 }
@@ -923,12 +925,7 @@ func TestAsyncOperationFromWorkflow(t *testing.T) {
923925 require .Equal (t , tc .testConfig .Namespace , link .GetWorkflowEvent ().GetNamespace ())
924926 require .Equal (t , handlerWfID , link .GetWorkflowEvent ().GetWorkflowId ())
925927 require .NotEmpty (t , link .GetWorkflowEvent ().GetRunId ())
926- require .True (t , proto .Equal (
927- & common.Link_WorkflowEvent_EventReference {
928- EventType : enumspb .EVENT_TYPE_WORKFLOW_EXECUTION_STARTED ,
929- },
930- link .GetWorkflowEvent ().GetEventRef (),
931- ))
928+ require .Equal (t , enumspb .EVENT_TYPE_WORKFLOW_EXECUTION_STARTED , link .GetWorkflowEvent ().GetEventRef ().GetEventType ())
932929 handlerRunID := link .GetWorkflowEvent ().GetRunId ()
933930
934931 // Check the link is added in the handler workflow.
@@ -952,21 +949,21 @@ func TestAsyncOperationFromWorkflow(t *testing.T) {
952949 // Verify that calling by name works.
953950 require .Equal (t , "foo" , targetEvent .GetWorkflowExecutionStartedEventAttributes ().WorkflowType .Name )
954951 // Verify that links are properly attached.
955- require .Len (t , targetEvent .GetLinks (), 1 )
956- require .True (t , proto .Equal (
957- & common.Link_WorkflowEvent {
958- Namespace : tc .testConfig .Namespace ,
959- WorkflowId : run .GetID (),
960- RunId : run .GetRunID (),
961- Reference : & common.Link_WorkflowEvent_EventRef {
962- EventRef : & common.Link_WorkflowEvent_EventReference {
963- EventId : nexusOperationScheduleEventID ,
964- EventType : enumspb .EVENT_TYPE_NEXUS_OPERATION_SCHEDULED ,
965- },
952+ completionCallbacks := targetEvent .GetWorkflowExecutionStartedEventAttributes ().GetCompletionCallbacks ()
953+ require .Len (t , completionCallbacks , 1 )
954+ expectedLink := & common.Link_WorkflowEvent {
955+ Namespace : tc .testConfig .Namespace ,
956+ WorkflowId : run .GetID (),
957+ RunId : run .GetRunID (),
958+ Reference : & common.Link_WorkflowEvent_EventRef {
959+ EventRef : & common.Link_WorkflowEvent_EventReference {
960+ EventId : nexusOperationScheduleEventID ,
961+ EventType : enumspb .EVENT_TYPE_NEXUS_OPERATION_SCHEDULED ,
966962 },
967963 },
968- targetEvent .GetLinks ()[0 ].GetWorkflowEvent (),
969- ))
964+ }
965+ require .Len (t , completionCallbacks [0 ].GetLinks (), 1 )
966+ require .True (t , proto .Equal (expectedLink , completionCallbacks [0 ].GetLinks ()[0 ].GetWorkflowEvent ()))
970967 })
971968
972969 t .Run ("OpFailed" , func (t * testing.T ) {
@@ -1276,7 +1273,10 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
12761273 defer cancel ()
12771274 tctx := newTestContext (t , ctx )
12781275
1279- handlerWorkflowID := uuid .NewString ()
1276+ handlerWfIDSuffix := uuid .NewString ()
1277+ getHandlerWfID := func (tcName string ) string {
1278+ return tcName + "-" + handlerWfIDSuffix
1279+ }
12801280 handlerWf := func (ctx workflow.Context , input string ) (string , error ) {
12811281 workflow .GetSignalChannel (ctx , "terminate" ).Receive (ctx , nil )
12821282 return "hello " + input , nil
@@ -1285,13 +1285,13 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
12851285 op := temporalnexus .NewWorkflowRunOperation (
12861286 "op" ,
12871287 handlerWf ,
1288- func (ctx context.Context , input string , opts nexus.StartOperationOptions ) (client.StartWorkflowOptions , error ) {
1288+ func (ctx context.Context , tcName string , opts nexus.StartOperationOptions ) (client.StartWorkflowOptions , error ) {
12891289 var conflictPolicy enumspb.WorkflowIdConflictPolicy
1290- if input == "conflict-policy-use-existing" {
1290+ if tcName == "conflict-policy-use-existing" {
12911291 conflictPolicy = enumspb .WORKFLOW_ID_CONFLICT_POLICY_USE_EXISTING
12921292 }
12931293 return client.StartWorkflowOptions {
1294- ID : handlerWorkflowID ,
1294+ ID : getHandlerWfID ( tcName ) ,
12951295 WorkflowIDConflictPolicy : conflictPolicy ,
12961296 }, nil
12971297 },
@@ -1302,7 +1302,7 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
13021302 CntErr int
13031303 }
13041304
1305- callerWf := func (ctx workflow.Context , input string , numCalls int ) (CallerWfOutput , error ) {
1305+ callerWf := func (ctx workflow.Context , tcName string , numCalls int ) (CallerWfOutput , error ) {
13061306 output := CallerWfOutput {}
13071307 var retError error
13081308
@@ -1314,7 +1314,7 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
13141314 wg .Add (1 )
13151315 workflow .Go (ctx , func (ctx workflow.Context ) {
13161316 defer wg .Done ()
1317- fut := client .ExecuteOperation (ctx , op , input , workflow.NexusOperationOptions {})
1317+ fut := client .ExecuteOperation (ctx , op , tcName , workflow.NexusOperationOptions {})
13181318 var exec workflow.NexusOperationExecution
13191319 err := fut .GetNexusOperationExecution ().Get (ctx , & exec )
13201320 if err != nil {
@@ -1339,7 +1339,7 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
13391339 err = fut .Get (ctx , & res )
13401340 if err != nil {
13411341 retError = err
1342- } else if res != "hello " + input {
1342+ } else if res != "hello " + tcName {
13431343 retError = fmt .Errorf ("unexpected result from handler workflow: %q" , res )
13441344 }
13451345 })
@@ -1351,7 +1351,7 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
13511351
13521352 if output .CntOk > 0 {
13531353 // signal handler workflow so it will complete
1354- workflow .SignalExternalWorkflow (ctx , handlerWorkflowID , "" , "terminate" , nil ).Get (ctx , nil )
1354+ workflow .SignalExternalWorkflow (ctx , getHandlerWfID ( tcName ) , "" , "terminate" , nil ).Get (ctx , nil )
13551355 }
13561356 wg .Wait (ctx )
13571357 return output , retError
@@ -1366,48 +1366,84 @@ func TestAsyncOperationFromWorkflow_MultipleCallers(t *testing.T) {
13661366 require .NoError (t , w .Start ())
13671367 t .Cleanup (w .Stop )
13681368
1369+ // number of concurrent Nexus operation calls
1370+ numCalls := 5
1371+
13691372 testCases := []struct {
1370- input string
1371- checkOutput func (t * testing.T , numCalls int , res CallerWfOutput , err error )
1373+ name string
1374+ expectedOk int
1375+ expectedErr int
13721376 }{
13731377 {
1374- input : "conflict-policy-fail" ,
1375- checkOutput : func (t * testing.T , numCalls int , res CallerWfOutput , err error ) {
1376- require .NoError (t , err )
1377- require .EqualValues (t , 1 , res .CntOk )
1378- require .EqualValues (t , numCalls - 1 , res .CntErr )
1379- },
1378+ name : "conflict-policy-fail" ,
1379+ expectedOk : 1 ,
1380+ expectedErr : numCalls - 1 ,
13801381 },
13811382 {
1382- input : "conflict-policy-use-existing" ,
1383- checkOutput : func (t * testing.T , numCalls int , res CallerWfOutput , err error ) {
1384- require .NoError (t , err )
1385- require .EqualValues (t , numCalls , res .CntOk )
1386- require .EqualValues (t , 0 , res .CntErr )
1387- },
1383+ name : "conflict-policy-use-existing" ,
1384+ expectedOk : numCalls ,
1385+ expectedErr : 0 ,
13881386 },
13891387 }
13901388
1391- // number of concurrent Nexus operation calls
1392- numCalls := 5
13931389 for _ , tc := range testCases {
1394- t .Run (tc .input , func (t * testing.T ) {
1390+ t .Run (tc .name , func (t * testing.T ) {
1391+ callerWfID := uuid .NewString ()
13951392 run , err := tctx .client .ExecuteWorkflow (
13961393 ctx ,
13971394 client.StartWorkflowOptions {
1395+ ID : callerWfID ,
13981396 TaskQueue : tctx .taskQueue ,
13991397 // The endpoint registry may take a bit to propagate to the history service, use a shorter
14001398 // workflow task timeout to speed up the attempts.
14011399 WorkflowTaskTimeout : time .Second ,
14021400 },
14031401 callerWf ,
1404- tc .input ,
1402+ tc .name ,
14051403 numCalls ,
14061404 )
14071405 require .NoError (t , err )
14081406 var res CallerWfOutput
14091407 err = run .Get (ctx , & res )
1410- tc .checkOutput (t , numCalls , res , err )
1408+ require .NoError (t , err )
1409+ require .Equal (t , tc .expectedOk , res .CntOk )
1410+ require .Equal (t , tc .expectedErr , res .CntErr )
1411+
1412+ cntEventRefLinks := 0
1413+ cntRequestIDRefLinks := 0
1414+ iter := tctx .client .GetWorkflowHistory (
1415+ context .Background (),
1416+ callerWfID ,
1417+ "" ,
1418+ false ,
1419+ enumspb .HISTORY_EVENT_FILTER_TYPE_ALL_EVENT ,
1420+ )
1421+ for iter .HasNext () {
1422+ event , err := iter .Next ()
1423+ require .NoError (t , err )
1424+ if event .GetEventType () != enumspb .EVENT_TYPE_NEXUS_OPERATION_STARTED {
1425+ continue
1426+ }
1427+ require .Len (t , event .GetLinks (), 1 )
1428+ link := event .GetLinks ()[0 ].GetWorkflowEvent ()
1429+ require .NotNil (t , link )
1430+ require .Equal (t , tctx .testConfig .Namespace , link .GetNamespace ())
1431+ require .Equal (t , getHandlerWfID (tc .name ), link .GetWorkflowId ())
1432+ switch link .Reference .(type ) {
1433+ case * common.Link_WorkflowEvent_EventRef :
1434+ cntEventRefLinks ++
1435+ require .Equal (t , int64 (1 ), link .GetEventRef ().GetEventId ())
1436+ require .Equal (t , enumspb .EVENT_TYPE_WORKFLOW_EXECUTION_STARTED , link .GetEventRef ().GetEventType ())
1437+ case * common.Link_WorkflowEvent_RequestIdRef :
1438+ cntRequestIDRefLinks ++
1439+ require .NotEmpty (t , link .GetRequestIdRef ().GetRequestId ())
1440+ require .Equal (t , enumspb .EVENT_TYPE_WORKFLOW_EXECUTION_OPTIONS_UPDATED , link .GetRequestIdRef ().GetEventType ())
1441+ default :
1442+ require .Fail (t , fmt .Sprintf ("Unexpected link reference type: %T" , link .Reference ))
1443+ }
1444+ }
1445+ require .Equal (t , 1 , cntEventRefLinks )
1446+ require .Equal (t , tc .expectedOk - 1 , cntRequestIDRefLinks )
14111447 })
14121448 }
14131449}
0 commit comments