7
7
8
8
"github.com/cschleiden/go-workflows/backend"
9
9
"github.com/cschleiden/go-workflows/client"
10
+ "github.com/cschleiden/go-workflows/internal/core"
10
11
"github.com/cschleiden/go-workflows/worker"
11
12
"github.com/cschleiden/go-workflows/workflow"
12
13
"github.com/google/uuid"
@@ -35,6 +36,8 @@ func Test_AutoExpiration(t *testing.T) {
35
36
return nil
36
37
}
37
38
39
+ w .RegisterWorkflow (wf )
40
+
38
41
wfi , err := c .CreateWorkflowInstance (ctx , client.WorkflowInstanceOptions {
39
42
InstanceID : uuid .NewString (),
40
43
}, wf )
@@ -51,3 +54,64 @@ func Test_AutoExpiration(t *testing.T) {
51
54
cancel ()
52
55
require .NoError (t , w .WaitForCompletion ())
53
56
}
57
+
58
+ func Test_AutoExpiration_SubWorkflow (t * testing.T ) {
59
+ if testing .Short () {
60
+ t .Skip ()
61
+ }
62
+
63
+ autoExpirationTime := time .Second * 1
64
+
65
+ redisClient := getClient ()
66
+ setup := getCreateBackend (redisClient , WithAutoExpiration (autoExpirationTime ))
67
+ b := setup ()
68
+
69
+ c := client .New (b )
70
+ w := worker .New (b , & worker .DefaultWorkerOptions )
71
+
72
+ ctx , cancel := context .WithCancel (context .Background ())
73
+
74
+ require .NoError (t , w .Start (ctx ))
75
+
76
+ swf := func (ctx workflow.Context ) (int , error ) {
77
+ return 42 , nil
78
+ }
79
+
80
+ swfInstanceID := uuid .NewString ()
81
+
82
+ wf := func (ctx workflow.Context ) (int , error ) {
83
+ r , err := workflow .CreateSubWorkflowInstance [int ](ctx , workflow.SubWorkflowOptions {
84
+ InstanceID : swfInstanceID ,
85
+ }, swf ).Get (ctx )
86
+
87
+ workflow .ScheduleTimer (ctx , time .Second * 2 ).Get (ctx )
88
+
89
+ return r , err
90
+ }
91
+
92
+ w .RegisterWorkflow (wf )
93
+ w .RegisterWorkflow (swf )
94
+
95
+ wfi , err := c .CreateWorkflowInstance (ctx , client.WorkflowInstanceOptions {
96
+ InstanceID : uuid .NewString (),
97
+ }, wf )
98
+ require .NoError (t , err )
99
+
100
+ r , err := client .GetWorkflowResult [int ](ctx , c , wfi , time .Second * 10 )
101
+ require .NoError (t , err )
102
+ require .Equal (t , 42 , r )
103
+
104
+ // Subworkflow should be expired by now
105
+ _ , err = b .GetWorkflowInstanceState (ctx , core .NewWorkflowInstance (swfInstanceID ))
106
+ require .ErrorIs (t , err , backend .ErrInstanceNotFound )
107
+
108
+ // Wait for redis to expire the keys
109
+ time .Sleep (autoExpirationTime )
110
+
111
+ // Main workflow should now be expired
112
+ _ , err = b .GetWorkflowInstanceState (ctx , wfi )
113
+ require .ErrorIs (t , err , backend .ErrInstanceNotFound )
114
+
115
+ cancel ()
116
+ require .NoError (t , w .WaitForCompletion ())
117
+ }
0 commit comments