-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set HTTP timeouts as downstream errors (#1063)
* add timeout checks for downstream errors * simplify * add tests * add wrapped error cases * update tests * Fix typo in comment Co-authored-by: Giuseppe Guerra <giuseppe.guerra@grafana.com> --------- Co-authored-by: Giuseppe Guerra <giuseppe.guerra@grafana.com>
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package backend | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsDownstreamError(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
err error | ||
expected bool | ||
}{ | ||
{ | ||
name: "nil", | ||
err: nil, | ||
expected: false, | ||
}, | ||
{ | ||
name: "downstream error", | ||
err: DownstreamError(nil), | ||
expected: true, | ||
}, | ||
{ | ||
name: "timeout network error", | ||
err: newFakeNetworkError(true, false), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped timeout network error", | ||
err: fmt.Errorf("oh no. err %w", newFakeNetworkError(true, false)), | ||
expected: true, | ||
}, | ||
{ | ||
name: "temporary timeout network error", | ||
err: newFakeNetworkError(true, true), | ||
expected: true, | ||
}, | ||
{ | ||
name: "non-timeout network error", | ||
err: newFakeNetworkError(false, false), | ||
expected: false, | ||
}, | ||
{ | ||
name: "os.ErrDeadlineExceeded", | ||
err: os.ErrDeadlineExceeded, | ||
expected: true, | ||
}, | ||
{ | ||
name: "os.ErrDeadlineExceeded", | ||
err: fmt.Errorf("error: %w", os.ErrDeadlineExceeded), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped os.ErrDeadlineExceeded", | ||
err: errors.Join(fmt.Errorf("oh no"), os.ErrDeadlineExceeded), | ||
expected: true, | ||
}, | ||
{ | ||
name: "other error", | ||
err: fmt.Errorf("other error"), | ||
expected: false, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert.Equalf(t, tc.expected, IsDownstreamError(tc.err), "IsDownstreamError(%v)", tc.err) | ||
}) | ||
} | ||
} | ||
|
||
var _ net.Error = &fakeNetworkError{} | ||
|
||
type fakeNetworkError struct { | ||
timeout bool | ||
temporary bool | ||
} | ||
|
||
func newFakeNetworkError(timeout, temporary bool) *fakeNetworkError { | ||
return &fakeNetworkError{ | ||
timeout: timeout, | ||
temporary: temporary, | ||
} | ||
} | ||
|
||
func (d *fakeNetworkError) Error() string { | ||
return "dummy timeout error" | ||
} | ||
|
||
func (d *fakeNetworkError) Timeout() bool { | ||
return d.timeout | ||
} | ||
|
||
func (d *fakeNetworkError) Temporary() bool { | ||
return d.temporary | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters