Skip to content

Commit

Permalink
Refactor testNewDDLAndStart to return error
Browse files Browse the repository at this point in the history
By returning an error instead of performing assertion, it allows the calleer to decide how to handle the errors.

This is also helpful when the caller wants more flexibility with the assertion (using check Vs require).
  • Loading branch information
clovis1122 committed Nov 21, 2021
1 parent e448d93 commit 3c72a5d
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 103 deletions.
14 changes: 7 additions & 7 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ type testColumnChangeSuite struct {
func (s *testColumnChangeSuite) SetUpSuite(c *C) {
SetWaitTimeWhenErrorOccurred(1 * time.Microsecond)
s.store = testCreateStore(c, "test_column_change")
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
defer func() {
err := d.Stop()
c.Assert(err, IsNil)
Expand All @@ -67,20 +67,20 @@ func (s *testColumnChangeSuite) TearDownSuite(c *C) {
}

func (s *testColumnChangeSuite) TestColumnChange(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
defer func() {
err := d.Stop()
c.Assert(err, IsNil)
}()
// create table t (c1 int, c2 int);
tblInfo := testTableInfo(c, d, "t", 2)
ctx := testNewContext(d)
err := ctx.NewTxn(context.Background())
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
// insert t values (1, 2);
Expand Down Expand Up @@ -163,12 +163,12 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
}

func (s *testColumnChangeSuite) TestModifyAutoRandColumnWithMetaKeyChanged(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
defer func() {
err := d.Stop()
c.Assert(err, IsNil)
Expand Down
42 changes: 21 additions & 21 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ type testColumnSuite struct {

func (s *testColumnSuite) SetUpSuite(c *C) {
s.store = testCreateStore(c, "test_column")
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)

s.dbInfo = testSchemaInfo(c, d, "test_column")
testCreateSchema(c, testNewContext(d), d, s.dbInfo)
Expand Down Expand Up @@ -185,12 +185,12 @@ func testDropColumns(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo,
}

func (s *testColumnSuite) TestColumnBasic(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
defer func() {
err := d.Stop()
c.Assert(err, IsNil)
Expand All @@ -208,7 +208,7 @@ func (s *testColumnSuite) TestColumnBasic(c *C) {
c.Assert(err, IsNil)
}

err := ctx.NewTxn(context.Background())
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

i := int64(0)
Expand Down Expand Up @@ -836,16 +836,16 @@ func (s *testColumnSuite) testGetColumn(t table.Table, name string, isExist bool
}

func (s *testColumnSuite) TestAddColumn(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
ctx := testNewContext(d)

err := ctx.NewTxn(context.Background())
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -924,16 +924,16 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
}

func (s *testColumnSuite) TestAddColumns(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
ctx := testNewContext(d)

err := ctx.NewTxn(context.Background())
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -1009,16 +1009,16 @@ func (s *testColumnSuite) TestAddColumns(c *C) {
}

func (s *testColumnSuite) TestDropColumn(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t2", 4)
ctx := testNewContext(d)

err := ctx.NewTxn(context.Background())
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -1085,16 +1085,16 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
}

func (s *testColumnSuite) TestDropColumns(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t2", 4)
ctx := testNewContext(d)

err := ctx.NewTxn(context.Background())
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -1154,12 +1154,12 @@ func (s *testColumnSuite) TestDropColumns(c *C) {
}

func (s *testColumnSuite) TestModifyColumn(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
ctx := testNewContext(d)

defer func() {
Expand Down Expand Up @@ -1222,12 +1222,12 @@ func (s *testColumnSuite) TestFieldCase(c *C) {
}

func (s *testColumnSuite) TestAutoConvertBlobTypeByLength(c *C) {
d := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
c,
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
// Close the customized ddl(worker goroutine included) after the test is finished, otherwise, it will
// cause go routine in TiDB leak test.
defer func() {
Expand Down
6 changes: 2 additions & 4 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ func TestT(t *testing.T) {
testleak.AfterTestT(t)()
}

func testNewDDLAndStart(ctx context.Context, c *C, options ...Option) *ddl {
func testNewDDLAndStart(ctx context.Context, options ...Option) (*ddl, error) {
// init infoCache and a stub infoSchema
ic := infoschema.NewCache(2)
ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, 0), 0)
options = append(options, WithInfoCache(ic))
d := newDDL(ctx, options...)
err := d.Start(nil)
c.Assert(err, IsNil)

return d
return d, err
}

func testCreateStore(c *C, name string) kv.Storage {
Expand Down
Loading

0 comments on commit 3c72a5d

Please sign in to comment.