Skip to content

Commit

Permalink
tests: reduce unnecessary time.sleep in keyspace group (#6632)
Browse files Browse the repository at this point in the history
ref #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
lhy1024 and ti-chi-bot[bot] committed Jun 19, 2023
1 parent df79250 commit 0934e64
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
8 changes: 5 additions & 3 deletions tests/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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 @@ -440,7 +442,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 @@ -649,7 +651,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 @@ -716,7 +718,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
3 changes: 0 additions & 3 deletions tests/pdctl/keyspace/keyspace_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -117,7 +116,6 @@ func TestSplitKeyspaceGroup(t *testing.T) {
re.NoError(err)
cmd := pdctlCmd.GetRootCmd()

time.Sleep(2 * time.Second)
tc.WaitLeader()
leaderServer := tc.GetServer(tc.GetLeader())
re.NoError(leaderServer.BootstrapCluster())
Expand Down Expand Up @@ -157,7 +155,6 @@ func TestExternalAllocNodeWhenStart(t *testing.T) {

cmd := pdctlCmd.GetRootCmd()

time.Sleep(2 * time.Second)
tc.WaitLeader()
leaderServer := tc.GetServer(tc.GetLeader())
re.NoError(leaderServer.BootstrapCluster())
Expand Down

0 comments on commit 0934e64

Please sign in to comment.