Skip to content

Commit

Permalink
tests: close client after finishing tests (#9113)
Browse files Browse the repository at this point in the history
ref #7969

Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx authored Mar 5, 2025
1 parent 5d8faba commit 3b505f5
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 7 deletions.
6 changes: 4 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ func TestClientCtx(t *testing.T) {
start := time.Now()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*3)
defer cancel()
_, err := NewClientWithContext(ctx, caller.TestComponent,
cli, err := NewClientWithContext(ctx, caller.TestComponent,
[]string{testClientURL}, SecurityOption{})
re.Error(err)
defer cli.Close()
re.Less(time.Since(start), time.Second*5)
}

func TestClientWithRetry(t *testing.T) {
re := require.New(t)
start := time.Now()
_, err := NewClientWithContext(context.TODO(), caller.TestComponent,
cli, err := NewClientWithContext(context.TODO(), caller.TestComponent,
[]string{testClientURL}, SecurityOption{}, opt.WithMaxErrorRetry(5))
re.Error(err)
defer cli.Close()
re.Less(time.Since(start), time.Second*10)
}
1 change: 1 addition & 0 deletions tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ func TestGetRegionWithBackoff(t *testing.T) {
// Initialize the client with context and backoff
client, err := pd.NewClientWithContext(ctx, caller.TestComponent, endpoints, pd.SecurityOption{})
re.NoError(err)
defer client.Close()

// Record the start time
start := time.Now()
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/mcs/tso/keyspace_group_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ func (suite *tsoKeyspaceGroupManagerTestSuite) dispatchClient(
wg.Add(1)
go func() {
defer wg.Done()
defer tsoClient.Close()
for {
select {
case <-ctx.Done():
Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/realcluster/cluster_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ func (s *clusterIDSuite) TestClientClusterID() {

pdEndpoints := getPDEndpoints(re)
// Try to create a client with the mixed endpoints.
_, err := pd.NewClientWithContext(
cli, err := pd.NewClientWithContext(
ctx, caller.TestComponent, pdEndpoints,
pd.SecurityOption{}, opt.WithMaxErrorRetry(1),
)
re.Error(err)
defer cli.Close()
re.Contains(err.Error(), "unmatched cluster id")
}
1 change: 1 addition & 0 deletions tests/integrations/realcluster/reboot_pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (s *rebootPDSuite) TestReloadLabel() {
ctx := context.Background()

pdHTTPCli := http.NewClient("pd-real-cluster-test", getPDEndpoints(re))
defer pdHTTPCli.Close()
resp, err := pdHTTPCli.GetStores(ctx)
re.NoError(err)
re.NotEmpty(resp.Stores)
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/realcluster/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (s *schedulerSuite) TestTransferLeader() {
defer cancel()

pdHTTPCli := http.NewClient("pd-real-cluster-test", getPDEndpoints(re))
defer pdHTTPCli.Close()
resp, err := pdHTTPCli.GetLeader(ctx)
re.NoError(err)
oldLeader := resp.Name
Expand Down Expand Up @@ -102,6 +103,7 @@ func (s *schedulerSuite) TestRegionLabelDenyScheduler() {
defer cancel()

pdHTTPCli := http.NewClient("pd-real-cluster-test", getPDEndpoints(re))
defer pdHTTPCli.Close()
regions, err := pdHTTPCli.GetRegions(ctx)
re.NoError(err)
re.NotEmpty(regions.Regions)
Expand Down Expand Up @@ -211,6 +213,7 @@ func (s *schedulerSuite) TestGrantOrEvictLeaderTwice() {
defer cancel()

pdHTTPCli := http.NewClient("pd-real-cluster-test", getPDEndpoints(re))
defer pdHTTPCli.Close()
regions, err := pdHTTPCli.GetRegions(ctx)
re.NoError(err)
re.NotEmpty(regions.Regions)
Expand Down
8 changes: 4 additions & 4 deletions tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,9 +945,9 @@ func TestRemovingProgress(t *testing.T) {
// store 2: (30-10)/(30+40) ~= 0.285
re.Equal("0.29", fmt.Sprintf("%.2f", p.Progress))
// store 2: 20/10s = 2
re.LessOrEqual(2.0, p.CurrentSpeed)
re.LessOrEqual(p.CurrentSpeed, 2.0)
// store 2: (10+40)/2 = 25s
re.GreaterOrEqual(25.0, p.LeftSeconds)
re.GreaterOrEqual(p.LeftSeconds, 25.0)

re.NoError(failpoint.Disable("github.com/tikv/pd/server/cluster/highFrequencyClusterJobs"))
}
Expand Down Expand Up @@ -1150,8 +1150,8 @@ func TestPreparingProgress(t *testing.T) {
re.NoError(json.Unmarshal(output, &p))
re.Equal("preparing", p.Action)
re.Equal("0.05", fmt.Sprintf("%.2f", p.Progress))
re.LessOrEqual(1.0, p.CurrentSpeed)
re.GreaterOrEqual(179.0, p.LeftSeconds)
re.LessOrEqual(p.CurrentSpeed, 1.0)
re.GreaterOrEqual(p.LeftSeconds, 179.0)
re.NoError(failpoint.Disable("github.com/tikv/pd/server/cluster/highFrequencyClusterJobs"))
}

Expand Down

0 comments on commit 3b505f5

Please sign in to comment.