Skip to content
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

Retry 502 errors one time #921

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/jen20/awspolicyequivalence v1.1.0
github.com/jinzhu/copier v0.2.3
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.2.7-0.20210317130019-8526a9584142
github.com/opentelekomcloud/gophertelekomcloud v0.2.7-0.20210319160506-316fc19ae099
github.com/unknwon/com v1.0.1
gopkg.in/yaml.v2 v2.4.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.2.7-0.20210317130019-8526a9584142 h1:Z3GXIhdiWk0PQdxF9w2bARJWCX4rUAitymnX2NziX14=
github.com/opentelekomcloud/gophertelekomcloud v0.2.7-0.20210317130019-8526a9584142/go.mod h1:pzEP1kduNwv+hrI9R6/DFU/NiX7Kr9NiFjpQ7kJQTsM=
github.com/opentelekomcloud/gophertelekomcloud v0.2.7-0.20210319160506-316fc19ae099 h1:jAp6XZZVRoMQ9FER9SuLpgLUOPwRGO+HPW+E6TpZDyk=
github.com/opentelekomcloud/gophertelekomcloud v0.2.7-0.20210319160506-316fc19ae099/go.mod h1:pzEP1kduNwv+hrI9R6/DFU/NiX7Kr9NiFjpQ7kJQTsM=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
84 changes: 84 additions & 0 deletions opentelekomcloud/common/cfg/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cfg

import (
"fmt"
"net/http"
"sync"
"testing"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

type failHandler struct {
ExpectedFailures int
ErrorCode int
FailCount int
OkCode int
OkResponse string

mut *sync.RWMutex
}

func (f *failHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if f.mut == nil {
f.mut = new(sync.RWMutex)
}

if f.OkCode == 0 {
f.OkCode = 200
}

defer func() { _ = r.Body.Close() }()
if f.FailCount < f.ExpectedFailures {
f.mut.Lock()
f.FailCount += 1
f.mut.Unlock()
w.WriteHeader(f.ErrorCode)
} else {
w.WriteHeader(f.OkCode)
_, _ = fmt.Fprintf(w, f.OkResponse)
}
}

const tokenOutput = `
{
"token":{
"methods":[
"password"
],
"roles":[],
"expires_at":"2017-06-03T02:19:49.000000Z",
"project":{},
"catalog":[],
"user": {},
"issued_at":"2017-06-03T01:19:49.000000Z"
}
}
`

func TestRoundTripperRetry(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

failHandler := &failHandler{
ExpectedFailures: 1,
ErrorCode: 502,
OkCode: 201,
OkResponse: tokenOutput,
}

th.Mux.Handle("/", failHandler)

cfg := &Config{MaxRetries: failHandler.ExpectedFailures}

_, err := cfg.genClient(golangsdk.AuthOptions{
IdentityEndpoint: th.Endpoint() + "v3",
Username: "user",
Password: "qwerty!",
DomainName: "DOMAIN001",
})

th.CheckNoErr(t, err)
th.AssertEquals(t, failHandler.ExpectedFailures, failHandler.FailCount)
}