Skip to content

Commit 6962b6c

Browse files
committed
Fix signaling in teste workflows
1 parent 26d75c9 commit 6962b6c

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

tester/tester.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,8 @@ type signaler[T any] struct {
646646
}
647647

648648
func (s *signaler[T]) SignalWorkflow(ctx context.Context, instanceID string, name string, arg interface{}) error {
649+
s.wt.SignalWorkflowInstance(core.NewWorkflowInstance(instanceID, ""), name, arg)
650+
649651
return nil
650652
}
651653

tester/tester_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,34 @@ func workflowTimerCancellation(ctx workflow.Context) (time.Time, error) {
204204
return workflow.Now(ctx), nil
205205
}
206206

207+
func Test_TimerRespondingWithoutNewEvents(t *testing.T) {
208+
tester := NewWorkflowTester[time.Time](workflowTimerRespondingWithoutNewEvents)
209+
210+
tester.ScheduleCallback(time.Duration(2*time.Second), func() {
211+
tester.SignalWorkflow("signal", "s42")
212+
})
213+
214+
tester.Execute()
215+
216+
require.True(t, tester.WorkflowFinished())
217+
218+
_, err := tester.WorkflowResult()
219+
require.Empty(t, err)
220+
}
221+
222+
func workflowTimerRespondingWithoutNewEvents(ctx workflow.Context) error {
223+
workflow.ScheduleTimer(ctx, 1*time.Second).Get(ctx)
224+
225+
workflow.Select(
226+
ctx,
227+
workflow.Receive(workflow.NewSignalChannel[any](ctx, "signal"), func(ctx workflow.Context, signal any, ok bool) {
228+
// do nothing
229+
}),
230+
)
231+
232+
return nil
233+
}
234+
207235
func Test_Signals(t *testing.T) {
208236
tester := NewWorkflowTester[string](workflowSignal)
209237
tester.ScheduleCallback(time.Duration(5*time.Second), func() {
@@ -263,3 +291,40 @@ func workflowSubWorkFlowsAndSignals(ctx workflow.Context) (string, error) {
263291
func workflowSum(ctx workflow.Context, valA, valB int) (int, error) {
264292
return valA + valB, nil
265293
}
294+
295+
func Test_SignalSubWorkflow(t *testing.T) {
296+
tester := NewWorkflowTester[int](workflowSubworkflowSignal)
297+
require.NoError(t, tester.Registry().RegisterWorkflow(waitForSignal))
298+
299+
tester.Execute()
300+
301+
require.True(t, tester.WorkflowFinished())
302+
wfR, wfErr := tester.WorkflowResult()
303+
require.Empty(t, wfErr)
304+
require.Equal(t, 42, wfR)
305+
}
306+
307+
func workflowSubworkflowSignal(ctx workflow.Context) (int, error) {
308+
sw := workflow.CreateSubWorkflowInstance[int](ctx, workflow.SubWorkflowOptions{
309+
InstanceID: "subworkflow",
310+
}, waitForSignal)
311+
312+
_, err := workflow.SignalWorkflow(ctx, "subworkflow", "signal", "").Get(ctx)
313+
if err != nil {
314+
return 0, err
315+
}
316+
317+
// Wait for subworkflow and return result
318+
return sw.Get(ctx)
319+
}
320+
321+
func waitForSignal(ctx workflow.Context) (int, error) {
322+
workflow.Select(
323+
ctx,
324+
workflow.Receive(workflow.NewSignalChannel[any](ctx, "signal"), func(ctx workflow.Context, signal any, ok bool) {
325+
// Do nothing
326+
}),
327+
)
328+
329+
return 42, nil
330+
}

0 commit comments

Comments
 (0)