Skip to content

Commit

Permalink
tests: reduce unnecessary time.sleep in keyspace group (tikv#6632)
Browse files Browse the repository at this point in the history
ref tikv#6599

Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and rleungx committed Aug 2, 2023
1 parent ec04adf commit 8645d1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 5 additions & 3 deletions tests/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ var (
WaitLeaderReturnDelay = 20 * time.Millisecond
// WaitLeaderCheckInterval represents the time interval of WaitLeader running check.
WaitLeaderCheckInterval = 500 * time.Millisecond
// WaitLeaderRetryTimes represents the maximum number of loops of WaitLeader.
WaitLeaderRetryTimes = 100
)

// TestServer is only for test.
Expand Down Expand Up @@ -410,7 +412,7 @@ func (s *TestServer) BootstrapCluster() error {
// make a test know the PD leader has been elected as soon as possible.
// If it exceeds the maximum number of loops, it will return nil.
func (s *TestServer) WaitLeader() bool {
for i := 0; i < 100; i++ {
for i := 0; i < WaitLeaderRetryTimes; i++ {
if s.server.GetMember().IsLeader() {
return true
}
Expand Down Expand Up @@ -619,7 +621,7 @@ func (c *TestCluster) GetFollower() string {
// If it exceeds the maximum number of loops, it will return an empty string.
func (c *TestCluster) WaitLeader(ops ...WaitOption) string {
option := &WaitOp{
retryTimes: 100,
retryTimes: WaitLeaderRetryTimes,
waitInterval: WaitLeaderCheckInterval,
}
for _, op := range ops {
Expand Down Expand Up @@ -686,7 +688,7 @@ func (c *TestCluster) ResignLeader() error {
// If it exceeds the maximum number of loops, it will return an empty string.
func (c *TestCluster) WaitAllocatorLeader(dcLocation string, ops ...WaitOption) string {
option := &WaitOp{
retryTimes: 100,
retryTimes: WaitLeaderRetryTimes,
waitInterval: WaitLeaderCheckInterval,
}
for _, op := range ops {
Expand Down
12 changes: 9 additions & 3 deletions tests/integrations/mcs/keyspace/tso_keyspace_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/stretchr/testify/suite"
bs "github.com/tikv/pd/pkg/basicserver"
"github.com/tikv/pd/pkg/mcs/utils"
Expand Down Expand Up @@ -54,6 +55,7 @@ func TestKeyspaceGroupTestSuite(t *testing.T) {
}

func (suite *keyspaceGroupTestSuite) SetupTest() {
suite.NoError(failpoint.Enable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes", `return(true)`))
ctx, cancel := context.WithCancel(context.Background())
suite.ctx = ctx
cluster, err := tests.NewTestAPICluster(suite.ctx, 1)
Expand All @@ -77,6 +79,7 @@ func (suite *keyspaceGroupTestSuite) SetupTest() {
func (suite *keyspaceGroupTestSuite) TearDownTest() {
suite.cleanupFunc()
suite.cluster.Destroy()
suite.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes"))
}

func (suite *keyspaceGroupTestSuite) TestAllocNodesUpdate() {
Expand Down Expand Up @@ -292,9 +295,12 @@ func (suite *keyspaceGroupTestSuite) TestDefaultKeyspaceGroup() {
mcs.WaitForPrimaryServing(suite.Require(), nodes)

// the default keyspace group is exist.
time.Sleep(2 * time.Second)
kg, code := suite.tryGetKeyspaceGroup(utils.DefaultKeyspaceGroupID)
suite.Equal(http.StatusOK, code)
var kg *endpoint.KeyspaceGroup
var code int
testutil.Eventually(suite.Require(), func() bool {
kg, code = suite.tryGetKeyspaceGroup(utils.DefaultKeyspaceGroupID)
return code == http.StatusOK && kg != nil
}, testutil.WithWaitFor(time.Second*1))
suite.Equal(utils.DefaultKeyspaceGroupID, kg.ID)
suite.Len(kg.Members, utils.DefaultKeyspaceGroupReplicaCount)
for _, member := range kg.Members {
Expand Down

0 comments on commit 8645d1b

Please sign in to comment.