Skip to content

Commit e31ba38

Browse files
edmondopshijieshengagautam478
authored
Addressing difference in workflow interceptors when using the testkit (#1257)
Co-authored-by: Shijie Sheng <[email protected]> Co-authored-by: agautam478 <[email protected]>
1 parent 196c51c commit e31ba38

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

internal/internal_workflow_testsuite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ func (env *testWorkflowEnvironmentImpl) newTestWorkflowEnvironmentForChild(param
362362
childEnv.startedHandler = startedHandler
363363
childEnv.testWorkflowEnvironmentShared = env.testWorkflowEnvironmentShared
364364
childEnv.workerOptions = env.workerOptions
365+
childEnv.workflowInterceptors = env.workflowInterceptors
365366
childEnv.workerOptions.DataConverter = params.dataConverter
366367
childEnv.workflowInterceptors = env.workflowInterceptors
367368
childEnv.registry = env.registry

internal/workflow_testsuite_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package internal
2323

2424
import (
2525
"context"
26+
"github.com/stretchr/testify/suite"
2627
"strings"
2728
"testing"
2829
"time"
@@ -127,3 +128,73 @@ func TestWorkflowReturnNil(t *testing.T) {
127128
err := env.GetWorkflowResult(&r)
128129
require.NoError(t, err)
129130
}
131+
132+
type InterceptorTestSuite struct {
133+
suite.Suite
134+
WorkflowTestSuite
135+
136+
env *TestWorkflowEnvironment
137+
testFactory InterceptorFactory
138+
}
139+
140+
type InterceptorFactory struct {
141+
workflowInterceptorInvocationCounter int
142+
childWorkflowInterceptorInvocationCounter int
143+
}
144+
145+
type Interceptor struct {
146+
WorkflowInterceptorBase
147+
workflowInterceptorInvocationCounter *int
148+
childWorkflowInterceptorInvocationCounter *int
149+
}
150+
151+
func (i *Interceptor) ExecuteWorkflow(ctx Context, workflowType string, args ...interface{}) []interface{} {
152+
*i.workflowInterceptorInvocationCounter += 1
153+
return i.Next.ExecuteWorkflow(ctx, workflowType, args...)
154+
}
155+
func (i *Interceptor) ExecuteChildWorkflow(ctx Context, workflowType string, args ...interface{}) ChildWorkflowFuture {
156+
*i.childWorkflowInterceptorInvocationCounter += 1
157+
return i.Next.ExecuteChildWorkflow(ctx, workflowType, args...)
158+
}
159+
160+
func (f *InterceptorFactory) NewInterceptor(_ *WorkflowInfo, next WorkflowInterceptor) WorkflowInterceptor {
161+
return &Interceptor{
162+
WorkflowInterceptorBase: WorkflowInterceptorBase{
163+
Next: next,
164+
},
165+
workflowInterceptorInvocationCounter: &f.workflowInterceptorInvocationCounter,
166+
childWorkflowInterceptorInvocationCounter: &f.childWorkflowInterceptorInvocationCounter,
167+
}
168+
}
169+
170+
func (s *InterceptorTestSuite) SetupTest() {
171+
// Create a test workflow environment with the trace interceptor configured.
172+
s.env = s.NewTestWorkflowEnvironment()
173+
s.testFactory = InterceptorFactory{}
174+
s.env.SetWorkerOptions(WorkerOptions{
175+
WorkflowInterceptorChainFactories: []WorkflowInterceptorFactory{
176+
&s.testFactory,
177+
},
178+
})
179+
}
180+
181+
func TestInterceptorTestSuite(t *testing.T) {
182+
suite.Run(t, new(InterceptorTestSuite))
183+
}
184+
185+
func (s *InterceptorTestSuite) Test_GeneralInterceptor_IsExecutedOnChildren() {
186+
r := s.Require()
187+
childWf := func(ctx Context) error {
188+
return nil
189+
}
190+
s.env.RegisterWorkflowWithOptions(childWf, RegisterWorkflowOptions{Name: "child"})
191+
wf := func(ctx Context) error {
192+
return ExecuteChildWorkflow(ctx, childWf).Get(ctx, nil)
193+
}
194+
s.env.RegisterWorkflowWithOptions(wf, RegisterWorkflowOptions{Name: "parent"})
195+
s.env.ExecuteWorkflow(wf)
196+
r.True(s.env.IsWorkflowCompleted())
197+
r.NoError(s.env.GetWorkflowError())
198+
r.Equal(s.testFactory.workflowInterceptorInvocationCounter, 2)
199+
r.Equal(s.testFactory.childWorkflowInterceptorInvocationCounter, 1)
200+
}

0 commit comments

Comments
 (0)