Skip to content

Commit

Permalink
*: Add the two states test (pingcap#3804)
Browse files Browse the repository at this point in the history
* ddl: add different state tests

* ddl: update

* ddl: clean up

* ddl: add a comment

* ddl: make more readable

* ddl: add a comment

* ddl: add the error check

* ddl: address comments
  • Loading branch information
zimulala authored and hanfei1991 committed Jul 25, 2017
1 parent 743f5c0 commit 5943198
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 41 deletions.
23 changes: 14 additions & 9 deletions ddl/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
goctx "golang.org/x/net/context"
)

type testDDLCallback struct {
type TestDDLCallback struct {
*BaseCallback

onJobRunBefore func(*model.Job)
onJobUpdated func(*model.Job)
onBgJobUpdated func(*model.Job)
onWatched func(ctx goctx.Context)
onJobRunBefore func(*model.Job)
onJobUpdated func(*model.Job)
OnJobUpdatedExported func(*model.Job)
onBgJobUpdated func(*model.Job)
onWatched func(ctx goctx.Context)
}

func (tc *testDDLCallback) OnJobRunBefore(job *model.Job) {
func (tc *TestDDLCallback) OnJobRunBefore(job *model.Job) {
if tc.onJobRunBefore != nil {
tc.onJobRunBefore(job)
return
Expand All @@ -38,7 +39,11 @@ func (tc *testDDLCallback) OnJobRunBefore(job *model.Job) {
tc.BaseCallback.OnJobRunBefore(job)
}

func (tc *testDDLCallback) OnJobUpdated(job *model.Job) {
func (tc *TestDDLCallback) OnJobUpdated(job *model.Job) {
if tc.OnJobUpdatedExported != nil {
tc.OnJobUpdatedExported(job)
return
}
if tc.onJobUpdated != nil {
tc.onJobUpdated(job)
return
Expand All @@ -47,7 +52,7 @@ func (tc *testDDLCallback) OnJobUpdated(job *model.Job) {
tc.BaseCallback.OnJobUpdated(job)
}

func (tc *testDDLCallback) OnBgJobUpdated(job *model.Job) {
func (tc *TestDDLCallback) OnBgJobUpdated(job *model.Job) {
if tc.onBgJobUpdated != nil {
tc.onBgJobUpdated(job)
return
Expand All @@ -56,7 +61,7 @@ func (tc *testDDLCallback) OnBgJobUpdated(job *model.Job) {
tc.BaseCallback.OnBgJobUpdated(job)
}

func (tc *testDDLCallback) OnWatched(ctx goctx.Context) {
func (tc *TestDDLCallback) OnWatched(ctx goctx.Context) {
if tc.onWatched != nil {
tc.onWatched(ctx)
return
Expand Down
12 changes: 6 additions & 6 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
c.Assert(err, IsNil)

var mu sync.Mutex
tc := &testDDLCallback{}
tc := &TestDDLCallback{}
// set up hook
prevState := model.StateNone
var (
Expand Down Expand Up @@ -125,7 +125,7 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
checkErr = errors.Trace(err)
}
}
d.setHook(tc)
d.SetHook(tc)
defaultValue := int64(3)
job := testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c3", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, defaultValue)
c.Assert(errors.ErrorStack(checkErr), Equals, "")
Expand All @@ -139,7 +139,7 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {

func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context, d *ddl, tblInfo *model.TableInfo) {
d.Stop()
tc := &testDDLCallback{}
tc := &TestDDLCallback{}
// set up hook
prevState := model.StateNone
var checkErr error
Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context
checkErr = errors.Trace(err)
}
}
d.setHook(tc)
d.SetHook(tc)
d.start(goctx.Background())
job := testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c3", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, nil)
c.Assert(errors.ErrorStack(checkErr), Equals, "")
Expand All @@ -187,7 +187,7 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context
func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx context.Context, d *ddl, tbl table.Table) {
d.Stop()
dropCol := tbl.Cols()[2]
tc := &testDDLCallback{}
tc := &TestDDLCallback{}
// set up hook
prevState := model.StateNone
var checkErr error
Expand All @@ -206,7 +206,7 @@ func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx context.Context, d *ddl
}
}
}
d.setHook(tc)
d.SetHook(tc)
d.start(goctx.Background())
c.Assert(errors.ErrorStack(checkErr), Equals, "")
testDropColumn(c, ctx, d, s.dbInfo, tbl.Meta(), dropCol.Name.L, false)
Expand Down
11 changes: 4 additions & 7 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (s *testColumnSuite) TestColumn(c *C) {
ctx := testNewContext(s.d)

testCreateTable(c, ctx, s.d, s.dbInfo, tblInfo)

t := testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)

num := 10
Expand Down Expand Up @@ -228,7 +227,6 @@ func (s *testColumnSuite) TestColumn(c *C) {

values, err = t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)

c.Assert(values, HasLen, 5)
c.Assert(values[0].GetInt64(), Equals, int64(202))
c.Assert(values[4].GetInt64(), Equals, int64(101))
Expand Down Expand Up @@ -763,7 +761,7 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
var hookErr error
checkOK := false

tc := &testDDLCallback{}
tc := &TestDDLCallback{}
tc.onJobUpdated = func(job *model.Job) {
mu.Lock()
defer mu.Unlock()
Expand Down Expand Up @@ -792,7 +790,7 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
}
}

d.setHook(tc)
d.SetHook(tc)

// Use local ddl for callback test.
s.d.Stop()
Expand Down Expand Up @@ -833,7 +831,6 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)

t := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)

colName := "c4"
Expand All @@ -849,7 +846,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
var hookErr error
var mu sync.Mutex

tc := &testDDLCallback{}
tc := &TestDDLCallback{}
tc.onJobUpdated = func(job *model.Job) {
mu.Lock()
defer mu.Unlock()
Expand All @@ -868,7 +865,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
}
}

d.setHook(tc)
d.SetHook(tc)

// Use local ddl for callback test.
s.d.Stop()
Expand Down
5 changes: 4 additions & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ type DDL interface {
OwnerManager() OwnerManager
// WorkerVars gets the session variables for DDL worker.
WorkerVars() *variable.SessionVars
// SetHook sets the hook. It's exported for testing.
SetHook(h Callback)
}

// Event is an event that a ddl operation happened.
Expand Down Expand Up @@ -451,7 +453,8 @@ func (d *ddl) callHookOnChanged(err error) error {
return errors.Trace(err)
}

func (d *ddl) setHook(h Callback) {
// SetHook implements DDL.SetHook interface.
func (d *ddl) SetHook(h Callback) {
d.hookMu.Lock()
defer d.hookMu.Unlock()

Expand Down
Loading

0 comments on commit 5943198

Please sign in to comment.