@@ -99,6 +99,7 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
9999 cancelContextAfter int
100100 attemptsExpected int
101101 errExpected error
102+ timer Timer
102103 }{
103104 {
104105 name : "condition successful is only one attempt" ,
@@ -203,45 +204,88 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
203204 attemptsExpected : 0 ,
204205 errExpected : context .DeadlineExceeded ,
205206 },
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+ },
206255 }
207256
208257 for _ , test := range tests {
209- for _ , immediate := range []bool {true , false } {
210- t .Run (fmt .Sprintf ("immediate=%t" , immediate ), func (t * testing.T ) {
211- for _ , sliding := range []bool {true , false } {
212- t .Run (fmt .Sprintf ("sliding=%t" , sliding ), func (t * testing.T ) {
213- t .Run (test .name , func (t * testing.T ) {
214- contextFn := test .context
215- if contextFn == nil {
216- contextFn = defaultContext
217- }
218- ctx , cancel := contextFn ()
219- defer cancel ()
220-
221- timer := Backoff {Duration : time .Microsecond }.Timer ()
222- attempts := 0
223- err := loopConditionUntilContext (ctx , timer , test .immediate , test .sliding , func (_ context.Context ) (bool , error ) {
224- attempts ++
225- defer func () {
226- if test .cancelContextAfter > 0 && test .cancelContextAfter == attempts {
227- cancel ()
228- }
229- }()
230- return test .callback (attempts )
231- })
232-
233- if test .errExpected != err {
234- t .Errorf ("expected error: %v but got: %v" , test .errExpected , err )
235- }
236-
237- if test .attemptsExpected != attempts {
238- t .Errorf ("expected attempts count: %d but got: %d" , test .attemptsExpected , attempts )
239- }
240- })
241- })
242- }
258+ t .Run (test .name , func (t * testing.T ) {
259+ contextFn := test .context
260+ if contextFn == nil {
261+ contextFn = defaultContext
262+ }
263+ ctx , cancel := contextFn ()
264+ defer cancel ()
265+
266+ timer := test .timer
267+ if timer == nil {
268+ timer = Backoff {Duration : time .Microsecond }.Timer ()
269+ }
270+ attempts := 0
271+ err := loopConditionUntilContext (ctx , timer , test .immediate , test .sliding , func (_ context.Context ) (bool , error ) {
272+ attempts ++
273+ defer func () {
274+ if test .cancelContextAfter > 0 && test .cancelContextAfter == attempts {
275+ cancel ()
276+ }
277+ }()
278+ return test .callback (attempts )
243279 })
244- }
280+
281+ if test .errExpected != err {
282+ t .Errorf ("expected error: %v but got: %v" , test .errExpected , err )
283+ }
284+
285+ if test .attemptsExpected != attempts {
286+ t .Errorf ("expected attempts count: %d but got: %d" , test .attemptsExpected , attempts )
287+ }
288+ })
245289 }
246290}
247291
0 commit comments