diff --git a/server/api/admin_test.go b/server/api/admin_test.go index ba9aaa875a4..f8fd0bcf74f 100644 --- a/server/api/admin_test.go +++ b/server/api/admin_test.go @@ -42,7 +42,7 @@ func TestAdminTestSuite(t *testing.T) { func (suite *adminTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/checker_test.go b/server/api/checker_test.go index a3ab815ffb7..d40a61f93a8 100644 --- a/server/api/checker_test.go +++ b/server/api/checker_test.go @@ -40,7 +40,7 @@ func TestCheckerTestSuite(t *testing.T) { func (suite *checkerTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/checker", addr, apiPrefix) diff --git a/server/api/cluster_test.go b/server/api/cluster_test.go index 496d75e6f38..5ef1e5584bf 100644 --- a/server/api/cluster_test.go +++ b/server/api/cluster_test.go @@ -41,7 +41,7 @@ func TestClusterTestSuite(t *testing.T) { func (suite *clusterTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/config_test.go b/server/api/config_test.go index 144e511979a..6480e8f3967 100644 --- a/server/api/config_test.go +++ b/server/api/config_test.go @@ -44,7 +44,7 @@ func (suite *configTestSuite) SetupSuite() { suite.svr, suite.cleanup = mustNewServer(re, func(cfg *config.Config) { cfg.Replication.EnablePlacementRules = false }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/hot_status_test.go b/server/api/hot_status_test.go index 66a4e29afb7..664d935563b 100644 --- a/server/api/hot_status_test.go +++ b/server/api/hot_status_test.go @@ -43,7 +43,7 @@ func TestHotStatusTestSuite(t *testing.T) { func (suite *hotStatusTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/hotspot", addr, apiPrefix) diff --git a/server/api/label_test.go b/server/api/label_test.go index b9503871a5a..14de872ed2b 100644 --- a/server/api/label_test.go +++ b/server/api/label_test.go @@ -119,7 +119,7 @@ func (suite *labelsStoreTestSuite) SetupSuite() { suite.svr, suite.cleanup = mustNewServer(re, func(cfg *config.Config) { cfg.Replication.StrictlyMatchLabel = false }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) @@ -205,7 +205,7 @@ func (suite *strictlyLabelsStoreTestSuite) SetupSuite() { cfg.Replication.StrictlyMatchLabel = true cfg.Replication.EnablePlacementRules = false }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) suite.grpcSvr = &server.GrpcServer{Server: suite.svr} addr := suite.svr.GetAddr() diff --git a/server/api/log_test.go b/server/api/log_test.go index f03472b8146..e85ed9fa120 100644 --- a/server/api/log_test.go +++ b/server/api/log_test.go @@ -39,7 +39,7 @@ func TestLogTestSuite(t *testing.T) { func (suite *logTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/admin", addr, apiPrefix) diff --git a/server/api/min_resolved_ts_test.go b/server/api/min_resolved_ts_test.go index 47c47713bff..a8a7252ed33 100644 --- a/server/api/min_resolved_ts_test.go +++ b/server/api/min_resolved_ts_test.go @@ -42,7 +42,7 @@ func (suite *minResolvedTSTestSuite) SetupSuite() { re := suite.Require() cluster.DefaultMinResolvedTSPersistenceInterval = time.Microsecond suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/operator_test.go b/server/api/operator_test.go index ba08b890b9b..871cb8cc355 100644 --- a/server/api/operator_test.go +++ b/server/api/operator_test.go @@ -55,7 +55,7 @@ func (suite *operatorTestSuite) SetupSuite() { re := suite.Require() suite.NoError(failpoint.Enable("github.com/tikv/pd/server/schedule/unexpectedOperator", "return(true)")) suite.svr, suite.cleanup = mustNewServer(re, func(cfg *config.Config) { cfg.Replication.MaxReplicas = 1 }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) @@ -178,7 +178,7 @@ func (suite *transferRegionOperatorTestSuite) SetupSuite() { re := suite.Require() suite.NoError(failpoint.Enable("github.com/tikv/pd/server/schedule/unexpectedOperator", "return(true)")) suite.svr, suite.cleanup = mustNewServer(re, func(cfg *config.Config) { cfg.Replication.MaxReplicas = 3 }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/pprof_test.go b/server/api/pprof_test.go index 3d80a325758..a0165aaf27d 100644 --- a/server/api/pprof_test.go +++ b/server/api/pprof_test.go @@ -38,7 +38,7 @@ func TestProfTestSuite(t *testing.T) { func (suite *profTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/debug", addr, apiPrefix) diff --git a/server/api/region_label_test.go b/server/api/region_label_test.go index 0165ec7d37e..b0cc1b60570 100644 --- a/server/api/region_label_test.go +++ b/server/api/region_label_test.go @@ -42,7 +42,7 @@ func TestRegionLabelTestSuite(t *testing.T) { func (suite *regionLabelTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/config/region-label/", addr, apiPrefix) diff --git a/server/api/region_test.go b/server/api/region_test.go index 168edd0e419..3bccc1f0af7 100644 --- a/server/api/region_test.go +++ b/server/api/region_test.go @@ -96,7 +96,7 @@ func TestRegionTestSuite(t *testing.T) { func (suite *regionTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) @@ -437,7 +437,7 @@ func TestGetRegionTestSuite(t *testing.T) { func (suite *getRegionTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) @@ -545,7 +545,7 @@ func TestGetRegionRangeHolesTestSuite(t *testing.T) { func (suite *getRegionRangeHolesTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) mustBootstrapCluster(re, suite.svr) @@ -594,7 +594,7 @@ func TestRegionsReplicatedTestSuite(t *testing.T) { func (suite *regionsReplicatedTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/rule_test.go b/server/api/rule_test.go index 63af8b19c1c..669fca34489 100644 --- a/server/api/rule_test.go +++ b/server/api/rule_test.go @@ -43,7 +43,7 @@ func TestRuleTestSuite(t *testing.T) { func (suite *ruleTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/config", addr, apiPrefix) diff --git a/server/api/scheduler_test.go b/server/api/scheduler_test.go index c4b30595967..bdcf85c2ea1 100644 --- a/server/api/scheduler_test.go +++ b/server/api/scheduler_test.go @@ -45,7 +45,7 @@ func TestScheduleTestSuite(t *testing.T) { func (suite *scheduleTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/schedulers", addr, apiPrefix) diff --git a/server/api/server_test.go b/server/api/server_test.go index b82dfc5ea21..8693b4b87ca 100644 --- a/server/api/server_test.go +++ b/server/api/server_test.go @@ -106,7 +106,7 @@ func mustNewCluster(re *require.Assertions, num int, opts ...func(cfg *config.Co } close(ch) // wait etcd and http servers - mustWaitLeader(re, svrs) + server.MustWaitLeader(re, svrs) // clean up clean := func() { @@ -122,23 +122,6 @@ func mustNewCluster(re *require.Assertions, num int, opts ...func(cfg *config.Co return cfgs, svrs, clean } -func mustWaitLeader(re *require.Assertions, svrs []*server.Server) { - testutil.Eventually(re, func() bool { - var leader *pdpb.Member - for _, svr := range svrs { - l := svr.GetLeader() - // All servers' GetLeader should return the same leader. - if l == nil || (leader != nil && l.GetMemberId() != leader.GetMemberId()) { - return false - } - if leader == nil { - leader = l - } - } - return true - }) -} - func mustBootstrapCluster(re *require.Assertions, s *server.Server) { grpcPDClient := testutil.MustNewGrpcClient(re, s.GetAddr()) req := &pdpb.BootstrapRequest{ @@ -164,7 +147,7 @@ func TestServiceTestSuite(t *testing.T) { func (suite *serviceTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) mustBootstrapCluster(re, suite.svr) mustPutStore(re, suite.svr, 1, metapb.StoreState_Up, metapb.NodeState_Serving, nil) diff --git a/server/api/service_gc_safepoint_test.go b/server/api/service_gc_safepoint_test.go index 291bba0fcaf..e1d1a451922 100644 --- a/server/api/service_gc_safepoint_test.go +++ b/server/api/service_gc_safepoint_test.go @@ -41,7 +41,7 @@ func TestServiceGCSafepointTestSuite(t *testing.T) { func (suite *serviceGCSafepointTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/service_middleware_test.go b/server/api/service_middleware_test.go index ac188dd8759..ef1ab6b941a 100644 --- a/server/api/service_middleware_test.go +++ b/server/api/service_middleware_test.go @@ -44,7 +44,7 @@ func (suite *auditMiddlewareTestSuite) SetupSuite() { suite.svr, suite.cleanup = mustNewServer(re, func(cfg *config.Config) { cfg.Replication.EnablePlacementRules = false }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) @@ -125,7 +125,7 @@ func TestRateLimitConfigTestSuite(t *testing.T) { func (suite *rateLimitConfigTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) mustBootstrapCluster(re, suite.svr) suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", suite.svr.GetAddr(), apiPrefix) } diff --git a/server/api/stats_test.go b/server/api/stats_test.go index 77c35b19679..c2a53597a90 100644 --- a/server/api/stats_test.go +++ b/server/api/stats_test.go @@ -41,7 +41,7 @@ func TestStatsTestSuite(t *testing.T) { func (suite *statsTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/store_test.go b/server/api/store_test.go index 64cb500164d..9f056ba111b 100644 --- a/server/api/store_test.go +++ b/server/api/store_test.go @@ -98,7 +98,7 @@ func (suite *storeTestSuite) SetupSuite() { // TODO: enable placmentrules re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re, func(cfg *config.Config) { cfg.Replication.EnablePlacementRules = false }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.grpcSvr = &server.GrpcServer{Server: suite.svr} diff --git a/server/api/trend_test.go b/server/api/trend_test.go index 972af465ef9..d8f0abcdac2 100644 --- a/server/api/trend_test.go +++ b/server/api/trend_test.go @@ -31,7 +31,7 @@ func TestTrend(t *testing.T) { re := require.New(t) svr, cleanup := mustNewServer(re) defer cleanup() - mustWaitLeader(re, []*server.Server{svr}) + server.MustWaitLeader(re, []*server.Server{svr}) mustBootstrapCluster(re, svr) for i := 1; i <= 3; i++ { diff --git a/server/api/tso_test.go b/server/api/tso_test.go index 2f59a2ecf07..039d3d534e2 100644 --- a/server/api/tso_test.go +++ b/server/api/tso_test.go @@ -42,7 +42,7 @@ func (suite *tsoTestSuite) SetupSuite() { cfg.EnableLocalTSO = true cfg.Labels[config.ZoneLabel] = "dc-1" }) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix) diff --git a/server/api/unsafe_operation_test.go b/server/api/unsafe_operation_test.go index 62df25c6b68..77c4149ec3b 100644 --- a/server/api/unsafe_operation_test.go +++ b/server/api/unsafe_operation_test.go @@ -40,7 +40,7 @@ func TestUnsafeOperationTestSuite(t *testing.T) { func (suite *unsafeOperationTestSuite) SetupSuite() { re := suite.Require() suite.svr, suite.cleanup = mustNewServer(re) - mustWaitLeader(re, []*server.Server{suite.svr}) + server.MustWaitLeader(re, []*server.Server{suite.svr}) addr := suite.svr.GetAddr() suite.urlPrefix = fmt.Sprintf("%s%s/api/v1/admin/unsafe", addr, apiPrefix) diff --git a/server/server_test.go b/server/server_test.go index f520314a5b1..17a7e330aff 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -49,20 +49,6 @@ func TestLeaderServerTestSuite(t *testing.T) { suite.Run(t, new(leaderServerTestSuite)) } -func (suite *leaderServerTestSuite) mustWaitLeader(svrs []*Server) *Server { - var leader *Server - testutil.Eventually(suite.Require(), func() bool { - for _, s := range svrs { - if !s.IsClosed() && s.member.IsLeader() { - leader = s - return true - } - } - return false - }) - return leader -} - func (suite *leaderServerTestSuite) SetupSuite() { suite.ctx, suite.cancel = context.WithCancel(context.Background()) suite.svrs = make(map[string]*Server) @@ -125,7 +111,7 @@ func (suite *leaderServerTestSuite) newTestServersWithCfgs(ctx context.Context, suite.NotNil(svr) svrs = append(svrs, svr) } - suite.mustWaitLeader(svrs) + MustWaitLeader(suite.Require(), svrs) cleanup := func() { for _, svr := range svrs { diff --git a/server/testutil.go b/server/testutil.go index 2534008a5b5..d268fc7da33 100644 --- a/server/testutil.go +++ b/server/testutil.go @@ -23,8 +23,10 @@ import ( "time" "github.com/pingcap/log" + "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/assertutil" "github.com/tikv/pd/pkg/tempurl" + "github.com/tikv/pd/pkg/testutil" "github.com/tikv/pd/pkg/typeutil" "github.com/tikv/pd/server/config" "go.etcd.io/etcd/embed" @@ -115,3 +117,18 @@ func NewTestMultiConfig(c *assertutil.Checker, count int) []*config.Config { return cfgs } + +// MustWaitLeader return the leader until timeout. +func MustWaitLeader(re *require.Assertions, svrs []*Server) *Server { + var leader *Server + testutil.Eventually(re, func() bool { + for _, s := range svrs { + if !s.IsClosed() && s.member.IsLeader() { + leader = s + return true + } + } + return false + }) + return leader +} diff --git a/tests/client/client_test.go b/tests/client/client_test.go index 8e67cfb4949..b964a93ca0b 100644 --- a/tests/client/client_test.go +++ b/tests/client/client_test.go @@ -695,7 +695,7 @@ func (suite *clientTestSuite) SetupSuite() { suite.grpcPDClient = testutil.MustNewGrpcClient(re, suite.srv.GetAddr()) suite.grpcSvr = &server.GrpcServer{Server: suite.srv} - suite.mustWaitLeader(map[string]*server.Server{suite.srv.GetAddr(): suite.srv}) + server.MustWaitLeader(re, []*server.Server{suite.srv}) suite.bootstrapServer(newHeader(suite.srv), suite.grpcPDClient) suite.ctx, suite.clean = context.WithCancel(context.Background()) @@ -728,19 +728,6 @@ func (suite *clientTestSuite) TearDownSuite() { suite.cleanup() } -func (suite *clientTestSuite) mustWaitLeader(svrs map[string]*server.Server) *server.Server { - for i := 0; i < 500; i++ { - for _, s := range svrs { - if !s.IsClosed() && s.GetMember().IsLeader() { - return s - } - } - time.Sleep(100 * time.Millisecond) - } - suite.FailNow("no leader") - return nil -} - func newHeader(srv *server.Server) *pdpb.RequestHeader { return &pdpb.RequestHeader{ ClusterId: srv.ClusterID(), diff --git a/tests/server/api/api_test.go b/tests/server/api/api_test.go index 012c09b287e..58323a9d236 100644 --- a/tests/server/api/api_test.go +++ b/tests/server/api/api_test.go @@ -274,7 +274,11 @@ func (suite *middlewareTestSuite) TestAuditPrometheusBackend() { // resign to test persist config oldLeaderName := leader.GetServer().Name() leader.GetServer().GetMember().ResignEtcdLeader(leader.GetServer().Context(), oldLeaderName, "") - suite.mustWaitLeader() + var servers []*server.Server + for _, s := range suite.cluster.GetServers() { + servers = append(servers, s.GetServer()) + } + server.MustWaitLeader(suite.Require(), servers) leader = suite.cluster.GetServer(suite.cluster.GetLeader()) timeUnix = time.Now().Unix() - 20 @@ -740,17 +744,3 @@ func sendRequest(re *require.Assertions, url string, method string, statusCode i resp.Body.Close() return output } - -func (suite *middlewareTestSuite) mustWaitLeader() *server.Server { - var leader *server.Server - testutil.Eventually(suite.Require(), func() bool { - for _, s := range suite.cluster.GetServers() { - if !s.GetServer().IsClosed() && s.GetServer().GetMember().IsLeader() { - leader = s.GetServer() - return true - } - } - return false - }) - return leader -} diff --git a/tests/server/config/config_test.go b/tests/server/config/config_test.go index 3f333c1722b..4321840c286 100644 --- a/tests/server/config/config_test.go +++ b/tests/server/config/config_test.go @@ -23,7 +23,6 @@ import ( "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/ratelimit" - "github.com/tikv/pd/pkg/testutil" "github.com/tikv/pd/server" "github.com/tikv/pd/tests" ) @@ -65,23 +64,13 @@ func TestRateLimitConfigReload(t *testing.T) { oldLeaderName := leader.GetServer().Name() leader.GetServer().GetMember().ResignEtcdLeader(leader.GetServer().Context(), oldLeaderName, "") - mustWaitLeader(re, cluster.GetServers()) + var servers []*server.Server + for _, s := range cluster.GetServers() { + servers = append(servers, s.GetServer()) + } + server.MustWaitLeader(re, servers) leader = cluster.GetServer(cluster.GetLeader()) re.True(leader.GetServer().GetServiceMiddlewarePersistOptions().IsRateLimitEnabled()) re.Len(leader.GetServer().GetServiceMiddlewarePersistOptions().GetRateLimitConfig().LimiterConfig, 1) } - -func mustWaitLeader(re *require.Assertions, svrs map[string]*tests.TestServer) *server.Server { - var leader *server.Server - testutil.Eventually(re, func() bool { - for _, svr := range svrs { - if !svr.GetServer().IsClosed() && svr.GetServer().GetMember().IsLeader() { - leader = svr.GetServer() - return true - } - } - return false - }) - return leader -} diff --git a/tests/server/member/member_test.go b/tests/server/member/member_test.go index 229b4756045..1cc1b9f91ff 100644 --- a/tests/server/member/member_test.go +++ b/tests/server/member/member_test.go @@ -315,7 +315,7 @@ func TestGetLeader(t *testing.T) { go sendRequest(re, wg, done, cfg.ClientUrls) time.Sleep(100 * time.Millisecond) - mustWaitLeader(re, []*server.Server{svr}) + server.MustWaitLeader(re, []*server.Server{svr}) re.NotNil(svr.GetLeader()) @@ -345,17 +345,3 @@ func sendRequest(re *require.Assertions, wg *sync.WaitGroup, done <-chan bool, a time.Sleep(10 * time.Millisecond) } } - -func mustWaitLeader(re *require.Assertions, svrs []*server.Server) *server.Server { - var leader *server.Server - testutil.Eventually(re, func() bool { - for _, s := range svrs { - if !s.IsClosed() && s.GetMember().IsLeader() { - leader = s - return true - } - } - return false - }) - return leader -}