Skip to content

Commit

Permalink
tidb: accelerate unit test (pingcap#2590)
Browse files Browse the repository at this point in the history
Avoid BootstrapSession everytime, just do it once in testSessionSuite.SetUpSuite
  • Loading branch information
tiancaiamao authored Feb 6, 2017
1 parent b15ed6d commit 8bafc3d
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 382 deletions.
20 changes: 16 additions & 4 deletions bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ import (
"github.com/pingcap/tidb/util/testleak"
)

func (s *testSessionSuite) TestBootstrap(c *C) {
var _ = Suite(&testBootstrapSuite{})

type testBootstrapSuite struct {
dbName string
dbNameBootstrap string
}

func (s *testBootstrapSuite) SetUpSuite(c *C) {
s.dbName = "test_bootstrap"
s.dbNameBootstrap = "test_main_db_bootstrap"
}

func (s *testBootstrapSuite) TestBootstrap(c *C) {
defer testleak.AfterTest(c)()
store := newStoreWithBootstrap(c, s.dbName)
se := newSession(c, store, s.dbName)
Expand Down Expand Up @@ -94,7 +106,7 @@ func globalVarsCount() int64 {
}

// Create a new session on store but only do ddl works.
func (s *testSessionSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {
func (s *testBootstrapSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {
ss := &session{
values: make(map[fmt.Stringer]interface{}),
store: store,
Expand All @@ -116,7 +128,7 @@ func (s *testSessionSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {

// When a session failed in bootstrap process (for example, the session is killed after doDDLWorks()).
// We should make sure that the following session could finish the bootstrap process.
func (s *testSessionSuite) TestBootstrapWithError(c *C) {
func (s *testBootstrapSuite) testBootstrapWithError(c *C) {
defer testleak.AfterTest(c)()
store := newStore(c, s.dbNameBootstrap)
s.bootstrapWithOnlyDDLWork(store, c)
Expand Down Expand Up @@ -152,7 +164,7 @@ func (s *testSessionSuite) TestBootstrapWithError(c *C) {
}

// Test case for upgrade
func (s *testSessionSuite) TestUpgrade(c *C) {
func (s *testBootstrapSuite) TestUpgrade(c *C) {
defer testleak.AfterTest(c)()
store := newStoreWithBootstrap(c, s.dbName)
se := newSession(c, store, s.dbName)
Expand Down
12 changes: 8 additions & 4 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
Expand All @@ -46,6 +47,7 @@ const defaultBatchSize = 1024

type testDBSuite struct {
store kv.Storage
dom *domain.Domain
schemaName string
tk *testkit.TestKit
s tidb.Session
Expand All @@ -62,7 +64,7 @@ func (s *testDBSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
localstore.MockRemoteStore = true

err = tidb.BootstrapSession(s.store)
s.dom, err = tidb.BootstrapSession(s.store)
c.Assert(err, IsNil)

s.s, err = tidb.CreateSession(s.store)
Expand All @@ -82,6 +84,8 @@ func (s *testDBSuite) TearDownSuite(c *C) {
localstore.MockRemoteStore = false
s.s.Execute("drop database if exists test_db")
s.s.Close()
s.dom.Close()
s.store.Close()
}

func (s *testDBSuite) testErrorCode(c *C, sql string, errCode int) {
Expand Down Expand Up @@ -759,7 +763,7 @@ func (s *testDBSuite) TestUpdateMultipleTable(c *C) {
defer testleak.AfterTest(c)
store, err := tidb.NewStore("memory://update_multiple_table")
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -820,7 +824,7 @@ func (s *testDBSuite) TestTruncateTable(c *C) {
defer testleak.AfterTest(c)
store, err := tidb.NewStore("memory://truncate_table")
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -880,7 +884,7 @@ func (s *testDBSuite) testRenameTable(c *C, storeStr, sql string) {
defer testleak.AfterTest(c)
store, err := tidb.NewStore("memory://" + storeStr)
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
s.tk = testkit.NewTestKit(c, store)
s.tk.MustExec("use test")
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *testSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
}
err := tidb.BootstrapSession(s.store)
_, err := tidb.BootstrapSession(s.store)
c.Assert(err, IsNil)
logLevel := os.Getenv("log_level")
log.SetLevelByString(logLevel)
Expand Down
6 changes: 3 additions & 3 deletions perfschema/perfschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *testPerfSchemaSuit) TestInsert(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
defer store.Close()
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
se := newSession(c, store, "")
defer se.Close()
Expand Down Expand Up @@ -110,7 +110,7 @@ func (p *testPerfSchemaSuit) TestInstrument(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_instrument_db")
c.Assert(err, IsNil)
defer store.Close()
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
se := newSession(c, store, "test_instrument_db")
defer se.Close()
Expand All @@ -126,7 +126,7 @@ func (p *testPerfSchemaSuit) TestConcurrentStatement(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_con_stmt")
c.Assert(err, IsNil)
defer store.Close()
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
se := newSession(c, store, "test_con_stmt")

Expand Down
2 changes: 1 addition & 1 deletion plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,6 @@ func newStoreWithBootstrap() (kv.Storage, error) {
if err != nil {
return nil, errors.Trace(err)
}
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
return store, errors.Trace(err)
}
2 changes: 1 addition & 1 deletion privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *testCacheSuite) SetUpSuite(c *C) {
privileges.Enable = true
store, err := tidb.NewStore("memory://mysql")
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
s.store = store
}
Expand Down
2 changes: 1 addition & 1 deletion privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func mustExec(c *C, se tidb.Session, sql string) {
func newStore(c *C, dbPath string) kv.Storage {
store, err := tidb.NewStore("memory" + "://" + dbPath)
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
return store
}
Expand Down
2 changes: 1 addition & 1 deletion server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (ts *TidbTestSuite) SetUpSuite(c *C) {
log.SetLevelByString("error")
store, err := tidb.NewStore("memory:///tmp/tidb")
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
_, err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
ts.tidbdrv = NewTiDBDriver(store)
cfg := &Config{
Expand Down
9 changes: 5 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ func CreateSession(store kv.Storage) (Session, error) {
}

// BootstrapSession runs the first time when the TiDB server start.
func BootstrapSession(store kv.Storage) error {
func BootstrapSession(store kv.Storage) (*domain.Domain, error) {
ver := getStoreBootstrapVersion(store)
if ver == notBootstrapped {
runInBootstrapSession(store, bootstrap)
Expand All @@ -739,11 +739,12 @@ func BootstrapSession(store kv.Storage) error {

se, err := createSession(store)
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}
err = sessionctx.GetDomain(se).LoadPrivilegeLoop(se)
dom := sessionctx.GetDomain(se)
err = dom.LoadPrivilegeLoop(se)

return errors.Trace(err)
return dom, errors.Trace(err)
}

// runInBootstrapSession create a special session for boostrap to run.
Expand Down
Loading

0 comments on commit 8bafc3d

Please sign in to comment.