Skip to content

Commit

Permalink
ddl/table_split_test.go: migrate test-infra to testify (#29960)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangggong committed Nov 23, 2021
1 parent 805a887 commit 2f119e3
Showing 1 changed file with 23 additions and 22 deletions.
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)
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)
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)
}

0 comments on commit 2f119e3

Please sign in to comment.