Skip to content

Commit

Permalink
use httptest instead of real http call
Browse files Browse the repository at this point in the history
remove ParseRetryAfter header + link to module which does it
  • Loading branch information
Jan Seidl committed Oct 10, 2020
1 parent 821cbc6 commit 7062aab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 49 deletions.
10 changes: 8 additions & 2 deletions examples/custom_retry_function_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package retry_test

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand All @@ -11,12 +13,16 @@ import (
)

func TestCustomRetryFunction(t *testing.T) {
url := "http://example.com"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
}))
defer ts.Close()

var body []byte

err := retry.Do(
func() error {
resp, err := http.Get(url)
resp, err := http.Get(ts.URL)

if err == nil {
defer func() {
Expand Down
66 changes: 21 additions & 45 deletions examples/delay_based_on_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,25 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"net/http/httptest"
"testing"
"time"

"github.com/avast/retry-go"
"github.com/stretchr/testify/assert"
)

func TestParseRetryAfter(t *testing.T) {
retryAfter, err := ParseRetryAfter("xyz")
assert.Error(t, err)

retryAfter, err = ParseRetryAfter("")
assert.Error(t, err)

testDur := 120 * time.Second

retryAfter, err = ParseRetryAfter("120")
assert.NoError(t, err)
assert.Equal(t, testDur, retryAfter, "time in seconds")

retryAfter, err = ParseRetryAfter(time.Now().Add(testDur).Format(time.RFC850))
assert.NoError(t, err)
assert.True(t, retryAfter > (115*time.Second), "time in seconds are ~120")
t.Log(retryAfter)
}

func ParseRetryAfter(ra string) (time.Duration, error) {
if ra == "" {
return 0, fmt.Errorf("Retry-After header was empty")
}

t, errParse := http.ParseTime(ra)
if errParse != nil {
if sec, errParse := strconv.Atoi(ra); errParse == nil {
return time.Duration(sec) * time.Second, nil
}
} else {
return t.Sub(time.Now()), nil
}

return 0, fmt.Errorf("Invalid Retr-After format %s", ra)
}

type RetryAfterError struct {
response http.Response
}

func (err RetryAfterError) Error() string {
return fmt.Sprintf("Request to %s fail %s (%d)", err.response.Request.RequestURI, err.response.Status, err.response.StatusCode)
return fmt.Sprintf(
"Request to %s fail %s (%d)",
err.response.Request.RequestURI,
err.response.Status,
err.response.StatusCode,
)
}

type SomeOtherError struct {
Expand All @@ -68,12 +37,16 @@ func (err SomeOtherError) Error() string {
}

func TestCustomRetryFunctionBasedOnKindOfError(t *testing.T) {
url := "http://example.com"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
}))
defer ts.Close()

var body []byte

err := retry.Do(
func() error {
resp, err := http.Get(url)
resp, err := http.Get(ts.URL)

if err == nil {
defer func() {
Expand All @@ -87,14 +60,12 @@ func TestCustomRetryFunctionBasedOnKindOfError(t *testing.T) {
return err
},
retry.DelayType(func(n uint, err error, config *retry.Config) time.Duration {
switch err.(type) {
switch e := err.(type) {
case RetryAfterError:
e := err.(RetryAfterError)
if dur, err := ParseRetryAfter(e.response.Header.Get("Retry-After")); err == nil {
return dur
if t, err := parseRetryAfter(e.response.Header.Get("Retry-After")); err == nil {
return time.Until(t)
}
case SomeOtherError:
e := err.(SomeOtherError)
return e.retryAfter
}

Expand All @@ -106,3 +77,8 @@ func TestCustomRetryFunctionBasedOnKindOfError(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, body)
}

// use https://github.com/aereal/go-httpretryafter instead
func parseRetryAfter(_ string) (time.Time, error) {
return time.Now().Add(1 * time.Second), nil
}
10 changes: 8 additions & 2 deletions examples/http_get_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package retry_test

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/avast/retry-go"
"github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
url := "http://example.com"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
}))
defer ts.Close()

var body []byte

err := retry.Do(
func() error {
resp, err := http.Get(url)
resp, err := http.Get(ts.URL)

if err == nil {
defer func() {
Expand Down

0 comments on commit 7062aab

Please sign in to comment.