Skip to content

Commit

Permalink
tests: add frequency check time to make resign test more stable (#8411)
Browse files Browse the repository at this point in the history
close #8319

Signed-off-by: husharp <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
HuSharp and ti-chi-bot[bot] authored Jul 19, 2024
1 parent 2d8e03f commit 8de0412
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pkg/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ func (m *EmbeddedEtcdMember) CampaignLeader(ctx context.Context, leaseTimeout in
failpoint.Return(m.leadership.Campaign(leaseTimeout, m.MemberValue()))
})

if m.leadership.GetCampaignTimesNum() > campaignLeaderFrequencyTimes {
checkTimes := campaignLeaderFrequencyTimes
failpoint.Inject("changeFrequencyTimes", func(val failpoint.Value) {
if v, ok := val.(int); ok {
checkTimes = v
}
})
if m.leadership.GetCampaignTimesNum() > checkTimes {
if err := m.ResignEtcdLeader(ctx, m.Name(), ""); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func TestClientLeaderChange(t *testing.T) {

func TestLeaderTransferAndMoveCluster(t *testing.T) {
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 3)
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/mcs/scheduling/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ func (suite *multipleServerTestSuite) TearDownSuite() {

func (suite *multipleServerTestSuite) TestReElectLeader() {
re := suite.Require()
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
tc, err := tests.NewTestSchedulingCluster(suite.ctx, 1, suite.backendEndpoints)
re.NoError(err)
defer tc.Destroy()
Expand Down
14 changes: 11 additions & 3 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func TestDamagedRegion(t *testing.T) {

func TestRegionStatistics(t *testing.T) {
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tc, err := tests.NewTestCluster(ctx, 3)
Expand Down Expand Up @@ -238,7 +242,7 @@ func TestRegionStatistics(t *testing.T) {
re.Len(regions, 1)

leaderServer.ResignLeader()
re.NotEqual(tc.WaitLeader(), leaderName)
re.NotEqual(leaderName, tc.WaitLeader())
leaderServer = tc.GetLeaderServer()
leaderName = leaderServer.GetServer().Name()
rc = leaderServer.GetRaftCluster()
Expand All @@ -255,11 +259,11 @@ func TestRegionStatistics(t *testing.T) {
re.False(r.LoadedFromStorage() && r.LoadedFromSync())

leaderServer.ResignLeader()
re.NotEqual(tc.WaitLeader(), leaderName)
re.NotEqual(leaderName, tc.WaitLeader())
leaderServer = tc.GetLeaderServer()
leaderName = leaderServer.GetServer().Name()
leaderServer.ResignLeader()
re.NotEqual(tc.WaitLeader(), leaderName)
re.NotEqual(leaderName, tc.WaitLeader())
rc = tc.GetLeaderServer().GetRaftCluster()
r = rc.GetRegion(region.Id)
re.NotNil(r)
Expand Down Expand Up @@ -1643,6 +1647,10 @@ func TestMinResolvedTS(t *testing.T) {
// See https://github.com/tikv/pd/issues/4941
func TestTransferLeaderBack(t *testing.T) {
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tc, err := tests.NewTestCluster(ctx, 2)
Expand Down
5 changes: 5 additions & 0 deletions tests/server/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"
"testing"

"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/syncutil"
Expand Down Expand Up @@ -107,6 +108,10 @@ func TestCommand(t *testing.T) {

func TestMonotonicID(t *testing.T) {
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 2)
Expand Down
6 changes: 5 additions & 1 deletion tests/server/region_syncer/region_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ func TestPrepareChecker(t *testing.T) {
// ref: https://github.com/tikv/pd/issues/6988
func TestPrepareCheckerWithTransferLeader(t *testing.T) {
re := require.New(t)
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/member/changeFrequencyTimes", "return(10)"))
defer func() {
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/member/changeFrequencyTimes"))
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker", `return(true)`))
Expand Down Expand Up @@ -298,7 +302,7 @@ func TestPrepareCheckerWithTransferLeader(t *testing.T) {
leaderServer = cluster.GetLeaderServer()
err = cluster.ResignLeader()
re.NoError(err)
re.NotEqual(leaderServer.GetLeader().GetName(), cluster.WaitLeader())
re.NotEqual(leaderServer.GetServer().Name(), cluster.WaitLeader())
rc = cluster.GetLeaderServer().GetServer().GetRaftCluster()
re.True(rc.IsPrepared())
re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker"))
Expand Down

0 comments on commit 8de0412

Please sign in to comment.