Skip to content

Commit

Permalink
Export SetContinuedAsNewRunID in the TestWorkflowEnvironment
Browse files Browse the repository at this point in the history
When building some workflows ("actors" like ones are a good example),
you sometimes want to be able to distinguish when a workflow is starting
and when it's being continued as new. This allows you to execute
different behavior like restoring some state from an external system.

One way to check if your current workflow execution has been continued
as new is to inspect the workflow info ContinuedAsNewRunID field. When
it's set, then you now you are not starting fresh.

When testing such workflows though, you may want to test behavior only
after a first ContinueAsNew call. The easiest way to do that is to
setup the test environment to fill up the workflow info
ContinuedAsNewRunID field.

This is what this commit is doing, it extends the
TestWorkflowEnvironment to expose a SetContinuedExecutionID field to an
arbitrary value.

This way in workflow unit tests, we can skip the code paths we don't
care about and don't resort to mocking activities and child workflows
for nothing leading to easier to implement and easier to read tests.
  • Loading branch information
lminaudier committed Sep 21, 2023
1 parent 22435a7 commit 341f533
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/internal_workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ func (env *testWorkflowEnvironmentImpl) setContinueAsNewSuggested(suggest bool)
env.workflowInfo.continueAsNewSuggested = suggest
}

func (env *testWorkflowEnvironmentImpl) setContinuedExecutionID(rid string) {
env.workflowInfo.ContinuedExecutionRunID = rid
}

func (env *testWorkflowEnvironmentImpl) newTestWorkflowEnvironmentForChild(params *ExecuteWorkflowParams, callback ResultHandler, startedHandler func(r WorkflowExecution, e error)) (*testWorkflowEnvironmentImpl, error) {
// create a new test env
childEnv := newTestWorkflowEnvironmentImpl(env.testSuite, env.registry)
Expand Down
6 changes: 6 additions & 0 deletions internal/internal_workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3747,6 +3747,12 @@ func (s *WorkflowTestSuiteUnitTest) Test_SetWorkerStopChannel() {
s.NotNil(env.workerStopChannel)
}

func (s *WorkflowTestSuiteUnitTest) Test_SetContinuedExecutionRunID() {
env := newTestWorkflowEnvironmentImpl(&s.WorkflowTestSuite, nil)
env.setContinuedExecutionID("some-run-id")
s.NotNil(env.workflowInfo.ContinuedExecutionRunID)
}

func (s *WorkflowTestSuiteUnitTest) Test_ActivityTimeoutWithDetails() {
count := 0
timeoutFn := func() error {
Expand Down
6 changes: 6 additions & 0 deletions internal/workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ func (e *TestWorkflowEnvironment) SetContinueAsNewSuggested(suggest bool) {
e.impl.setContinueAsNewSuggested(suggest)
}

// SetContinuedExecutionID sets the value that is returned from
// GetInfo(ctx).ContinuedExecutionRunID
func (e *TestWorkflowEnvironment) SetContinuedAsNewRunID(rid string) {
e.impl.setContinuedExecutionID(rid)
}

// OnActivity setup a mock call for activity. Parameter activity must be activity function (func) or activity name (string).
// You must call Return() with appropriate parameters on the returned *MockCallWrapper instance. The supplied parameters to
// the Return() call should either be a function that has exact same signature as the mocked activity, or it should be
Expand Down

0 comments on commit 341f533

Please sign in to comment.