Skip to content

Commit

Permalink
*: move some tests and make some changes (#35450)
Browse files Browse the repository at this point in the history
ref #32031
  • Loading branch information
xiongjiwei authored Jun 16, 2022
1 parent ed11cf2 commit bd848d4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions ddl/ddl_api_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
22 changes: 0 additions & 22 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 13 additions & 13 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion ddl/reorg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion ddl/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions owner/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions util/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Context struct {
cancel context.CancelFunc
sm util.SessionManager
pcache *kvcache.SimpleLRUCache
level kvrpcpb.DiskFullOpt
}

type wrapTxn struct {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit bd848d4

Please sign in to comment.