forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener_test.go
226 lines (181 loc) · 4.43 KB
/
listener_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
package pg_test
import (
"net"
"time"
"github.com/go-pg/pg/v9"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Context("Listener", func() {
var db *pg.DB
var ln *pg.Listener
BeforeEach(func() {
opt := pgOptions()
opt.PoolSize = 2
opt.PoolTimeout = time.Second
db = pg.Connect(opt)
ln = db.Listen("test_channel")
})
var _ = AfterEach(func() {
_ = ln.Close()
_ = db.Close()
})
It("implements Stringer", func() {
Expect(ln.String()).To(Equal("Listener(test_channel)"))
_ = ln.Channel()
Expect(ln.String()).To(Equal("Listener(test_channel, gopg:ping)"))
})
It("reuses connection", func() {
for i := 0; i < 100; i++ {
_, _, err := ln.ReceiveTimeout(time.Nanosecond)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(MatchRegexp(".+ i/o timeout"))
}
st := db.PoolStats()
Expect(st.Hits).To(Equal(uint32(0)))
Expect(st.Misses).To(Equal(uint32(0)))
Expect(st.Timeouts).To(Equal(uint32(0)))
Expect(st.TotalConns).To(Equal(uint32(1)))
Expect(st.IdleConns).To(Equal(uint32(0)))
})
It("listens for notifications", func() {
wait := make(chan struct{}, 2)
go func() {
defer GinkgoRecover()
wait <- struct{}{}
channel, payload, err := ln.Receive()
Expect(err).NotTo(HaveOccurred())
Expect(channel).To(Equal("test_channel"))
Expect(payload).To(Equal(""))
wait <- struct{}{}
}()
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
Fail("timeout")
}
_, err := db.Exec("NOTIFY test_channel")
Expect(err).NotTo(HaveOccurred())
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
Fail("timeout")
}
})
It("is closed when DB is closed", func() {
wait := make(chan struct{}, 2)
go func() {
defer GinkgoRecover()
wait <- struct{}{}
_, _, err := ln.Receive()
Expect(err.Error()).To(SatisfyAny(
Equal("EOF"),
MatchRegexp(`use of closed (file or )?network connection$`),
))
wait <- struct{}{}
}()
select {
case <-wait:
// ok
case <-time.After(time.Second):
Fail("timeout")
}
select {
case <-wait:
Fail("Receive is not blocked")
case <-time.After(time.Second):
// ok
}
Expect(db.Close()).To(BeNil())
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
Fail("Listener is not closed")
}
_, _, err := ln.Receive()
Expect(err).To(MatchError("pg: listener is closed"))
err = ln.Close()
Expect(err).To(MatchError("pg: listener is closed"))
})
It("returns an error on timeout", func() {
channel, payload, err := ln.ReceiveTimeout(time.Second)
Expect(err.(net.Error).Timeout()).To(BeTrue())
Expect(channel).To(Equal(""))
Expect(payload).To(Equal(""))
})
It("reconnects on bad connection", func() {
cn := ln.CurrentConn()
Expect(cn).NotTo(BeNil())
cn.SetNetConn(&badConn{})
err := ln.Listen("test_channel2")
Expect(err).Should(MatchError("bad connection"))
err = ln.Listen("test_channel2")
Expect(err).NotTo(HaveOccurred())
})
It("reconnects on receive error", func() {
cn := ln.CurrentConn()
Expect(cn).NotTo(BeNil())
cn.SetNetConn(&badConn{})
_, _, err := ln.ReceiveTimeout(time.Second)
Expect(err).Should(MatchError("bad connection"))
_, _, err = ln.ReceiveTimeout(time.Second)
Expect(err.(net.Error).Timeout()).To(BeTrue())
wait := make(chan struct{}, 2)
go func() {
defer GinkgoRecover()
wait <- struct{}{}
_, _, lnerr := ln.Receive()
Expect(lnerr).NotTo(HaveOccurred())
wait <- struct{}{}
}()
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
Fail("timeout")
}
select {
case <-wait:
Fail("Receive is not blocked")
case <-time.After(time.Second):
// ok
}
_, err = db.Exec("NOTIFY test_channel")
Expect(err).NotTo(HaveOccurred())
select {
case <-wait:
// ok
case <-time.After(3 * time.Second):
Fail("timeout")
}
})
It("supports concurrent Listen and Receive", func() {
const N = 100
wg := performAsync(N, func(_ int) {
_, err := db.Exec("NOTIFY test_channel")
Expect(err).NotTo(HaveOccurred())
})
done := make(chan struct{})
go func() {
defer GinkgoRecover()
for i := 0; i < N; i++ {
_, _, err := ln.ReceiveTimeout(5 * time.Second)
Expect(err).NotTo(HaveOccurred())
}
close(done)
}()
for i := 0; i < N; i++ {
err := ln.Listen("test_channel")
Expect(err).NotTo(HaveOccurred())
}
select {
case <-done:
wg.Wait()
case <-time.After(30 * time.Second):
Fail("timeout")
}
})
})