Skip to content

Commit 1d17cf8

Browse files
authored
Merge pull request #368 from decibelcooper/tester/cancel-workflow
feat(tester): cancel workflow instance
2 parents fb4e75c + a522d19 commit 1d17cf8

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

tester/tester.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,33 @@ func (wt *workflowTester[TResult]) sendEvent(wfi *core.WorkflowInstance, event *
541541
w.pendingEvents = append(w.pendingEvents, event)
542542
}
543543

544+
// CancelWorkflow cancels the workflow under test.
545+
func (wt *workflowTester[TResult]) CancelWorkflow() {
546+
_ = wt.CancelWorkflowInstance(wt.wfi)
547+
}
548+
549+
// CancelWorkflowInstance cancels the given workflow instance.
550+
func (wt *workflowTester[TResult]) CancelWorkflowInstance(wfi *core.WorkflowInstance) error {
551+
if wt.getWorkflow(wfi) == nil {
552+
return backend.ErrInstanceNotFound
553+
}
554+
555+
wt.callbacks <- func() *history.WorkflowEvent {
556+
e := history.NewPendingEvent(
557+
wt.clock.Now(),
558+
history.EventType_WorkflowExecutionCanceled,
559+
&history.ExecutionCanceledAttributes{},
560+
)
561+
562+
return &history.WorkflowEvent{
563+
WorkflowInstance: wfi,
564+
HistoryEvent: e,
565+
}
566+
}
567+
568+
return nil
569+
}
570+
544571
// SignalWorkflow sends a signal to the workflow under test.
545572
func (wt *workflowTester[TResult]) SignalWorkflow(name string, value any) {
546573
wt.SignalWorkflowInstance(wt.wfi, name, value)

tester/tester_subworkflow_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tester
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -109,6 +110,47 @@ func Test_SubWorkflow_Mocked_Failure(t *testing.T) {
109110
tester.AssertExpectations(t)
110111
}
111112

113+
func Test_SubWorkflow_Cancel(t *testing.T) {
114+
subWorkflow := func(ctx workflow.Context) error {
115+
_, _ = ctx.Done().Receive(ctx)
116+
return ctx.Err()
117+
}
118+
119+
workflowWithSub := func(ctx workflow.Context) error {
120+
_, err := workflow.CreateSubWorkflowInstance[any](
121+
ctx,
122+
workflow.DefaultSubWorkflowOptions,
123+
subWorkflow,
124+
).Get(ctx)
125+
if err != nil {
126+
return fmt.Errorf("subworkflow: %w", err)
127+
}
128+
129+
return nil
130+
}
131+
132+
tester := NewWorkflowTester[string](workflowWithSub)
133+
tester.Registry().RegisterWorkflow(subWorkflow)
134+
135+
var subWorkflowInstance *core.WorkflowInstance
136+
137+
tester.ListenSubWorkflow(func(instance *core.WorkflowInstance, _ string) {
138+
subWorkflowInstance = instance
139+
})
140+
141+
tester.ScheduleCallback(time.Millisecond, func() {
142+
require.NoError(t, tester.CancelWorkflowInstance(subWorkflowInstance))
143+
})
144+
145+
tester.Execute(context.Background())
146+
147+
require.True(t, tester.WorkflowFinished())
148+
149+
_, err := tester.WorkflowResult()
150+
require.EqualError(t, err, "subworkflow: context canceled")
151+
tester.AssertExpectations(t)
152+
}
153+
112154
func Test_SubWorkflow_Signals(t *testing.T) {
113155
subWorkflow := func(ctx workflow.Context, input string) (string, error) {
114156
c := workflow.NewSignalChannel[string](ctx, "subworkflow-signal")

tester/tester_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ func activityLongRunning(ctx context.Context) (int, error) {
161161
return 42, nil
162162
}
163163

164+
func Test_CancelWorkflow(t *testing.T) {
165+
tester := NewWorkflowTester[any](func(ctx workflow.Context) error {
166+
_, _ = ctx.Done().Receive(ctx)
167+
return ctx.Err()
168+
})
169+
tester.ScheduleCallback(time.Duration(time.Second), func() {
170+
tester.CancelWorkflow()
171+
})
172+
173+
tester.Execute(context.Background())
174+
175+
require.True(t, tester.WorkflowFinished())
176+
_, err := tester.WorkflowResult()
177+
require.EqualError(t, err, "context canceled")
178+
}
179+
164180
func Test_Signals(t *testing.T) {
165181
tester := NewWorkflowTester[string](workflowSignal)
166182
tester.ScheduleCallback(time.Duration(5*time.Second), func() {

0 commit comments

Comments
 (0)