Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UpdateWorkflowByID to TestWorkflowEnvironment #1264

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading