Skip to content

Commit

Permalink
fix(compute/metadata): return an error when all retries have failed (#…
Browse files Browse the repository at this point in the history
…5063)

Minor change to ensure an error is returned to the caller when all retries have failed.

Fixes #5062
  • Loading branch information
Deiz authored Nov 3, 2021
1 parent 54cbf4c commit c792a0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compute/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) {
break
}
if reqErr != nil {
return "", "", nil
return "", "", reqErr
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
Expand Down
25 changes: 20 additions & 5 deletions compute/metadata/metadata_go113_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestRetry(t *testing.T) {
failCode int
failErr error
response string
expectError bool
}{
{
name: "no retries",
Expand All @@ -49,6 +50,12 @@ func TestRetry(t *testing.T) {
failErr: io.ErrUnexpectedEOF,
timesToFail: 1,
},
{
name: "retry io.ErrUnexpectedEOF permanent",
failErr: io.ErrUnexpectedEOF,
timesToFail: maxRetryAttempts + 1,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -60,15 +67,23 @@ func TestRetry(t *testing.T) {
}
c := NewClient(&http.Client{Transport: ft})
s, err := c.Get("")
if err != nil {
if tt.expectError && err == nil {
t.Fatalf("did not receive expected error")
} else if !tt.expectError && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ft.called != ft.failedAttempts+1 {
t.Fatalf("failed %d times, want %d", ft.called, ft.failedAttempts+1)
}
if s != tt.response {

expectedCount := ft.failedAttempts + 1
if tt.expectError {
expectedCount = ft.failedAttempts
} else if s != tt.response {
// Responses are only meaningful if err == nil
t.Fatalf("c.Get() = %q, want %q", s, tt.response)
}

if ft.called != expectedCount {
t.Fatalf("failed %d times, want %d", ft.called, expectedCount)
}
})
}
}
Expand Down

0 comments on commit c792a0d

Please sign in to comment.