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

Expose SetContinuedAsNewRunID in the TestWorkflowEnvironment #1246

Conversation

lminaudier
Copy link
Contributor

@lminaudier lminaudier commented Sep 21, 2023

What was changed

This extends the TestWorkflowEnvironment to expose a new SetContinuedAsNewRunID API to set the workflow under test ContinuedAsNewRunID field in its WorkflowInfo.

Why?

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. It is nice because you don't need any special field in your workflow inputs to detect such cases.

Example:

func MyWorkflow(ctx workflow.Context, input string) error {
    if workflow.GetInfo(ctx).ContinuedAsNewRunID == "" {
        var v string
        err := workflow.ExecuteActivity(ctx, MyActivity).Get(ctx, &v)
        if err != nil {
            return err
        }
        input = v
    }

    s = selector.NewSelector()
    s.AddReceive(..., func() {
        c.Receive(ctx, &v)
        input = v
    })
    s.AddReceive(...)
    s.Select(ctx)

    return workflow.NewContinueAsNewError(ctx, MyWorkflow, input)
}

When testing such workflows though, you may want to test behavior only after a first ContinueAsNew call has been made. 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.

Checklist

  1. Closes

N/A

  1. How was this tested:

I only added a unit test (so far).

  1. Any docs updates needed?

We probably want to rely on Godocs for that API.


Thoughts?

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.
@lminaudier lminaudier requested a review from a team as a code owner September 21, 2023 11:21
@lminaudier lminaudier force-pushed the test-workflow-env-set-continued-as-new-run-id branch from 49a350e to 341f533 Compare September 21, 2023 11:22
Copy link
Contributor

@Quinn-With-Two-Ns Quinn-With-Two-Ns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution

Let's be consistent with the workflow info field name.

Co-authored-by: Chad Retz <[email protected]>
@lminaudier lminaudier force-pushed the test-workflow-env-set-continued-as-new-run-id branch from 06d4318 to 3b84267 Compare September 22, 2023 12:57
@Quinn-With-Two-Ns Quinn-With-Two-Ns merged commit bfb39ac into temporalio:master Sep 24, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants