From 341f533b468ce0593768e8fec55c180ecd4895be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Minaudier?= Date: Thu, 21 Sep 2023 12:56:15 +0200 Subject: [PATCH 1/2] Export SetContinuedAsNewRunID in the TestWorkflowEnvironment 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. --- internal/internal_workflow_testsuite.go | 4 ++++ internal/internal_workflow_testsuite_test.go | 6 ++++++ internal/workflow_testsuite.go | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/internal/internal_workflow_testsuite.go b/internal/internal_workflow_testsuite.go index 88ecf1b02..42b12cfdb 100644 --- a/internal/internal_workflow_testsuite.go +++ b/internal/internal_workflow_testsuite.go @@ -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) diff --git a/internal/internal_workflow_testsuite_test.go b/internal/internal_workflow_testsuite_test.go index 916372d91..87472e619 100644 --- a/internal/internal_workflow_testsuite_test.go +++ b/internal/internal_workflow_testsuite_test.go @@ -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 { diff --git a/internal/workflow_testsuite.go b/internal/workflow_testsuite.go index 5b0c6fd02..24257f4a4 100644 --- a/internal/workflow_testsuite.go +++ b/internal/workflow_testsuite.go @@ -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 From 3b842670e42a04eb51a00fc42917040478447eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Minaudier?= Date: Fri, 22 Sep 2023 14:52:02 +0200 Subject: [PATCH 2/2] Renamed SetContinuedAsNewRunID to SetContinuedExecutionRunID Let's be consistent with the workflow info field name. Co-authored-by: Chad Retz --- internal/internal_workflow_testsuite.go | 2 +- internal/internal_workflow_testsuite_test.go | 2 +- internal/workflow_testsuite.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/internal_workflow_testsuite.go b/internal/internal_workflow_testsuite.go index 42b12cfdb..c1be8b4d7 100644 --- a/internal/internal_workflow_testsuite.go +++ b/internal/internal_workflow_testsuite.go @@ -353,7 +353,7 @@ func (env *testWorkflowEnvironmentImpl) setContinueAsNewSuggested(suggest bool) env.workflowInfo.continueAsNewSuggested = suggest } -func (env *testWorkflowEnvironmentImpl) setContinuedExecutionID(rid string) { +func (env *testWorkflowEnvironmentImpl) setContinuedExecutionRunID(rid string) { env.workflowInfo.ContinuedExecutionRunID = rid } diff --git a/internal/internal_workflow_testsuite_test.go b/internal/internal_workflow_testsuite_test.go index 87472e619..fbd618cc6 100644 --- a/internal/internal_workflow_testsuite_test.go +++ b/internal/internal_workflow_testsuite_test.go @@ -3749,7 +3749,7 @@ func (s *WorkflowTestSuiteUnitTest) Test_SetWorkerStopChannel() { func (s *WorkflowTestSuiteUnitTest) Test_SetContinuedExecutionRunID() { env := newTestWorkflowEnvironmentImpl(&s.WorkflowTestSuite, nil) - env.setContinuedExecutionID("some-run-id") + env.setContinuedExecutionRunID("some-run-id") s.NotNil(env.workflowInfo.ContinuedExecutionRunID) } diff --git a/internal/workflow_testsuite.go b/internal/workflow_testsuite.go index 24257f4a4..7df65e353 100644 --- a/internal/workflow_testsuite.go +++ b/internal/workflow_testsuite.go @@ -318,10 +318,10 @@ func (e *TestWorkflowEnvironment) SetContinueAsNewSuggested(suggest bool) { e.impl.setContinueAsNewSuggested(suggest) } -// SetContinuedExecutionID sets the value that is returned from +// SetContinuedExecutionRunID sets the value that is returned from // GetInfo(ctx).ContinuedExecutionRunID -func (e *TestWorkflowEnvironment) SetContinuedAsNewRunID(rid string) { - e.impl.setContinuedExecutionID(rid) +func (e *TestWorkflowEnvironment) SetContinuedExecutionRunID(rid string) { + e.impl.setContinuedExecutionRunID(rid) } // OnActivity setup a mock call for activity. Parameter activity must be activity function (func) or activity name (string).