From 50470ad935b4ac49fe1f791aeb600d9759b0424f Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Fri, 21 Dec 2018 19:47:53 +0800 Subject: [PATCH] refine test --- ddl/db_partition_test.go | 3 +- ddl/db_test.go | 42 +++-------------------- ddl/failtest/fail_db_test.go | 64 +++--------------------------------- util/testutil/testutil.go | 37 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 97 deletions(-) diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index 78629e4dbc073..606aa6fa67063 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -38,6 +38,7 @@ import ( "github.com/pingcap/tidb/util/admin" "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/testkit" + "github.com/pingcap/tidb/util/testutil" ) func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { @@ -952,7 +953,7 @@ func (s *testIntegrationSuite) TestPartitionDropIndex(c *C) { } c.Assert(idx1, NotNil) - sessionExecInGoroutine(c, s.store, "drop index idx1 on partition_drop_idx;", done) + testutil.SessionExecInGoroutine(c, s.store, "drop index idx1 on partition_drop_idx;", done) ticker := time.NewTicker(s.lease / 2) defer ticker.Stop() LOOP: diff --git a/ddl/db_test.go b/ddl/db_test.go index 261651b7dff49..6a63a181ff67e 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -608,7 +608,7 @@ func (s *testDBSuite) testAddIndex(c *C, testPartition bool, createTableSQL stri s.mustExec(c, sql) otherKeys = append(otherKeys, v) - sessionExecInGoroutine(c, s.store, "create index c3_index on test_add_index (c3)", done) + testutil.SessionExecInGoroutine(c, s.store, "create index c3_index on test_add_index (c3)", done) deletedKeys := make(map[int]struct{}) @@ -753,7 +753,7 @@ func (s *testDBSuite) TestDropIndex(c *C) { } c.Assert(c3idx, NotNil) - sessionExecInGoroutine(c, s.store, "drop index c3_index on test_drop_index", done) + testutil.SessionExecInGoroutine(c, s.store, "drop index c3_index on test_drop_index", done) ticker := time.NewTicker(s.lease / 2) defer ticker.Stop() @@ -915,38 +915,6 @@ func sessionExec(c *C, s kv.Storage, sql string) { se.Close() } -func sessionExecInGoroutine(c *C, s kv.Storage, sql string, done chan error) { - execMultiSQLInGoroutine(c, s, "test_db", []string{sql}, done) -} - -func execMultiSQLInGoroutine(c *C, s kv.Storage, dbName string, multiSQL []string, done chan error) { - go func() { - se, err := session.CreateSession4Test(s) - if err != nil { - done <- errors.Trace(err) - return - } - defer se.Close() - _, err = se.Execute(context.Background(), "use "+dbName) - if err != nil { - done <- errors.Trace(err) - return - } - for _, sql := range multiSQL { - rs, err := se.Execute(context.Background(), sql) - if err != nil { - done <- errors.Trace(err) - return - } - if rs != nil { - done <- errors.Errorf("RecordSet should be empty.") - return - } - done <- nil - } - }() -} - func (s *testDBSuite) testAddColumn(c *C) { done := make(chan error, 1) @@ -956,7 +924,7 @@ func (s *testDBSuite) testAddColumn(c *C) { s.mustExec(c, "insert into t2 values (?, ?, ?)", i, i, i) } - sessionExecInGoroutine(c, s.store, "alter table t2 add column c4 int default -1", done) + testutil.SessionExecInGoroutine(c, s.store, "alter table t2 add column c4 int default -1", done) ticker := time.NewTicker(s.lease / 2) defer ticker.Stop() @@ -1091,7 +1059,7 @@ func (s *testDBSuite) testDropColumn(c *C) { } // get c4 column id - sessionExecInGoroutine(c, s.store, "alter table t2 drop column c4", done) + testutil.SessionExecInGoroutine(c, s.store, "alter table t2 drop column c4", done) ticker := time.NewTicker(s.lease / 2) defer ticker.Stop() @@ -1516,7 +1484,7 @@ func (s *testDBSuite) TestAddNotNullColumn(c *C) { s.tk.MustExec("create table tnn (c1 int primary key auto_increment, c2 int)") s.tk.MustExec("insert tnn (c2) values (0)" + strings.Repeat(",(0)", 99)) done := make(chan error, 1) - sessionExecInGoroutine(c, s.store, "alter table tnn add column c3 int not null default 3", done) + testutil.SessionExecInGoroutine(c, s.store, "alter table tnn add column c3 int not null default 3", done) updateCnt := 0 out: for { diff --git a/ddl/failtest/fail_db_test.go b/ddl/failtest/fail_db_test.go index ff28212948261..552f00d22fa0d 100644 --- a/ddl/failtest/fail_db_test.go +++ b/ddl/failtest/fail_db_test.go @@ -16,9 +16,6 @@ package ddl_test import ( "context" "fmt" - "github.com/pingcap/errors" - "github.com/pingcap/tidb/sessionctx/variable" - "math" "math/rand" "os" "sync/atomic" @@ -26,6 +23,7 @@ import ( "time" . "github.com/pingcap/check" + "github.com/pingcap/errors" gofail "github.com/pingcap/gofail/runtime" "github.com/pingcap/parser" "github.com/pingcap/parser/model" @@ -33,11 +31,13 @@ import ( "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/session" + "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/store/mockstore/mocktikv" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/testkit" "github.com/pingcap/tidb/util/testleak" + "github.com/pingcap/tidb/util/testutil" ) func TestT(t *testing.T) { @@ -325,33 +325,12 @@ func (s *testFailDBSuite) TestAddIndexWorkerNum(c *C) { done := make(chan error, 1) start := -10 - defaultBatchSize := 2048 - num := defaultBatchSize + num := 4096 // first add some rows for i := start; i < num; i++ { sql := fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", i, i, i) tk.MustExec(sql) } - // Add some discrete rows. - maxBatch := 20 - batchCnt := 100 - otherKeys := make([]int, 0, batchCnt*maxBatch) - // Make sure there are no duplicate keys. - base := defaultBatchSize * 20 - for i := 1; i < batchCnt; i++ { - n := base + i*defaultBatchSize + i - for j := 0; j < rand.Intn(maxBatch); j++ { - n += j - sql := fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", n, n, n) - tk.MustExec(sql) - otherKeys = append(otherKeys, n) - } - } - // Encounter the value of math.MaxInt64 in middle of - v := math.MaxInt64 - defaultBatchSize/2 - sql := fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", v, v, v) - tk.MustExec(sql) - otherKeys = append(otherKeys, v) is := s.dom.InfoSchema() schemaName := model.NewCIStr("test_db") @@ -372,8 +351,7 @@ func (s *testFailDBSuite) TestAddIndexWorkerNum(c *C) { gofail.Enable("github.com/pingcap/tidb/ddl/checkIndexWorkerNum", `return(true)`) defer gofail.Disable("github.com/pingcap/tidb/ddl/checkIndexWorkerNum") - sessionExecInGoroutine(c, s.store, "create index c3_index on test_add_index (c3)", done) - + testutil.SessionExecInGoroutine(c, s.store, "create index c3_index on test_add_index (c3)", done) checkNum := 0 LOOP: @@ -395,35 +373,3 @@ LOOP: tk.MustExec("admin check table test_add_index") tk.MustExec("drop table test_add_index") } - -func sessionExecInGoroutine(c *C, s kv.Storage, sql string, done chan error) { - execMultiSQLInGoroutine(c, s, "test_db", []string{sql}, done) -} - -func execMultiSQLInGoroutine(c *C, s kv.Storage, dbName string, multiSQL []string, done chan error) { - go func() { - se, err := session.CreateSession4Test(s) - if err != nil { - done <- errors.Trace(err) - return - } - defer se.Close() - _, err = se.Execute(context.Background(), "use "+dbName) - if err != nil { - done <- errors.Trace(err) - return - } - for _, sql := range multiSQL { - rs, err := se.Execute(context.Background(), sql) - if err != nil { - done <- errors.Trace(err) - return - } - if rs != nil { - done <- errors.Errorf("RecordSet should be empty.") - return - } - done <- nil - } - }() -} diff --git a/util/testutil/testutil.go b/util/testutil/testutil.go index 003756b0fd405..657a6ab3028f5 100644 --- a/util/testutil/testutil.go +++ b/util/testutil/testutil.go @@ -14,10 +14,14 @@ package testutil import ( + "context" "fmt" "strings" "github.com/pingcap/check" + "github.com/pingcap/errors" + "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" ) @@ -108,3 +112,36 @@ func RowsWithSep(sep string, args ...string) [][]interface{} { } return rows } + +// SessionExecInGoroutine export for testing +func SessionExecInGoroutine(c *check.C, s kv.Storage, sql string, done chan error) { + execMultiSQLInGoroutine(c, s, "test_db", []string{sql}, done) +} + +func execMultiSQLInGoroutine(c *check.C, s kv.Storage, dbName string, multiSQL []string, done chan error) { + go func() { + se, err := session.CreateSession4Test(s) + if err != nil { + done <- errors.Trace(err) + return + } + defer se.Close() + _, err = se.Execute(context.Background(), "use "+dbName) + if err != nil { + done <- errors.Trace(err) + return + } + for _, sql := range multiSQL { + rs, err := se.Execute(context.Background(), sql) + if err != nil { + done <- errors.Trace(err) + return + } + if rs != nil { + done <- errors.Errorf("RecordSet should be empty.") + return + } + done <- nil + } + }() +}