Skip to content

Commit

Permalink
ddl: migrate test-infra to testify for add index tests (#32864)
Browse files Browse the repository at this point in the history
close #32863
  • Loading branch information
tisonkun authored Mar 7, 2022
1 parent 164bee7 commit c9001b3
Show file tree
Hide file tree
Showing 11 changed files with 999 additions and 1,016 deletions.
881 changes: 881 additions & 0 deletions ddl/add_index_test.go

Large diffs are not rendered by default.

85 changes: 34 additions & 51 deletions ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ func TestColumnTypeChangeStateBetweenInteger(t *testing.T) {
require.Equal(t, 2, len(tbl.Cols()))
require.NotNil(t, tk.GetModifyColumn("test", "t", "c2", false))

originalHook := dom.DDL().GetHook()
defer dom.DDL().(ddl.DDLForTest).SetHook(originalHook)

hook := &ddl.TestDDLCallback{Do: dom}
var checkErr error
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand Down Expand Up @@ -160,12 +157,12 @@ func TestColumnTypeChangeStateBetweenInteger(t *testing.T) {
}
}
}
dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)
// Alter sql will modify column c2 to tinyint not null.
SQL := "alter table t modify column c2 tinyint not null"
tk.MustExec(SQL)
// Assert the checkErr in the job of every state.
require.Nil(t, checkErr)
require.NoError(t, checkErr)

// Check the col meta after the column type change.
tbl = tk.GetTableByName("test", "t")
Expand Down Expand Up @@ -194,42 +191,35 @@ func TestRollbackColumnTypeChangeBetweenInteger(t *testing.T) {
require.Equal(t, 2, len(tbl.Cols()))
require.NotNil(t, tk.GetModifyColumn("test", "t", "c2", false))

originalHook := dom.DDL().GetHook()
defer dom.DDL().(ddl.DDLForTest).SetHook(originalHook)

hook := &ddl.TestDDLCallback{Do: dom}
// Mock roll back at model.StateNone.
customizeHookRollbackAtState(hook, tbl, model.StateNone)
dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)
// Alter sql will modify column c2 to bigint not null.
SQL := "alter table t modify column c2 int not null"
_, err := tk.Exec(SQL)
require.Error(t, err)
require.Equal(t, "[ddl:1]MockRollingBackInCallBack-queueing", err.Error())
err := tk.ExecToErr(SQL)
require.EqualError(t, err, "[ddl:1]MockRollingBackInCallBack-queueing")
assertRollBackedColUnchanged(t, tk)

// Mock roll back at model.StateDeleteOnly.
customizeHookRollbackAtState(hook, tbl, model.StateDeleteOnly)
dom.DDL().(ddl.DDLForTest).SetHook(hook)
_, err = tk.Exec(SQL)
require.Error(t, err)
require.Equal(t, "[ddl:1]MockRollingBackInCallBack-delete only", err.Error())
dom.DDL().SetHook(hook)
err = tk.ExecToErr(SQL)
require.EqualError(t, err, "[ddl:1]MockRollingBackInCallBack-delete only")
assertRollBackedColUnchanged(t, tk)

// Mock roll back at model.StateWriteOnly.
customizeHookRollbackAtState(hook, tbl, model.StateWriteOnly)
dom.DDL().(ddl.DDLForTest).SetHook(hook)
_, err = tk.Exec(SQL)
require.Error(t, err)
require.Equal(t, "[ddl:1]MockRollingBackInCallBack-write only", err.Error())
dom.DDL().SetHook(hook)
err = tk.ExecToErr(SQL)
require.EqualError(t, err, "[ddl:1]MockRollingBackInCallBack-write only")
assertRollBackedColUnchanged(t, tk)

// Mock roll back at model.StateWriteReorg.
customizeHookRollbackAtState(hook, tbl, model.StateWriteReorganization)
dom.DDL().(ddl.DDLForTest).SetHook(hook)
_, err = tk.Exec(SQL)
require.Error(t, err)
require.Equal(t, "[ddl:1]MockRollingBackInCallBack-write reorganization", err.Error())
dom.DDL().SetHook(hook)
err = tk.ExecToErr(SQL)
require.EqualError(t, err, "[ddl:1]MockRollingBackInCallBack-write reorganization")
assertRollBackedColUnchanged(t, tk)
}

