From 13a8fe13f2722e0e081f229b5a0a20287c11954d Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Thu, 10 Oct 2024 15:13:40 +0800 Subject: [PATCH] fix TestStoreWatch panic Signed-off-by: Ryan Leung --- .../integrations/mcs/scheduling/meta_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/integrations/mcs/scheduling/meta_test.go b/tests/integrations/mcs/scheduling/meta_test.go index f1a8805e207a..d134336966a4 100644 --- a/tests/integrations/mcs/scheduling/meta_test.go +++ b/tests/integrations/mcs/scheduling/meta_test.go @@ -26,6 +26,7 @@ import ( "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/mcs/scheduling/server/meta" "github.com/tikv/pd/pkg/utils/testutil" + "github.com/tikv/pd/server/cluster" "github.com/tikv/pd/tests" ) @@ -80,12 +81,12 @@ func (suite *metaTestSuite) TestStoreWatch() { ) re.NoError(err) for i := uint64(1); i <= 4; i++ { - suite.pdLeaderServer.GetServer().GetRaftCluster().PutMetaStore( + suite.getRaftCluster().PutMetaStore( &metapb.Store{Id: i, Address: fmt.Sprintf("mock-%d", i), State: metapb.StoreState_Up, NodeState: metapb.NodeState_Serving, LastHeartbeat: time.Now().UnixNano()}, ) } - suite.pdLeaderServer.GetRaftCluster().RemoveStore(2, false) + suite.getRaftCluster().RemoveStore(2, false) testutil.Eventually(re, func() bool { s := cluster.GetStore(2) if s == nil { @@ -97,13 +98,13 @@ func (suite *metaTestSuite) TestStoreWatch() { testutil.Eventually(re, func() bool { return cluster.GetStore(2).GetState() == metapb.StoreState_Tombstone }) - re.NoError(suite.pdLeaderServer.GetRaftCluster().RemoveTombStoneRecords()) + re.NoError(suite.getRaftCluster().RemoveTombStoneRecords()) testutil.Eventually(re, func() bool { return cluster.GetStore(2) == nil }) // test synchronized store labels - suite.pdLeaderServer.GetServer().GetRaftCluster().PutMetaStore( + suite.getRaftCluster().PutMetaStore( &metapb.Store{Id: 5, Address: "mock-5", State: metapb.StoreState_Up, NodeState: metapb.NodeState_Serving, LastHeartbeat: time.Now().UnixNano(), Labels: []*metapb.StoreLabel{{Key: "zone", Value: "z1"}}}, ) testutil.Eventually(re, func() bool { @@ -113,3 +114,13 @@ func (suite *metaTestSuite) TestStoreWatch() { return cluster.GetStore(5).GetLabels()[0].GetValue() == "z1" }) } + +func (suite *metaTestSuite) getRaftCluster() *cluster.RaftCluster { + re := suite.Require() + leaderName := suite.cluster.WaitLeader() + re.NotEmpty(leaderName) + leaderServer := suite.cluster.GetServer(leaderName) + cluster := leaderServer.GetServer().GetRaftCluster() + re.NotNil(cluster) + return cluster +}