Fix maxTimes comment to clarify infinity waits and add a PollInfinite function.

pull/6/head
jayunit100 2015-07-29 17:14:18 -04:00
parent c5bffaaf31
commit 3b295ee342
2 changed files with 9 additions and 4 deletions

View File

@ -43,14 +43,18 @@ type ConditionFunc func() (done bool, err error)
// Poll tries a condition func until it returns true, an error, or the timeout // Poll tries a condition func until it returns true, an error, or the timeout
// is reached. condition will always be invoked at least once but some intervals // 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. // may be missed if the condition takes too long or the time window is too short.
// If you pass maxTimes = 0, Poll will loop until condition returns true or an // If you want to Poll something forever, see PollInfinite.
// error.
// Poll always waits the interval before the first check of the condition. // Poll always waits the interval before the first check of the condition.
// TODO: create a separate PollImmediate function that does not wait. // TODO: create a separate PollImmediate function that does not wait.
func Poll(interval, timeout time.Duration, condition ConditionFunc) error { func Poll(interval, timeout time.Duration, condition ConditionFunc) error {
return WaitFor(poller(interval, timeout), condition) return WaitFor(poller(interval, timeout), condition)
} }
// PollInfinite polls forever.
func PollInfinite(interval time.Duration, condition ConditionFunc) error {
return WaitFor(poller(interval, 0), condition)
}
// WaitFunc creates a channel that receives an item every time a test // WaitFunc creates a channel that receives an item every time a test
// should be executed and is closed when the last test should be invoked. // should be executed and is closed when the last test should be invoked.
type WaitFunc func() <-chan struct{} type WaitFunc func() <-chan struct{}
@ -81,7 +85,7 @@ func WaitFor(wait WaitFunc, c ConditionFunc) error {
// poller returns a WaitFunc that will send to the channel every // poller returns a WaitFunc that will send to the channel every
// interval until timeout has elapsed and then close the channel. // interval until timeout has elapsed and then close the channel.
// Over very short intervals you may receive no ticks before // Over very short intervals you may receive no ticks before
// the channel is closed closed. If maxTimes is 0, the channel // the channel is closed. If timeout is 0, the channel
// will never be closed. // will never be closed.
func poller(interval, timeout time.Duration) WaitFunc { func poller(interval, timeout time.Duration) WaitFunc {
return WaitFunc(func() <-chan struct{} { return WaitFunc(func() <-chan struct{} {

View File

@ -91,9 +91,10 @@ func TestPollForever(t *testing.T) {
} }
return false, nil return false, nil
}) })
if err := Poll(time.Microsecond, 0, f); err != nil { if err := PollInfinite(time.Microsecond, f); err != nil {
t.Fatalf("unexpected error %v", err) t.Fatalf("unexpected error %v", err)
} }
close(ch) close(ch)
complete <- struct{}{} complete <- struct{}{}
}() }()