-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add max_retries for client HTTP calls (#463)
- Loading branch information
1 parent
86f2cd6
commit 5f5a2ab
Showing
9 changed files
with
556 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/huaweicloud/golangsdk" | ||
th "github.com/huaweicloud/golangsdk/testhelper" | ||
) | ||
|
||
func testRequestRetry(t *testing.T, count int) { | ||
th.SetupHTTP() | ||
defer th.TeardownHTTP() | ||
|
||
retryCount := count | ||
|
||
var info = struct { | ||
retries int | ||
mut *sync.RWMutex | ||
}{ | ||
0, | ||
new(sync.RWMutex), | ||
} | ||
|
||
th.Mux.HandleFunc("/route/", func(w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
_, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
t.Errorf("Error hadling test request") | ||
} | ||
if info.retries < retryCount { | ||
info.mut.RLock() | ||
info.retries += 1 | ||
info.mut.RUnlock() | ||
panic(err) // simulate EOF | ||
} | ||
w.WriteHeader(500) | ||
_, _ = fmt.Fprintf(w, `%v`, info.retries) | ||
}) | ||
|
||
cfg := &Config{MaxRetries: retryCount} | ||
_, err := genClient(cfg, golangsdk.AuthOptions{ | ||
IdentityEndpoint: fmt.Sprintf("%s/route", th.Endpoint()), | ||
}) | ||
_, ok := err.(golangsdk.ErrDefault500) | ||
th.AssertEquals(t, true, ok) | ||
th.AssertEquals(t, retryCount, info.retries) | ||
} | ||
|
||
func TestRequestRetry(t *testing.T) { | ||
t.Run("TestRequestMultipleRetries", func(t *testing.T) { testRequestRetry(t, 2) }) | ||
t.Run("TestRequestSingleRetry", func(t *testing.T) { testRequestRetry(t, 1) }) | ||
t.Run("TestRequestZeroRetry", func(t *testing.T) { testRequestRetry(t, 0) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.