Skip to content

Commit 4b81202

Browse files
seriousbenLiang Mei
andauthored
Testsuite: Support interceptors in child workflows mocks (#1039)
Co-authored-by: Liang Mei <[email protected]>
1 parent 163e45b commit 4b81202

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

internal/internal_workflow.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ func newWorkflowContext(env workflowEnvironment, interceptors WorkflowIntercepto
438438
return rootCtx
439439
}
440440

441-
func newWorkflowInterceptors(env workflowEnvironment, factories []WorkflowInterceptorFactory) (WorkflowInterceptor, *workflowEnvironmentInterceptor) {
441+
func newWorkflowInterceptors(
442+
env workflowEnvironment,
443+
factories []WorkflowInterceptorFactory,
444+
) (WorkflowInterceptor, *workflowEnvironmentInterceptor) {
442445
envInterceptor := &workflowEnvironmentInterceptor{env: env}
443446
var interceptor WorkflowInterceptor = envInterceptor
444447
for i := len(factories) - 1; i >= 0; i-- {

internal/internal_workflow_testsuite.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,8 +1394,10 @@ func (w *workflowExecutorWrapper) Execute(ctx Context, input []byte) (result []b
13941394
// the main loop, but the mock could block if it is configured to wait. So we need to use a separate goroutinue to
13951395
// run the mock, and resume after mock call returns.
13961396
mockReadyChannel := NewChannel(ctx)
1397-
// make a copy of the context for getMockReturn() call to avoid race condition
1398-
ctxCopy := newWorkflowContext(w.env, nil, nil)
1397+
// Make a copy of the context for getMockReturn() call to avoid race condition.
1398+
// Use existing interceptors from env.
1399+
envInterceptor := &workflowEnvironmentInterceptor{env: env}
1400+
ctxCopy := newWorkflowContext(w.env, envInterceptor, envInterceptor)
13991401
go func() {
14001402
// getMockReturn could block if mock is configured to wait. The returned mockRet is what has been configured
14011403
// for the mock by using MockCallWrapper.Return(). The mockRet could be mock values or mock function. We process

internal/internal_workflow_testsuite_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ func (s *WorkflowTestSuiteUnitTest) Test_ChildWorkflow_Mock() {
700700
if err != nil {
701701
return "", err
702702
}
703+
704+
cwo = ChildWorkflowOptions{WorkflowID: "workflow-id", ExecutionStartToCloseTimeout: time.Minute}
705+
ctx = WithChildWorkflowOptions(ctx, cwo)
703706
var heartbeatWorkflowResult string
704707
err = ExecuteChildWorkflow(ctx, testWorkflowHeartbeat, "slow", time.Hour).Get(ctx, &heartbeatWorkflowResult)
705708
if err != nil {
@@ -718,6 +721,13 @@ func (s *WorkflowTestSuiteUnitTest) Test_ChildWorkflow_Mock() {
718721

719722
env.OnActivity(testActivityHello, mock.Anything, mock.Anything).Return("mock_msg", nil)
720723
env.OnWorkflow(testWorkflowHeartbeat, mock.Anything, mock.Anything, mock.Anything).
724+
Run(func(a mock.Arguments) {
725+
ctx := a.Get(0).(Context)
726+
727+
// Ensure GetWorkflowInfo is usable in mocks.
728+
info := GetWorkflowInfo(ctx)
729+
s.Equal("workflow-id", info.WorkflowExecution.ID)
730+
}).
721731
Return("mock_heartbeat", nil)
722732
env.ExecuteWorkflow(workflowFn)
723733

0 commit comments

Comments
 (0)