-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpgxlisten_test.go
More file actions
222 lines (184 loc) · 5.69 KB
/
pgxlisten_test.go
File metadata and controls
222 lines (184 loc) · 5.69 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
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
package pgxlisten_test
import (
"context"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxtest"
"github.com/jackc/pgxlisten"
"github.com/stretchr/testify/require"
)
var defaultConnTestRunner pgxtest.ConnTestRunner = pgxtest.DefaultConnTestRunner()
func TestListenerListenDispatchesNotifications(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
defaultConnTestRunner.RunTest(ctx, t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
listener := &pgxlisten.Listener{
Connect: func(ctx context.Context) (*pgx.Conn, error) {
config := defaultConnTestRunner.CreateConfig(ctx, t)
return pgx.ConnectConfig(ctx, config)
},
}
fooChan := make(chan *pgconn.Notification)
barChan := make(chan *pgconn.Notification)
listener.Handle("foo", pgxlisten.HandlerFunc(func(ctx context.Context, notification *pgconn.Notification, conn *pgx.Conn) error {
select {
case fooChan <- notification:
case <-ctx.Done():
}
return nil
}))
listener.Handle("bar", pgxlisten.HandlerFunc(func(ctx context.Context, notification *pgconn.Notification, conn *pgx.Conn) error {
select {
case barChan <- notification:
case <-ctx.Done():
}
return nil
}))
listenerCtx, listenerCtxCancel := context.WithCancel(ctx)
defer listenerCtxCancel()
listenerDoneChan := make(chan struct{})
go func() {
listener.Listen(listenerCtx)
close(listenerDoneChan)
}()
// No way to know when Listener is ready so wait a little.
time.Sleep(2 * time.Second)
type notificationTest struct {
goChan chan *pgconn.Notification
channel string
payload string
}
notificationTests := []notificationTest{
{goChan: fooChan, channel: "foo", payload: "a"},
{goChan: fooChan, channel: "foo", payload: "b"},
{goChan: barChan, channel: "bar", payload: "c"},
{goChan: fooChan, channel: "foo", payload: "d"},
{goChan: barChan, channel: "bar", payload: "e"},
}
// Send all notifications.
for i, nt := range notificationTests {
_, err := conn.Exec(ctx, `select pg_notify($1, $2)`, nt.channel, nt.payload)
require.NoErrorf(t, err, "%d", i)
}
// Receive all notifications.
for i, nt := range notificationTests {
select {
case notification := <-nt.goChan:
require.Equalf(t, nt.channel, notification.Channel, "%d", i)
require.Equalf(t, nt.payload, notification.Payload, "%d", i)
case <-ctx.Done():
t.Fatalf("%d. %v", i, ctx.Err())
}
}
listenerCtxCancel()
// Wait for Listen to finish.
select {
case <-listenerDoneChan:
case <-ctx.Done():
t.Fatalf("ctx cancelled while waiting for Listen() to return: %v", ctx.Err())
}
})
}
type msgHandler struct {
ctx context.Context
ch chan string
}
func (h *msgHandler) HandleNotification(ctx context.Context, notification *pgconn.Notification, conn *pgx.Conn) error {
select {
case h.ch <- notification.Payload:
case <-ctx.Done():
}
return nil
}
func (h *msgHandler) HandleBacklog(ctx context.Context, channel string, conn *pgx.Conn) error {
var msg string
rows, err := conn.Query(ctx, `SELECT msg FROM pgxlisten_test`)
if err != nil {
return err
}
defer rows.Close()
_, err = pgx.ForEachRow(rows, []any{&msg}, func() error {
h.ch <- msg
return nil
})
return err
}
func TestListenerListenHandlesBacklog(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
ctr := defaultConnTestRunner
ctr.AfterConnect = func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
_, err := conn.Exec(ctx, `drop table if exists pgxlisten_test;
create table pgxlisten_test (id int primary key generated by default as identity, msg text not null);
`)
require.NoError(t, err)
}
ctr.AfterTest = func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
_, err := conn.Exec(ctx, `drop table if exists pgxlist_test;`)
require.NoError(t, err)
}
ctr.RunTest(ctx, t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
backlogMsgs := []string{"a", "b", "c"}
for _, msg := range backlogMsgs {
_, err := conn.Exec(ctx, `insert into pgxlisten_test (msg) values ($1);`, msg)
require.NoError(t, err)
}
listener := &pgxlisten.Listener{
Connect: func(ctx context.Context) (*pgx.Conn, error) {
config := ctr.CreateConfig(ctx, t)
return pgx.ConnectConfig(ctx, config)
},
}
fooChan := make(chan string, 8)
handler := &msgHandler{
ctx: ctx,
ch: fooChan,
}
listener.Handle("foo", handler)
listenerCtx, listenerCtxCancel := context.WithCancel(ctx)
defer listenerCtxCancel()
listenerDoneChan := make(chan struct{})
go func() {
listener.Listen(listenerCtx)
close(listenerDoneChan)
}()
// No way to know when Listener is ready so wait a little.
time.Sleep(2 * time.Second)
type notificationTest struct {
payload string
}
notificationMsgs := []string{"d", "e"}
// Send all notifications.
for i, msg := range notificationMsgs {
_, err := conn.Exec(ctx, `select pg_notify($1, $2)`, "foo", msg)
require.NoErrorf(t, err, "%d", i)
}
// Receive all backlog notifications.
for i, expected := range backlogMsgs {
select {
case actual := <-fooChan:
require.Equalf(t, expected, actual, "%d", i)
case <-ctx.Done():
t.Fatalf("%d. %v", i, ctx.Err())
}
}
// Receive all notifications.
for i, expected := range notificationMsgs {
select {
case actual := <-fooChan:
require.Equalf(t, expected, actual, "%d", i)
case <-ctx.Done():
t.Fatalf("%d. %v", i, ctx.Err())
}
}
listenerCtxCancel()
// Wait for Listen to finish.
select {
case <-listenerDoneChan:
case <-ctx.Done():
t.Fatalf("ctx cancelled while waiting for Listen() to return: %v", ctx.Err())
}
})
}