Debian Codegraph @

redis_test.go

raw
1 package redis_test
2
3 import (
4 "bytes"
5 "net"
6 "time"
7
8 "gopkg.in/redis.v5"
9
10 . "github.com/onsi/ginkgo"
11 . "github.com/onsi/gomega"
12 )
13
14 var _ = Describe("Client", func() {
15 var client *redis.Client
16
17 BeforeEach(func() {
18 client = redis.NewClient(redisOptions())
19 Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
20 })
21
22 AfterEach(func() {
23 client.Close()
24 })
25
26 It("should Stringer", func() {
27 Expect(client.String()).To(Equal("Redis<:6380 db:15>"))
28 })
29
30 It("should ping", func() {
31 val, err := client.Ping().Result()
32 Expect(err).NotTo(HaveOccurred())
33 Expect(val).To(Equal("PONG"))
34 })
35
36 It("should return pool stats", func() {
37 Expect(client.PoolStats()).To(BeAssignableToTypeOf(&redis.PoolStats{}))
38 })
39
40 It("should support custom dialers", func() {
41 custom := redis.NewClient(&redis.Options{
42 Addr: ":1234",
43 Dialer: func() (net.Conn, error) {
44 return net.Dial("tcp", redisAddr)
45 },
46 })
47
48 val, err := custom.Ping().Result()
49 Expect(err).NotTo(HaveOccurred())
50 Expect(val).To(Equal("PONG"))
51 Expect(custom.Close()).NotTo(HaveOccurred())
52 })
53
54 It("should close", func() {
55 Expect(client.Close()).NotTo(HaveOccurred())
56 err := client.Ping().Err()
57 Expect(err).To(MatchError("redis: client is closed"))
58 })
59
60 It("should close pubsub without closing the client", func() {
61 pubsub, err := client.Subscribe()
62 Expect(pubsub.Close()).NotTo(HaveOccurred())
63
64 _, err = pubsub.Receive()
65 Expect(err).To(MatchError("redis: client is closed"))
66 Expect(client.Ping().Err()).NotTo(HaveOccurred())
67 })
68
69 It("should close Tx without closing the client", func() {
70 err := client.Watch(func(tx *redis.Tx) error {
71 _, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
72 pipe.Ping()
73 return nil
74 })
75 return err
76 })
77 Expect(err).NotTo(HaveOccurred())
78
79 Expect(client.Ping().Err()).NotTo(HaveOccurred())
80 })
81
82 It("should close pipeline without closing the client", func() {
83 pipeline := client.Pipeline()
84 Expect(pipeline.Close()).NotTo(HaveOccurred())
85
86 pipeline.Ping()
87 _, err := pipeline.Exec()
88 Expect(err).To(MatchError("redis: client is closed"))
89
90 Expect(client.Ping().Err()).NotTo(HaveOccurred())
91 })
92
93 It("should close pubsub when client is closed", func() {
94 pubsub, err := client.Subscribe()
95 Expect(client.Close()).NotTo(HaveOccurred())
96
97 _, err = pubsub.Receive()
98 Expect(err).To(HaveOccurred())
99
100 Expect(pubsub.Close()).NotTo(HaveOccurred())
101 })
102
103 It("should close pipeline when client is closed", func() {
104 pipeline := client.Pipeline()
105 Expect(client.Close()).NotTo(HaveOccurred())
106 Expect(pipeline.Close()).NotTo(HaveOccurred())
107 })
108
109 It("should select DB", func() {
110 db2 := redis.NewClient(&redis.Options{
111 Addr: redisAddr,
112 DB: 2,
113 })
114 Expect(db2.FlushDb().Err()).NotTo(HaveOccurred())
115 Expect(db2.Get("db").Err()).To(Equal(redis.Nil))
116 Expect(db2.Set("db", 2, 0).Err()).NotTo(HaveOccurred())
117
118 n, err := db2.Get("db").Int64()
119 Expect(err).NotTo(HaveOccurred())
120 Expect(n).To(Equal(int64(2)))
121
122 Expect(client.Get("db").Err()).To(Equal(redis.Nil))
123
124 Expect(db2.FlushDb().Err()).NotTo(HaveOccurred())
125 Expect(db2.Close()).NotTo(HaveOccurred())
126 })
127
128 It("processes custom commands", func() {
129 cmd := redis.NewCmd("PING")
130 client.Process(cmd)
131
132 // Flush buffers.
133 Expect(client.Echo("hello").Err()).NotTo(HaveOccurred())
134
135 Expect(cmd.Err()).NotTo(HaveOccurred())
136 Expect(cmd.Val()).To(Equal("PONG"))
137 })
138
139 It("should retry command on network error", func() {
140 Expect(client.Close()).NotTo(HaveOccurred())
141
142 client = redis.NewClient(&redis.Options{
143 Addr: redisAddr,
144 MaxRetries: 1,
145 })
146
147 // Put bad connection in the pool.
148 cn, _, err := client.Pool().Get()
149 Expect(err).NotTo(HaveOccurred())
150
151 cn.SetNetConn(&badConn{})
152 err = client.Pool().Put(cn)
153 Expect(err).NotTo(HaveOccurred())
154
155 err = client.Ping().Err()
156 Expect(err).NotTo(HaveOccurred())
157 })
158
159 It("should update conn.UsedAt on read/write", func() {
160 cn, _, err := client.Pool().Get()
161 Expect(err).NotTo(HaveOccurred())
162 Expect(cn.UsedAt).NotTo(BeZero())
163 createdAt := cn.UsedAt()
164
165 err = client.Pool().Put(cn)
166 Expect(err).NotTo(HaveOccurred())
167 Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue())
168
169 err = client.Ping().Err()
170 Expect(err).NotTo(HaveOccurred())
171
172 cn, _, err = client.Pool().Get()
173 Expect(err).NotTo(HaveOccurred())
174 Expect(cn).NotTo(BeNil())
175 Expect(cn.UsedAt().After(createdAt)).To(BeTrue())
176 })
177
178 It("should process command with special chars", func() {
179 set := client.Set("key", "hello1\r\nhello2\r\n", 0)
180 Expect(set.Err()).NotTo(HaveOccurred())
181 Expect(set.Val()).To(Equal("OK"))
182
183 get := client.Get("key")
184 Expect(get.Err()).NotTo(HaveOccurred())
185 Expect(get.Val()).To(Equal("hello1\r\nhello2\r\n"))
186 })
187
188 It("should handle big vals", func() {
189 bigVal := bytes.Repeat([]byte{'*'}, 2e6)
190
191 err := client.Set("key", bigVal, 0).Err()
192 Expect(err).NotTo(HaveOccurred())
193
194 // Reconnect to get new connection.
195 Expect(client.Close()).NotTo(HaveOccurred())
196 client = redis.NewClient(redisOptions())
197
198 got, err := client.Get("key").Bytes()
199 Expect(err).NotTo(HaveOccurred())
200 Expect(got).To(Equal(bigVal))
201 })
202
203 It("should call WrapProcess", func() {
204 var wrapperFnCalled bool
205
206 client.WrapProcess(func(oldProcess func(redis.Cmder) error) func(redis.Cmder) error {
207 return func(cmd redis.Cmder) error {
208 wrapperFnCalled = true
209 return oldProcess(cmd)
210 }
211 })
212
213 Expect(client.Ping().Err()).NotTo(HaveOccurred())
214
215 Expect(wrapperFnCalled).To(BeTrue())
216 })
217 })
218
219 var _ = Describe("Client timeout", func() {
220 var client *redis.Client
221
222 AfterEach(func() {
223 Expect(client.Close()).NotTo(HaveOccurred())
224 })
225
226 testTimeout := func() {
227 It("Ping timeouts", func() {
228 err := client.Ping().Err()
229 Expect(err).To(HaveOccurred())
230 Expect(err.(net.Error).Timeout()).To(BeTrue())
231 })
232
233 It("Pipeline timeouts", func() {
234 _, err := client.Pipelined(func(pipe *redis.Pipeline) error {
235 pipe.Ping()
236 return nil
237 })
238 Expect(err).To(HaveOccurred())
239 Expect(err.(net.Error).Timeout()).To(BeTrue())
240 })
241
242 It("Subscribe timeouts", func() {
243 _, err := client.Subscribe("_")
244 Expect(err).To(HaveOccurred())
245 Expect(err.(net.Error).Timeout()).To(BeTrue())
246 })
247
248 It("Tx timeouts", func() {
249 err := client.Watch(func(tx *redis.Tx) error {
250 return tx.Ping().Err()
251 })
252 Expect(err).To(HaveOccurred())
253 Expect(err.(net.Error).Timeout()).To(BeTrue())
254 })
255
256 It("Tx Pipeline timeouts", func() {
257 err := client.Watch(func(tx *redis.Tx) error {
258 _, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
259 pipe.Ping()
260 return nil
261 })
262 return err
263 })
264 Expect(err).To(HaveOccurred())
265 Expect(err.(net.Error).Timeout()).To(BeTrue())
266 })
267 }
268
269 Context("read timeout", func() {
270 BeforeEach(func() {
271 opt := redisOptions()
272 opt.ReadTimeout = time.Nanosecond
273 opt.WriteTimeout = -1
274 client = redis.NewClient(opt)
275 })
276
277 testTimeout()
278 })
279
280 Context("write timeout", func() {
281 BeforeEach(func() {
282 opt := redisOptions()
283 opt.ReadTimeout = -1
284 opt.WriteTimeout = time.Nanosecond
285 client = redis.NewClient(opt)
286 })
287
288 testTimeout()
289 })
290 })