Skip to content

Commit 5916a9f

Browse files
AxeZhank8s-publishing-bot
authored andcommitted
add new test cases
Kubernetes-commit: aee73352853885d115e77fd12520858085212094
1 parent 41ffa42 commit 5916a9f

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

pkg/util/wait/loop_test.go

+53-1
Original file line numberDiff line numberDiff line change
@@ -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,6 +204,54 @@ 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 {
@@ -214,7 +263,10 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
214263
ctx, cancel := contextFn()
215264
defer cancel()
216265

217-
timer := Backoff{Duration: time.Microsecond}.Timer()
266+
timer := test.timer
267+
if timer == nil {
268+
timer = Backoff{Duration: time.Microsecond}.Timer()
269+
}
218270
attempts := 0
219271
err := loopConditionUntilContext(ctx, timer, test.immediate, test.sliding, func(_ context.Context) (bool, error) {
220272
attempts++

0 commit comments

Comments
 (0)