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
maxim
January 26, 2024, 7:10pm
2
Use RegisterDelayedCallback with 0 duration.
// QueryWorkflowByID queries a child workflow by its ID and returns the result synchronously
func (e *TestWorkflowEnvironment) QueryWorkflowByID(workflowID, queryType string, args ...interface{}) (converter.EncodedValue, error) {
return e.impl.queryWorkflowByID(workflowID, queryType, args...)
}
// RegisterDelayedCallback creates a new timer with specified delayDuration using workflow clock (not wall clock). When
// the timer fires, the callback will be called. By default, this test suite uses mock clock which automatically move
// forward to fire next timer when workflow is blocked. Use this API to make some event (like activity completion,
// signal or workflow cancellation) at desired time.
//
// Use 0 delayDuration to send a signal to simulate SignalWithStart. Note that a 0 duration delay will *not* work with
// Queries, as the workflow will not have had a chance to register any query handlers.
func (e *TestWorkflowEnvironment) RegisterDelayedCallback(callback func(), delayDuration time.Duration) {
e.impl.registerDelayedCallback(callback, delayDuration)
}
// SetActivityTaskQueue set the affinity between activity and taskqueue. By default, activity can be invoked by any taskqueue
// in this test environment. Use this SetActivityTaskQueue() to set affinity between activity and a taskqueue. Once
// activity is set to a particular taskqueue, that activity will only be available to that taskqueue.
func (e *TestWorkflowEnvironment) SetActivityTaskQueue(taskqueue string, activityFn ...interface{}) {
e.impl.setActivityTaskQueue(taskqueue, activityFn...)
1 Like
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)