From 0a5763375e54daf10f2f9ce164c1c5ab43b848ac Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Wed, 23 Oct 2024 18:01:47 +0800 Subject: [PATCH] fixed testcases Signed-off-by: Boyang Lyu --- pkg/core/region.go | 20 ++++++++++---------- server/api/stats_test.go | 27 ++++++++++++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/pkg/core/region.go b/pkg/core/region.go index 6b8a244430d..60998e90d8a 100644 --- a/pkg/core/region.go +++ b/pkg/core/region.go @@ -1791,22 +1791,22 @@ func (r *RegionsInfo) GetRegionCount(startKey, endKey []byte) int { end := ®ionItem{&RegionInfo{meta: &metapb.Region{StartKey: endKey}}} // check if both keys are in the uncovered ranges. - startItemt, startIndex := r.tree.tree.GetWithIndex(start) - endItemt, eit := r.tree.tree.GetWithIndex(end) - if startItemt == nil { - startItemt = r.tree.tree.GetAt(startIndex - 1) + startItem, startIndex := r.tree.tree.GetWithIndex(start) + endItem, eit := r.tree.tree.GetWithIndex(end) + if startItem == nil { + startItem = r.tree.tree.GetAt(startIndex - 1) } - if endItemt == nil { - endItemt = r.tree.tree.GetAt(eit - 1) + if endItem == nil { + endItem = r.tree.tree.GetAt(eit - 1) } startInAnInterval := false - if startItemt != nil { - startInAnInterval = (bytes.Compare(startItemt.GetStartKey(), startKey) <= 0) && (bytes.Compare(startKey, startItemt.GetEndKey()) <= 0) + if startItem != nil { + startInAnInterval = (bytes.Compare(startItem.GetStartKey(), startKey) <= 0) && (bytes.Compare(startKey, startItem.GetEndKey()) <= 0) } endInAnInterval := false - if endItemt != nil { - endInAnInterval = (bytes.Compare(endItemt.GetStartKey(), endKey) <= 0) && (bytes.Compare(endKey, endItemt.GetEndKey()) <= 0) + if endItem != nil { + endInAnInterval = (bytes.Compare(endItem.GetStartKey(), endKey) <= 0) && (bytes.Compare(endKey, endItem.GetEndKey()) <= 0) } if startIndex == eit && (!startInAnInterval) && (!endInAnInterval) { return 0 diff --git a/server/api/stats_test.go b/server/api/stats_test.go index d13d3b9db68..e60883b8334 100644 --- a/server/api/stats_test.go +++ b/server/api/stats_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/pingcap/kvproto/pkg/metapb" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/statistics" @@ -205,17 +206,25 @@ func (suite *statsTestSuite) TestRegionStats() { } } -func (suite *statsTestSuite) TestRegionStatsHoles() { - statsURL := suite.urlPrefix + "/stats/region" +func TestRegionStatsHoles(t *testing.T) { + re := require.New(t) + svr, cleanup := mustNewServer(re) + defer cleanup() + server.MustWaitLeader(re, []*server.Server{svr}) + + addr := svr.GetAddr() + urlPrefix := fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) + + mustBootstrapCluster(re, svr) + + statsURL := urlPrefix + "/stats/region" epoch := &metapb.RegionEpoch{ ConfVer: 1, Version: 1, } - re := suite.Require() - // range holes - regions_withholes := []*core.RegionInfo{ + regionsWithholes := []*core.RegionInfo{ core.NewRegionInfo( &metapb.Region{ Id: 1, @@ -268,8 +277,8 @@ func (suite *statsTestSuite) TestRegionStatsHoles() { ), } - for _, r := range regions_withholes { - mustRegionHeartbeat(re, suite.svr, r) + for _, r := range regionsWithholes { + mustRegionHeartbeat(re, svr, r) } // holes in between : @@ -282,8 +291,8 @@ func (suite *statsTestSuite) TestRegionStatsHoles() { for i := 0; i < 4; i++ { startKey := url.QueryEscape(startKeys[i]) endKey := url.QueryEscape(endKeys[i]) - args_withholes := fmt.Sprintf("?start_key=%s&end_key=%s&count", startKey, endKey) - res, err := testDialClient.Get(statsURL + args_withholes) + argsWithHoles := fmt.Sprintf("?start_key=%s&end_key=%s&count", startKey, endKey) + res, err := testDialClient.Get(statsURL + argsWithHoles) re.NoError(err) stats := &statistics.RegionStats{} err = apiutil.ReadJSON(res.Body, stats)