From bd848d4509d32124809fca133095c3875f643a63 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Thu, 16 Jun 2022 21:18:34 +0800 Subject: [PATCH] *: move some tests and make some changes (#35450) ref pingcap/tidb#32031 --- ddl/column_test.go | 2 +- ddl/ddl_api_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++ ddl/ddl_test.go | 22 --------------------- ddl/ddl_worker_test.go | 26 ++++++++++++------------- ddl/reorg_test.go | 2 +- ddl/schema_test.go | 3 ++- domain/domain.go | 2 +- owner/manager_test.go | 4 ---- util/mock/context.go | 6 ++++-- 9 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 ddl/ddl_api_test.go diff --git a/ddl/column_test.go b/ddl/column_test.go index fe6fd52ee5558..b29ea2d32101e 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -890,7 +890,7 @@ func TestDropColumns(t *testing.T) { d.SetHook(tc) - jobID := testDropColumns(tk, t, testNewContext(store), tableID, colNames, false, dom) + jobID := testDropColumns(tk, t, testkit.NewTestKit(t, store).Session(), tableID, colNames, false, dom) testCheckJobDone(t, store, jobID, false) mu.Lock() hErr := hookErr diff --git a/ddl/ddl_api_test.go b/ddl/ddl_api_test.go new file mode 100644 index 0000000000000..e915dd33d7678 --- /dev/null +++ b/ddl/ddl_api_test.go @@ -0,0 +1,44 @@ +// Copyright 2022 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ddl_test + +import ( + "testing" + + "github.com/pingcap/tidb/parser/model" + "github.com/stretchr/testify/require" +) + +func TestIsJobRollbackable(t *testing.T) { + cases := []struct { + tp model.ActionType + state model.SchemaState + result bool + }{ + {model.ActionDropIndex, model.StateNone, true}, + {model.ActionDropIndex, model.StateDeleteOnly, false}, + {model.ActionDropSchema, model.StateDeleteOnly, false}, + {model.ActionDropColumn, model.StateDeleteOnly, false}, + {model.ActionDropColumns, model.StateDeleteOnly, false}, + {model.ActionDropIndexes, model.StateDeleteOnly, false}, + } + job := &model.Job{} + for _, ca := range cases { + job.Type = ca.tp + job.SchemaState = ca.state + re := job.IsRollbackable() + require.Equal(t, ca.result, re) + } +} diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index fa21ba73fa988..105da6b6a71ca 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -985,28 +985,6 @@ func TestGetHistoryDDLJobs(t *testing.T) { require.NoError(t, err) } -func TestIsJobRollbackable(t *testing.T) { - cases := []struct { - tp model.ActionType - state model.SchemaState - result bool - }{ - {model.ActionDropIndex, model.StateNone, true}, - {model.ActionDropIndex, model.StateDeleteOnly, false}, - {model.ActionDropSchema, model.StateDeleteOnly, false}, - {model.ActionDropColumn, model.StateDeleteOnly, false}, - {model.ActionDropColumns, model.StateDeleteOnly, false}, - {model.ActionDropIndexes, model.StateDeleteOnly, false}, - } - job := &model.Job{} - for _, ca := range cases { - job.Type = ca.tp - job.SchemaState = ca.state - re := job.IsRollbackable() - require.Equal(t, ca.result, re) - } -} - func TestError(t *testing.T) { kvErrs := []*terror.Error{ dbterror.ErrDDLJobNotFound, diff --git a/ddl/ddl_worker_test.go b/ddl/ddl_worker_test.go index f39aa90f1a881..082fa3fe6aa7d 100644 --- a/ddl/ddl_worker_test.go +++ b/ddl/ddl_worker_test.go @@ -1,16 +1,16 @@ -//// Copyright 2015 PingCAP, Inc. -//// -//// Licensed under the Apache License, Version 2.0 (the "License"); -//// you may not use this file except in compliance with the License. -//// You may obtain a copy of the License at -//// -//// http://www.apache.org/licenses/LICENSE-2.0 -//// -//// Unless required by applicable law or agreed to in writing, software -//// distributed under the License is distributed on an "AS IS" BASIS, -//// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -//// See the License for the specific language governing permissions and -//// limitations under the License. +// Copyright 2015 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. // package ddl_test diff --git a/ddl/reorg_test.go b/ddl/reorg_test.go index b3e373cb75fd7..e365a0e2d1a7e 100644 --- a/ddl/reorg_test.go +++ b/ddl/reorg_test.go @@ -50,7 +50,7 @@ func TestReorgOwner(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() { diff --git a/ddl/schema_test.go b/ddl/schema_test.go index befee5bdeb44b..58e7996401f0b 100644 --- a/ddl/schema_test.go +++ b/ddl/schema_test.go @@ -312,7 +312,7 @@ func TestSchemaWaitJob(t *testing.T) { genIDs, err := genGlobalIDs(store, 1) require.NoError(t, err) schemaID := genIDs[0] - doDDLJobErr(t, schemaID, 0, model.ActionCreateSchema, []interface{}{dbInfo}, se, d2, store) + doDDLJobErr(t, schemaID, 0, model.ActionCreateSchema, []interface{}{dbInfo}, testkit.NewTestKit(t, store).Session(), d2, store) } func doDDLJobErr(t *testing.T, schemaID, tableID int64, tp model.ActionType, args []interface{}, ctx sessionctx.Context, d ddl.DDL, store kv.Storage) *model.Job { @@ -324,6 +324,7 @@ func doDDLJobErr(t *testing.T, schemaID, tableID int64, tp model.ActionType, arg BinlogInfo: &model.HistoryInfo{}, } // TODO: check error detail + ctx.SetValue(sessionctx.QueryString, "skip") require.Error(t, d.DoDDLJob(ctx, job)) testCheckJobCancelled(t, store, job, nil) diff --git a/domain/domain.go b/domain/domain.go index 5c7064920a3a5..e9eed74e24da6 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -794,7 +794,7 @@ func (do *Domain) Init(ddlLease time.Duration, sysExecutorFactory func(*Domain) sysFac := func() (pools.Resource, error) { return sysExecutorFactory(do) } - sysCtxPool := pools.NewResourcePool(sysFac, 2, 2, resourceIdleTimeout) + sysCtxPool := pools.NewResourcePool(sysFac, 128, 128, resourceIdleTimeout) ctx, cancelFunc := context.WithCancel(context.Background()) do.cancel = cancelFunc var callback ddl.Callback diff --git a/owner/manager_test.go b/owner/manager_test.go index ae080fbe85b63..3668c4d060ad2 100644 --- a/owner/manager_test.go +++ b/owner/manager_test.go @@ -65,10 +65,6 @@ func TestSingle(t *testing.T) { WithInfoCache(ic), ) require.NoError(t, d.OwnerManager().CampaignOwner()) - defer func() { - _ = d.Stop() - }() - isOwner := checkOwner(d, true) require.True(t, isOwner) diff --git a/util/mock/context.go b/util/mock/context.go index 5877b12fa6225..af2070f990b21 100644 --- a/util/mock/context.go +++ b/util/mock/context.go @@ -55,6 +55,7 @@ type Context struct { cancel context.CancelFunc sm util.SessionManager pcache *kvcache.SimpleLRUCache + level kvrpcpb.DiskFullOpt } type wrapTxn struct { @@ -91,12 +92,12 @@ func (c *Context) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex // SetDiskFullOpt sets allowed options of current operation in each TiKV disk usage level. func (c *Context) SetDiskFullOpt(level kvrpcpb.DiskFullOpt) { - c.txn.Transaction.SetDiskFullOpt(kvrpcpb.DiskFullOpt_AllowedOnAlmostFull) + c.level = level } // ClearDiskFullOpt clears allowed options of current operation in each TiKV disk usage level. func (c *Context) ClearDiskFullOpt() { - c.txn.Transaction.ClearDiskFullOpt() + c.level = kvrpcpb.DiskFullOpt_NotAllowedOnFull } // ExecuteInternal implements sqlexec.SQLExecutor ExecuteInternal interface. @@ -260,6 +261,7 @@ func (c *Context) RollbackTxn(ctx context.Context) { // CommitTxn indicates an expected call of CommitTxn. func (c *Context) CommitTxn(ctx context.Context) error { defer c.sessionVars.SetInTxn(false) + c.txn.SetDiskFullOpt(c.level) if c.txn.Valid() { return c.txn.Commit(ctx) }