-
-
Notifications
You must be signed in to change notification settings - Fork 241
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 retrying capability #85
Comments
Hello, can I give it a try? |
Hey, package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gavv/httpexpect/v2"
rhttp "github.com/hashicorp/go-retryablehttp"
)
func TestRetry(t *testing.T) {
fail := true
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if fail {
fail = false
http.Error(w, "Try again!!", 500)
}
})
server := httptest.NewServer(handler)
defer server.Close()
e := httpexpect.New(t, server.URL)
e.GET("/").WithClient(rhttp.NewClient().StandardClient()).
Expect().
Status(http.StatusOK)
} It's your call obviously, but talking as a user I see no reason to add code to support this "natively". |
@bmihaylov thanks, good insight! Since httpexpect already hides HTTP client from you by default, and since this library is mostly a convenient wrapper (on top of net.http and a few other libs), I think it may make sense to provide out of the box retry support with a few simple retry policies. If you're happy with them, you can use it; in a more complicated use case, you can inject custom http client. |
Done. Commit: d0e64a7 |
Add three new Request methods:
WithRetry enabled retrying. If it's not called, test fails after first round trip failure. If it's called, test will try up to maxRetry times and only then fail.
WithRetryDelay controls pauses between retries. If it's not called, default pause is 1 second. If it's called, pause starts from minDelay and grows exponentially until it reaches maxDelay or we're out of retries.
WithRetryPolicy controls what to retry. If it's not called, default policy is RetryNetworkAndServerErrors, i.e. retry temporary network errors and temporary server errors (5xx). RetryNetworkErrors means retry only temporary network errors. RetryAnyErrors means retry absolutely all errors.
Previous attempt was here: #73. In particular, see #73 (comment)
We should also add documentation comments for new methods, cover them with tests (ideally both unit and e2e, but at least e2e), and add a new section to quick start in README.
The text was updated successfully, but these errors were encountered: