forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanal_test.go
342 lines (309 loc) · 9.1 KB
/
canal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package canal
import (
"flag"
"fmt"
"testing"
"time"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/siddontang/go-log/log"
)
var testHost = flag.String("host", "127.0.0.1", "MySQL host")
func Test(t *testing.T) {
TestingT(t)
}
type canalTestSuite struct {
c *Canal
}
var _ = Suite(&canalTestSuite{})
const (
miA = 0
miB = -1
miC = 1
umiA = 0
umiB = 1
umiC = 16777215
)
func (s *canalTestSuite) SetUpSuite(c *C) {
cfg := NewDefaultConfig()
cfg.Addr = fmt.Sprintf("%s:3306", *testHost)
cfg.User = "root"
cfg.HeartbeatPeriod = 200 * time.Millisecond
cfg.ReadTimeout = 300 * time.Millisecond
cfg.Dump.ExecutionPath = "mysqldump"
cfg.Dump.TableDB = "test"
cfg.Dump.Tables = []string{"canal_test"}
cfg.Dump.Where = "id>0"
// include & exclude config
cfg.IncludeTableRegex = make([]string, 1)
cfg.IncludeTableRegex[0] = ".*\\.canal_test"
cfg.ExcludeTableRegex = make([]string, 2)
cfg.ExcludeTableRegex[0] = "mysql\\..*"
cfg.ExcludeTableRegex[1] = ".*\\..*_inner"
var err error
s.c, err = NewCanal(cfg)
c.Assert(err, IsNil)
s.execute(c, "DROP TABLE IF EXISTS test.canal_test")
sql := `
CREATE TABLE IF NOT EXISTS test.canal_test (
id int AUTO_INCREMENT,
content blob DEFAULT NULL,
name varchar(100),
mi mediumint(8) NOT NULL DEFAULT 0,
umi mediumint(8) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY(id)
)ENGINE=innodb;
`
s.execute(c, sql)
s.execute(c, "DELETE FROM test.canal_test")
s.execute(c, "INSERT INTO test.canal_test (content, name, mi, umi) VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)",
"1", "a", miA, umiA,
`\0\ndsfasdf`, "b", miC, umiC,
"", "c", miB, umiB,
)
s.execute(c, "SET GLOBAL binlog_format = 'ROW'")
s.c.SetEventHandler(&testEventHandler{c: c})
go func() {
set, _ := mysql.ParseGTIDSet("mysql", "")
err = s.c.StartFromGTID(set)
c.Assert(err, IsNil)
}()
}
func (s *canalTestSuite) TearDownSuite(c *C) {
// To test the heartbeat and read timeout,so need to sleep 1 seconds without data transmission
c.Logf("Start testing the heartbeat and read timeout")
time.Sleep(time.Second)
if s.c != nil {
s.c.Close()
s.c = nil
}
}
func (s *canalTestSuite) execute(c *C, query string, args ...interface{}) *mysql.Result {
r, err := s.c.Execute(query, args...)
c.Assert(err, IsNil)
return r
}
type testEventHandler struct {
DummyEventHandler
c *C
}
func (h *testEventHandler) OnRow(e *RowsEvent) error {
log.Infof("OnRow %s %v\n", e.Action, e.Rows)
umi, ok := e.Rows[0][4].(uint32) // 4th col is umi. mysqldump gives uint64 instead of uint32
if ok && (umi != umiA && umi != umiB && umi != umiC) {
return fmt.Errorf("invalid unsigned medium int %d", umi)
}
return nil
}
func (h *testEventHandler) String() string {
return "testEventHandler"
}
func (h *testEventHandler) OnPosSynced(header *replication.EventHeader, p mysql.Position, set mysql.GTIDSet, f bool) error {
return nil
}
func (s *canalTestSuite) TestCanal(c *C) {
<-s.c.WaitDumpDone()
for i := 1; i < 10; i++ {
s.execute(c, "INSERT INTO test.canal_test (name) VALUES (?)", fmt.Sprintf("%d", i))
}
s.execute(c, "INSERT INTO test.canal_test (mi,umi) VALUES (?,?), (?,?), (?,?)",
miA, umiA,
miC, umiC,
miB, umiB,
)
s.execute(c, "ALTER TABLE test.canal_test ADD `age` INT(5) NOT NULL AFTER `name`")
s.execute(c, "INSERT INTO test.canal_test (name,age) VALUES (?,?)", "d", "18")
err := s.c.CatchMasterPos(10 * time.Second)
c.Assert(err, IsNil)
}
func (s *canalTestSuite) TestCanalFilter(c *C) {
// included
sch, err := s.c.GetTable("test", "canal_test")
c.Assert(err, IsNil)
c.Assert(sch, NotNil)
_, err = s.c.GetTable("not_exist_db", "canal_test")
c.Assert(errors.Trace(err), Not(Equals), ErrExcludedTable)
// excluded
sch, err = s.c.GetTable("test", "canal_test_inner")
c.Assert(errors.Cause(err), Equals, ErrExcludedTable)
c.Assert(sch, IsNil)
sch, err = s.c.GetTable("mysql", "canal_test")
c.Assert(errors.Cause(err), Equals, ErrExcludedTable)
c.Assert(sch, IsNil)
sch, err = s.c.GetTable("not_exist_db", "not_canal_test")
c.Assert(errors.Cause(err), Equals, ErrExcludedTable)
c.Assert(sch, IsNil)
}
func TestCreateTableExp(t *testing.T) {
cases := []string{
"CREATE TABLE /*generated by server */ mydb.mytable (`id` int(10)) ENGINE=InnoDB",
"CREATE TABLE `mydb`.`mytable` (`id` int(10)) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS mydb.`mytable` (`id` int(10)) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS `mydb`.mytable (`id` int(10)) ENGINE=InnoDB",
}
table := "mytable"
db := "mydb"
pr := parser.New()
for _, s := range cases {
stmts, _, err := pr.Parse(s, "", "")
if err != nil {
t.Fatalf("TestCreateTableExp:case %s failed\n", s)
}
for _, st := range stmts {
nodes := parseStmt(st)
if len(nodes) == 0 {
continue
}
if nodes[0].db != db || nodes[0].table != table {
t.Fatalf("TestCreateTableExp:case %s failed\n", s)
}
}
}
}
func TestAlterTableExp(t *testing.T) {
cases := []string{
"ALTER TABLE /*generated by server*/ `mydb`.`mytable` ADD `field2` DATE NULL AFTER `field1`;",
"ALTER TABLE `mytable` ADD `field2` DATE NULL AFTER `field1`;",
"ALTER TABLE mydb.mytable ADD `field2` DATE NULL AFTER `field1`;",
"ALTER TABLE mytable ADD `field2` DATE NULL AFTER `field1`;",
"ALTER TABLE mydb.mytable ADD field2 DATE NULL AFTER `field1`;",
}
table := "mytable"
db := "mydb"
pr := parser.New()
for _, s := range cases {
stmts, _, err := pr.Parse(s, "", "")
if err != nil {
t.Fatalf("TestAlterTableExp:case %s failed\n", s)
}
for _, st := range stmts {
nodes := parseStmt(st)
if len(nodes) == 0 {
continue
}
rdb := nodes[0].db
rtable := nodes[0].table
if (len(rdb) > 0 && rdb != db) || rtable != table {
t.Fatalf("TestAlterTableExp:case %s failed db %s,table %s\n", s, rdb, rtable)
}
}
}
}
func TestRenameTableExp(t *testing.T) {
cases := []string{
"rename /* generate by server */table `mydb`.`mytable0` to `mydb`.`mytable0tmp`",
"rename table `mytable0` to `mytable0tmp`",
"rename table mydb.mytable0 to mydb.mytable0tmp",
"rename table mytable0 to mytable0tmp",
"rename table `mydb`.`mytable0` to `mydb`.`mytable0tmp`, `mydb`.`mytable1` to `mydb`.`mytable1tmp`",
"rename table `mytable0` to `mytable0tmp`, `mytable1` to `mytable1tmp`",
"rename table mydb.mytable0 to mydb.mytable0tmp, mydb.mytable1 to mydb.mytable1tmp",
"rename table mytable0 to mytable0tmp, mytable1 to mytabletmp",
}
baseTable := "mytable"
db := "mydb"
pr := parser.New()
for _, s := range cases {
stmts, _, err := pr.Parse(s, "", "")
if err != nil {
t.Fatalf("TestRenameTableExp:case %s failed\n", s)
}
for _, st := range stmts {
nodes := parseStmt(st)
if len(nodes) == 0 {
continue
}
for i, node := range nodes {
rdb := node.db
rtable := node.table
table := fmt.Sprintf("%s%d", baseTable, i)
if (len(rdb) > 0 && rdb != db) || rtable != table {
t.Fatalf("TestRenameTableExp:case %s failed db %s,table %s\n", s, rdb, rtable)
}
}
}
}
}
func TestDropTableExp(t *testing.T) {
cases := []string{
"drop table test0",
"DROP TABLE test0",
"DROP TABLE test0",
"DROP table IF EXISTS test.test0",
"drop table `test0`",
"DROP TABLE `test0`",
"DROP table IF EXISTS `test`.`test0`",
"DROP TABLE `test0` /* generated by server */",
"DROP /*generated by server */ table if exists test0",
"DROP table if exists `test0`",
"DROP table if exists test.test0",
"DROP table if exists `test`.test0",
"DROP table if exists `test`.`test0`",
"DROP table if exists test.`test0`",
"DROP table if exists test.`test0`",
}
baseTable := "test"
db := "test"
pr := parser.New()
for _, s := range cases {
stmts, _, err := pr.Parse(s, "", "")
if err != nil {
t.Fatalf("TestDropTableExp:case %s failed\n", s)
}
for _, st := range stmts {
nodes := parseStmt(st)
if len(nodes) == 0 {
continue
}
for i, node := range nodes {
rdb := node.db
rtable := node.table
table := fmt.Sprintf("%s%d", baseTable, i)
if (len(rdb) > 0 && rdb != db) || rtable != table {
t.Fatalf("TestDropTableExp:case %s failed db %s,table %s\n", s, rdb, rtable)
}
}
}
}
}
func TestWithoutSchemeExp(t *testing.T) {
cases := []replication.QueryEvent{
{
Schema: []byte("test"),
Query: []byte("drop table test0"),
},
{
Schema: []byte("test"),
Query: []byte("rename table `test0` to `testtmp`"),
},
{
Schema: []byte("test"),
Query: []byte("ALTER TABLE `test0` ADD `field2` DATE NULL AFTER `field1`;"),
},
{
Schema: []byte("test"),
Query: []byte("CREATE TABLE IF NOT EXISTS test0 (`id` int(10)) ENGINE=InnoDB"),
},
}
table := "test0"
db := "test"
pr := parser.New()
for _, s := range cases {
stmts, _, err := pr.Parse(string(s.Query), "", "")
if err != nil {
t.Fatalf("TestCreateTableExp:case %s failed\n", s.Query)
}
for _, st := range stmts {
nodes := parseStmt(st)
if len(nodes) == 0 {
continue
}
if nodes[0].db != "" || nodes[0].table != table || string(s.Schema) != db {
t.Fatalf("TestCreateTableExp:case %s failed\n", s.Query)
}
}
}
}