Debian Codegraph @

internal/pool/pool_test.go

raw
1 package pool_test
2
3 import (
4 "errors"
5 "testing"
6 "time"
7
8 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/gomega"
10
11 "gopkg.in/redis.v5/internal/pool"
12 )
13
14 var _ = Describe("ConnPool", func() {
15 var connPool *pool.ConnPool
16
17 BeforeEach(func() {
18 connPool = pool.NewConnPool(
19 dummyDialer, 10, time.Hour, time.Millisecond, time.Millisecond)
20 })
21
22 AfterEach(func() {
23 connPool.Close()
24 })
25
26 It("should unblock client when conn is removed", func() {
27 // Reserve one connection.
28 cn, _, err := connPool.Get()
29 Expect(err).NotTo(HaveOccurred())
30
31 // Reserve all other connections.
32 var cns []*pool.Conn
33 for i := 0; i < 9; i++ {
34 cn, _, err := connPool.Get()
35 Expect(err).NotTo(HaveOccurred())
36 cns = append(cns, cn)
37 }
38
39 started := make(chan bool, 1)
40 done := make(chan bool, 1)
41 go func() {
42 defer GinkgoRecover()
43
44 started <- true
45 _, _, err := connPool.Get()
46 Expect(err).NotTo(HaveOccurred())
47 done <- true
48
49 err = connPool.Put(cn)
50 Expect(err).NotTo(HaveOccurred())
51 }()
52 <-started
53
54 // Check that Get is blocked.
55 select {
56 case <-done:
57 Fail("Get is not blocked")
58 default:
59 // ok
60 }
61
62 err = connPool.Remove(cn, errors.New("test"))
63 Expect(err).NotTo(HaveOccurred())
64
65 // Check that Ping is unblocked.
66 select {
67 case <-done:
68 // ok
69 case <-time.After(time.Second):
70 Fail("Get is not unblocked")
71 }
72
73 for _, cn := range cns {
74 err = connPool.Put(cn)
75 Expect(err).NotTo(HaveOccurred())
76 }
77 })
78 })
79
80 var _ = Describe("conns reaper", func() {
81 const idleTimeout = time.Minute
82
83 var connPool *pool.ConnPool
84 var conns, idleConns, closedConns []*pool.Conn
85
86 BeforeEach(func() {
87 connPool = pool.NewConnPool(
88 dummyDialer, 10, time.Second, idleTimeout, time.Hour)
89
90 closedConns = nil
91 connPool.OnClose = func(cn *pool.Conn) error {
92 closedConns = append(closedConns, cn)
93 return nil
94 }
95
96 conns = nil
97
98 // add stale connections
99 idleConns = nil
100 for i := 0; i < 3; i++ {
101 cn, _, err := connPool.Get()
102 Expect(err).NotTo(HaveOccurred())
103 cn.SetUsedAt(time.Now().Add(-2 * idleTimeout))
104 conns = append(conns, cn)
105 idleConns = append(idleConns, cn)
106 }
107
108 // add fresh connections
109 for i := 0; i < 3; i++ {
110 cn, _, err := connPool.Get()
111 Expect(err).NotTo(HaveOccurred())
112 conns = append(conns, cn)
113 }
114
115 for _, cn := range conns {
116 Expect(connPool.Put(cn)).NotTo(HaveOccurred())
117 }
118
119 Expect(connPool.Len()).To(Equal(6))
120 Expect(connPool.FreeLen()).To(Equal(6))
121
122 n, err := connPool.ReapStaleConns()
123 Expect(err).NotTo(HaveOccurred())
124 Expect(n).To(Equal(3))
125 })
126
127 AfterEach(func() {
128 _ = connPool.Close()
129 Expect(connPool.Len()).To(Equal(0))
130 Expect(connPool.FreeLen()).To(Equal(0))
131 Expect(len(closedConns)).To(Equal(len(conns)))
132 Expect(closedConns).To(ConsistOf(conns))
133 })
134
135 It("reaps stale connections", func() {
136 Expect(connPool.Len()).To(Equal(3))
137 Expect(connPool.FreeLen()).To(Equal(3))
138 })
139
140 It("does not reap fresh connections", func() {
141 n, err := connPool.ReapStaleConns()
142 Expect(err).NotTo(HaveOccurred())
143 Expect(n).To(Equal(0))
144 })
145
146 It("stale connections are closed", func() {
147 Expect(len(closedConns)).To(Equal(len(idleConns)))
148 Expect(closedConns).To(ConsistOf(idleConns))
149 })
150
151 It("pool is functional", func() {
152 for j := 0; j < 3; j++ {
153 var freeCns []*pool.Conn
154 for i := 0; i < 3; i++ {
155 cn, _, err := connPool.Get()
156 Expect(err).NotTo(HaveOccurred())
157 Expect(cn).NotTo(BeNil())
158 freeCns = append(freeCns, cn)
159 }
160
161 Expect(connPool.Len()).To(Equal(3))
162 Expect(connPool.FreeLen()).To(Equal(0))
163
164 cn, _, err := connPool.Get()
165 Expect(err).NotTo(HaveOccurred())
166 Expect(cn).NotTo(BeNil())
167 conns = append(conns, cn)
168
169 Expect(connPool.Len()).To(Equal(4))
170 Expect(connPool.FreeLen()).To(Equal(0))
171
172 err = connPool.Remove(cn, errors.New("test"))
173 Expect(err).NotTo(HaveOccurred())
174
175 Expect(connPool.Len()).To(Equal(3))
176 Expect(connPool.FreeLen()).To(Equal(0))
177
178 for _, cn := range freeCns {
179 err := connPool.Put(cn)
180 Expect(err).NotTo(HaveOccurred())
181 }
182
183 Expect(connPool.Len()).To(Equal(3))
184 Expect(connPool.FreeLen()).To(Equal(3))
185 }
186 })
187 })
188
189 var _ = Describe("race", func() {
190 var connPool *pool.ConnPool
191 var C, N int
192
193 BeforeEach(func() {
194 C, N = 10, 1000
195 if testing.Short() {
196 C = 4
197 N = 100
198 }
199 })
200
201 AfterEach(func() {
202 connPool.Close()
203 })
204
205 It("does not happen on Get, Put, and Remove", func() {
206 connPool = pool.NewConnPool(
207 dummyDialer, 10, time.Minute, time.Millisecond, time.Millisecond)
208
209 perform(C, func(id int) {
210 for i := 0; i < N; i++ {
211 cn, _, err := connPool.Get()
212 Expect(err).NotTo(HaveOccurred())
213 if err == nil {
214 Expect(connPool.Put(cn)).NotTo(HaveOccurred())
215 }
216 }
217 }, func(id int) {
218 for i := 0; i < N; i++ {
219 cn, _, err := connPool.Get()
220 Expect(err).NotTo(HaveOccurred())
221 if err == nil {
222 Expect(connPool.Remove(cn, errors.New("test"))).NotTo(HaveOccurred())
223 }
224 }
225 })
226 })
227
228 It("does not happen on Get and PopFree", func() {
229 connPool = pool.NewConnPool(
230 dummyDialer, 10, time.Minute, time.Second, time.Millisecond)
231
232 perform(C, func(id int) {
233 for i := 0; i < N; i++ {
234 cn, _, err := connPool.Get()
235 Expect(err).NotTo(HaveOccurred())
236 if err == nil {
237 Expect(connPool.Put(cn)).NotTo(HaveOccurred())
238 }
239
240 cn = connPool.PopFree()
241 if cn != nil {
242 Expect(connPool.Put(cn)).NotTo(HaveOccurred())
243 }
244 }
245 })
246 })
247 })