@@ -121,3 +121,77 @@ func Test_AutoExpiration_SubWorkflow(t *testing.T) {
121
121
_ , err = b .GetWorkflowInstanceState (ctx , wfi )
122
122
require .ErrorIs (t , err , backend .ErrInstanceNotFound )
123
123
}
124
+
125
+ func Test_AutoExpiration_ContinueAsNew_SubWorkflow (t * testing.T ) {
126
+ if testing .Short () {
127
+ t .Skip ()
128
+ }
129
+
130
+ autoExpirationTime := time .Second * 2
131
+
132
+ redisClient := getClient ()
133
+ setup := getCreateBackend (redisClient , WithAutoExpiration (0 ), WithAutoExpirationContinueAsNew (autoExpirationTime ))
134
+ b := setup ()
135
+
136
+ c := client .New (b )
137
+ w := worker .New (b , nil )
138
+
139
+ ctx , cancel := context .WithCancel (context .Background ())
140
+
141
+ require .NoError (t , w .Start (ctx ))
142
+ defer func () {
143
+ cancel ()
144
+
145
+ require .NoError (t , w .WaitForCompletion ())
146
+ }()
147
+
148
+ var swfInstances []* workflow.Instance
149
+
150
+ swf := func (ctx workflow.Context , iteration int ) (int , error ) {
151
+ if iteration > 3 {
152
+ return 42 , nil
153
+ }
154
+
155
+ // Keep track of continuedasnew instances
156
+ swfInstances = append (swfInstances , workflow .WorkflowInstance (ctx ))
157
+
158
+ return 0 , workflow .ContinueAsNew (ctx , iteration + 1 )
159
+ }
160
+
161
+ swfInstanceID := uuid .NewString ()
162
+
163
+ wf := func (ctx workflow.Context ) (int , error ) {
164
+ l := workflow .Logger (ctx )
165
+ l .Debug ("Starting sub workflow" , "instanceID" , swfInstanceID )
166
+
167
+ r , err := workflow .CreateSubWorkflowInstance [int ](ctx , workflow.SubWorkflowOptions {
168
+ InstanceID : swfInstanceID ,
169
+ }, swf , 0 ).Get (ctx )
170
+
171
+ workflow .ScheduleTimer (ctx , time .Second * 2 ).Get (ctx )
172
+
173
+ return r , err
174
+ }
175
+
176
+ w .RegisterWorkflow (wf )
177
+ w .RegisterWorkflow (swf )
178
+
179
+ wfi , err := c .CreateWorkflowInstance (ctx , client.WorkflowInstanceOptions {
180
+ InstanceID : uuid .NewString (),
181
+ }, wf )
182
+ require .NoError (t , err )
183
+
184
+ // Wait for redis to expire the keys
185
+ time .Sleep (autoExpirationTime * 2 )
186
+
187
+ // Main workflow should still be there
188
+ r , err := client .GetWorkflowResult [int ](ctx , c , wfi , time .Second * 10 )
189
+ require .NoError (t , err )
190
+ require .Equal (t , 42 , r )
191
+
192
+ // All continued-as-new sub-workflow instances should be expired
193
+ for _ , swfInstance := range swfInstances {
194
+ _ , err = b .GetWorkflowInstanceState (ctx , swfInstance )
195
+ require .ErrorIs (t , err , backend .ErrInstanceNotFound )
196
+ }
197
+ }
0 commit comments