mirror of
https://github.com/imjasonh/staticmaps
synced 2026-07-18 23:09:11 +00:00
Add backoff http.RoundTripper that that handles exponential backoff
This commit is contained in:
parent
41f9b8b763
commit
f9c0e3df19
3 changed files with 85 additions and 1 deletions
35
backoff_test.go
Normal file
35
backoff_test.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package maps
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBackoff(t *testing.T) {
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "fail", http.StatusInternalServerError)
|
||||
}))
|
||||
defer s.Close()
|
||||
tries := 10
|
||||
sleeps := 0
|
||||
bt := &backoff{
|
||||
MaxTries: tries,
|
||||
sleep: func(d time.Duration) {
|
||||
sleeps += 1
|
||||
t.Logf("sleeping %s", d)
|
||||
},
|
||||
}
|
||||
c := &http.Client{Transport: bt}
|
||||
r, _ := http.NewRequest("GET", s.URL, nil)
|
||||
if _, err := c.Do(r); err == nil {
|
||||
t.Errorf("expected error")
|
||||
}
|
||||
if sleeps != tries-1 {
|
||||
t.Errorf("unexpected # of calls to sleep, got %d, want %d", tries-1, sleeps)
|
||||
}
|
||||
if bt.tries != tries {
|
||||
t.Errorf("got %d tries, want %d", bt.tries, tries)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue