Skip to content

Commit

Permalink
tests/client: unify http client in tests (#8199)
Browse files Browse the repository at this point in the history
ref #7969

Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp authored May 20, 2024
1 parent 6ded6f8 commit 3e672b2
Show file tree
Hide file tree
Showing 18 changed files with 465 additions and 475 deletions.
15 changes: 4 additions & 11 deletions tests/integrations/mcs/keyspace/tso_keyspace_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type keyspaceGroupTestSuite struct {
cluster *tests.TestCluster
server *tests.TestServer
backendEndpoints string
dialClient *http.Client
}

func TestKeyspaceGroupTestSuite(t *testing.T) {
Expand All @@ -67,11 +66,6 @@ func (suite *keyspaceGroupTestSuite) SetupTest() {
suite.server = cluster.GetLeaderServer()
re.NoError(suite.server.BootstrapCluster())
suite.backendEndpoints = suite.server.GetAddr()
suite.dialClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
suite.cleanupFunc = func() {
cancel()
}
Expand All @@ -81,7 +75,6 @@ func (suite *keyspaceGroupTestSuite) TearDownTest() {
re := suite.Require()
suite.cleanupFunc()
suite.cluster.Destroy()
suite.dialClient.CloseIdleConnections()
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes"))
}

Expand Down Expand Up @@ -347,7 +340,7 @@ func (suite *keyspaceGroupTestSuite) tryAllocNodesForKeyspaceGroup(re *require.A
re.NoError(err)
httpReq, err := http.NewRequest(http.MethodPost, suite.server.GetAddr()+keyspaceGroupsPrefix+fmt.Sprintf("/%d/alloc", id), bytes.NewBuffer(data))
re.NoError(err)
resp, err := suite.dialClient.Do(httpReq)
resp, err := tests.TestDialClient.Do(httpReq)
re.NoError(err)
defer resp.Body.Close()
nodes := make([]endpoint.KeyspaceGroupMember, 0)
Expand All @@ -364,7 +357,7 @@ func (suite *keyspaceGroupTestSuite) tryCreateKeyspaceGroup(re *require.Assertio
re.NoError(err)
httpReq, err := http.NewRequest(http.MethodPost, suite.server.GetAddr()+keyspaceGroupsPrefix, bytes.NewBuffer(data))
re.NoError(err)
resp, err := suite.dialClient.Do(httpReq)
resp, err := tests.TestDialClient.Do(httpReq)
re.NoError(err)
defer resp.Body.Close()
return resp.StatusCode
Expand All @@ -373,7 +366,7 @@ func (suite *keyspaceGroupTestSuite) tryCreateKeyspaceGroup(re *require.Assertio
func (suite *keyspaceGroupTestSuite) tryGetKeyspaceGroup(re *require.Assertions, id uint32) (*endpoint.KeyspaceGroup, int) {
httpReq, err := http.NewRequest(http.MethodGet, suite.server.GetAddr()+keyspaceGroupsPrefix+fmt.Sprintf("/%d", id), http.NoBody)
re.NoError(err)
resp, err := suite.dialClient.Do(httpReq)
resp, err := tests.TestDialClient.Do(httpReq)
re.NoError(err)
defer resp.Body.Close()
kg := &endpoint.KeyspaceGroup{}
Expand All @@ -390,7 +383,7 @@ func (suite *keyspaceGroupTestSuite) trySetNodesForKeyspaceGroup(re *require.Ass
re.NoError(err)
httpReq, err := http.NewRequest(http.MethodPatch, suite.server.GetAddr()+keyspaceGroupsPrefix+fmt.Sprintf("/%d", id), bytes.NewBuffer(data))
re.NoError(err)
resp, err := suite.dialClient.Do(httpReq)
resp, err := tests.TestDialClient.Do(httpReq)
re.NoError(err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
Expand Down
16 changes: 8 additions & 8 deletions tests/integrations/mcs/members/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type memberTestSuite struct {
cluster *tests.TestCluster
server *tests.TestServer
backendEndpoints string
dialClient pdClient.Client
pdClient pdClient.Client
}

func TestMemberTestSuite(t *testing.T) {
Expand All @@ -53,7 +53,7 @@ func (suite *memberTestSuite) SetupTest() {
suite.server = cluster.GetLeaderServer()
re.NoError(suite.server.BootstrapCluster())
suite.backendEndpoints = suite.server.GetAddr()
suite.dialClient = pdClient.NewClient("mcs-member-test", []string{suite.server.GetAddr()})
suite.pdClient = pdClient.NewClient("mcs-member-test", []string{suite.server.GetAddr()})

// TSO
nodes := make(map[string]bs.Server)
Expand Down Expand Up @@ -86,30 +86,30 @@ func (suite *memberTestSuite) TearDownTest() {
for _, cleanup := range suite.cleanupFunc {
cleanup()
}
if suite.dialClient != nil {
suite.dialClient.Close()
if suite.pdClient != nil {
suite.pdClient.Close()
}
suite.cluster.Destroy()
}

func (suite *memberTestSuite) TestMembers() {
re := suite.Require()
members, err := suite.dialClient.GetMicroServiceMembers(suite.ctx, "tso")
members, err := suite.pdClient.GetMicroServiceMembers(suite.ctx, "tso")
re.NoError(err)
re.Len(members, utils.DefaultKeyspaceGroupReplicaCount)

members, err = suite.dialClient.GetMicroServiceMembers(suite.ctx, "scheduling")
members, err = suite.pdClient.GetMicroServiceMembers(suite.ctx, "scheduling")
re.NoError(err)
re.Len(members, 3)
}

func (suite *memberTestSuite) TestPrimary() {
re := suite.Require()
primary, err := suite.dialClient.GetMicroServicePrimary(suite.ctx, "tso")
primary, err := suite.pdClient.GetMicroServicePrimary(suite.ctx, "tso")
re.NoError(err)
re.NotEmpty(primary)

primary, err = suite.dialClient.GetMicroServicePrimary(suite.ctx, "scheduling")
primary, err = suite.pdClient.GetMicroServicePrimary(suite.ctx, "scheduling")
re.NoError(err)
re.NotEmpty(primary)
}
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ func (suite *resourceManagerClientTestSuite) TestBasicResourceGroupCURD() {
}
createJSON, err := json.Marshal(group)
re.NoError(err)
resp, err := http.Post(getAddr(i)+"/resource-manager/api/v1/config/group", "application/json", strings.NewReader(string(createJSON)))
resp, err := tests.TestDialClient.Post(getAddr(i)+"/resource-manager/api/v1/config/group", "application/json", strings.NewReader(string(createJSON)))
re.NoError(err)
resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
Expand All @@ -982,7 +982,7 @@ func (suite *resourceManagerClientTestSuite) TestBasicResourceGroupCURD() {
}

// Get Resource Group
resp, err = http.Get(getAddr(i) + "/resource-manager/api/v1/config/group/" + tcase.name)
resp, err = tests.TestDialClient.Get(getAddr(i) + "/resource-manager/api/v1/config/group/" + tcase.name)
re.NoError(err)
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
Expand All @@ -995,7 +995,7 @@ func (suite *resourceManagerClientTestSuite) TestBasicResourceGroupCURD() {

// Last one, Check list and delete all resource groups
if i == len(testCasesSet1)-1 {
resp, err := http.Get(getAddr(i) + "/resource-manager/api/v1/config/groups")
resp, err := tests.TestDialClient.Get(getAddr(i) + "/resource-manager/api/v1/config/groups")
re.NoError(err)
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func (suite *resourceManagerClientTestSuite) TestBasicResourceGroupCURD() {
}

// verify again
resp1, err := http.Get(getAddr(i) + "/resource-manager/api/v1/config/groups")
resp1, err := tests.TestDialClient.Get(getAddr(i) + "/resource-manager/api/v1/config/groups")
re.NoError(err)
re.Equal(http.StatusOK, resp1.StatusCode)
respString1, err := io.ReadAll(resp1.Body)
Expand Down
10 changes: 5 additions & 5 deletions tests/integrations/mcs/resourcemanager/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestResourceManagerServer(t *testing.T) {
// Test registered REST HTTP Handler
url := addr + "/resource-manager/api/v1/config"
{
resp, err := http.Get(url + "/groups")
resp, err := tests.TestDialClient.Get(url + "/groups")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
Expand All @@ -78,13 +78,13 @@ func TestResourceManagerServer(t *testing.T) {
}
createJSON, err := json.Marshal(group)
re.NoError(err)
resp, err := http.Post(url+"/group", "application/json", strings.NewReader(string(createJSON)))
resp, err := tests.TestDialClient.Post(url+"/group", "application/json", strings.NewReader(string(createJSON)))
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
}
{
resp, err := http.Get(url + "/group/pingcap")
resp, err := tests.TestDialClient.Get(url + "/group/pingcap")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
Expand All @@ -95,7 +95,7 @@ func TestResourceManagerServer(t *testing.T) {

// Test metrics handler
{
resp, err := http.Get(addr + "/metrics")
resp, err := tests.TestDialClient.Get(addr + "/metrics")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
Expand All @@ -106,7 +106,7 @@ func TestResourceManagerServer(t *testing.T) {

// Test status handler
{
resp, err := http.Get(addr + "/status")
resp, err := tests.TestDialClient.Get(addr + "/status")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
Expand Down
Loading

0 comments on commit 3e672b2

Please sign in to comment.