Test workflow w async activity not yet complete

Why is this test failing? Why does IsWorkflowComplete return true when the activity has returned activity.ErrResultPending. I’ve verified the activity is executed.

func Workflow(context workflow.Context) (bool, error) {
	activityExecution := workflow.ExecuteActivity(context, Activity)
	var result bool
	_ = activityExecution.Get(context, &result)
	return result, nil
}

func Activity(context context.Context) (bool, error) {
	_ = activity.GetInfo(context).TaskToken
	// pass TaskToken to external system
	return false, activity.ErrResultPending
}

func (testSuite *MyTestSuite) TestWorkflowNotComplete() {
	testSuite.env.RegisterActivity(Activity)
	testSuite.env.ExecuteWorkflow(Workflow)
	testSuite.False(testSuite.env.IsWorkflowCompleted())
}

IsWorkflowComplete returns true.

Thank you in advance.

Please don’t ignore errors. Change the workflow code to:

func Workflow(context workflow.Context) (bool, error) {
	activityExecution := workflow.ExecuteActivity(context, Activity)
	var result bool
	err := activityExecution.Get(context, &result)
	return result, err
}

And check the error after calling ExecuteWorkflow:

func (testSuite *MyTestSuite) TestWorkflowNotComplete() {
	testSuite.env.RegisterActivity(Activity)
	testSuite.env.ExecuteWorkflow(Workflow)
        testSuite.NoError(t, testSuite.env.GetWorkflowError())
	testSuite.False(testSuite.env.IsWorkflowCompleted())
}

And you’ll see the following error:

  Error:      	Received unexpected error:
   	            	workflow execution error (type: Workflow, workflowID: default-test-workflow-id, runID: default-test-run-id): A valid StartToClose or ScheduleToCloseTimeout is not set on command. (type: InvalidArgument, retryable: true)

So the fix is:

	context = workflow.WithActivityOptions(context, ActivityOptions{StartToCloseTimeout: 10 * time.Second})

After the fix the whole workflow times out as expected.

Thank you for the response, maxim. After modifying the code as directed I received the error:
workflow execution error (type: Workflow, workflowID: default-test-workflow-id, runID: default-test-run-id): deadline exceeded (type: ScheduleToClose)

Adding the following didn’t change the result:
ScheduleToCloseTimeout: 10 * time.Second,

Here is my new code that results in a ScheduleToClose timeout error:

func Workflow(context workflow.Context) (bool, error) {
	context = workflow.WithActivityOptions(
		context,
		workflow.ActivityOptions{
			StartToCloseTimeout: 10 * time.Second,
			ScheduleToCloseTimeout: 10 * time.Second,
		})
	activityExecution := workflow.ExecuteActivity(context, Activity)
	var result bool
	err := activityExecution.Get(context, &result)
	return result, err
}

func Activity(context context.Context) (bool, error) {
	_ = activity.GetInfo(context).TaskToken
	// pass TaskToken to external system
	return false, activity.ErrResultPending
}

func (testSuite *MyTestSuite) TestWorkflowNotComplete() {
	testSuite.env.RegisterActivity(Activity)
	testSuite.env.ExecuteWorkflow(Workflow)
	testSuite.NoError(testSuite.env.GetWorkflowError())
	testSuite.False(testSuite.env.IsWorkflowCompleted())
}

I am unsure where to go from here.

In creating the simple example from my actual code under test I had inadvertently ignored the error in the workflow. I also have the timeouts in my actual code, and I was failing to check GetWorkflowError(). After adding it to my test, my actual code under test is failing on the same error as the above code.

Thank you again for your help with testing this negative case.

So it works as expected in this case. The activity is never completed, so it times out. As StartToClose timeout and the ScheduleToStart timeout are the same it times out with ScheduleToStart. The workflow propagates the activity error up, so you see it as the cause of the workflow failure.

I’m attempting to test the result of a query of a workflow that is not yet complete. Is there an example that tests that use case? My test completes in less than 300 milliseconds. How is timeout elapsing? Is there a mock clock I need to control?

Apologies if this comes across as an XY problem. Even though my ultimate goal is testing the query, the problem presented is something I wish to understand.

Thank you again for the help.

I see. The test framework automatically forwards the workflow clock forward to support testing long-running workflows. To test query perform it from a delayed callback.

My test is passing now, thank you. I like how time is managed with this abstraction. I’m impressed with the API design overall.

1 Like