From 6a5f23e4b40744a75867ac1ddc68a21d8a20e305 Mon Sep 17 00:00:00 2001 From: George Paulose Date: Fri, 30 Oct 2020 16:38:02 +0000 Subject: [PATCH 1/4] Added option and test case --- client.go | 13 +++++++++++++ client_test.go | 10 +++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index b5730c1b..b5739e4b 100644 --- a/client.go +++ b/client.go @@ -554,6 +554,19 @@ func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client { return c } +// AddRetryAfterErrorCondition adds the basic condition of retrying after enncoutering +// an error from the http response +func (c *Client) AddRetryAfterErrorCondition() *Client { + c.AddRetryCondition(func(response *Response, err error) bool { + if response.IsError() { + return true + } + + return false + }) + return c +} + // SetTLSClientConfig method sets TLSClientConfig for underling client Transport. // // For Example: diff --git a/client_test.go b/client_test.go index 70d67b2f..e7b5ee42 100644 --- a/client_test.go +++ b/client_test.go @@ -352,6 +352,15 @@ func TestClientOptions(t *testing.T) { client.SetRetryMaxWaitTime(mrwt) assertEqual(t, mrwt, client.RetryMaxWaitTime) + client.AddRetryAfterErrorCondition() + equal(client.RetryConditions[0], func(response *Response, err error) bool { + if response.IsError() { + return true + } + + return false + }) + err := &AuthError{} client.SetError(err) if reflect.TypeOf(err) == client.Error { @@ -611,4 +620,3 @@ func TestNewWithLocalAddr(t *testing.T) { assertNil(t, err) assertEqual(t, resp.String(), "TestGet: text response") } - From 316e673ab43550176501caf2c20afbecc3a4b757 Mon Sep 17 00:00:00 2001 From: George Paulose Date: Mon, 2 Nov 2020 14:32:50 +0000 Subject: [PATCH 2/4] Fixed typo --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index b5739e4b..f45dc46d 100644 --- a/client.go +++ b/client.go @@ -554,7 +554,7 @@ func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client { return c } -// AddRetryAfterErrorCondition adds the basic condition of retrying after enncoutering +// AddRetryAfterErrorCondition adds the basic condition of retrying after encountering // an error from the http response func (c *Client) AddRetryAfterErrorCondition() *Client { c.AddRetryCondition(func(response *Response, err error) bool { From 9264d26e53d49eeb7f7ddbac59cb4238a86a0b49 Mon Sep 17 00:00:00 2001 From: George Paulose Date: Mon, 2 Nov 2020 18:32:09 +0000 Subject: [PATCH 3/4] Added test case --- retry_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/retry_test.go b/retry_test.go index cfd7b7a8..2f069058 100644 --- a/retry_test.go +++ b/retry_test.go @@ -612,6 +612,7 @@ func TestClientRetryCount(t *testing.T) { ) resp, err := c.R().Get(ts.URL + "/set-retrycount-test") + t.Logf("resp: %v", resp) assertEqual(t, "", resp.Status()) assertEqual(t, "", resp.Proto()) assertEqual(t, 0, resp.StatusCode()) @@ -626,6 +627,31 @@ func TestClientRetryCount(t *testing.T) { strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\""))) } +func TestClientErrorRetry(t *testing.T) { + ts := createGetServer(t) + defer ts.Close() + + c := dc(). + SetTimeout(time.Second * 3). + SetRetryCount(1). + AddRetryAfterErrorCondition() + + resp, err := c.R(). + SetHeader(hdrContentTypeKey, "application/json; charset=utf-8"). + SetJSONEscapeHTML(false). + SetResult(AuthSuccess{}). + Get(ts.URL + "/set-retry-error-recover") + + assertError(t, err) + + authSuccess := resp.Result().(*AuthSuccess) + + assertEqual(t, http.StatusOK, resp.StatusCode()) + assertEqual(t, "hello", authSuccess.Message) + + assertNil(t, resp.Error()) +} + func filler(*Response, error) bool { return false } From bc39be1e9c5d0f235ce8d36a2ab2355e69be56a1 Mon Sep 17 00:00:00 2001 From: George Paulose Date: Mon, 2 Nov 2020 18:34:21 +0000 Subject: [PATCH 4/4] Removed log --- retry_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/retry_test.go b/retry_test.go index 2f069058..19d274d1 100644 --- a/retry_test.go +++ b/retry_test.go @@ -612,7 +612,6 @@ func TestClientRetryCount(t *testing.T) { ) resp, err := c.R().Get(ts.URL + "/set-retrycount-test") - t.Logf("resp: %v", resp) assertEqual(t, "", resp.Status()) assertEqual(t, "", resp.Proto()) assertEqual(t, 0, resp.StatusCode())