From d00bb7f251a60f0c0e58612d6a13cdc3954c67fd Mon Sep 17 00:00:00 2001 From: okJiang <819421878@qq.com> Date: Thu, 2 Jan 2025 14:42:42 +0800 Subject: [PATCH 1/4] make TestPreparingProgress stable Signed-off-by: okJiang <819421878@qq.com> --- tests/server/api/api_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/server/api/api_test.go b/tests/server/api/api_test.go index faa22ce08f4..e762e7dac95 100644 --- a/tests/server/api/api_test.go +++ b/tests/server/api/api_test.go @@ -1094,8 +1094,22 @@ func TestPreparingProgress(t *testing.T) { func sendRequest(re *require.Assertions, url string, method string, statusCode int) []byte { req, _ := http.NewRequest(method, url, http.NoBody) - resp, err := tests.TestDialClient.Do(req) - re.NoError(err) + var ( + err error + resp *http.Response + ) + testutil.Eventually(re, func() bool { + resp, err = tests.TestDialClient.Do(req) + re.NoError(err) + // Due to service unavailability caused by environmental issues, + // we will retry it. + if resp.StatusCode == http.StatusServiceUnavailable { + resp.Body.Close() + return false + } + return true + }) + re.Equal(statusCode, resp.StatusCode) output, err := io.ReadAll(resp.Body) re.NoError(err) From d0c52a78209b7249248ed935fabfbff6f5bbced5 Mon Sep 17 00:00:00 2001 From: okJiang <819421878@qq.com> Date: Thu, 2 Jan 2025 15:00:09 +0800 Subject: [PATCH 2/4] fix lint Signed-off-by: okJiang <819421878@qq.com> --- tests/server/api/api_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/server/api/api_test.go b/tests/server/api/api_test.go index e762e7dac95..4e3e729da76 100644 --- a/tests/server/api/api_test.go +++ b/tests/server/api/api_test.go @@ -1110,9 +1110,9 @@ func sendRequest(re *require.Assertions, url string, method string, statusCode i return true }) + defer resp.Body.Close() re.Equal(statusCode, resp.StatusCode) output, err := io.ReadAll(resp.Body) re.NoError(err) - resp.Body.Close() return output } From ca8e229c1961af047a4f18f4724014e9448594cf Mon Sep 17 00:00:00 2001 From: okJiang <819421878@qq.com> Date: Thu, 2 Jan 2025 15:20:23 +0800 Subject: [PATCH 3/4] skip lint Signed-off-by: okJiang <819421878@qq.com> --- tests/server/api/api_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/server/api/api_test.go b/tests/server/api/api_test.go index 4e3e729da76..17a136ffb1e 100644 --- a/tests/server/api/api_test.go +++ b/tests/server/api/api_test.go @@ -1099,6 +1099,7 @@ func sendRequest(re *require.Assertions, url string, method string, statusCode i resp *http.Response ) testutil.Eventually(re, func() bool { + //nolint:bodyclose resp, err = tests.TestDialClient.Do(req) re.NoError(err) // Due to service unavailability caused by environmental issues, From 9cb45031a56125967d9d35332c0ff85e05167f91 Mon Sep 17 00:00:00 2001 From: okJiang <819421878@qq.com> Date: Thu, 2 Jan 2025 16:20:37 +0800 Subject: [PATCH 4/4] fix comment Signed-off-by: okJiang <819421878@qq.com> --- tests/server/api/api_test.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/server/api/api_test.go b/tests/server/api/api_test.go index 17a136ffb1e..44a0ae69a46 100644 --- a/tests/server/api/api_test.go +++ b/tests/server/api/api_test.go @@ -1092,28 +1092,24 @@ func TestPreparingProgress(t *testing.T) { re.NoError(failpoint.Disable("github.com/tikv/pd/server/cluster/highFrequencyClusterJobs")) } -func sendRequest(re *require.Assertions, url string, method string, statusCode int) []byte { +func sendRequest(re *require.Assertions, url string, method string, statusCode int) (output []byte) { req, _ := http.NewRequest(method, url, http.NoBody) - var ( - err error - resp *http.Response - ) + testutil.Eventually(re, func() bool { - //nolint:bodyclose - resp, err = tests.TestDialClient.Do(req) + resp, err := tests.TestDialClient.Do(req) re.NoError(err) + defer resp.Body.Close() + // Due to service unavailability caused by environmental issues, // we will retry it. if resp.StatusCode == http.StatusServiceUnavailable { - resp.Body.Close() return false } + re.Equal(statusCode, resp.StatusCode) + output, err = io.ReadAll(resp.Body) + re.NoError(err) return true }) - defer resp.Body.Close() - re.Equal(statusCode, resp.StatusCode) - output, err := io.ReadAll(resp.Body) - re.NoError(err) return output }