Skip to content

Commit

Permalink
add new test cases
Browse files Browse the repository at this point in the history
Kubernetes-commit: aee73352853885d115e77fd12520858085212094
  • Loading branch information
AxeZhan authored and k8s-publishing-bot committed Sep 13, 2023
1 parent 41ffa42 commit 5916a9f
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion pkg/util/wait/loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
cancelContextAfter int
attemptsExpected int
errExpected error
timer Timer
}{
{
name: "condition successful is only one attempt",
Expand Down Expand Up @@ -203,6 +204,54 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
attemptsExpected: 0,
errExpected: context.DeadlineExceeded,
},
{
name: "context canceled before the second execution and immediate",
immediate: true,
context: func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Second)
},
callback: func(attempts int) (bool, error) {
return false, nil
},
attemptsExpected: 1,
errExpected: context.DeadlineExceeded,
timer: Backoff{Duration: 2 * time.Second}.Timer(),
},
{
name: "immediate and long duration of condition and sliding false",
immediate: true,
sliding: false,
context: func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Second)
},
callback: func(attempts int) (bool, error) {
if attempts >= 4 {
return true, nil
}
time.Sleep(time.Second / 5)
return false, nil
},
attemptsExpected: 4,
timer: Backoff{Duration: time.Second / 5, Jitter: 0.001}.Timer(),
},
{
name: "immediate and long duration of condition and sliding true",
immediate: true,
sliding: true,
context: func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Second)
},
callback: func(attempts int) (bool, error) {
if attempts >= 4 {
return true, nil
}
time.Sleep(time.Second / 5)
return false, nil
},
errExpected: context.DeadlineExceeded,
attemptsExpected: 3,
timer: Backoff{Duration: time.Second / 5, Jitter: 0.001}.Timer(),
},
}

for _, test := range tests {
Expand All @@ -214,7 +263,10 @@ func Test_loopConditionUntilContext_semantic(t *testing.T) {
ctx, cancel := contextFn()
defer cancel()

timer := Backoff{Duration: time.Microsecond}.Timer()
timer := test.timer
if timer == nil {
timer = Backoff{Duration: time.Microsecond}.Timer()
}
attempts := 0
err := loopConditionUntilContext(ctx, timer, test.immediate, test.sliding, func(_ context.Context) (bool, error) {
attempts++
Expand Down

0 comments on commit 5916a9f

Please sign in to comment.