Skip to content

Commit

Permalink
Fix other unit tests for: Failfast for explicit error http request in…
Browse files Browse the repository at this point in the history
…stead of retrying (#296)
  • Loading branch information
larry4xie committed Dec 29, 2023
1 parent c717e75 commit 126c701
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions component/remote/async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func initMockNotifyAndConfigServerWithTwoErrResponse() *httptest.Server {
return runMockConfigServer(handlerMap, onlynormaltworesponse)
}

//run mock config server
// run mock config server
func runMockConfigServer(handlerMap map[string]func(http.ResponseWriter, *http.Request),
notifyHandler func(http.ResponseWriter, *http.Request)) *httptest.Server {
appConfig := env.InitFileConfig()
Expand Down Expand Up @@ -177,11 +177,12 @@ func initNotifications() *config.AppConfig {
return appConfig
}

//Error response
//will hold 5s and keep response 404
func runErrorResponse() *httptest.Server {
// Error response
// will hold 5s and keep response 404
func runErrorResponse(status int, body []byte) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.WriteHeader(status)
_, _ = w.Write(body)
}))

return ts
Expand Down Expand Up @@ -281,10 +282,26 @@ func TestGetRemoteConfig(t *testing.T) {
}

func TestErrorGetRemoteConfig(t *testing.T) {
tests := []struct {
name string
status int
errContained string
}{
{name: "500", status: http.StatusInternalServerError, errContained: "over Max Retry Still Error"},
{name: "404", status: http.StatusNotFound, errContained: "Connect Apollo Server Fail"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testErrorGetRemoteConfig(t, tt.status, tt.errContained)
})
}
}

func testErrorGetRemoteConfig(t *testing.T, status int, errContained string) {
//clear
initNotifications()
appConfig := initNotifications()
server1 := runErrorResponse()
server1 := runErrorResponse(status, nil)
appConfig.IP = server1.URL
server.SetNextTryConnTime(appConfig.GetHost(), 0)

Expand All @@ -303,7 +320,7 @@ func TestErrorGetRemoteConfig(t *testing.T) {
t.Log("remoteConfigs:", remoteConfigs)
t.Log("remoteConfigs size:", len(remoteConfigs))

Assert(t, "over Max Retry Still Error", Equal(err.Error()))
Assert(t, err.Error(), StartWith(errContained))
}

func TestCreateApolloConfigWithJson(t *testing.T) {
Expand Down

0 comments on commit 126c701

Please sign in to comment.