Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl/table_split_test.go: migrate test-infra to testify #29960

Merged
merged 8 commits into from
Nov 23, 2021
45 changes: 23 additions & 22 deletions ddl/table_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,36 @@ package ddl_test
import (
"context"
"sync/atomic"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/testkit"

"github.com/tikv/client-go/v2/tikv"
)

type testDDLTableSplitSuite struct{}
"github.com/stretchr/testify/require"
)

var _ = Suite(&testDDLTableSplitSuite{})
func TestTableSplit(t *testing.T) {
t.Parallel()

func (s *testDDLTableSplitSuite) TestTableSplit(c *C) {
store, err := mockstore.NewMockStore()
c.Assert(err, IsNil)
require.NoError(t, err)
defer func() {
err := store.Close()
c.Assert(err, IsNil)
require.NoError(t, err)
}()
session.SetSchemaLease(100 * time.Millisecond)
session.DisableStats4Test()
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 1)
dom, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
require.NoError(t, err)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// Synced split table region.
tk.MustExec("set global tidb_scatter_region = 1")
Expand All @@ -56,32 +57,32 @@ func (s *testDDLTableSplitSuite) TestTableSplit(c *C) {
defer dom.Close()
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 0)
infoSchema := dom.InfoSchema()
c.Assert(infoSchema, NotNil)
t, err := infoSchema.TableByName(model.NewCIStr("mysql"), model.NewCIStr("tidb"))
c.Assert(err, IsNil)
checkRegionStartWithTableID(c, t.Meta().ID, store.(kvStore))
require.NotNil(t, infoSchema, nil)
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
tbl, err := infoSchema.TableByName(model.NewCIStr("mysql"), model.NewCIStr("tidb"))
require.NoError(t, err)
checkRegionStartWithTableID(t, tbl.Meta().ID, store.(kvStore))

t, err = infoSchema.TableByName(model.NewCIStr("test"), model.NewCIStr("t_part"))
c.Assert(err, IsNil)
pi := t.Meta().GetPartitionInfo()
c.Assert(pi, NotNil)
tbl, err = infoSchema.TableByName(model.NewCIStr("test"), model.NewCIStr("t_part"))
require.NoError(t, err)
pi := tbl.Meta().GetPartitionInfo()
require.NotNil(t, pi, nil)
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
for _, def := range pi.Definitions {
checkRegionStartWithTableID(c, def.ID, store.(kvStore))
checkRegionStartWithTableID(t, def.ID, store.(kvStore))
}
}

type kvStore interface {
GetRegionCache() *tikv.RegionCache
}

func checkRegionStartWithTableID(c *C, id int64, store kvStore) {
func checkRegionStartWithTableID(t *testing.T, id int64, store kvStore) {
regionStartKey := tablecodec.EncodeTablePrefix(id)
var loc *tikv.KeyLocation
var err error
cache := store.GetRegionCache()
loc, err = cache.LocateKey(tikv.NewBackoffer(context.Background(), 5000), regionStartKey)
c.Assert(err, IsNil)
require.NoError(t, err)
// Region cache may be out of date, so we need to drop this expired region and load it again.
cache.InvalidateCachedRegion(loc.Region)
c.Assert(loc.StartKey, BytesEquals, []byte(regionStartKey))
require.Equal(t, []byte(regionStartKey), loc.StartKey)
}