diff --git a/helper/resource/wait.go b/helper/resource/wait.go index 7c12fc476..77a7a01b0 100644 --- a/helper/resource/wait.go +++ b/helper/resource/wait.go @@ -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 } diff --git a/helper/resource/wait_test.go b/helper/resource/wait_test.go index 23451fbd4..bb17d9fe4 100644 --- a/helper/resource/wait_test.go +++ b/helper/resource/wait_test.go @@ -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()