Skip to content

Commit

Permalink
fixed testcases
Browse files Browse the repository at this point in the history
Signed-off-by: Boyang Lyu <[email protected]>
  • Loading branch information
Jackl9u committed Oct 23, 2024
1 parent 54de081 commit 0a57633
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
20 changes: 10 additions & 10 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -1791,22 +1791,22 @@ func (r *RegionsInfo) GetRegionCount(startKey, endKey []byte) int {
end := &regionItem{&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
Expand Down
27 changes: 18 additions & 9 deletions server/api/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 :
Expand All @@ -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)
Expand Down

0 comments on commit 0a57633

Please sign in to comment.