Skip to content

Commit

Permalink
Allow setting activity timeouts in testsuite
Browse files Browse the repository at this point in the history
Add 2 method to TestActivityEnvironment:
- SetActivityStartToCloseTimeout
- SetActivityScheduleToCloseTimeout

This will allow changing the default 10 minute timeouts in the current test environments.
  • Loading branch information
ohad83 committed Aug 5, 2023
1 parent 3aba985 commit c27397f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/internal_workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ type (
onTimerScheduledListener func(timerID string, duration time.Duration)
onTimerFiredListener func(timerID string)
onTimerCanceledListener func(timerID string)

activityScheduleToCloseTimeout time.Duration
activityStartToCloseTimeout time.Duration
}

// testWorkflowEnvironmentImpl is the environment that runs the workflow/activity unit tests.
Expand Down Expand Up @@ -236,6 +239,9 @@ func newTestWorkflowEnvironmentImpl(s *WorkflowTestSuite, parentRegistry *regist
callbackChannel: make(chan testCallbackHandle, 1000),
testTimeout: 3 * time.Second,
expectedMockCalls: make(map[string]struct{}),

activityScheduleToCloseTimeout: 600 * time.Second,
activityStartToCloseTimeout: 600 * time.Second,
},

workflowInfo: &WorkflowInfo{
Expand Down Expand Up @@ -544,8 +550,8 @@ func (env *testWorkflowEnvironmentImpl) executeActivity(

parameters := ExecuteActivityParams{
ExecuteActivityOptions: ExecuteActivityOptions{
ScheduleToCloseTimeout: 600 * time.Second,
StartToCloseTimeout: 600 * time.Second,
ScheduleToCloseTimeout: env.activityScheduleToCloseTimeout,
StartToCloseTimeout: env.activityStartToCloseTimeout,
},
ActivityType: *activityType,
Input: input,
Expand Down
46 changes: 46 additions & 0 deletions internal/internal_workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4031,3 +4031,49 @@ func (s *WorkflowTestSuiteUnitTest) Test_WorkflowGetCurrentHistoryLength() {
s.NoError(env.GetWorkflowResult(&result))
s.Equal(17, result)
}

func (s *WorkflowTestSuiteUnitTest) Test_ActivityWithStartToCloseTimeout() {
timeout := 100 * time.Millisecond

timeoutActivity := func(ctx context.Context) error {
time.Sleep(timeout * 2)
return nil
}

noTimeoutActivity := func(ctx context.Context) error {
time.Sleep(timeout / 2)
return nil
}

env := s.NewTestActivityEnvironment()
env.SetActivityStartToCloseTimeout(timeout)
env.RegisterActivity(timeoutActivity)
env.RegisterActivity(noTimeoutActivity)
_, err := env.ExecuteActivity(timeoutActivity)
s.Error(err)
_, err = env.ExecuteActivity(noTimeoutActivity)
s.NoError(err)
}

func (s *WorkflowTestSuiteUnitTest) Test_ActivityWithScheduleToCloseTimeout() {
timeout := 100 * time.Millisecond

timeoutActivity := func(ctx context.Context) error {
time.Sleep(timeout * 2)
return nil
}

noTimeoutActivity := func(ctx context.Context) error {
time.Sleep(timeout / 2)
return nil
}

env := s.NewTestActivityEnvironment()
env.SetActivityScheduleToCloseTimeout(timeout)
env.RegisterActivity(timeoutActivity)
env.RegisterActivity(noTimeoutActivity)
_, err := env.ExecuteActivity(timeoutActivity)
s.Error(err)
_, err = env.ExecuteActivity(noTimeoutActivity)
s.NoError(err)
}
10 changes: 10 additions & 0 deletions internal/workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ func (t *TestActivityEnvironment) SetOnActivityHeartbeatListener(
return t
}

// SetActivityStartToCloseTimeout sets the start to close timeouts of activities in the test environment.
func (e *TestActivityEnvironment) SetActivityStartToCloseTimeout(timeout time.Duration) {
e.impl.testWorkflowEnvironmentShared.activityStartToCloseTimeout = timeout
}

// SetActivityStartToCloseTimeout sets the schedule to close timeouts of activities in the test environment
func (e *TestActivityEnvironment) SetActivityScheduleToCloseTimeout(timeout time.Duration) {
e.impl.testWorkflowEnvironmentShared.activityScheduleToCloseTimeout = timeout
}

// RegisterWorkflow registers workflow implementation with the TestWorkflowEnvironment
func (e *TestWorkflowEnvironment) RegisterWorkflow(w interface{}) {
e.impl.RegisterWorkflow(w)
Expand Down

0 comments on commit c27397f

Please sign in to comment.