From 7e1cd3481935a9d3c998141dc7ec93e4007f7cda Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 30 Jun 2016 18:07:56 -0700 Subject: [PATCH] Timing out is not success --- helper/resource/wait.go | 10 +++++++++- helper/resource/wait_test.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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()