|
| 1 | +package tester |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/cschleiden/go-workflows/workflow" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_Timer(t *testing.T) { |
| 12 | + tester := NewWorkflowTester[timerResult](workflowTimer) |
| 13 | + start := tester.Now() |
| 14 | + |
| 15 | + tester.Execute() |
| 16 | + |
| 17 | + require.True(t, tester.WorkflowFinished()) |
| 18 | + wr, _ := tester.WorkflowResult() |
| 19 | + require.True(t, start.Equal(wr.T1)) |
| 20 | + |
| 21 | + e := start.Add(30 * time.Second) |
| 22 | + require.True(t, e.Equal(wr.T2), "expected %v, got %v", e, wr.T2) |
| 23 | +} |
| 24 | + |
| 25 | +type timerResult struct { |
| 26 | + T1 time.Time |
| 27 | + T2 time.Time |
| 28 | +} |
| 29 | + |
| 30 | +func workflowTimer(ctx workflow.Context) (timerResult, error) { |
| 31 | + t1 := workflow.Now(ctx) |
| 32 | + |
| 33 | + workflow.ScheduleTimer(ctx, 30*time.Second).Get(ctx) |
| 34 | + |
| 35 | + t2 := workflow.Now(ctx) |
| 36 | + |
| 37 | + workflow.ScheduleTimer(ctx, 30*time.Second).Get(ctx) |
| 38 | + |
| 39 | + return timerResult{ |
| 40 | + T1: t1, |
| 41 | + T2: t2, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +func Test_TimerCancellation(t *testing.T) { |
| 46 | + tester := NewWorkflowTester[time.Time](workflowTimerCancellation) |
| 47 | + start := tester.Now() |
| 48 | + |
| 49 | + tester.Execute() |
| 50 | + |
| 51 | + require.True(t, tester.WorkflowFinished()) |
| 52 | + |
| 53 | + wfR, _ := tester.WorkflowResult() |
| 54 | + require.True(t, start.Equal(wfR), "expected %v, got %v", start, wfR) |
| 55 | +} |
| 56 | + |
| 57 | +func workflowTimerCancellation(ctx workflow.Context) (time.Time, error) { |
| 58 | + tctx, cancel := workflow.WithCancel(ctx) |
| 59 | + t := workflow.ScheduleTimer(tctx, 30*time.Second) |
| 60 | + cancel() |
| 61 | + |
| 62 | + _, _ = t.Get(ctx) |
| 63 | + |
| 64 | + return workflow.Now(ctx), nil |
| 65 | +} |
| 66 | + |
| 67 | +func Test_TimerSubworkflowCancellation(t *testing.T) { |
| 68 | + tester := NewWorkflowTester[time.Time](workflowSubWorkflowTimerCancellation) |
| 69 | + tester.Registry().RegisterWorkflow(timerCancellationSubWorkflow) |
| 70 | + |
| 71 | + tester.Execute() |
| 72 | + |
| 73 | + require.True(t, tester.WorkflowFinished()) |
| 74 | + |
| 75 | + _, wfErr := tester.WorkflowResult() |
| 76 | + require.Empty(t, wfErr) |
| 77 | +} |
| 78 | + |
| 79 | +func workflowSubWorkflowTimerCancellation(ctx workflow.Context) error { |
| 80 | + _, err := workflow.CreateSubWorkflowInstance[any](ctx, workflow.DefaultSubWorkflowOptions, timerCancellationSubWorkflow).Get(ctx) |
| 81 | + |
| 82 | + // Wait long enough for the second timer in timerCancellationSubWorkflow to fire |
| 83 | + workflow.Sleep(ctx, 20*time.Second) |
| 84 | + |
| 85 | + return err |
| 86 | +} |
| 87 | + |
| 88 | +func timerCancellationSubWorkflow(ctx workflow.Context) error { |
| 89 | + tctx, cancel := workflow.WithCancel(ctx) |
| 90 | + |
| 91 | + t := workflow.ScheduleTimer(ctx, 2*time.Second) |
| 92 | + t2 := workflow.ScheduleTimer(tctx, 10*time.Second) |
| 93 | + |
| 94 | + workflow.Select( |
| 95 | + ctx, |
| 96 | + workflow.Await(t, func(ctx workflow.Context, f workflow.Future[struct{}]) { |
| 97 | + // Cancel t2 |
| 98 | + cancel() |
| 99 | + }), |
| 100 | + workflow.Await(t2, func(ctx workflow.Context, f workflow.Future[struct{}]) { |
| 101 | + // do nothing here, should never fire |
| 102 | + panic("timer should have been cancelled") |
| 103 | + }), |
| 104 | + ) |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func Test_TimerRespondingWithoutNewEvents(t *testing.T) { |
| 110 | + tester := NewWorkflowTester[time.Time](workflowTimerRespondingWithoutNewEvents) |
| 111 | + |
| 112 | + tester.ScheduleCallback(time.Duration(2*time.Second), func() { |
| 113 | + tester.SignalWorkflow("signal", "s42") |
| 114 | + }) |
| 115 | + |
| 116 | + tester.Execute() |
| 117 | + |
| 118 | + require.True(t, tester.WorkflowFinished()) |
| 119 | + |
| 120 | + _, err := tester.WorkflowResult() |
| 121 | + require.Empty(t, err) |
| 122 | +} |
| 123 | + |
| 124 | +func workflowTimerRespondingWithoutNewEvents(ctx workflow.Context) error { |
| 125 | + workflow.ScheduleTimer(ctx, 1*time.Second).Get(ctx) |
| 126 | + |
| 127 | + workflow.Select( |
| 128 | + ctx, |
| 129 | + workflow.Receive(workflow.NewSignalChannel[any](ctx, "signal"), func(ctx workflow.Context, signal any, ok bool) { |
| 130 | + // do nothing |
| 131 | + }), |
| 132 | + ) |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
0 commit comments