Merge pull request #7448 from dtolnay/timeout

Timing out is not success
This commit is contained in:
Paul Hinze 2016-07-01 14:28:11 -05:00 committed by GitHub
commit ad27190852
2 changed files with 23 additions and 1 deletions

View File

@ -36,12 +36,20 @@ func Retry(timeout time.Duration, f RetryFunc) error {
},
}
c.WaitForState()
_, waitErr := c.WaitForState()
// Need to acquire the lock here to be able to avoid race using resultErr as
// the return value
resultErrMu.Lock()
defer resultErrMu.Unlock()
// resultErr may be nil because the wait timed out and resultErr was never
// set; this is still an error
if resultErr == nil {
return waitErr
}
// resultErr takes precedence over waitErr if both are set because it is
// more likely to be useful
return resultErr
}

View File

@ -38,6 +38,20 @@ func TestRetry_timeout(t *testing.T) {
}
}
func TestRetry_hang(t *testing.T) {
t.Parallel()
f := func() *RetryError {
time.Sleep(2 * time.Second)
return nil
}
err := Retry(1*time.Second, f)
if err == nil {
t.Fatal("should error")
}
}
func TestRetry_error(t *testing.T) {
t.Parallel()