forked from moiot/gravity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_server_test.go
More file actions
157 lines (122 loc) · 3.88 KB
/
local_server_test.go
File metadata and controls
157 lines (122 loc) · 3.88 KB
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
package dcp_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"database/sql"
"github.com/moiot/gravity/dcp"
"github.com/moiot/gravity/dcp/checker"
"github.com/moiot/gravity/pkg/utils"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/moiot/gravity/dcp/barrier"
"github.com/moiot/gravity/dcp/collector"
)
const dcpTestDB = "dcp_test"
var _ = PDescribe("Local server", func() {
dbConfig := utils.TestConfig()
var db *sql.DB
var shutDown chan struct{}
var alarm chan checker.Result
var closed chan error
barrierConfig := barrier.Config{
Db: *dbConfig,
TickerSeconds: 2,
}
collectorConfigs := []collector.Config{
&collector.MysqlConfig{
Db: collector.DbConfig{
Name: "db",
Host: dbConfig.Host,
Port: uint(dbConfig.Port),
Username: dbConfig.Username,
Password: dbConfig.Password,
ServerId: 999,
},
TagConfigs: []collector.TagConfig{
{
Tag: "src",
Tables: []collector.SchemaAndTable{
{
Schema: dcpTestDB,
Table: "src",
PrimaryKeyIdx: 0,
},
},
},
{
Tag: "target",
Tables: []collector.SchemaAndTable{
{
Schema: dcpTestDB,
Table: "target",
PrimaryKeyIdx: 0,
},
},
},
},
},
}
checkerConfig := checker.Config{
SourceTag: "src",
TargetTags: []string{"target"},
TimeoutSeconds: 2,
}
waitTime := checkerConfig.TimeoutSeconds + barrierConfig.TickerSeconds
BeforeEach(func() {
var err error
shutDown = make(chan struct{})
alarm = make(chan checker.Result, 10)
closed = make(chan error)
db, err = utils.CreateDBConnection(dbConfig)
Expect(err).ShouldNot(HaveOccurred())
exec(db, fmt.Sprintf("drop database if exists %s", dcpTestDB))
exec(db, fmt.Sprintf("create database if not exists %s", dcpTestDB))
exec(db, fmt.Sprintf(`
CREATE TABLE if not EXISTS %s.src (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
v bigint(11) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`, dcpTestDB))
exec(db, fmt.Sprintf(`
CREATE TABLE if not EXISTS %s.target (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
v bigint(11) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`, dcpTestDB))
exec(db, "reset master")
go func() {
closed <- dcp.StartLocal(&barrierConfig, collectorConfigs, &checkerConfig, shutDown, alarm)
}()
})
AfterEach(func() {
close(shutDown)
Eventually(closed).Should(Receive(BeNil()))
if db != nil {
exec(db, fmt.Sprintf("drop database if exists %s", dcpTestDB))
exec(db, "reset master")
db.Close()
}
})
It("Should report Same when sync", func() {
Expect(<-alarm).Should(BeAssignableToTypeOf(&checker.Ready{}))
exec(db, fmt.Sprintf("insert into %s.src(id, v) VALUES (1, 0)", dcpTestDB))
exec(db, fmt.Sprintf("insert into %s.target(id, v) VALUES (1, 0)", dcpTestDB))
Eventually(alarm, time.Second*time.Duration(waitTime)).Should(Receive(BeAssignableToTypeOf(&checker.Same{})))
})
It("Should report Diff on different modification", func() {
Expect(<-alarm).Should(BeAssignableToTypeOf(&checker.Ready{}))
exec(db, fmt.Sprintf("insert into %s.src(id, v) VALUES (1, 0)", dcpTestDB))
exec(db, fmt.Sprintf("insert into %s.target(id, v) VALUES (1, 1)", dcpTestDB))
Eventually(alarm, time.Second*time.Duration(waitTime)).Should(Receive(BeAssignableToTypeOf(&checker.Diff{})))
})
It("Should report Diff when lack of target data", func() { //barrier is still received, Timeout just for no barrier
Expect(<-alarm).Should(BeAssignableToTypeOf(&checker.Ready{}))
exec(db, fmt.Sprintf("insert into %s.src(id, v) VALUES (1, 0)", dcpTestDB))
Eventually(alarm, time.Second*time.Duration(waitTime)).Should(Receive(BeAssignableToTypeOf(&checker.Diff{})))
})
})
func exec(db *sql.DB, stmt string) {
_, err := db.Exec(stmt)
Expect(err).ShouldNot(HaveOccurred(), stmt)
}