How to Unit Test a Workflow that Starts via SignalWithStartWorkflow()?

Hi,

I have a workflow I’d like to test. It looks something like:

func AddWorkflow(ctx workflow.Context) (int, error) {
	total := 0
	ch := workflow.GetSignalChannel(ctx, "number")
	for {
		var d int
		if ok := ch.ReceiveAsync(&d); !ok {
			return total, nil
		}
		// start some activities
		total += d
	}
}

It is always started via a call to SignalWithStartWorkflow().

How do I unit test this? The following doesn’t work as expected.

s.env.ExecuteWorkflow(AddWorkflow)
s.env.SignalWorkflow("number", 22)

Thanks!

Albert

Use RegisterDelayedCallback with 0 duration.

1 Like

Thank you!

For others, the final code looks like:

s.env.RegisterDelayedCallback(func() {
    s.env.SignalWorkflow("number", 22)
    s.env.SignalWorkflow("number", 66)
    s.env.SignalWorkflow("number", 123)
}, 0) // must be BEFORE "ExecuteWorkflow()"

s.env.ExecuteWorkflow(AddWorkflow)