Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE 1125: Addressing difference in workflow interceptors when using the testkit #1257

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/internal_workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ func (env *testWorkflowEnvironmentImpl) newTestWorkflowEnvironmentForChild(param
childEnv.startedHandler = startedHandler
childEnv.testWorkflowEnvironmentShared = env.testWorkflowEnvironmentShared
childEnv.workerOptions = env.workerOptions
childEnv.workflowInterceptors = env.workflowInterceptors
childEnv.workerOptions.DataConverter = params.dataConverter
childEnv.workflowInterceptors = env.workflowInterceptors
childEnv.registry = env.registry
Expand Down
71 changes: 71 additions & 0 deletions internal/workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package internal

import (
"context"
"github.com/stretchr/testify/suite"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -127,3 +128,73 @@ func TestWorkflowReturnNil(t *testing.T) {
err := env.GetWorkflowResult(&r)
require.NoError(t, err)
}

type InterceptorTestSuite struct {
suite.Suite
WorkflowTestSuite

env *TestWorkflowEnvironment
testFactory InterceptorFactory
}

type InterceptorFactory struct {
workflowInterceptorInvocationCounter int
childWorkflowInterceptorInvocationCounter int
}

type Interceptor struct {
WorkflowInterceptorBase
workflowInterceptorInvocationCounter *int
childWorkflowInterceptorInvocationCounter *int
}

func (i *Interceptor) ExecuteWorkflow(ctx Context, workflowType string, args ...interface{}) []interface{} {
*i.workflowInterceptorInvocationCounter += 1
return i.Next.ExecuteWorkflow(ctx, workflowType, args...)
}
func (i *Interceptor) ExecuteChildWorkflow(ctx Context, workflowType string, args ...interface{}) ChildWorkflowFuture {
*i.childWorkflowInterceptorInvocationCounter += 1
return i.Next.ExecuteChildWorkflow(ctx, workflowType, args...)
}

func (f *InterceptorFactory) NewInterceptor(_ *WorkflowInfo, next WorkflowInterceptor) WorkflowInterceptor {
return &Interceptor{
WorkflowInterceptorBase: WorkflowInterceptorBase{
Next: next,
},
workflowInterceptorInvocationCounter: &f.workflowInterceptorInvocationCounter,
childWorkflowInterceptorInvocationCounter: &f.childWorkflowInterceptorInvocationCounter,
}
}

func (s *InterceptorTestSuite) SetupTest() {
// Create a test workflow environment with the trace interceptor configured.
s.env = s.NewTestWorkflowEnvironment()
s.testFactory = InterceptorFactory{}
s.env.SetWorkerOptions(WorkerOptions{
WorkflowInterceptorChainFactories: []WorkflowInterceptorFactory{
&s.testFactory,
},
})
}

func TestInterceptorTestSuite(t *testing.T) {
suite.Run(t, new(InterceptorTestSuite))
}

func (s *InterceptorTestSuite) Test_GeneralInterceptor_IsExecutedOnChildren() {
r := s.Require()
childWf := func(ctx Context) error {
return nil
}
s.env.RegisterWorkflowWithOptions(childWf, RegisterWorkflowOptions{Name: "child"})
wf := func(ctx Context) error {
return ExecuteChildWorkflow(ctx, childWf).Get(ctx, nil)
}
s.env.RegisterWorkflowWithOptions(wf, RegisterWorkflowOptions{Name: "parent"})
s.env.ExecuteWorkflow(wf)
r.True(s.env.IsWorkflowCompleted())
r.NoError(s.env.GetWorkflowError())
r.Equal(s.testFactory.workflowInterceptorInvocationCounter, 2)
r.Equal(s.testFactory.childWorkflowInterceptorInvocationCounter, 1)
}