Skip to content

Commit

Permalink
ddl: improve test to avoid cycle import (#32058)
Browse files Browse the repository at this point in the history
ref #31716
  • Loading branch information
hawkingrei authored Mar 14, 2022
1 parent 0cd9b24 commit ffaeb3e
Show file tree
Hide file tree
Showing 33 changed files with 432 additions and 382 deletions.
36 changes: 32 additions & 4 deletions ddl/attributes_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,46 @@ import (
"fmt"
"math"
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/store/gcworker"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/gcutil"
"github.com/stretchr/testify/require"
)

// MockGC is used to make GC work in the test environment.
func MockGC(tk *testkit.TestKit) (string, string, string, func()) {
originGC := util.IsEmulatorGCEnable()
resetGC := func() {
if originGC {
util.EmulatorGCEnable()
} else {
util.EmulatorGCDisable()
}
}

// disable emulator GC.
// Otherwise emulator GC will delete table record as soon as possible after execute drop table ddl.
util.EmulatorGCDisable()
gcTimeFormat := "20060102-15:04:05 -0700 MST"
timeBeforeDrop := time.Now().Add(0 - 48*60*60*time.Second).Format(gcTimeFormat)
timeAfterDrop := time.Now().Add(48 * 60 * 60 * time.Second).Format(gcTimeFormat)
safePointSQL := `INSERT HIGH_PRIORITY INTO mysql.tidb VALUES ('tikv_gc_safe_point', '%[1]s', '')
ON DUPLICATE KEY
UPDATE variable_value = '%[1]s'`
// clear GC variables first.
tk.MustExec("delete from mysql.tidb where variable_name in ( 'tikv_gc_safe_point','tikv_gc_enable' )")
return timeBeforeDrop, timeAfterDrop, safePointSQL, resetGC
}

func TestAlterTableAttributes(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table alter_t (c int);`)
Expand Down Expand Up @@ -179,7 +207,7 @@ PARTITION BY RANGE (c) (
PARTITION p1 VALUES LESS THAN (11)
);`)

timeBeforeDrop, _, safePointSQL, resetGC := testkit.MockGC(tk)
timeBeforeDrop, _, safePointSQL, resetGC := MockGC(tk)
defer resetGC()

// Set GC safe point
Expand Down Expand Up @@ -222,7 +250,7 @@ PARTITION BY RANGE (c) (
PARTITION p1 VALUES LESS THAN (11)
);`)

timeBeforeDrop, _, safePointSQL, resetGC := testkit.MockGC(tk)
timeBeforeDrop, _, safePointSQL, resetGC := MockGC(tk)
defer resetGC()

// Set GC safe point
Expand Down Expand Up @@ -285,7 +313,7 @@ PARTITION BY RANGE (c) (
failpoint.Disable("github.com/pingcap/tidb/store/gcworker/ignoreDeleteRangeFailed")
}()

timeBeforeDrop, _, safePointSQL, resetGC := testkit.MockGC(tk)
timeBeforeDrop, _, safePointSQL, resetGC := MockGC(tk)
defer resetGC()

// Set GC safe point
Expand Down Expand Up @@ -339,7 +367,7 @@ PARTITION BY RANGE (c) (
failpoint.Disable("github.com/pingcap/tidb/store/gcworker/ignoreDeleteRangeFailed")
}()

timeBeforeDrop, _, safePointSQL, resetGC := testkit.MockGC(tk)
timeBeforeDrop, _, safePointSQL, resetGC := MockGC(tk)
defer resetGC()

// Set GC safe point
Expand Down
9 changes: 5 additions & 4 deletions ddl/column_modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/external"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/admin"
"github.com/pingcap/tidb/util/mock"
Expand Down Expand Up @@ -120,7 +121,7 @@ AddLoop:
})
}

tbl := tk.GetTableByName("test", "t2")
tbl := external.GetTableByName(t, tk, "test", "t2")
i := 0
j := 0
require.NoError(t, tk.Session().NewTxn(context.Background()))
Expand Down Expand Up @@ -388,7 +389,7 @@ func TestRenameColumn(t *testing.T) {
tk.MustExec("use test")

assertColNames := func(tableName string, colNames ...string) {
cols := tk.GetTableByName("test", tableName).Cols()
cols := external.GetTableByName(t, tk, "test", tableName).Cols()
require.Equal(t, len(colNames), len(cols))
for i := range cols {
require.Equal(t, strings.ToLower(colNames[i]), cols[i].Name.L)
Expand Down Expand Up @@ -506,7 +507,7 @@ func TestCancelDropColumn(t *testing.T) {
err := tk.ExecToErr("alter table test_drop_column drop column c3")
var col1 *table.Column
var idx1 table.Index
tbl := tk.GetTableByName("test", "test_drop_column")
tbl := external.GetTableByName(t, tk, "test", "test_drop_column")
for _, col := range tbl.Cols() {
if strings.EqualFold(col.Name.L, "c3") {
col1 = col
Expand Down Expand Up @@ -607,7 +608,7 @@ func TestCancelDropColumns(t *testing.T) {
c3IdxID = testGetIndexID(t, tk.Session(), "test", "test_drop_column", "idx_c3")
}
err := tk.ExecToErr("alter table test_drop_column drop column c3, drop column c4")
tbl := tk.GetTableByName("test", "test_drop_column")
tbl := external.GetTableByName(t, tk, "test", "test_drop_column")
col3 := table.FindCol(tbl.Cols(), "c3")
col4 := table.FindCol(tbl.Cols(), "c4")
var idx3 table.Index
Expand Down
Loading

0 comments on commit ffaeb3e

Please sign in to comment.