-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release2.1] reparo: add unit test && add safe mode (#662)
- Loading branch information
1 parent
5ba5f7b
commit d3361c7
Showing
14 changed files
with
495 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package syncer | ||
|
||
import ( | ||
"github.com/pingcap/check" | ||
) | ||
|
||
type testMemorySuite struct{} | ||
|
||
var _ = check.Suite(&testMemorySuite{}) | ||
|
||
func (s *testMemorySuite) TestMemorySyncer(c *check.C) { | ||
syncer, err := newMemSyncer() | ||
c.Assert(err, check.IsNil) | ||
|
||
syncTest(c, Syncer(syncer)) | ||
|
||
binlog := syncer.GetBinlogs() | ||
c.Assert(binlog, check.HasLen, 2) | ||
|
||
err = syncer.Close() | ||
c.Assert(err, check.IsNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package syncer | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
sqlmock "github.com/DATA-DOG/go-sqlmock" | ||
"github.com/pingcap/check" | ||
pb "github.com/pingcap/tidb-binlog/proto/binlog" | ||
) | ||
|
||
type testMysqlSuite struct{} | ||
|
||
var _ = check.Suite(&testMysqlSuite{}) | ||
|
||
func (s *testMysqlSuite) TestMysqlSyncer(c *check.C) { | ||
s.testMysqlSyncer(c, true) | ||
s.testMysqlSyncer(c, false) | ||
} | ||
|
||
func (s *testMysqlSuite) testMysqlSyncer(c *check.C, safemode bool) { | ||
var ( | ||
mock sqlmock.Sqlmock | ||
) | ||
originWorkerCount := defaultWorkerCount | ||
defaultWorkerCount = 1 | ||
defer func() { | ||
defaultWorkerCount = originWorkerCount | ||
}() | ||
|
||
oldCreateDB := createDB | ||
createDB = func(string, string, string, int) (db *sql.DB, err error) { | ||
db, mock, err = sqlmock.New() | ||
return | ||
} | ||
defer func() { | ||
createDB = oldCreateDB | ||
}() | ||
|
||
syncer, err := newMysqlSyncer(&DBConfig{}, safemode) | ||
c.Assert(err, check.IsNil) | ||
|
||
mock.ExpectBegin() | ||
mock.ExpectExec("create database test").WillReturnResult(sqlmock.NewResult(0, 0)) | ||
mock.ExpectCommit() | ||
|
||
mock.ExpectQuery("show columns from `test`.`t1`").WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}).AddRow("a", "int", "YES", "", "NULL", "").AddRow("b", "varchar(24)", "YES", "", "NULL", "").AddRow("c", "varchar(24)", "YES", "", "NULL", "")) | ||
|
||
rows := sqlmock.NewRows([]string{"Table", "Non_unique", "Key_name", "Seq_in_index", "Column_name", "Collation", "Cardinality", "Sub_part", "Packed", "Null", "Index_type", "Comment", "Index_comment"}) | ||
mock.ExpectQuery("show index from `test`.`t1`").WillReturnRows(rows) | ||
|
||
mock.ExpectBegin() | ||
insertPattern := "INSERT INTO" | ||
if safemode { | ||
insertPattern = "REPLACE INTO" | ||
} | ||
mock.ExpectExec(insertPattern).WithArgs(1, "test", nil).WillReturnResult(sqlmock.NewResult(0, 1)) | ||
mock.ExpectExec("DELETE FROM").WithArgs(1, "test").WillReturnResult(sqlmock.NewResult(0, 1)) | ||
if safemode { | ||
mock.ExpectExec("DELETE FROM").WithArgs().WillReturnResult(sqlmock.NewResult(0, 1)) | ||
mock.ExpectExec(insertPattern).WithArgs(nil, nil, "abc").WillReturnResult(sqlmock.NewResult(0, 1)) | ||
} else { | ||
mock.ExpectExec("UPDATE").WithArgs("abc", "test").WillReturnResult(sqlmock.NewResult(0, 1)) | ||
} | ||
mock.ExpectCommit() | ||
|
||
syncTest(c, Syncer(syncer)) | ||
|
||
err = syncer.Close() | ||
c.Assert(err, check.IsNil) | ||
} | ||
|
||
func syncTest(c *check.C, syncer Syncer) { | ||
ddlBinlog := &pb.Binlog{ | ||
Tp: pb.BinlogType_DDL, | ||
DdlQuery: []byte("create database test;"), | ||
} | ||
dmlBinlog := &pb.Binlog{ | ||
Tp: pb.BinlogType_DML, | ||
DmlData: &pb.DMLData{ | ||
Events: generateDMLEvents(c), | ||
}, | ||
} | ||
|
||
binlogs := make([]*pb.Binlog, 0, 2) | ||
err := syncer.Sync(ddlBinlog, func(binlog *pb.Binlog) { | ||
c.Log(binlog) | ||
binlogs = append(binlogs, binlog) | ||
}) | ||
c.Assert(err, check.IsNil) | ||
|
||
err = syncer.Sync(dmlBinlog, func(binlog *pb.Binlog) { | ||
c.Log(binlog) | ||
binlogs = append(binlogs, binlog) | ||
}) | ||
c.Assert(err, check.IsNil) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
c.Assert(binlogs, check.HasLen, 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package syncer | ||
|
||
import ( | ||
"strings" | ||
|
||
capturer "github.com/kami-zh/go-capturer" | ||
"github.com/pingcap/check" | ||
pb "github.com/pingcap/tidb-binlog/proto/binlog" | ||
) | ||
|
||
type testPrintSuite struct{} | ||
|
||
var _ = check.Suite(&testPrintSuite{}) | ||
|
||
func (s *testPrintSuite) TestPrintSyncer(c *check.C) { | ||
syncer, err := newPrintSyncer() | ||
c.Assert(err, check.IsNil) | ||
|
||
out := capturer.CaptureStdout(func() { | ||
syncTest(c, Syncer(syncer)) | ||
}) | ||
|
||
c.Assert(out, check.Equals, | ||
"DDL query: create database test;\n"+ | ||
"schema: test; table: t1; type: Insert\n"+ | ||
"a(int): 1\n"+ | ||
"b(varchar): test\n"+ | ||
"schema: test; table: t1; type: Delete\n"+ | ||
"a(int): 1\n"+ | ||
"b(varchar): test\n"+ | ||
"schema: test; table: t1; type: Update\n"+ | ||
"c(varchar): test => abc\n") | ||
|
||
err = syncer.Close() | ||
c.Assert(err, check.IsNil) | ||
} | ||
|
||
func (s *testPrintSuite) TestPrintEventHeader(c *check.C) { | ||
schema := "test" | ||
table := "t1" | ||
event := &pb.Event{ | ||
Tp: pb.EventType_Insert, | ||
SchemaName: &schema, | ||
TableName: &table, | ||
} | ||
|
||
out := capturer.CaptureStdout(func() { | ||
printEventHeader(event) | ||
}) | ||
lines := strings.Split(strings.TrimSpace(out), "\n") | ||
c.Assert(lines, check.HasLen, 1) | ||
c.Assert(lines[0], check.Matches, ".*schema: test; table: t1; type: Insert.*") | ||
} | ||
|
||
func (s *testPrintSuite) TestPrintDDL(c *check.C) { | ||
ddlBinlog := &pb.Binlog{ | ||
Tp: pb.BinlogType_DDL, | ||
DdlQuery: []byte("create database test;"), | ||
} | ||
|
||
out := capturer.CaptureStdout(func() { | ||
printDDL(ddlBinlog) | ||
}) | ||
lines := strings.Split(strings.TrimSpace(out), "\n") | ||
c.Assert(lines, check.HasLen, 1) | ||
c.Assert(lines[0], check.Matches, ".*DDL query: create database test;.*") | ||
} | ||
|
||
func (s *testPrintSuite) TestPrintRow(c *check.C) { | ||
cols := generateColumns(c) | ||
|
||
insertEvent := &pb.Event{ | ||
Tp: pb.EventType_Insert, | ||
Row: [][]byte{cols[0], cols[1]}, | ||
} | ||
|
||
out := capturer.CaptureStdout(func() { | ||
printEvent(insertEvent) | ||
}) | ||
lines := strings.Split(strings.TrimSpace(out), "\n") | ||
c.Assert(lines, check.HasLen, 3) | ||
c.Assert(lines[0], check.Equals, "schema: ; table: ; type: Insert") | ||
c.Assert(lines[1], check.Equals, "a(int): 1") | ||
c.Assert(lines[2], check.Equals, "b(varchar): test") | ||
|
||
deleteEvent := &pb.Event{ | ||
Tp: pb.EventType_Delete, | ||
Row: [][]byte{cols[0], cols[1]}, | ||
} | ||
out = capturer.CaptureStdout(func() { | ||
printEvent(deleteEvent) | ||
}) | ||
lines = strings.Split(strings.TrimSpace(out), "\n") | ||
c.Assert(lines, check.HasLen, 3) | ||
c.Assert(lines[0], check.Equals, "schema: ; table: ; type: Delete") | ||
c.Assert(lines[1], check.Equals, "a(int): 1") | ||
c.Assert(lines[2], check.Equals, "b(varchar): test") | ||
|
||
updateEvent := &pb.Event{ | ||
Tp: pb.EventType_Update, | ||
Row: [][]byte{cols[2]}, | ||
} | ||
out = capturer.CaptureStdout(func() { | ||
printEvent(updateEvent) | ||
}) | ||
lines = strings.Split(strings.TrimSpace(out), "\n") | ||
c.Assert(lines, check.HasLen, 2) | ||
c.Assert(lines[0], check.Equals, "schema: ; table: ; type: Update") | ||
c.Assert(lines[1], check.Equals, "c(varchar): test => abc") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.