Expand Down Expand Up @@ -948,12 +938,9 @@ func TestColumnTypeChangeIgnoreDisplayLength(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

originalHook := dom.DDL().GetHook()
defer dom.DDL().(ddl.DDLForTest).SetHook(originalHook)

var assertResult bool
assertHasAlterWriteReorg := func(tbl table.Table) {
// Restore the assert result to false.
// Restore assertResult to false.
assertResult = false
hook := &ddl.TestDDLCallback{Do: dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand All @@ -964,7 +951,7 @@ func TestColumnTypeChangeIgnoreDisplayLength(t *testing.T) {
assertResult = true
}
}
dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)
}

// Change int to tinyint.
Expand All @@ -974,7 +961,7 @@ func TestColumnTypeChangeIgnoreDisplayLength(t *testing.T) {
tbl := tk.GetTableByName("test", "t")
assertHasAlterWriteReorg(tbl)
tk.MustExec("alter table t modify column a tinyint(3)")
require.Equal(t, true, assertResult)
require.True(t, assertResult)

// Change tinyint to tinyint
// Although display length is decreased, default flen is the same, reorg is not needed.
Expand All @@ -983,7 +970,7 @@ func TestColumnTypeChangeIgnoreDisplayLength(t *testing.T) {
tbl = tk.GetTableByName("test", "t")
assertHasAlterWriteReorg(tbl)
tk.MustExec("alter table t modify column a tinyint(1)")
require.Equal(t, false, assertResult)
require.False(t, assertResult)
tk.MustExec("drop table if exists t")
}

Expand Down Expand Up @@ -1678,10 +1665,10 @@ func TestChangingColOriginDefaultValue(t *testing.T) {
i++
}
}
dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)
tk.MustExec("alter table t modify column b tinyint NOT NULL")
dom.DDL().(ddl.DDLForTest).SetHook(originalHook)
require.Nil(t, checkErr)
dom.DDL().SetHook(originalHook)
require.NoError(t, checkErr)
// Since getReorgInfo will stagnate StateWriteReorganization for a ddl round, so insert should exec 3 times.
tk.MustQuery("select * from t order by a").Check(testkit.Rows("1 -1", "2 -2", "3 3", "4 4", "5 5"))
tk.MustExec("drop table if exists t")
Expand Down Expand Up @@ -1764,10 +1751,10 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastSucc(t *testing.T) {
i++
}

dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)
tk.MustExec("alter table t modify column c date NOT NULL")
dom.DDL().(ddl.DDLForTest).SetHook(originalHook)
require.Nil(t, checkErr)
dom.DDL().SetHook(originalHook)
require.NoError(t, checkErr)
// Since getReorgInfo will stagnate StateWriteReorganization for a ddl round, so insert should exec 3 times.
tk.MustQuery("select * from t order by a").Check(
testkit.Rows("1 -1 1971-06-09", "2 -2 1971-06-09", "5 5 2021-06-06", "6 6 2021-06-06", "7 7 2021-06-06"))
Expand Down Expand Up @@ -1824,10 +1811,10 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastFail(t *testing.T) {
}
}

dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)
tk.MustExec("alter table t modify column x DATETIME NULL DEFAULT '3771-02-28 13:00:11' AFTER b;")
dom.DDL().(ddl.DDLForTest).SetHook(originalHook)
require.Nil(t, checkErr)
dom.DDL().SetHook(originalHook)
require.NoError(t, checkErr)
tk.MustQuery("select * from t order by a").Check(testkit.Rows())
tk.MustExec("drop table if exists t")
}
Expand Down Expand Up @@ -1925,9 +1912,6 @@ func TestDDLExitWhenCancelMeetPanic(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockExceedErrorLimit"))
}()

originalHook := dom.DDL().GetHook()
defer dom.DDL().(ddl.DDLForTest).SetHook(originalHook)

hook := &ddl.TestDDLCallback{Do: dom}
var jobID int64
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand All @@ -1938,21 +1922,20 @@ func TestDDLExitWhenCancelMeetPanic(t *testing.T) {
jobID = job.ID
}
}
dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)

// when it panics in write-reorg state, the job will be pulled up as a cancelling job. Since drop-index with
// write-reorg can't be cancelled, so it will be converted to running state and try again (dead loop).
_, err := tk.Exec("alter table t drop index b")
require.Error(t, err)
require.Equal(t, "[ddl:-1]panic in handling DDL logic and error count beyond the limitation 3, cancelled", err.Error())
require.Equal(t, true, jobID > 0)
err := tk.ExecToErr("alter table t drop index b")
require.EqualError(t, err, "[ddl:-1]panic in handling DDL logic and error count beyond the limitation 3, cancelled")
require.Less(t, int64(0), jobID)

