diff --git a/test/e2e/common/autoscaling_utils.go b/test/e2e/common/autoscaling_utils.go index 11f53923825..52fd8b312c7 100644 --- a/test/e2e/common/autoscaling_utils.go +++ b/test/e2e/common/autoscaling_utils.go @@ -332,27 +332,33 @@ func (rc *ResourceConsumer) GetReplicas() int { } func (rc *ResourceConsumer) WaitForReplicas(desiredReplicas int) { - timeout := 15 * time.Minute - for start := time.Now(); time.Since(start) < timeout; time.Sleep(20 * time.Second) { - if desiredReplicas == rc.GetReplicas() { - framework.Logf("%s: current replicas number is equal to desired replicas number: %d", rc.kind, desiredReplicas) - return - } else { - framework.Logf("%s: current replicas number %d waiting to be %d", rc.kind, rc.GetReplicas(), desiredReplicas) - } - } - framework.Failf("timeout waiting %v for pods size to be %d", timeout, desiredReplicas) + duration := 15 * time.Minute + interval := 20 * time.Second + err := wait.PollImmediate(interval, duration, func() (bool, error) { + replicas := rc.GetReplicas() + framework.Logf("waiting for %d replicas (current: %d)", desiredReplicas, replicas) + return replicas == desiredReplicas, nil // Expected number of replicas found. Exit. + }) + framework.ExpectNoErrorWithOffset(1, err, "timeout waiting %v for %d replicas", duration, desiredReplicas) } -func (rc *ResourceConsumer) EnsureDesiredReplicas(desiredReplicas int, timeout time.Duration) { - for start := time.Now(); time.Since(start) < timeout; time.Sleep(10 * time.Second) { - actual := rc.GetReplicas() - if desiredReplicas != actual { - framework.Failf("Number of replicas has changed: expected %v, got %v", desiredReplicas, actual) +func (rc *ResourceConsumer) EnsureDesiredReplicas(desiredReplicas int, duration time.Duration) { + interval := 10 * time.Second + err := wait.PollImmediate(interval, duration, func() (bool, error) { + replicas := rc.GetReplicas() + framework.Logf("expecting there to be %d replicas (are: %d)", desiredReplicas, replicas) + if replicas != desiredReplicas { + return false, fmt.Errorf("number of replicas changed unexpectedly") + } else { + return false, nil // Expected number of replicas found. Continue polling until timeout. } - framework.Logf("Number of replicas is as expected") + }) + // The call above always returns an error, but if it is timeout, it's OK (condition satisfied all the time). + if err == wait.ErrWaitTimeout { + framework.Logf("Number of replicas was stable over %v", duration) + return } - framework.Logf("Number of replicas was stable over %v", timeout) + framework.ExpectNoErrorWithOffset(1, err) } // Pause stops background goroutines responsible for consuming resources. diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index 8dd5aca4962..4807e729cbf 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -294,7 +294,7 @@ func Failf(format string, args ...interface{}) { func FailfWithOffset(offset int, format string, args ...interface{}) { msg := fmt.Sprintf(format, args...) log("INFO", msg) - Fail(nowStamp()+": "+msg, 1 + offset) + Fail(nowStamp()+": "+msg, 1+offset) } func Skipf(format string, args ...interface{}) { @@ -1932,7 +1932,7 @@ func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) { if err != nil { Logf("Unexpected error occurred: %v", err) } - ExpectWithOffset(1 + offset, err).NotTo(HaveOccurred(), explain...) + ExpectWithOffset(1+offset, err).NotTo(HaveOccurred(), explain...) } func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) {