Timing out is not success

This commit is contained in:
David Tolnay 2016-06-30 18:07:56 -07:00
parent 1a4bf5cc89
commit 7e1cd34819
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()