Hello, I’m seeing a race condition whenever i call client.ListWorkflow() to get workflow executions. In this basic test, I’m launching 5 workflows and asserting that ListWorkflow().executions has a length of 5.
However, the assertions do not work unless I call time.sleep(). I’m wondering if this race condition is known, and if there’s a better way to handle this without calling time.sleep(). Code is below
func (suite *WorkflowTestSuite) TestWorkflowCount() {
queryResponseBefore, err := suite.client.ListWorkflow(
context.Background(),
&workflowservice.ListWorkflowExecutionsRequest{
Namespace: "default",
Query: fmt.Sprintf("WorkflowId='%s'", "123"),
},
)
if err != nil {
log.Fatal("Error querying workflows before execution: ", err)
}
// number of workflows with specified ID before launching workflows
totalCountBefore := len(queryResponseBefore.GetExecutions())
// Execute 5 workflows
for i := 0; i < 5; i++ {
workflowRun, err := suite.client.ExecuteWorkflow(context.Background(), suite.workflowOptions, common.Workflow, fmt.Sprintf("%d", i))
if err != nil {
suite.T().Fatalf("Workflow could not be executed: %v", err.Error())
}
var val interface{}
err = workflowRun.Get(context.Background(), &val)
if err != nil {
suite.T().Fatalf("Failed to complete Workflow %d: %v", i, err.Error())
}
}
// sleep needs to be here for ListWorkflow().getExecutions to return a correct number.
time.Sleep(time.Second * 2)
// Fetch the total count of workflows after execution
queryResponseAfter, err := suite.client.ListWorkflow(
context.Background(),
&workflowservice.ListWorkflowExecutionsRequest{
Namespace: "default",
Query: fmt.Sprintf("WorkflowId='%s'", "123"),
},
)
if err != nil {
log.Fatal("Error querying workflows after execution: ", err)
}
totalCountAfter := len(queryResponseAfter.GetExecutions())
// Assert the total count before execution plus 5 equals the total count after execution
assert.Equal(suite.T(), totalCountBefore+5, totalCountAfter)
}