@@ -99,6 +99,7 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
99
99
cancelContextAfter int
100
100
attemptsExpected int
101
101
errExpected error
102
+ timer Timer
102
103
}{
103
104
{
104
105
name : "condition successful is only one attempt" ,
@@ -203,6 +204,54 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
203
204
attemptsExpected : 0 ,
204
205
errExpected : context .DeadlineExceeded ,
205
206
},
207
+ {
208
+ name : "context canceled before the second execution and immediate" ,
209
+ immediate : true ,
210
+ context : func () (context.Context , context.CancelFunc ) {
211
+ return context .WithTimeout (context .Background (), time .Second )
212
+ },
213
+ callback : func (attempts int ) (bool , error ) {
214
+ return false , nil
215
+ },
216
+ attemptsExpected : 1 ,
217
+ errExpected : context .DeadlineExceeded ,
218
+ timer : Backoff {Duration : 2 * time .Second }.Timer (),
219
+ },
220
+ {
221
+ name : "immediate and long duration of condition and sliding false" ,
222
+ immediate : true ,
223
+ sliding : false ,
224
+ context : func () (context.Context , context.CancelFunc ) {
225
+ return context .WithTimeout (context .Background (), time .Second )
226
+ },
227
+ callback : func (attempts int ) (bool , error ) {
228
+ if attempts >= 4 {
229
+ return true , nil
230
+ }
231
+ time .Sleep (time .Second / 5 )
232
+ return false , nil
233
+ },
234
+ attemptsExpected : 4 ,
235
+ timer : Backoff {Duration : time .Second / 5 , Jitter : 0.001 }.Timer (),
236
+ },
237
+ {
238
+ name : "immediate and long duration of condition and sliding true" ,
239
+ immediate : true ,
240
+ sliding : true ,
241
+ context : func () (context.Context , context.CancelFunc ) {
242
+ return context .WithTimeout (context .Background (), time .Second )
243
+ },
244
+ callback : func (attempts int ) (bool , error ) {
245
+ if attempts >= 4 {
246
+ return true , nil
247
+ }
248
+ time .Sleep (time .Second / 5 )
249
+ return false , nil
250
+ },
251
+ errExpected : context .DeadlineExceeded ,
252
+ attemptsExpected : 3 ,
253
+ timer : Backoff {Duration : time .Second / 5 , Jitter : 0.001 }.Timer (),
254
+ },
206
255
}
207
256
208
257
for _ , test := range tests {
@@ -214,7 +263,10 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
214
263
ctx , cancel := contextFn ()
215
264
defer cancel ()
216
265
217
- timer := Backoff {Duration : time .Microsecond }.Timer ()
266
+ timer := test .timer
267
+ if timer == nil {
268
+ timer = Backoff {Duration : time .Microsecond }.Timer ()
269
+ }
218
270
attempts := 0
219
271
err := loopConditionUntilContext (ctx , timer , test .immediate , test .sliding , func (_ context.Context ) (bool , error ) {
220
272
attempts ++
0 commit comments