Merge pull request #14365 from ncdc/poll-immediately
Check the condition immediately in util.Wait funcs
This commit is contained in:
		@@ -44,8 +44,6 @@ type ConditionFunc func() (done bool, err error)
 | 
				
			|||||||
// 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 want to Poll something forever, see PollInfinite.
 | 
					// 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 {
 | 
					func Poll(interval, timeout time.Duration, condition ConditionFunc) error {
 | 
				
			||||||
	return WaitFor(poller(interval, timeout), condition)
 | 
						return WaitFor(poller(interval, timeout), condition)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -90,7 +88,12 @@ func WaitFor(wait WaitFunc, c ConditionFunc) error {
 | 
				
			|||||||
func poller(interval, timeout time.Duration) WaitFunc {
 | 
					func poller(interval, timeout time.Duration) WaitFunc {
 | 
				
			||||||
	return WaitFunc(func() <-chan struct{} {
 | 
						return WaitFunc(func() <-chan struct{} {
 | 
				
			||||||
		ch := make(chan struct{})
 | 
							ch := make(chan struct{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		go func() {
 | 
							go func() {
 | 
				
			||||||
 | 
								// send to the channel once immediately, rather than waiting for the first
 | 
				
			||||||
 | 
								// interval to elapse
 | 
				
			||||||
 | 
								ch <- struct{}{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			tick := time.NewTicker(interval)
 | 
								tick := time.NewTicker(interval)
 | 
				
			||||||
			defer tick.Stop()
 | 
								defer tick.Stop()
 | 
				
			||||||
			var after <-chan time.Time
 | 
								var after <-chan time.Time
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,8 +40,8 @@ DRAIN:
 | 
				
			|||||||
			t.Errorf("unexpected timeout after poll")
 | 
								t.Errorf("unexpected timeout after poll")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if count > 3 {
 | 
						if count < 2 {
 | 
				
			||||||
		t.Errorf("expected up to three values, got %d", count)
 | 
							t.Errorf("expected at least two values, got %d", count)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user