Skip to content

Commit

Permalink
try to fix test race
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 committed Jan 18, 2022
1 parent 32afbc7 commit d2c1a95
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
13 changes: 4 additions & 9 deletions pkg/sqlmodel/reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/stretchr/testify/require"

cdcmodel "github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/dm/pkg/log"
)

func TestIdentity(t *testing.T) {
Expand Down Expand Up @@ -62,11 +61,9 @@ func TestSplit(t *testing.T) {
require.NotEqual(t, delIDKey, insIDKey)
}

func TestReduce(t *testing.T) {
t.Parallel()

func (s *dpanicSuite) TestReduce() {
source := &cdcmodel.TableName{Schema: "db", Table: "tb1"}
sourceTI := mockTableInfo(t, "CREATE TABLE tb1 (c INT PRIMARY KEY, c2 INT)")
sourceTI := mockTableInfo(s.T(), "CREATE TABLE tb1 (c INT PRIMARY KEY, c2 INT)")

cases := []struct {
pre1 []interface{}
Expand Down Expand Up @@ -121,15 +118,13 @@ func TestReduce(t *testing.T) {
changeAfter.lazyInitIdentityInfo()

change2.Reduce(change1)
require.Equal(t, changeAfter, change2)
s.Equal(changeAfter, change2)
}

// test reduce on IdentityUpdated will DPanic
err := log.InitLogger(&log.Config{Level: "debug"})
require.NoError(t, err)
change1 := NewRowChange(source, nil, []interface{}{1, 2}, []interface{}{3, 4}, sourceTI, nil, nil)
change2 := NewRowChange(source, nil, []interface{}{3, 4}, []interface{}{5, 6}, sourceTI, nil, nil)
require.Panics(t, func() {
s.Panics(func() {
change2.Reduce(change1)
})
}
69 changes: 35 additions & 34 deletions pkg/sqlmodel/row_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
timodel "github.com/pingcap/tidb/parser/model"
timock "github.com/pingcap/tidb/util/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

cdcmodel "github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/dm/pkg/log"
Expand All @@ -38,6 +39,19 @@ func mockTableInfo(t *testing.T, sql string) *timodel.TableInfo {
return ti
}

type dpanicSuite struct {
suite.Suite
}

func (s *dpanicSuite) SetupSuite() {
err := log.InitLogger(&log.Config{Level: "debug"})
s.NoError(err)
}

func TestDpanicSuite(t *testing.T) {
suite.Run(t, new(dpanicSuite))
}

func TestNewRowChange(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -77,31 +91,24 @@ func TestNewRowChange(t *testing.T) {
require.Equal(t, expected, actual)
}

func TestRowChangeType(t *testing.T) {
t.Parallel()

err := log.InitLogger(&log.Config{Level: "debug"})
require.NoError(t, err)

func (s *dpanicSuite) TestRowChangeType() {
change := &RowChange{preValues: []interface{}{1}}
change.calculateType()
require.Equal(t, RowChangeDelete, change.tp)
s.Equal(RowChangeDelete, change.tp)
change = &RowChange{preValues: []interface{}{1}, postValues: []interface{}{2}}
change.calculateType()
require.Equal(t, RowChangeUpdate, change.tp)
s.Equal(RowChangeUpdate, change.tp)
change = &RowChange{postValues: []interface{}{1}}
change.calculateType()
require.Equal(t, RowChangeInsert, change.tp)
s.Equal(RowChangeInsert, change.tp)

require.Panics(t, func() {
s.Panics(func() {
change = &RowChange{}
change.calculateType()
})
}

func TestGenDelete(t *testing.T) {
t.Parallel()

func (s *dpanicSuite) TestGenDelete() {
source := &cdcmodel.TableName{Schema: "db", Table: "tb1"}
target := &cdcmodel.TableName{Schema: "db", Table: "tb2"}

Expand Down Expand Up @@ -166,32 +173,28 @@ func TestGenDelete(t *testing.T) {
}

for _, c := range cases {
sourceTI := mockTableInfo(t, c.sourceCreateSQL)
targetTI := mockTableInfo(t, c.targetCreateSQL)
sourceTI := mockTableInfo(s.T(), c.sourceCreateSQL)
targetTI := mockTableInfo(s.T(), c.targetCreateSQL)
change := NewRowChange(source, target, c.preValues, nil, sourceTI, targetTI, nil)
sql, args := change.GenSQL(DMLDelete)
require.Equal(t, c.expectedSQL, sql)
require.Equal(t, c.expectedArgs, args)
s.Equal(c.expectedSQL, sql)
s.Equal(c.expectedArgs, args)
}

// a RowChangeUpdate can still generate DELETE SQL
sourceTI := mockTableInfo(t, "CREATE TABLE tb1 (id INT PRIMARY KEY, name INT)")
sourceTI := mockTableInfo(s.T(), "CREATE TABLE tb1 (id INT PRIMARY KEY, name INT)")
change := NewRowChange(source, nil, []interface{}{1, 2}, []interface{}{3, 4}, sourceTI, nil, nil)
sql, args := change.GenSQL(DMLDelete)
require.Equal(t, "DELETE FROM `db`.`tb1` WHERE `id` = ? LIMIT 1", sql)
require.Equal(t, []interface{}{1}, args)
s.Equal("DELETE FROM `db`.`tb1` WHERE `id` = ? LIMIT 1", sql)
s.Equal([]interface{}{1}, args)

err := log.InitLogger(&log.Config{Level: "debug"})
require.NoError(t, err)
change = NewRowChange(source, nil, nil, []interface{}{3, 4}, sourceTI, nil, nil)
require.Panics(t, func() {
s.Panics(func() {
change.GenSQL(DMLDelete)
})
}

func TestGenUpdate(t *testing.T) {
t.Parallel()

func (s *dpanicSuite) TestGenUpdate() {
source := &cdcmodel.TableName{Schema: "db", Table: "tb1"}
target := &cdcmodel.TableName{Schema: "db", Table: "tb2"}

Expand Down Expand Up @@ -253,19 +256,17 @@ func TestGenUpdate(t *testing.T) {
}

for _, c := range cases {
sourceTI := mockTableInfo(t, c.sourceCreateSQL)
targetTI := mockTableInfo(t, c.targetCreateSQL)
sourceTI := mockTableInfo(s.T(), c.sourceCreateSQL)
targetTI := mockTableInfo(s.T(), c.targetCreateSQL)
change := NewRowChange(source, target, c.preValues, c.postValues, sourceTI, targetTI, nil)
sql, args := change.GenSQL(DMLUpdate)
require.Equal(t, c.expectedSQL, sql)
require.Equal(t, c.expectedArgs, args)
s.Equal(c.expectedSQL, sql)
s.Equal(c.expectedArgs, args)
}

sourceTI := mockTableInfo(t, "CREATE TABLE tb1 (id INT PRIMARY KEY, name INT)")
err := log.InitLogger(&log.Config{Level: "debug"})
require.NoError(t, err)
sourceTI := mockTableInfo(s.T(), "CREATE TABLE tb1 (id INT PRIMARY KEY, name INT)")
change := NewRowChange(source, nil, nil, []interface{}{3, 4}, sourceTI, nil, nil)
require.Panics(t, func() {
s.Panics(func() {
change.GenSQL(DMLUpdate)
})
}
Expand Down

0 comments on commit d2c1a95

Please sign in to comment.