From 95041ab0ae118443f0fdd20a6b5436acd6915850 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Tue, 22 Sep 2015 13:22:12 -0400 Subject: [PATCH] Check the condition immediately in util.Wait funcs Have poller() send to the channel once, immediately, before the ticker starts. This way, Poll, PollInfinite, and WaitFor will check the condition immediately, instead of waiting for the poller's interval to elapse once before doing the initial condition check. --- pkg/util/wait/wait.go | 7 +++++-- pkg/util/wait/wait_test.go | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/util/wait/wait.go b/pkg/util/wait/wait.go index 61dab005a1b..84c06ed6378 100644 --- a/pkg/util/wait/wait.go +++ b/pkg/util/wait/wait.go @@ -44,8 +44,6 @@ type ConditionFunc func() (done bool, err error) // is reached. condition will always be invoked at least once but some intervals // may be missed if the condition takes too long or the time window is too short. // If you want to Poll something forever, see PollInfinite. -// Poll always waits the interval before the first check of the condition. -// TODO: create a separate PollImmediate function that does not wait. func Poll(interval, timeout time.Duration, condition ConditionFunc) error { return WaitFor(poller(interval, timeout), condition) } @@ -90,7 +88,12 @@ func WaitFor(wait WaitFunc, c ConditionFunc) error { func poller(interval, timeout time.Duration) WaitFunc { return WaitFunc(func() <-chan struct{} { ch := make(chan struct{}) + go func() { + // send to the channel once immediately, rather than waiting for the first + // interval to elapse + ch <- struct{}{} + tick := time.NewTicker(interval) defer tick.Stop() var after <-chan time.Time diff --git a/pkg/util/wait/wait_test.go b/pkg/util/wait/wait_test.go index c7121050305..ae3734444fe 100644 --- a/pkg/util/wait/wait_test.go +++ b/pkg/util/wait/wait_test.go @@ -38,8 +38,8 @@ DRAIN: t.Errorf("unexpected timeout after poll") } } - if count > 3 { - t.Errorf("expected up to three values, got %d", count) + if count < 2 { + t.Errorf("expected at least two values, got %d", count) } }