// Verification of the history job state.
var job *model.Job
err = kv.RunInNewTxn(context.Background(), store, false, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
m := meta.NewMeta(txn)
var err1 error
job, err1 = t.GetHistoryDDLJob(jobID)
job, err1 = m.GetHistoryDDLJob(jobID)
return errors2.Trace(err1)
})
require.NoError(t, err)
Expand Down Expand Up @@ -2025,7 +2008,7 @@ func TestCancelCTCInReorgStateWillCauseGoroutineLeak(t *testing.T) {
jobID = job.ID
}
}
dom.DDL().(ddl.DDLForTest).SetHook(hook)
dom.DDL().SetHook(hook)

tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")
Expand Down
42 changes: 21 additions & 21 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func (s *stateChangeSuite) TestShowCreateTable() {
}
d := s.dom.DDL()
originalCallback := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalCallback)
d.(ddl.DDLForTest).SetHook(callback)
defer d.SetHook(originalCallback)
d.SetHook(callback)
for _, tc := range testCases {
tk.MustExec(tc.sql)
s.Require().NoError(checkErr)
Expand Down Expand Up @@ -200,7 +200,7 @@ func (s *stateChangeSuite) TestDropNotNullColumn() {
}
}

d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)
tk.MustExec("alter table t drop column a")
s.Require().NoError(checkErr)
sqlNum++
Expand All @@ -212,7 +212,7 @@ func (s *stateChangeSuite) TestDropNotNullColumn() {
sqlNum++
tk.MustExec("alter table t3 drop column d")
s.Require().NoError(checkErr)
d.(ddl.DDLForTest).SetHook(originalCallback)
d.SetHook(originalCallback)
tk.MustExec("drop table t, t1, t2, t3")
}

Expand Down Expand Up @@ -315,8 +315,8 @@ func (s *stateChangeSuite) test(alterTableSQL string, testInfo *testExecInfo) {
}
d := s.dom.DDL()
originalCallback := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalCallback)
d.(ddl.DDLForTest).SetHook(callback)
defer d.SetHook(originalCallback)
d.SetHook(callback)
s.tk.MustExec(alterTableSQL)
s.Require().NoError(testInfo.compileSQL(4))
s.Require().NoError(testInfo.execSQL(4))
Expand Down Expand Up @@ -847,10 +847,10 @@ func (s *stateChangeSuite) runTestInSchemaState(
}
d := s.dom.DDL()
originalCallback := d.GetHook()
d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)
s.tk.MustExec(alterTableSQL)
s.Require().NoError(checkErr)
d.(ddl.DDLForTest).SetHook(originalCallback)
d.SetHook(originalCallback)

if expectQuery != nil {
tk := testkit.NewTestKit(s.T(), s.store)
Expand Down Expand Up @@ -897,7 +897,7 @@ func (s *stateChangeSuite) TestShowIndex() {

d := s.dom.DDL()
originalCallback := d.GetHook()
d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)
alterTableSQL := `alter table t add index c2(c2)`
s.tk.MustExec(alterTableSQL)
s.Require().NoError(checkErr)
Expand All @@ -906,7 +906,7 @@ func (s *stateChangeSuite) TestShowIndex() {
"t 0 PRIMARY 1 c1 A 0 <nil> <nil> BTREE YES <nil> NO",
"t 1 c2 1 c2 A 0 <nil> <nil> YES BTREE YES <nil> NO",
))
d.(ddl.DDLForTest).SetHook(originalCallback)
d.SetHook(originalCallback)

s.tk.MustExec(`create table tr(
id int, name varchar(50),
Expand Down Expand Up @@ -1215,7 +1215,7 @@ func (s *stateChangeSuite) prepareTestControlParallelExecSQL() (*testkit.TestKit
}
d := s.dom.DDL()
originalCallback := d.GetHook()
d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)

tk1 := testkit.NewTestKit(s.T(), s.store)
tk1.MustExec("use test_db_state")
Expand Down Expand Up @@ -1266,7 +1266,7 @@ func (s *stateChangeSuite) testControlParallelExecSQL(preSQL, sql1, sql2 string,
);`)

