Skip to content

Commit 12a14f0

Browse files
committed
Add unit tester tests
1 parent 0cf465b commit 12a14f0

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tester
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/cschleiden/go-workflows/workflow"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_ContinueAsNew(t *testing.T) {
12+
wf := func(ctx workflow.Context, run int) (int, error) {
13+
run = run + 1
14+
if run < 3 {
15+
return run, workflow.ContinueAsNew(ctx, run)
16+
}
17+
18+
return run, nil
19+
}
20+
21+
tester := NewWorkflowTester[int](wf)
22+
23+
tester.Execute(context.Background(), 0)
24+
25+
require.True(t, tester.WorkflowFinished())
26+
27+
wfR, _ := tester.WorkflowResult()
28+
require.Equal(t, 3, wfR)
29+
tester.AssertExpectations(t)
30+
}
31+
32+
func Test_ContinueAsNew_SubWorkflow(t *testing.T) {
33+
swf := func(ctx workflow.Context, run int) (int, error) {
34+
l := workflow.Logger(ctx)
35+
36+
run = run + 1
37+
if run < 3 {
38+
l.Debug("continue as new", "run", run)
39+
return run, workflow.ContinueAsNew(ctx, run)
40+
}
41+
42+
return run, nil
43+
}
44+
45+
wf := func(ctx workflow.Context, run int) (int, error) {
46+
return workflow.CreateSubWorkflowInstance[int](ctx, workflow.DefaultSubWorkflowOptions, swf, run).Get(ctx)
47+
}
48+
49+
tester := NewWorkflowTester[int](wf)
50+
51+
tester.registry.RegisterWorkflow(swf)
52+
53+
tester.Execute(context.Background(), 0)
54+
55+
require.True(t, tester.WorkflowFinished())
56+
57+
wfR, _ := tester.WorkflowResult()
58+
require.Equal(t, 3, wfR)
59+
tester.AssertExpectations(t)
60+
}

0 commit comments

Comments
 (0)