Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test of retry support #179

Merged
merged 11 commits into from Dec 10, 2022
23 changes: 23 additions & 0 deletions mocks_test.go
Expand Up @@ -14,9 +14,15 @@ type mockClient struct {
req *http.Request
resp http.Response
err error
cb func(req *http.Request) // callback in .Do
}

func (c *mockClient) Do(req *http.Request) (*http.Response, error) {
defer func() {
if c.cb != nil {
c.cb(req)
}
}()
c.req = req
if c.err == nil {
c.resp.Header = c.req.Header
Expand Down Expand Up @@ -258,3 +264,20 @@ func (wc *mockWebsocketConn) SetWriteDeadline(t time.Time) error {
func (wc *mockWebsocketConn) Subprotocol() string {
return wc.subprotocol
}

type mockNetError struct {
isTimeout bool
isTemporary bool
}

func (e *mockNetError) Error() string {
return "mock net error"
}

func (e *mockNetError) Timeout() bool {
return e.isTimeout
}

func (e *mockNetError) Temporary() bool {
return e.isTemporary
}
4 changes: 3 additions & 1 deletion request.go
Expand Up @@ -38,6 +38,7 @@ type Request struct {
maxRetries int
minRetryDelay time.Duration
maxRetryDelay time.Duration
sleepFn func(d time.Duration)

timeout time.Duration

Expand Down Expand Up @@ -122,6 +123,7 @@ func newRequest(
maxRetries: 0,
minRetryDelay: time.Millisecond * 50,
maxRetryDelay: time.Second * 5,
sleepFn: time.Sleep,
}

r.initPath(path, pathargs...)
Expand Down Expand Up @@ -1920,7 +1922,7 @@ func (r *Request) retryRequest(reqFunc func() (*http.Response, error)) (
resp.Body.Close()
}

time.Sleep(delay)
r.sleepFn(delay)

delay *= 2
if delay > r.maxRetryDelay {
Expand Down