tk1, tk2, ch, originalCallback := s.prepareTestControlParallelExecSQL()
defer s.dom.DDL().(ddl.DDLForTest).SetHook(originalCallback)
defer s.dom.DDL().SetHook(originalCallback)

var err1 error
var err2 error
Expand Down Expand Up @@ -1303,7 +1303,7 @@ func (s *stateChangeSuite) TestParallelUpdateTableReplica() {
s.tk.MustExec("alter table t1 set tiflash replica 3 location labels 'a','b';")

tk1, tk2, ch, originalCallback := s.prepareTestControlParallelExecSQL()
defer s.dom.DDL().(ddl.DDLForTest).SetHook(originalCallback)
defer s.dom.DDL().SetHook(originalCallback)

t1 := tk1.GetTableByName("test_db_state", "t1")

Expand Down Expand Up @@ -1345,8 +1345,8 @@ func (s *stateChangeSuite) testParallelExecSQL(sql string) {

d := s.dom.DDL()
originalCallback := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalCallback)
d.(ddl.DDLForTest).SetHook(callback)
defer d.SetHook(originalCallback)
d.SetHook(callback)
wg.Run(func() {
err2 = tk1.ExecToErr(sql)
})
Expand Down Expand Up @@ -1672,7 +1672,7 @@ func (s *stateChangeSuite) TestCreateExpressionIndex() {
var checkErr error
d := s.dom.DDL()
originalCallback := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalCallback)
defer d.SetHook(originalCallback)
callback := &ddl.TestDDLCallback{}
callback.OnJobUpdatedExported = func(job *model.Job) {
if checkErr != nil {
Expand Down Expand Up @@ -1713,7 +1713,7 @@ func (s *stateChangeSuite) TestCreateExpressionIndex() {
}
}

d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)
tk.MustExec("alter table t add index idx((b+1))")
s.Require().NoError(checkErr)
tk.MustExec("admin check table t")
Expand All @@ -1738,7 +1738,7 @@ func (s *stateChangeSuite) TestCreateUniqueExpressionIndex() {
var checkErr error
d := s.dom.DDL()
originalCallback := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalCallback)
defer d.SetHook(originalCallback)
callback := &ddl.TestDDLCallback{}
callback.OnJobUpdatedExported = func(job *model.Job) {
if checkErr != nil {
Expand Down Expand Up @@ -1825,7 +1825,7 @@ func (s *stateChangeSuite) TestCreateUniqueExpressionIndex() {
}
}

d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)
tk.MustExec("alter table t add unique index idx((a*b+1))")
s.Require().NoError(checkErr)
tk.MustExec("admin check table t")
Expand All @@ -1849,7 +1849,7 @@ func (s *stateChangeSuite) TestDropExpressionIndex() {
var checkErr error
d := s.dom.DDL()
originalCallback := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalCallback)
defer d.SetHook(originalCallback)
callback := &ddl.TestDDLCallback{}
callback.OnJobUpdatedExported = func(job *model.Job) {
if checkErr != nil {
Expand Down Expand Up @@ -1885,7 +1885,7 @@ func (s *stateChangeSuite) TestDropExpressionIndex() {
}
}

d.(ddl.DDLForTest).SetHook(callback)
d.SetHook(callback)
tk.MustExec("alter table t drop index idx")
s.Require().NoError(checkErr)
tk.MustExec("admin check table t")
Expand Down
4 changes: 2 additions & 2 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2825,9 +2825,9 @@ func testPartitionCancelAddIndex(t *testing.T, store kv.Storage, d ddl.DDL, leas
defer tk.MustExec(fmt.Sprintf("set @@global.tidb_ddl_reorg_batch_size = %v", originBatchSize.Rows()[0][0]))
hook.OnJobUpdatedExported, c3IdxInfo, checkErr = backgroundExecOnJobUpdatedExportedT(tk, store, hook, idxName)
originHook := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originHook)
defer d.SetHook(originHook)
jobIDExt := wrapJobIDExtCallback(hook)
d.(ddl.DDLForTest).SetHook(jobIDExt)
d.SetHook(jobIDExt)
done := make(chan error, 1)
go backgroundExecT(store, addIdxSQL, done)

Expand Down
Loading

0 comments on commit c9001b3

Please sign in to comment.