diff --git a/br/pkg/mock/mock_cluster.go b/br/pkg/mock/mock_cluster.go index c60f4764b4ffc..e00246835c365 100644 --- a/br/pkg/mock/mock_cluster.go +++ b/br/pkg/mock/mock_cluster.go @@ -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. @@ -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) diff --git a/br/pkg/mock/mock_cluster_test.go b/br/pkg/mock/mock_cluster_test.go index bee7220613f2d..01dde0c158901 100644 --- a/br/pkg/mock/mock_cluster_test.go +++ b/br/pkg/mock/mock_cluster_test.go @@ -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() }