Skip to content

Commit

Permalink
ddl: move some tests to package ddl_test (pingcap#34308)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored Apr 28, 2022
1 parent 93859dd commit 83ee6c7
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 339 deletions.
6 changes: 3 additions & 3 deletions ddl/db_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func TestRenameTableWithLocked(t *testing.T) {

func TestRenameTable2(t *testing.T) {
isAlterTable := false
testRenameTable(t, "rename table %s to %s", isAlterTable)
renameTableTest(t, "rename table %s to %s", isAlterTable)
}

func TestAlterTableRenameTable(t *testing.T) {
isAlterTable := true
testRenameTable(t, "alter table %s rename to %s", isAlterTable)
renameTableTest(t, "alter table %s rename to %s", isAlterTable)
}

func testRenameTable(t *testing.T, sql string, isAlterTable bool) {
func renameTableTest(t *testing.T, sql string, isAlterTable bool) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
Expand Down
13 changes: 7 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6282,7 +6282,7 @@ func (d *ddl) LockTables(ctx sessionctx.Context, stmt *ast.LockTablesStmt) error
}

unlockTables := ctx.GetAllTableLocks()
arg := &lockTablesArg{
arg := &LockTablesArg{
LockTables: lockTables,
UnlockTables: unlockTables,
SessionInfo: sessionInfo,
Expand Down Expand Up @@ -6310,7 +6310,7 @@ func (d *ddl) UnlockTables(ctx sessionctx.Context, unlockTables []model.TableLoc
if len(unlockTables) == 0 {
return nil
}
arg := &lockTablesArg{
arg := &LockTablesArg{
UnlockTables: unlockTables,
SessionInfo: model.SessionInfo{
ServerID: d.GetID(),
Expand Down Expand Up @@ -6338,7 +6338,7 @@ func (d *ddl) CleanDeadTableLock(unlockTables []model.TableLockTpInfo, se model.
if len(unlockTables) == 0 {
return nil
}
arg := &lockTablesArg{
arg := &LockTablesArg{
UnlockTables: unlockTables,
SessionInfo: se,
}
Expand Down Expand Up @@ -6405,7 +6405,7 @@ func (d *ddl) CleanupTableLock(ctx sessionctx.Context, tables []*ast.TableName)
return nil
}

arg := &lockTablesArg{
arg := &LockTablesArg{
UnlockTables: cleanupTables,
IsCleanup: true,
}
Expand All @@ -6424,7 +6424,8 @@ func (d *ddl) CleanupTableLock(ctx sessionctx.Context, tables []*ast.TableName)
return errors.Trace(err)
}

type lockTablesArg struct {
// LockTablesArg is the argument for LockTables, export for test.
type LockTablesArg struct {
LockTables []model.TableLockTpInfo
IndexOfLock int
UnlockTables []model.TableLockTpInfo
Expand Down Expand Up @@ -6955,7 +6956,7 @@ func (d *ddl) DropPlacementPolicy(ctx sessionctx.Context, stmt *ast.DropPlacemen
return err
}

if err = checkPlacementPolicyNotInUseFromInfoSchema(is, policy); err != nil {
if err = CheckPlacementPolicyNotInUseFromInfoSchema(is, policy); err != nil {
return err
}

Expand Down
66 changes: 25 additions & 41 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
Expand All @@ -35,6 +37,8 @@ import (
"github.com/stretchr/testify/require"
)

const testLease = 5 * time.Millisecond

type DDLForTest interface {
// SetInterceptor sets the interceptor.
SetInterceptor(h Interceptor)
Expand Down Expand Up @@ -108,10 +112,6 @@ func checkEqualTable(t *testing.T, t1, t2 *model.TableInfo) {
require.Equal(t, t1.AutoIncID, t2.AutoIncID)
}

func checkHistoryJob(t *testing.T, job *model.Job) {
require.Equal(t, job.State, model.JobStateSynced)
}

func checkHistoryJobArgs(t *testing.T, ctx sessionctx.Context, id int64, args *historyJobArgs) {
txn, err := ctx.Txn(true)
require.NoError(t, err)
Expand All @@ -135,19 +135,6 @@ func checkHistoryJobArgs(t *testing.T, ctx sessionctx.Context, id int64, args *h
}
}

func buildCreateIdxJob(dbInfo *model.DBInfo, tblInfo *model.TableInfo, unique bool, indexName string, colName string) *model.Job {
return &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionAddIndex,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{unique, model.NewCIStr(indexName),
[]*ast.IndexPartSpecification{{
Column: &ast.ColumnName{Name: model.NewCIStr(colName)},
Length: types.UnspecifiedLength}}},
}
}

func TestGetIntervalFromPolicy(t *testing.T) {
policy := []time.Duration{
1 * time.Second,
Expand Down Expand Up @@ -516,30 +503,27 @@ func testCheckSchemaState(test *testing.T, d *ddl, dbInfo *model.DBInfo, state m
}
}

func doDDLJobErr(t *testing.T, schemaID, tableID int64, tp model.ActionType, args []interface{}, ctx sessionctx.Context, d *ddl) *model.Job {
job := &model.Job{
SchemaID: schemaID,
TableID: tableID,
Type: tp,
Args: args,
BinlogInfo: &model.HistoryInfo{},
}
// TODO: check error detail
require.Error(t, d.DoDDLJob(ctx, job))
testCheckJobCancelled(t, d.store, job, nil)

return job
}

func testCheckJobCancelled(t *testing.T, store kv.Storage, job *model.Job, state *model.SchemaState) {
require.NoError(t, kv.RunInNewTxn(context.Background(), store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
historyJob, err := m.GetHistoryDDLJob(job.ID)
require.NoError(t, err)
require.True(t, historyJob.IsCancelled() || historyJob.IsRollbackDone(), "history job %s", historyJob)
if state != nil {
require.Equal(t, historyJob.SchemaState, *state)
func testGetTableWithError(d *ddl, schemaID, tableID int64) (table.Table, error) {
var tblInfo *model.TableInfo
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
var err1 error
tblInfo, err1 = t.GetTable(schemaID, tableID)
if err1 != nil {
return errors.Trace(err1)
}
return nil
}))
})
if err != nil {
return nil, errors.Trace(err)
}
if tblInfo == nil {
return nil, errors.New("table not found")
}
alloc := autoid.NewAllocator(d.store, schemaID, tblInfo.ID, false, autoid.RowIDAllocType)
tbl, err := table.TableFromMeta(autoid.NewAllocators(alloc), tblInfo)
if err != nil {
return nil, errors.Trace(err)
}
return tbl, nil
}
97 changes: 25 additions & 72 deletions ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ddl
package ddl_test

import (
"context"
"strings"
"sync"
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)

const testLease = 5 * time.Millisecond

func testCreateForeignKey(t *testing.T, d *ddl, ctx sessionctx.Context, dbInfo *model.DBInfo, tblInfo *model.TableInfo, fkName string, keys []string, refTable string, refKeys []string, onDelete ast.ReferOptionType, onUpdate ast.ReferOptionType) *model.Job {
func testCreateForeignKey(t *testing.T, d ddl.DDL, ctx sessionctx.Context, dbInfo *model.DBInfo, tblInfo *model.TableInfo, fkName string, keys []string, refTable string, refKeys []string, onDelete ast.ReferOptionType, onUpdate ast.ReferOptionType) *model.Job {
FKName := model.NewCIStr(fkName)
Keys := make([]model.CIStr, len(keys))
for i, key := range keys {
Expand Down Expand Up @@ -71,7 +68,7 @@ func testCreateForeignKey(t *testing.T, d *ddl, ctx sessionctx.Context, dbInfo *
return job
}

func testDropForeignKey(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, foreignKeyName string) *model.Job {
func testDropForeignKey(t *testing.T, ctx sessionctx.Context, d ddl.DDL, dbInfo *model.DBInfo, tblInfo *model.TableInfo, foreignKeyName string) *model.Job {
job := &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Expand Down Expand Up @@ -101,53 +98,31 @@ func getForeignKey(t table.Table, name string) *model.FKInfo {
}

func TestForeignKey(t *testing.T) {
store := createMockStore(t)
defer func() {
err := store.Close()
require.NoError(t, err)
}()

d, err := testNewDDLAndStart(
context.Background(),
WithStore(store),
WithLease(testLease),
)
require.NoError(t, err)
defer func() {
err := d.Stop()
require.NoError(t, err)
}()
store, dom, clean := testkit.CreateMockStoreAndDomainWithSchemaLease(t, testLease)
defer clean()

dbInfo, err := testSchemaInfo(d, "test_foreign")
d := dom.DDL()
dbInfo, err := testSchemaInfo(store, "test_foreign")
require.NoError(t, err)
ctx := testNewContext(d)
testCreateSchema(t, ctx, d, dbInfo)
tblInfo, err := testTableInfo(d, "t", 3)
require.NoError(t, err)

err = ctx.NewTxn(context.Background())
testCreateSchema(t, testkit.NewTestKit(t, store).Session(), dom.DDL(), dbInfo)
tblInfo, err := testTableInfo(store, "t", 3)
require.NoError(t, err)

testCreateTable(t, ctx, d, dbInfo, tblInfo)

txn, err := ctx.Txn(true)
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
testCreateTable(t, testkit.NewTestKit(t, store).Session(), d, dbInfo, tblInfo)

// fix data race
var mu sync.Mutex
checkOK := false
var hookErr error
tc := &TestDDLCallback{}
tc.onJobUpdated = func(job *model.Job) {
tc := &ddl.TestDDLCallback{}
tc.OnJobUpdatedExported = func(job *model.Job) {
if job.State != model.JobStateDone {
return
}
mu.Lock()
defer mu.Unlock()
var t table.Table
t, err = testGetTableWithError(d, dbInfo.ID, tblInfo.ID)
t, err = testGetTableWithError(store, dbInfo.ID, tblInfo.ID)
if err != nil {
hookErr = errors.Trace(err)
return
Expand All @@ -163,11 +138,9 @@ func TestForeignKey(t *testing.T) {
defer d.SetHook(originalHook)
d.SetHook(tc)

ctx := testkit.NewTestKit(t, store).Session()
job := testCreateForeignKey(t, d, ctx, dbInfo, tblInfo, "c1_fk", []string{"c1"}, "t2", []string{"c1"}, ast.ReferOptionCascade, ast.ReferOptionSetNull)
testCheckJobDone(t, d, job, true)
txn, err = ctx.Txn(true)
require.NoError(t, err)
err = txn.Commit(context.Background())
testCheckJobDone(t, store, job.ID, true)
require.NoError(t, err)
mu.Lock()
hErr := hookErr
Expand All @@ -182,15 +155,15 @@ func TestForeignKey(t *testing.T) {
checkOK = false
mu.Unlock()
// fix data race pr/#9491
tc2 := &TestDDLCallback{}
tc2.onJobUpdated = func(job *model.Job) {
tc2 := &ddl.TestDDLCallback{}
tc2.OnJobUpdatedExported = func(job *model.Job) {
if job.State != model.JobStateDone {
return
}
mu.Lock()
defer mu.Unlock()
var t table.Table
t, err = testGetTableWithError(d, dbInfo.ID, tblInfo.ID)
t, err = testGetTableWithError(store, dbInfo.ID, tblInfo.ID)
if err != nil {
hookErr = errors.Trace(err)
return
Expand All @@ -205,38 +178,18 @@ func TestForeignKey(t *testing.T) {
d.SetHook(tc2)

job = testDropForeignKey(t, ctx, d, dbInfo, tblInfo, "c1_fk")
testCheckJobDone(t, d, job, false)
testCheckJobDone(t, store, job.ID, false)
mu.Lock()
hErr = hookErr
ok = checkOK
mu.Unlock()
require.NoError(t, hErr)
require.True(t, ok)
d.SetHook(originalHook)

err = ctx.NewTxn(context.Background())
require.NoError(t, err)

job = testDropTable(t, ctx, d, dbInfo, tblInfo)
testCheckJobDone(t, d, job, false)
tk := testkit.NewTestKit(t, store)
jobID := testDropTable(tk, t, dbInfo.Name.L, tblInfo.Name.L, dom)
testCheckJobDone(t, store, jobID, false)

txn, err = ctx.Txn(true)
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
}

func testCheckJobDone(t *testing.T, d *ddl, job *model.Job, isAdd bool) {
require.NoError(t, kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
historyJob, err := m.GetHistoryDDLJob(job.ID)
require.NoError(t, err)
checkHistoryJob(t, historyJob)
if isAdd {
require.Equal(t, historyJob.SchemaState, model.StatePublic)
} else {
require.Equal(t, historyJob.SchemaState, model.StateNone)
}

return nil
}))
}
10 changes: 6 additions & 4 deletions ddl/placement_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,14 @@ func checkPlacementPolicyNotInUse(d *ddlCtx, t *meta.Meta, policy *model.PolicyI
}
is := d.infoCache.GetLatest()
if is.SchemaMetaVersion() == currVer {
return checkPlacementPolicyNotInUseFromInfoSchema(is, policy)
return CheckPlacementPolicyNotInUseFromInfoSchema(is, policy)
}

return checkPlacementPolicyNotInUseFromMeta(t, policy)
return CheckPlacementPolicyNotInUseFromMeta(t, policy)
}

func checkPlacementPolicyNotInUseFromInfoSchema(is infoschema.InfoSchema, policy *model.PolicyInfo) error {
// CheckPlacementPolicyNotInUseFromInfoSchema export for test.
func CheckPlacementPolicyNotInUseFromInfoSchema(is infoschema.InfoSchema, policy *model.PolicyInfo) error {
for _, dbInfo := range is.AllSchemas() {
if ref := dbInfo.PlacementPolicyRef; ref != nil && ref.ID == policy.ID {
return dbterror.ErrPlacementPolicyInUse.GenWithStackByArgs(policy.Name)
Expand Down Expand Up @@ -375,7 +376,8 @@ func getPlacementPolicyDependedObjectsIDs(t *meta.Meta, policy *model.PolicyInfo
return dbIDs, partIDs, tblInfos, nil
}

func checkPlacementPolicyNotInUseFromMeta(t *meta.Meta, policy *model.PolicyInfo) error {
// CheckPlacementPolicyNotInUseFromMeta export for test.
func CheckPlacementPolicyNotInUseFromMeta(t *meta.Meta, policy *model.PolicyInfo) error {
schemas, err := t.ListDatabases()
if err != nil {
return err
Expand Down
Loading

0 comments on commit 83ee6c7

Please sign in to comment.