Skip to content

Commit

Permalink
test: improve some tests (#35092)
Browse files Browse the repository at this point in the history
ref #32031
  • Loading branch information
xiongjiwei authored Jun 1, 2022
1 parent 04f9bcf commit 26df62f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 17 deletions.
3 changes: 0 additions & 3 deletions br/pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,6 @@ func WriteBackupDDLJobs(metaWriter *metautil.MetaWriter, store kv.Storage, lastB
return errors.Trace(err)
}
log.Debug("get all jobs", zap.Int("jobs", len(allJobs)))
if err != nil {
return errors.Trace(err)
}
historyJobs, err := ddl.GetAllHistoryDDLJobs(snapMeta)
if err != nil {
return errors.Trace(err)
Expand Down
5 changes: 1 addition & 4 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,7 @@ func checkEqualTable(t *testing.T, t1, t2 *model.TableInfo) {
}

func checkHistoryJobArgs(t *testing.T, ctx sessionctx.Context, id int64, args *historyJobArgs) {
txn, err := ctx.Txn(true)
require.NoError(t, err)
tran := meta.NewMeta(txn)
historyJob, err := tran.GetHistoryDDLJob(id)
historyJob, err := ddl.GetHistoryJobByID(ctx, id)
require.NoError(t, err)
require.Greater(t, historyJob.BinlogInfo.FinishedTS, uint64(0))

Expand Down
6 changes: 3 additions & 3 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func TestAddColumn(t *testing.T) {

d.SetHook(tc)

jobID := testCreateColumn(tk, t, testNewContext(store), tableID, newColName, "", defaultColValue, dom)
jobID := testCreateColumn(tk, t, testkit.NewTestKit(t, store).Session(), tableID, newColName, "", defaultColValue, dom)
testCheckJobDone(t, store, jobID, true)

require.True(t, checkOK)
Expand Down Expand Up @@ -762,7 +762,7 @@ func TestAddColumns(t *testing.T) {

d.SetHook(tc)

jobID := testCreateColumns(tk, t, testNewContext(store), tableID, newColNames, positions, defaultColValue, dom)
jobID := testCreateColumns(tk, t, testkit.NewTestKit(t, store).Session(), tableID, newColNames, positions, defaultColValue, dom)

testCheckJobDone(t, store, jobID, true)
mu.Lock()
Expand Down Expand Up @@ -825,7 +825,7 @@ func TestDropColumnInColumnTest(t *testing.T) {

d.SetHook(tc)

jobID := testDropColumnInternal(tk, t, testNewContext(store), tableID, colName, false, dom)
jobID := testDropColumnInternal(tk, t, testkit.NewTestKit(t, store).Session(), tableID, colName, false, dom)
testCheckJobDone(t, store, jobID, false)
mu.Lock()
hErr := hookErr
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ func TestParallelAlterAndDropSchema(t *testing.T) {
func prepareTestControlParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Domain) (*testkit.TestKit, *testkit.TestKit, chan struct{}, ddl.Callback) {
callback := &ddl.TestDDLCallback{}
times := 0
callback.OnJobUpdatedExported = func(job *model.Job) {
callback.OnJobRunBeforeExported = func(job *model.Job) {
if times != 0 {
return
}
Expand Down
5 changes: 5 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,3 +1180,8 @@ func GetHistoryJobByID(sess sessionctx.Context, id int64) (*model.Job, error) {
func AddHistoryDDLJob(t *meta.Meta, job *model.Job, updateRawArgs bool) error {
return t.AddHistoryDDLJob(job, updateRawArgs)
}

// GetLastHistoryDDLJobsIterator gets latest N history ddl jobs iterator.
func GetLastHistoryDDLJobsIterator(m *meta.Meta) (meta.LastJobIterator, error) {
return m.GetLastHistoryDDLJobsIterator()
}
4 changes: 4 additions & 0 deletions ddl/reorg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -68,6 +69,9 @@ func TestReorgOwner(t *testing.T) {
require.NoError(t, err)

num := 10
ctx = testkit.NewTestKit(t, store).Session()
err = sessiontxn.NewTxn(context.Background(), ctx)
require.NoError(t, err)
for i := 0; i < num; i++ {
_, err := tbl.AddRecord(ctx, types.MakeDatums(i, i, i))
require.NoError(t, err)
Expand Down
9 changes: 8 additions & 1 deletion ddl/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -223,6 +224,8 @@ func TestSchema(t *testing.T) {
testCheckTableState(t, store, dbInfo, tblInfo1, model.StatePublic)
testCheckJobDone(t, store, tJob1.ID, true)
tbl1 := testGetTable(t, domain, tblInfo1.ID)
err = sessiontxn.NewTxn(context.Background(), tk.Session())
require.NoError(t, err)
for i := 1; i <= 100; i++ {
_, err := tbl1.AddRecord(tk.Session(), types.MakeDatums(i, i, i))
require.NoError(t, err)
Expand All @@ -235,6 +238,8 @@ func TestSchema(t *testing.T) {
testCheckTableState(t, store, dbInfo, tblInfo2, model.StatePublic)
testCheckJobDone(t, store, tJob2.ID, true)
tbl2 := testGetTable(t, domain, tblInfo2.ID)
err = sessiontxn.NewTxn(context.Background(), tk2.Session())
require.NoError(t, err)
for i := 1; i <= 1034; i++ {
_, err := tbl2.AddRecord(tk2.Session(), types.MakeDatums(i, i, i))
require.NoError(t, err)
Expand Down Expand Up @@ -283,7 +288,7 @@ func TestSchemaWaitJob(t *testing.T) {
)
err := d2.Start(pools.NewResourcePool(func() (pools.Resource, error) {
return testkit.NewTestKit(t, store).Session(), nil
}, 2, 2, 5))
}, 20, 20, 5))
require.NoError(t, err)
defer func() {
err := d2.Stop()
Expand All @@ -292,6 +297,8 @@ func TestSchemaWaitJob(t *testing.T) {

// d2 must not be owner.
d2.OwnerManager().RetireOwner()
// wait one-second makes d2 stop pick up jobs.
time.Sleep(1 * time.Second)

dbInfo, err := testSchemaInfo(store, "test_schema")
require.NoError(t, err)
Expand Down
9 changes: 7 additions & 2 deletions ddl/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
Expand All @@ -38,8 +39,10 @@ func TestDDLStatsInfo(t *testing.T) {
testCreateSchema(t, testkit.NewTestKit(t, store).Session(), d, dbInfo)
tblInfo, err := testTableInfo(store, "t", 2)
require.NoError(t, err)
testCreateTable(t, testkit.NewTestKit(t, store).Session(), d, dbInfo, tblInfo)
ctx := testkit.NewTestKit(t, store).Session()
testCreateTable(t, ctx, d, dbInfo, tblInfo)
err = sessiontxn.NewTxn(context.Background(), ctx)
require.NoError(t, err)

m := testGetTable(t, domain, tblInfo.ID)
// insert t values (1, 1), (2, 2), (3, 3)
Expand All @@ -49,6 +52,7 @@ func TestDDLStatsInfo(t *testing.T) {
require.NoError(t, err)
_, err = m.AddRecord(ctx, types.MakeDatums(3, 3))
require.NoError(t, err)
ctx.StmtCommit()
require.NoError(t, ctx.CommitTxn(context.Background()))

job := buildCreateIdxJob(dbInfo, tblInfo, true, "idx", "c1")
Expand All @@ -58,6 +62,7 @@ func TestDDLStatsInfo(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/checkBackfillWorkerNum"))
}()

ctx = testkit.NewTestKit(t, store).Session()
done := make(chan error, 1)
go func() {
ctx.SetValue(sessionctx.QueryString, "skip")
Expand All @@ -76,7 +81,7 @@ func TestDDLStatsInfo(t *testing.T) {
varMap, err := d.Stats(nil)
wg.Done()
require.NoError(t, err)
require.Equal(t, varMap[ddlJobReorgHandle], "1")
require.Equal(t, "1", varMap[ddlJobReorgHandle])
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -182,12 +183,15 @@ func TestTable(t *testing.T) {
require.NoError(t, err)
doDDLJobErr(t, dbInfo.ID, newTblInfo.ID, model.ActionCreateTable, []interface{}{newTblInfo}, ctx, d, store)

ctx = testkit.NewTestKit(t, store).Session()
require.NoError(t, sessiontxn.NewTxn(context.Background(), ctx))
count := 2000
tbl := testGetTable(t, domain, tblInfo.ID)
for i := 1; i <= count; i++ {
_, err := tbl.AddRecord(ctx, types.MakeDatums(i, i, i))
require.NoError(t, err)
}
require.NoError(t, ctx.CommitTxn(context.Background()))

jobID := testDropTable(testkit.NewTestKit(t, store), t, dbInfo.Name.L, tblInfo.Name.L, domain)
testCheckJobDone(t, store, jobID, false)
Expand Down Expand Up @@ -314,8 +318,7 @@ func TestRenameTables(t *testing.T) {

job := testRenameTables(t, ctx, d, []int64{dbInfo.ID, dbInfo.ID}, []int64{dbInfo.ID, dbInfo.ID}, []*model.CIStr{&newTblInfos[0].Name, &newTblInfos[1].Name}, []int64{tblInfos[0].ID, tblInfos[1].ID}, []*model.CIStr{&dbInfo.Name, &dbInfo.Name}, []*model.CIStr{&tblInfos[0].Name, &tblInfos[1].Name})

txn, _ := ctx.Txn(true)
historyJob, err := meta.NewMeta(txn).GetHistoryDDLJob(job.ID)
historyJob, err := ddl.GetHistoryJobByID(testkit.NewTestKit(t, store).Session(), job.ID)
require.NoError(t, err)
wantTblInfos := historyJob.BinlogInfo.MultipleTableInfos
require.Equal(t, wantTblInfos[0].Name.L, "tt1")
Expand Down
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (e *DDLJobRetriever) initial(txn kv.Transaction) error {
if err != nil {
return err
}
e.historyJobIter, err = m.GetLastHistoryDDLJobsIterator()
e.historyJobIter, err = ddl.GetLastHistoryDDLJobsIterator(m)
if err != nil {
return err
}
Expand Down

0 comments on commit 26df62f

Please sign in to comment.