Skip to content

Commit

Permalink
WorkflowTestSuite.UpdateWorkflow generate an update ID if none (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Dec 3, 2024
1 parent 2c6bc1d commit aa4535d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/internal_workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,9 @@ func (env *testWorkflowEnvironmentImpl) updateWorkflow(name string, id string, u
if err != nil {
panic(err)
}
if id == "" {
id = uuid.NewString()
}

if env.updateMap == nil {
env.updateMap = make(map[string]*updateResult)
Expand Down
4 changes: 3 additions & 1 deletion internal/workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,9 @@ func (e *TestWorkflowEnvironment) QueryWorkflow(queryType string, args ...interf
return e.impl.queryWorkflow(queryType, args...)
}

// UpdateWorkflow sends an update to the currently running workflow.
// UpdateWorkflow sends an update to the currently running workflow. The updateName is the name of the update handler
// to be invoked. The updateID is a unique identifier for the update. If updateID is an empty string a UUID will be generated.
// The update callbacks are used to handle the update. The args are the arguments to be passed to the update handler.
func (e *TestWorkflowEnvironment) UpdateWorkflow(updateName, updateID string, uc UpdateCallbacks, args ...interface{}) {
e.impl.updateWorkflow(updateName, updateID, uc, args...)
}
Expand Down
61 changes: 61 additions & 0 deletions internal/workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,67 @@ func TestWorkflowUpdateOrder(t *testing.T) {
require.Equal(t, 1, result)
}

func TestWorkflowUpdateIdGeneration(t *testing.T) {
var suite WorkflowTestSuite
env := suite.NewTestWorkflowEnvironment()
env.RegisterDelayedCallback(func() {
env.UpdateWorkflow("update", "", &TestUpdateCallback{
OnReject: func(err error) {
require.Fail(t, "update should not be rejected")
},
OnAccept: func() {},
OnComplete: func(interface{}, error) {},
})
}, 0)
env.RegisterDelayedCallback(func() {
env.UpdateWorkflow("update", "", &TestUpdateCallback{
OnReject: func(err error) {
},
OnAccept: func() {
require.Fail(t, "update should be rejected")
},
OnComplete: func(interface{}, error) {
},
})
}, time.Second)

env.ExecuteWorkflow(func(ctx Context) (int, error) {
var inflightUpdates int
var ranUpdates int
err := SetUpdateHandler(ctx, "update", func(ctx Context) error {
inflightUpdates++
ranUpdates++
defer func() {
inflightUpdates--
}()
return Sleep(ctx, time.Hour)
}, UpdateHandlerOptions{
Validator: func() error {
if inflightUpdates > 0 {
return errors.New("inflight updates should be 0")
}
return nil
},
})
if err != nil {
return 0, err
}
err = Await(ctx, func() bool { return inflightUpdates == 0 })
if err != nil {
return 0, err
}
err = Sleep(ctx, time.Minute)
if err != nil {
return 0, err
}
return ranUpdates, err
})
require.NoError(t, env.GetWorkflowError())
var result int
require.NoError(t, env.GetWorkflowResult(&result))
require.Equal(t, 1, result)
}

func TestWorkflowNotRegisteredRejected(t *testing.T) {
var suite WorkflowTestSuite
// Test UpdateWorkflowByID works with custom ID
Expand Down

0 comments on commit aa4535d

Please sign in to comment.