Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br/pkg/mock: migrate test-infra to testify #30034

Merged
merged 4 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}