-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
152 lines (123 loc) · 3.12 KB
/
client_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
package llsr
import (
"bytes"
"database/sql"
"testing"
"time"
"github.com/liquidm/llsr/decoderbufs"
)
type testConnCallback func(*testing.T, *sql.DB)
type testClientCallback func(*testing.T, Client, *sql.DB)
type DummyConverter struct{}
func (*DummyConverter) Convert(change *decoderbufs.RowMessage, valuesMap ValuesMap) interface{} {
var buf bytes.Buffer
switch change.GetOp() {
case decoderbufs.Op_INSERT:
buf.WriteString("INSERT ")
case decoderbufs.Op_UPDATE:
buf.WriteString("UPDATE ")
case decoderbufs.Op_DELETE:
buf.WriteString("DELETE ")
}
buf.WriteString(change.GetTable())
return buf.String()
}
func testConfig() *DatabaseConfig {
config := NewDatabaseConfig(dbName())
config.User = dbUser()
return config
}
func expectClientEvent(t *testing.T, c Client, eventType EventType) {
eventFound := make(chan bool)
go func() {
for {
event := <-c.Events()
if event.Type == eventType {
eventFound <- true
return
}
}
}()
select {
case <-eventFound:
case <-time.Tick(5 * time.Second):
t.Fatal("Timeout")
}
}
func expectClientUpdate(t *testing.T, c Client, updateMsg string) {
updateFound := make(chan bool)
go func() {
for {
i := <-c.Updates()
update := i.(string)
if update == updateMsg {
updateFound <- true
return
}
}
}()
select {
case <-updateFound:
case <-time.Tick(5 * time.Second):
t.Fatal("Timeout")
}
}
func withTestConnection(t *testing.T, cb testConnCallback) {
db, err := sql.Open("postgres", "sslmode=disable user="+dbUser()+" dbname="+dbName())
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE llsr_test_table (id int primary key, txt text NOT NULL);")
if err != nil {
t.Fatal(err)
}
defer db.Exec("DROP TABLE llsr_test_table")
_, err = db.Exec("SELECT * FROM pg_create_logical_replication_slot('llsr_test_slot', 'decoderbufs')")
if err != nil {
t.Fatal(err)
}
defer db.Exec("SELECT * FROM pg_drop_replication_slot('llsr_test_slot')")
cb(t, db)
}
func withTestClient(t *testing.T, cb testClientCallback) {
withTestConnection(t, func(t *testing.T, db *sql.DB) {
c, err := NewClient(testConfig(), &DummyConverter{}, "llsr_test_slot", 0)
if err != nil {
t.Fatal(err)
}
client := c.(*client)
if err != nil {
t.Fatal(err)
}
defer client.Close()
time.Sleep(1e9)
cb(t, client, db)
})
}
func TestClientEvents(t *testing.T) {
withTestClient(t, func(t *testing.T, c Client, db *sql.DB) {
client := c.(*client)
client.stream.Close()
expectClientEvent(t, client, EventReconnect)
})
}
func TestClientUpdates(t *testing.T) {
withTestClient(t, func(t *testing.T, client Client, db *sql.DB) {
_, err := db.Exec("INSERT INTO llsr_test_table (id, txt) VALUES(1, 'foo')")
if err != nil {
t.Fatal(err)
}
expectClientUpdate(t, client, "INSERT llsr_test_table")
_, err = db.Exec("UPDATE llsr_test_table SET txt = 'bar' WHERE id = 1")
if err != nil {
t.Fatal(err)
}
expectClientUpdate(t, client, "UPDATE llsr_test_table")
_, err = db.Exec("DELETE FROM llsr_test_table WHERE id = 1")
if err != nil {
t.Fatal(err)
}
expectClientUpdate(t, client, "DELETE llsr_test_table")
})
}