Skip to content

Commit

Permalink
refine test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 committed Dec 21, 2018
1 parent d60f065 commit 50470ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 97 deletions.
3 changes: 2 additions & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down
42 changes: 5 additions & 37 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
64 changes: 5 additions & 59 deletions ddl/failtest/fail_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ package ddl_test
import (
"context"
"fmt"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx/variable"
"math"
"math/rand"
"os"
"sync/atomic"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
gofail "github.com/pingcap/gofail/runtime"
"github.com/pingcap/parser"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/ddl"
"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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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
}
}()
}
37 changes: 37 additions & 0 deletions util/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
}()
}

0 comments on commit 50470ad

Please sign in to comment.