Skip to content

Commit

Permalink
*: remove duplicated functions (tikv#5224)
Browse files Browse the repository at this point in the history
ref tikv#4813

Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored and CabinfeverB committed Jul 14, 2022
1 parent bee2eb3 commit 45a45cb
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 80 deletions.
16 changes: 15 additions & 1 deletion pkg/assertutil/assertutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@

package assertutil

import "github.com/stretchr/testify/require"

// Checker accepts the injection of check functions and context from test files.
// Any check function should be set before usage unless the test will fail.
type Checker struct {
IsNil func(obtained interface{})
FailNow func()
}

// NewChecker creates Checker with FailNow function.
// NewChecker creates Checker.
func NewChecker() *Checker {
return &Checker{}
}

// CheckerWithNilAssert creates Checker with nil assert function.
func CheckerWithNilAssert(re *require.Assertions) *Checker {
checker := NewChecker()
checker.FailNow = func() {
re.FailNow("should be nil")
}
checker.IsNil = func(obtained interface{}) {
re.Nil(obtained)
}
return checker
}

// AssertNil calls the injected IsNil assertion.
func (c *Checker) AssertNil(obtained interface{}) {
if c.IsNil == nil {
Expand Down
3 changes: 2 additions & 1 deletion server/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/pkg/assertutil"
"github.com/tikv/pd/pkg/testutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/config"
Expand Down Expand Up @@ -78,7 +79,7 @@ var zapLogOnce sync.Once
func mustNewCluster(re *require.Assertions, num int, opts ...func(cfg *config.Config)) ([]*config.Config, []*server.Server, cleanUpFunc) {
ctx, cancel := context.WithCancel(context.Background())
svrs := make([]*server.Server, 0, num)
cfgs := server.NewTestMultiConfig(checkerWithNilAssert(re), num)
cfgs := server.NewTestMultiConfig(assertutil.CheckerWithNilAssert(re), num)

ch := make(chan *server.Server, num)
for _, cfg := range cfgs {
Expand Down
13 changes: 1 addition & 12 deletions server/api/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ import (
"github.com/tikv/pd/server/config"
)

func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
re.FailNow("should be nil")
}
checker.IsNil = func(obtained interface{}) {
re.Nil(obtained)
}
return checker
}

func TestGetVersion(t *testing.T) {
// TODO: enable it.
t.Skip("Temporary disable. See issue: https://github.com/tikv/pd/issues/1893")
Expand All @@ -51,7 +40,7 @@ func TestGetVersion(t *testing.T) {
temp, _ := os.Create(fname)
os.Stdout = temp

cfg := server.NewTestSingleConfig(checkerWithNilAssert(re))
cfg := server.NewTestSingleConfig(assertutil.CheckerWithNilAssert(re))
reqCh := make(chan struct{})
go func() {
<-reqCh
Expand Down
13 changes: 1 addition & 12 deletions server/join/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,10 @@ import (
"github.com/tikv/pd/server"
)

func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
re.FailNow("")
}
checker.IsNil = func(obtained interface{}) {
re.Nil(obtained)
}
return checker
}

// A PD joins itself.
func TestPDJoinsItself(t *testing.T) {
re := require.New(t)
cfg := server.NewTestSingleConfig(checkerWithNilAssert(re))
cfg := server.NewTestSingleConfig(assertutil.CheckerWithNilAssert(re))
defer testutil.CleanServer(cfg.DataDir)
cfg.Join = cfg.AdvertiseClientUrls
re.Error(PrepareJoinCluster(cfg))
Expand Down
23 changes: 6 additions & 17 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ func (suite *leaderServerTestSuite) mustWaitLeader(svrs []*Server) *Server {
return leader
}

func (suite *leaderServerTestSuite) checkerWithNilAssert() *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
suite.FailNow("should be nil")
}
checker.IsNil = func(obtained interface{}) {
suite.Nil(obtained)
}
return checker
}

func (suite *leaderServerTestSuite) SetupSuite() {
suite.ctx, suite.cancel = context.WithCancel(context.Background())
suite.svrs = make(map[string]*Server)

cfgs := NewTestMultiConfig(suite.checkerWithNilAssert(), 3)
cfgs := NewTestMultiConfig(assertutil.CheckerWithNilAssert(suite.Require()), 3)

ch := make(chan *Server, 3)
for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -153,7 +142,7 @@ func (suite *leaderServerTestSuite) newTestServersWithCfgs(ctx context.Context,
func (suite *leaderServerTestSuite) TestCheckClusterID() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfgs := NewTestMultiConfig(suite.checkerWithNilAssert(), 2)
cfgs := NewTestMultiConfig(assertutil.CheckerWithNilAssert(suite.Require()), 2)
for i, cfg := range cfgs {
cfg.DataDir = fmt.Sprintf("/tmp/test_pd_check_clusterID_%d", i)
// Clean up before testing.
Expand Down Expand Up @@ -209,7 +198,7 @@ func (suite *leaderServerTestSuite) TestRegisterServerHandler() {
}
return mux, info, nil
}
cfg := NewTestSingleConfig(suite.checkerWithNilAssert())
cfg := NewTestSingleConfig(assertutil.CheckerWithNilAssert(suite.Require()))
ctx, cancel := context.WithCancel(context.Background())
svr, err := CreateServer(ctx, cfg, mokHandler)
suite.NoError(err)
Expand Down Expand Up @@ -248,7 +237,7 @@ func (suite *leaderServerTestSuite) TestSourceIpForHeaderForwarded() {
}
return mux, info, nil
}
cfg := NewTestSingleConfig(suite.checkerWithNilAssert())
cfg := NewTestSingleConfig(assertutil.CheckerWithNilAssert(suite.Require()))
ctx, cancel := context.WithCancel(context.Background())
svr, err := CreateServer(ctx, cfg, mokHandler)
suite.NoError(err)
Expand Down Expand Up @@ -291,7 +280,7 @@ func (suite *leaderServerTestSuite) TestSourceIpForHeaderXReal() {
}
return mux, info, nil
}
cfg := NewTestSingleConfig(suite.checkerWithNilAssert())
cfg := NewTestSingleConfig(assertutil.CheckerWithNilAssert(suite.Require()))
ctx, cancel := context.WithCancel(context.Background())
svr, err := CreateServer(ctx, cfg, mokHandler)
suite.NoError(err)
Expand Down Expand Up @@ -334,7 +323,7 @@ func (suite *leaderServerTestSuite) TestSourceIpForHeaderBoth() {
}
return mux, info, nil
}
cfg := NewTestSingleConfig(suite.checkerWithNilAssert())
cfg := NewTestSingleConfig(assertutil.CheckerWithNilAssert(suite.Require()))
ctx, cancel := context.WithCancel(context.Background())
svr, err := CreateServer(ctx, cfg, mokHandler)
suite.NoError(err)
Expand Down
13 changes: 1 addition & 12 deletions tests/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func TestClientTestSuite(t *testing.T) {
func (suite *clientTestSuite) SetupSuite() {
var err error
re := suite.Require()
suite.srv, suite.cleanup, err = server.NewTestServer(suite.checkerWithNilAssert())
suite.srv, suite.cleanup, err = server.NewTestServer(assertutil.CheckerWithNilAssert(re))
suite.NoError(err)
suite.grpcPDClient = testutil.MustNewGrpcClient(re, suite.srv.GetAddr())
suite.grpcSvr = &server.GrpcServer{Server: suite.srv}
Expand Down Expand Up @@ -728,17 +728,6 @@ func (suite *clientTestSuite) TearDownSuite() {
suite.cleanup()
}

func (suite *clientTestSuite) checkerWithNilAssert() *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
suite.FailNow("should be nil")
}
checker.IsNil = func(obtained interface{}) {
suite.Nil(obtained)
}
return checker
}

func (suite *clientTestSuite) mustWaitLeader(svrs map[string]*server.Server) *server.Server {
for i := 0; i < 500; i++ {
for _, s := range svrs {
Expand Down
3 changes: 2 additions & 1 deletion tests/pdctl/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/log"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/pkg/assertutil"
"github.com/tikv/pd/pkg/testutil"
"github.com/tikv/pd/server"
cmd "github.com/tikv/pd/tools/pd-ctl/pdctl"
Expand All @@ -47,7 +48,7 @@ func TestSendAndGetComponent(t *testing.T) {
}
return mux, info, nil
}
cfg := server.NewTestSingleConfig(checkerWithNilAssert(re))
cfg := server.NewTestSingleConfig(assertutil.CheckerWithNilAssert(re))
ctx, cancel := context.WithCancel(context.Background())
svr, err := server.CreateServer(ctx, cfg, handler)
re.NoError(err)
Expand Down
12 changes: 0 additions & 12 deletions tests/pdctl/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/assertutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/api"
"github.com/tikv/pd/server/core"
Expand Down Expand Up @@ -122,14 +121,3 @@ func MustPutRegion(re *require.Assertions, cluster *tests.TestCluster, regionID,
re.NoError(err)
return r
}

func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
re.FailNow("should be nil")
}
checker.IsNil = func(obtained interface{}) {
re.Nil(obtained)
}
return checker
}
13 changes: 1 addition & 12 deletions tests/server/member/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
re.FailNow("should be nil")
}
checker.IsNil = func(obtained interface{}) {
re.Nil(obtained)
}
return checker
}

func TestMemberDelete(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -314,7 +303,7 @@ func TestGetLeader(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfg := server.NewTestSingleConfig(checkerWithNilAssert(re))
cfg := server.NewTestSingleConfig(assertutil.CheckerWithNilAssert(re))
wg := &sync.WaitGroup{}
wg.Add(1)
done := make(chan bool)
Expand Down

0 comments on commit 45a45cb

Please sign in to comment.