-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
173 lines (157 loc) · 4.68 KB
/
reader_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
package pgchangestream
import (
"context"
"database/sql"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/jackc/pgx"
)
var ep *EphemeralPostgres
func getReplicationConn(t *testing.T) *pgx.ReplicationConn {
conn, err := pgx.ReplicationConnect(
pgx.ConnConfig{Database: ep.Name(), Host: ep.Hostname()},
)
if err != nil {
t.Fatal(err)
}
return conn
}
func dbExec(t *testing.T, db *sql.DB, sql string) {
if _, err := db.Exec(sql); err != nil {
t.Fatal(err)
}
}
func TestReadAndRestart(t *testing.T) {
db, err := ep.Open()
if err != nil {
t.Fatal(err)
}
dbExec(t, db, `CREATE TABLE test (i SERIAL PRIMARY KEY, s TEXT);`)
dbExec(t, db, `SELECT * FROM pg_create_logical_replication_slot('stream_slot', 'test_decoding');`)
// Looks like postgres <=9.5 can't drop a database with an inactive slot...
defer dbExec(t, db, `SELECT * FROM pg_drop_replication_slot('stream_slot');`)
outChan := make(chan Message, 1)
stop := startReader(t, "stream_slot", outChan)
defer stop()
expectNoMessage(t, outChan)
dbExec(t, db, `INSERT INTO test (s) VALUES ('a');`)
expectMessage(t, outChan, true, "BEGIN")
expectMessage(t, outChan, true, `table public.test: INSERT: i[integer]:1 s[text]:'a'`)
expectMessage(t, outChan, true, "COMMIT")
expectNoMessage(t, outChan)
stop()
stop = startReader(t, "stream_slot", outChan)
defer stop()
expectNoMessage(t, outChan)
dbExec(t, db, `INSERT INTO test (s) VALUES ('b');`)
expectMessage(t, outChan, true, "BEGIN")
expectMessage(t, outChan, true, `table public.test: INSERT: i[integer]:2 s[text]:'b'`)
expectMessage(t, outChan, true, "COMMIT")
expectNoMessage(t, outChan)
stop()
}
func TestReadAndRestartPartialCommit(t *testing.T) {
db, err := ep.Open()
if err != nil {
t.Fatal(err)
}
dbExec(t, db, `CREATE TABLE test (i SERIAL PRIMARY KEY, s TEXT);`)
dbExec(t, db, `SELECT * FROM pg_create_logical_replication_slot('stream_slot', 'test_decoding');`)
// Looks like postgres <=9.5 can't drop a database with an inactive slot...
defer dbExec(t, db, `SELECT * FROM pg_drop_replication_slot('stream_slot');`)
outChan := make(chan Message, 1)
stop := startReader(t, "stream_slot", outChan)
defer stop()
expectNoMessage(t, outChan)
dbExec(t, db, `INSERT INTO test (s) VALUES ('a');`)
dbExec(t, db, `INSERT INTO test (s) VALUES ('b');`)
expectMessage(t, outChan, true, "BEGIN")
expectMessage(t, outChan, true, `table public.test: INSERT: i[integer]:1 s[text]:'a'`)
expectMessage(t, outChan, true, "COMMIT")
expectMessage(t, outChan, false, "BEGIN")
expectMessage(t, outChan, false, `table public.test: INSERT: i[integer]:2 s[text]:'b'`)
expectMessage(t, outChan, false, "COMMIT")
expectNoMessage(t, outChan)
stop()
stop = startReader(t, "stream_slot", outChan)
defer stop()
expectMessage(t, outChan, false, "BEGIN")
expectMessage(t, outChan, false, `table public.test: INSERT: i[integer]:2 s[text]:'b'`)
expectMessage(t, outChan, false, "COMMIT")
expectNoMessage(t, outChan)
stop()
stop = startReader(t, "stream_slot", outChan)
defer stop()
expectMessage(t, outChan, true, "BEGIN")
expectMessage(t, outChan, true, `table public.test: INSERT: i[integer]:2 s[text]:'b'`)
expectMessage(t, outChan, true, "COMMIT")
expectNoMessage(t, outChan)
stop()
stop = startReader(t, "stream_slot", outChan)
defer stop()
expectNoMessage(t, outChan)
stop()
}
// TestMain sets up a postgres cluster in `ep` for use in the tests.
func TestMain(m *testing.M) {
os.Exit(TestingMainWithPostgres(m, &ep))
}
func expectMessage(t *testing.T, outChan <-chan Message, commit bool, prefix string) {
t.Helper()
select {
case <-time.After(time.Second):
t.Fatal("did not see expected message")
case msg := <-outChan:
if msg == nil {
t.Fatal("channel sent nil message")
}
data := string(msg.Data())
if !strings.HasPrefix(data, prefix) {
t.Errorf("unexpected message %q, expected prefix %q", data, prefix)
}
if commit {
msg.Commit()
}
}
}
func expectNoMessage(t *testing.T, outChan <-chan Message) {
t.Helper()
select {
case msg := <-outChan:
t.Errorf("expected no message, got %q", string(msg.Data()))
default:
return
}
}
func startReader(t *testing.T, slotName string, outChan chan<- Message) (stop func()) {
conn := getReplicationConn(t)
reader := NewStreamReader(conn, slotName, outChan)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := reader.Listen(ctx)
select {
case <-ctx.Done():
if err != ctx.Err() {
t.Errorf("expected context error %q, got %q", ctx.Err(), err)
}
default:
t.Errorf("errored before context finished: %v", err)
}
}()
stopped := false
return func() {
if stopped {
return
}
cancel()
wg.Wait()
conn.Close()
stopped = true
}
}