-
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.
update log and add safe mode config (#652)
- Loading branch information
1 parent
76433a7
commit 4650b1b
Showing
6 changed files
with
162 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
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("SELECT column_name, extra FROM information_schema.columns").WithArgs("test", "t1").WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("a", "").AddRow("b", "").AddRow("c", "")) | ||
|
||
rows := sqlmock.NewRows([]string{"non_unique", "index_name", "seq_in_index", "column_name"}) | ||
mock.ExpectQuery("SELECT non_unique, index_name, seq_in_index, column_name FROM information_schema.statistics"). | ||
WithArgs("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,41 @@ | ||
package syncer | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/pingcap/check" | ||
) | ||
|
||
type testSyncerSuite struct{} | ||
|
||
var _ = check.Suite(&testSyncerSuite{}) | ||
|
||
func (s *testSyncerSuite) TestNewSyncer(c *check.C) { | ||
cfg := new(DBConfig) | ||
|
||
testCases := []struct { | ||
typeStr string | ||
tp reflect.Type | ||
checker check.Checker | ||
}{ | ||
{ | ||
"print", | ||
reflect.TypeOf(new(printSyncer)), | ||
check.Equals, | ||
}, { | ||
"memory", | ||
reflect.TypeOf(new(MemSyncer)), | ||
check.Equals, | ||
}, { | ||
"print", | ||
reflect.TypeOf(new(MemSyncer)), | ||
check.Not(check.Equals), | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
syncer, err := New(testCase.typeStr, cfg, false) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(reflect.TypeOf(syncer), testCase.checker, testCase.tp) | ||
} | ||
} |