Skip to content

Commit

Permalink
br/pkg/mock: migrate test-infra to testify
Browse files Browse the repository at this point in the history
This closes #28174 .

Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Nov 23, 2021
1 parent b87f9d1 commit 9ac89e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
30 changes: 18 additions & 12 deletions br/pkg/mock/mock_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,49 @@ type Cluster struct {
kv.Storage
*server.TiDBDriver
*domain.Domain
DSN string
PDClient pd.Client
DSN string
PDClient pd.Client
HttpServer *http.Server
}

// NewCluster create a new mock cluster.
func NewCluster() (*Cluster, error) {
cluster := &Cluster{}

pprofOnce.Do(func() {
go func() {
// Make sure pprof is registered.
_ = pprof.Handler
addr := "0.0.0.0:12235"
log.Info("start pprof", zap.String("addr", addr))
if e := http.ListenAndServe(addr, nil); e != nil {
cluster.HttpServer = &http.Server{Addr: addr}
if e := cluster.HttpServer.ListenAndServe(); e != nil {
log.Warn("fail to start pprof", zap.String("addr", addr), zap.Error(e))
}
}()
})

var mockCluster testutils.Cluster
storage, err := mockstore.NewMockStore(
mockstore.WithClusterInspector(func(c testutils.Cluster) {
mockstore.BootstrapWithSingleStore(c)
mockCluster = c
cluster.Cluster = c
}),
)
if err != nil {
return nil, errors.Trace(err)
}
cluster.Storage = storage

session.SetSchemaLease(0)
session.DisableStats4Test()
dom, err := session.BootstrapSession(storage)
if err != nil {
return nil, errors.Trace(err)
}
return &Cluster{
Storage: storage,
Cluster: mockCluster,
Domain: dom,
PDClient: storage.(tikv.Storage).GetRegionCache().PDClient(),
}, nil
cluster.Domain = dom

cluster.PDClient = storage.(tikv.Storage).GetRegionCache().PDClient()
return cluster, nil
}

// Start runs a mock cluster.
Expand Down Expand Up @@ -130,11 +133,14 @@ func (mock *Cluster) Stop() {
mock.Domain.Close()
}
if mock.Storage != nil {
mock.Storage.Close()
_ = mock.Storage.Close()
}
if mock.Server != nil {
mock.Server.Close()
}
if mock.HttpServer != nil {
_ = mock.HttpServer.Close()
}
}

type configOverrider func(*mysql.Config)
Expand Down
37 changes: 12 additions & 25 deletions br/pkg/mock/mock_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,19 @@ package mock_test
import (
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/br/pkg/mock"
"github.com/pingcap/tidb/util/testleak"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)

func Test(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testClusterSuite{})

type testClusterSuite struct {
mock *mock.Cluster
}

func (s *testClusterSuite) SetUpSuite(c *C) {
var err error
s.mock, err = mock.NewCluster()
c.Assert(err, IsNil)
}

func (s *testClusterSuite) TearDownSuite(c *C) {
testleak.AfterTest(c)()
}

func (s *testClusterSuite) TestSmoke(c *C) {
c.Assert(s.mock.Start(), IsNil)
s.mock.Stop()
func TestSmoke(t *testing.T) {
defer goleak.VerifyNone(
t,
goleak.IgnoreTopFunction("github.com/klauspost/compress/zstd.(*blockDec).startDecoder"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"))
m, err := mock.NewCluster()
require.NoError(t, err)
require.NoError(t, m.Start())
m.Stop()
}

0 comments on commit 9ac89e6

Please sign in to comment.