-
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.
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 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,85 @@ | ||
package backend | ||
|
||
import ( | ||
"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: "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: "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