Skip to content

Commit

Permalink
Add UpdateWorkflowByID to TestWorkflowEnvironment (#1264)
Browse files Browse the repository at this point in the history
Add UpdateWorkflowByID to TestWorkflowEnvironment
  • Loading branch information
Quinn-With-Two-Ns authored Oct 12, 2023
1 parent 83199af commit a6cdcd7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
16 changes: 16 additions & 0 deletions internal/internal_workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,22 @@ func (env *testWorkflowEnvironmentImpl) updateWorkflow(name string, id string, u
env.updateHandler(name, id, data, nil, uc)
}

func (env *testWorkflowEnvironmentImpl) updateWorkflowByID(workflowID, name, id string, uc UpdateCallbacks, args ...interface{}) error {
if workflowHandle, ok := env.runningWorkflows[workflowID]; ok {
if workflowHandle.handled {
return serviceerror.NewNotFound(fmt.Sprintf("Workflow %v already completed", workflowID))
}
data, err := encodeArgs(env.GetDataConverter(), args)
if err != nil {
panic(err)
}
env.updateHandler(name, id, data, nil, uc)
return nil
}

return serviceerror.NewNotFound(fmt.Sprintf("Workflow %v not exists", workflowID))
}

func (env *testWorkflowEnvironmentImpl) queryWorkflowByID(workflowID, queryType string, args ...interface{}) (converter.EncodedValue, error) {
if workflowHandle, ok := env.runningWorkflows[workflowID]; ok {
data, err := encodeArgs(workflowHandle.env.GetDataConverter(), args)
Expand Down
9 changes: 7 additions & 2 deletions internal/workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,13 @@ func (e *TestWorkflowEnvironment) QueryWorkflow(queryType string, args ...interf
}

// UpdateWorkflow sends an update to the currently running workflow.
func (e *TestWorkflowEnvironment) UpdateWorkflow(name string, id string, uc UpdateCallbacks, args ...interface{}) {
e.impl.updateWorkflow(name, id, uc, args...)
func (e *TestWorkflowEnvironment) UpdateWorkflow(updateName, updateID string, uc UpdateCallbacks, args ...interface{}) {
e.impl.updateWorkflow(updateName, updateID, uc, args...)
}

// UpdateWorkflowByID sends an update to a running workflow by its ID.
func (e *TestWorkflowEnvironment) UpdateWorkflowByID(workflowID, updateName, updateID string, uc UpdateCallbacks, args interface{}) error {
return e.impl.updateWorkflowByID(workflowID, updateName, updateID, uc, args)
}

// QueryWorkflowByID queries a child workflow by its ID and returns the result synchronously
Expand Down
34 changes: 34 additions & 0 deletions internal/workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ func TestWorkflowIDSignalWorkflowByID(t *testing.T) {
require.Equal(t, "id is: my-workflow-id", str)
}

func TestWorkflowIDUpdateWorkflowByID(t *testing.T) {
var suite WorkflowTestSuite
// Test UpdateWorkflowByID works with custom ID
env := suite.NewTestWorkflowEnvironment()
env.RegisterDelayedCallback(func() {
err := env.UpdateWorkflowByID("my-workflow-id", "update", "id", &updateCallback{
reject: func(err error) {
require.Fail(t, "update should not be rejected")
},
accept: func() {},
complete: func(interface{}, error) {},
}, "input")
require.NoError(t, err)
}, time.Second)

env.SetStartWorkflowOptions(StartWorkflowOptions{ID: "my-workflow-id"})
env.ExecuteWorkflow(func(ctx Context) (string, error) {
var result string
err := SetUpdateHandler(ctx, "update", func(ctx Context, input string) error {
result = input
return nil
}, UpdateHandlerOptions{})
if err != nil {
return "", err
}
err = Await(ctx, func() bool { return result != "" })
return result, err
})
require.NoError(t, env.GetWorkflowError())
var str string
require.NoError(t, env.GetWorkflowResult(&str))
require.Equal(t, "input", str)
}

func TestWorkflowStartTimeInsideTestWorkflow(t *testing.T) {
var suite WorkflowTestSuite
env := suite.NewTestWorkflowEnvironment()
Expand Down

0 comments on commit a6cdcd7

Please sign in to comment.