RegisterDelayedCallback ignores callbacks after workflow finished, but does not complain either

Hi,

I’m using Go SDK and I did simple Workflow with QueryHandler, Timer and Signal.
After I start Workflow, I can ask with QueryHandler to current status. The workflow ends when timer fires or signal is received. Depends what comes first.

My problem is with testing this Workflow. I registered some callbacks, but the callback which should be triggered after workflow ends is not triggered at all. And also workflowEnv.AssertExpectations does not complain there are unused mocks.

// I'm using Ginkgo as a test framework
var _ = Describe("Workflow", func() {
    verifyTokenFunc :=  func() {
		var queryResult bool
		val, err := workflowEnv.QueryWorkflow(workflow.ValidateTokenQueryType, lastRecoveryToken)
		Expect(err).To(Succeed())
		Expect(val).To(Not(BeNil()))

		err = val.Get(&queryResult)
		Expect(err).To(Succeed())
		Expect(queryResult).To(BeTrue())
	}

    AfterEach(func() {
		if !workflowEnv.AssertExpectations(testingT) {
			Fail("There are some unused mocks")
		}
	})

    It("...", func(){
        workflowEnv = testSuite.NewTestWorkflowEnvironment()
		workflowEnv.RegisterActivity(activity.New(...))
		
		// This is OK, succeeding
		workflowEnv.RegisterDelayedCallback(verifyTokenFunc, 5*time.Minute)
		// Will signal workflow and it will end workflow execution
		workflowEnv.RegisterDelayedCallback(func() {
			workflowEnv.SignalWorkflow(workflow.PasswordRecoveredSignalName, nil)
		}, 50*time.Minute)
		// verifyTokenFunc is never executed, but workflowEnv.AssertExpectations(testingT) is not complaining either
		// But if it would be called, the test would fail, because token is not valid anymore
		workflowEnv.RegisterDelayedCallback(verifyTokenFunc, 51*time.Minute)
        
		workflowEnv.ExecuteWorkflow(workflow.PasswordRecoveryWorkflowV1, workflowParam)				
	    Expect(workflowEnv.IsWorkflowCompleted()).To(BeTrue())
		Expect(workflowEnv.GetWorkflowError()).To(Succeed())
    })
})

I understand that you maybe have reasons not to call callbacks when workflow is ended. And there is this in the comment AssertExpectations asserts that everything specified with On and Return was called.... However I cannot check that all registered callbacks were used.

I know, I can call verifyTokenFunc() after the line workflowEnv.ExecuteWorkflow() however I still think the problem described above should be solved.


EDIT: Realize I have a test case which sends the signal. After this, workflow should ends. But I cannot test if the workflow ended because of inner timeout or because of signal. In the example above I would be able to test with 3rd callback - the verifyTokenFunc must success. But now it will pass in all cases.

Thank you

1 Like

There is no need to use a delayed callback after the workflow is completed. You can run the query directly after ExecuteWorkflow returns. Try changing your code to:

    It("...", func(){
        workflowEnv = testSuite.NewTestWorkflowEnvironment()
		workflowEnv.RegisterActivity(activity.New(...))
		
		// This is OK, succeeding
		workflowEnv.RegisterDelayedCallback(verifyTokenFunc, 5*time.Minute)
		// Will signal workflow and it will end workflow execution
		workflowEnv.RegisterDelayedCallback(func() {
			workflowEnv.SignalWorkflow(workflow.PasswordRecoveredSignalName, nil)
		}, 50*time.Minute)        
		workflowEnv.ExecuteWorkflow(workflow.PasswordRecoveryWorkflowV1, workflowParam)	
		verifyTokenFunc() // Query can be called here

	    Expect(workflowEnv.IsWorkflowCompleted()).To(BeTrue())
		Expect(workflowEnv.GetWorkflowError()).To(Succeed())
    })