Skip to content

Commit

Permalink
helper/resource: Add timeout to TimeoutError msg (hashicorp#8773)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Sep 15, 2016
1 parent f9db946 commit 86acdcc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
18 changes: 14 additions & 4 deletions helper/resource/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"fmt"
"strings"
"time"
)

type NotFoundError struct {
Expand Down Expand Up @@ -41,6 +42,7 @@ func (e *UnexpectedStateError) Error() string {
type TimeoutError struct {
LastError error
LastState string
Timeout time.Duration
ExpectedState []string
}

Expand All @@ -50,16 +52,24 @@ func (e *TimeoutError) Error() string {
expectedState = fmt.Sprintf("state to become '%s'", strings.Join(e.ExpectedState, ", "))
}

lastState := ""
extraInfo := make([]string, 0)
if e.LastState != "" {
lastState = fmt.Sprintf(" (last state: '%s')", e.LastState)
extraInfo = append(extraInfo, fmt.Sprintf("last state: '%s'", e.LastState))
}
if e.Timeout > 0 {
extraInfo = append(extraInfo, fmt.Sprintf("timeout: %s", e.Timeout.String()))
}

suffix := ""
if len(extraInfo) > 0 {
suffix = fmt.Sprintf(" (%s)", strings.Join(extraInfo, ", "))
}

if e.LastError != nil {
return fmt.Sprintf("timeout while waiting for %s%s: %s",
expectedState, lastState, e.LastError)
expectedState, suffix, e.LastError)
}

return fmt.Sprintf("timeout while waiting for %s%s",
expectedState, lastState)
expectedState, suffix)
}
1 change: 1 addition & 0 deletions helper/resource/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
return nil, &TimeoutError{
LastError: r.Error,
LastState: r.State,
Timeout: conf.Timeout,
ExpectedState: conf.Target,
}
}
Expand Down
6 changes: 3 additions & 3 deletions helper/resource/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestWaitForState_inconsistent_negative(t *testing.T) {
if err == nil {
t.Fatal("Expected timeout error. No error returned.")
}
expectedErr := "timeout while waiting for state to become 'done' (last state: 'done')"
expectedErr := "timeout while waiting for state to become 'done' (last state: 'done', timeout: 90ms)"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}
Expand All @@ -126,7 +126,7 @@ func TestWaitForState_timeout(t *testing.T) {
t.Fatal("Expected timeout error. No error returned.")
}

expectedErr := "timeout while waiting for state to become 'running'"
expectedErr := "timeout while waiting for state to become 'running' (timeout: 1ms)"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestWaitForState_failureEmpty(t *testing.T) {
if err == nil {
t.Fatal("Expected timeout error. Got none.")
}
expectedErr := "timeout while waiting for resource to be gone (last state: 'pending')"
expectedErr := "timeout while waiting for resource to be gone (last state: 'pending', timeout: 100ms)"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}
Expand Down

0 comments on commit 86acdcc

Please sign in to comment.