2012-07-25 17:00:50 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2016-03-16 17:57:24 +03:00
|
|
|
"bytes"
|
2012-08-06 12:33:49 +04:00
|
|
|
"net"
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2012-08-09 15:51:43 +04:00
|
|
|
|
2016-10-09 13:49:28 +03:00
|
|
|
"gopkg.in/redis.v5"
|
2015-05-14 16:13:45 +03:00
|
|
|
)
|
2015-04-28 18:14:19 +03:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
var _ = Describe("Client", func() {
|
|
|
|
var client *redis.Client
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
BeforeEach(func() {
|
2016-03-16 17:57:24 +03:00
|
|
|
client = redis.NewClient(redisOptions())
|
|
|
|
Expect(client.FlushDb().Err()).To(BeNil())
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
AfterEach(func() {
|
2015-05-02 16:11:18 +03:00
|
|
|
client.Close()
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
|
|
|
|
2015-05-15 15:21:28 +03:00
|
|
|
It("should Stringer", func() {
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(client.String()).To(Equal("Redis<:6380 db:15>"))
|
2015-05-15 15:21:28 +03:00
|
|
|
})
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
It("should ping", func() {
|
|
|
|
val, err := client.Ping().Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("PONG"))
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
2013-02-02 14:50:43 +04:00
|
|
|
|
2016-01-19 19:36:40 +03:00
|
|
|
It("should return pool stats", func() {
|
|
|
|
Expect(client.PoolStats()).To(BeAssignableToTypeOf(&redis.PoolStats{}))
|
|
|
|
})
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
It("should support custom dialers", func() {
|
|
|
|
custom := redis.NewClient(&redis.Options{
|
2016-03-16 17:57:24 +03:00
|
|
|
Addr: ":1234",
|
2015-01-15 18:51:22 +03:00
|
|
|
Dialer: func() (net.Conn, error) {
|
|
|
|
return net.Dial("tcp", redisAddr)
|
|
|
|
},
|
|
|
|
})
|
2014-09-30 12:46:56 +04:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
val, err := custom.Ping().Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("PONG"))
|
|
|
|
Expect(custom.Close()).NotTo(HaveOccurred())
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
2012-08-25 16:35:39 +04:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
It("should close", func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
err := client.Ping().Err()
|
|
|
|
Expect(err).To(MatchError("redis: client is closed"))
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
2012-08-25 16:35:39 +04:00
|
|
|
|
2016-03-02 14:26:05 +03:00
|
|
|
It("should close pubsub without closing the client", func() {
|
2016-07-21 16:04:40 +03:00
|
|
|
pubsub, err := client.Subscribe()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(pubsub.Close()).NotTo(HaveOccurred())
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2016-07-21 16:04:40 +03:00
|
|
|
_, err = pubsub.Receive()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).To(MatchError("redis: client is closed"))
|
|
|
|
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
It("should close Tx without closing the client", func() {
|
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
2016-10-13 14:36:15 +03:00
|
|
|
_, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
|
|
|
|
pipe.Ping()
|
2016-05-02 15:54:15 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
2016-05-02 15:54:15 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-02 14:26:05 +03:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
|
|
|
|
2016-03-02 14:26:05 +03:00
|
|
|
It("should close pipeline without closing the client", func() {
|
2015-01-15 18:51:22 +03:00
|
|
|
pipeline := client.Pipeline()
|
|
|
|
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
2014-05-11 11:42:40 +04:00
|
|
|
|
|
|
|
pipeline.Ping()
|
2015-01-15 18:51:22 +03:00
|
|
|
_, err := pipeline.Exec()
|
|
|
|
Expect(err).To(MatchError("redis: client is closed"))
|
2016-03-02 14:26:05 +03:00
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
2015-06-03 16:45:46 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should close pubsub when client is closed", func() {
|
2016-07-21 16:04:40 +03:00
|
|
|
pubsub, err := client.Subscribe()
|
2015-06-03 16:45:46 +03:00
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
2016-07-21 16:04:40 +03:00
|
|
|
|
|
|
|
_, err = pubsub.Receive()
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
2015-06-03 16:45:46 +03:00
|
|
|
Expect(pubsub.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should close pipeline when client is closed", func() {
|
|
|
|
pipeline := client.Pipeline()
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
2014-05-11 11:42:40 +04:00
|
|
|
})
|
2014-07-08 12:24:19 +04:00
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
It("should select DB", func() {
|
|
|
|
db2 := redis.NewClient(&redis.Options{
|
2015-01-15 18:51:22 +03:00
|
|
|
Addr: redisAddr,
|
2016-03-16 17:57:24 +03:00
|
|
|
DB: 2,
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(db2.FlushDb().Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(db2.Get("db").Err()).To(Equal(redis.Nil))
|
|
|
|
Expect(db2.Set("db", 2, 0).Err()).NotTo(HaveOccurred())
|
2012-08-11 18:42:10 +04:00
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
n, err := db2.Get("db").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(2)))
|
2012-08-26 13:18:42 +04:00
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(client.Get("db").Err()).To(Equal(redis.Nil))
|
2013-12-30 15:45:04 +04:00
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(db2.FlushDb().Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(db2.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
2015-07-13 13:45:37 +03:00
|
|
|
|
2016-04-12 19:41:56 +03:00
|
|
|
It("processes custom commands", func() {
|
2016-03-16 17:57:24 +03:00
|
|
|
cmd := redis.NewCmd("PING")
|
|
|
|
client.Process(cmd)
|
2016-04-12 19:41:56 +03:00
|
|
|
|
|
|
|
// Flush buffers.
|
|
|
|
Expect(client.Echo("hello").Err()).NotTo(HaveOccurred())
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(cmd.Err()).NotTo(HaveOccurred())
|
2016-04-12 19:41:56 +03:00
|
|
|
Expect(cmd.Val()).To(Equal("PONG"))
|
2015-07-13 13:45:37 +03:00
|
|
|
})
|
|
|
|
|
2015-05-10 15:33:04 +03:00
|
|
|
It("should retry command on network error", func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
client = redis.NewClient(&redis.Options{
|
|
|
|
Addr: redisAddr,
|
|
|
|
MaxRetries: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Put bad connection in the pool.
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, _, err := client.Pool().Get()
|
2015-05-10 15:33:04 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-09-06 13:50:16 +03:00
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
cn.NetConn = &badConn{}
|
2015-10-13 12:02:29 +03:00
|
|
|
err = client.Pool().Put(cn)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-10 15:33:04 +03:00
|
|
|
|
|
|
|
err = client.Ping().Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
2016-03-02 14:26:05 +03:00
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
It("should update conn.UsedAt on read/write", func() {
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, _, err := client.Pool().Get()
|
2016-03-02 14:26:05 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-04 11:03:50 +03:00
|
|
|
Expect(cn.UsedAt).NotTo(BeZero())
|
|
|
|
createdAt := cn.UsedAt
|
|
|
|
|
2016-03-02 14:26:05 +03:00
|
|
|
err = client.Pool().Put(cn)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-04 11:03:50 +03:00
|
|
|
Expect(cn.UsedAt.Equal(createdAt)).To(BeTrue())
|
2016-03-02 14:26:05 +03:00
|
|
|
|
|
|
|
err = client.Ping().Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, _, err = client.Pool().Get()
|
2016-03-17 19:00:47 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-02 14:26:05 +03:00
|
|
|
Expect(cn).NotTo(BeNil())
|
2016-03-12 11:52:13 +03:00
|
|
|
Expect(cn.UsedAt.After(createdAt)).To(BeTrue())
|
2016-03-02 14:26:05 +03:00
|
|
|
})
|
2016-03-16 17:57:24 +03:00
|
|
|
|
|
|
|
It("should escape special chars", func() {
|
|
|
|
set := client.Set("key", "hello1\r\nhello2\r\n", 0)
|
|
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
|
|
|
|
get := client.Get("key")
|
|
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(get.Val()).To(Equal("hello1\r\nhello2\r\n"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should handle big vals", func() {
|
|
|
|
bigVal := string(bytes.Repeat([]byte{'*'}, 1<<17)) // 128kb
|
|
|
|
|
|
|
|
err := client.Set("key", bigVal, 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
// Reconnect to get new connection.
|
|
|
|
Expect(client.Close()).To(BeNil())
|
|
|
|
client = redis.NewClient(redisOptions())
|
|
|
|
|
|
|
|
got, err := client.Get("key").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(got)).To(Equal(len(bigVal)))
|
|
|
|
Expect(got).To(Equal(bigVal))
|
|
|
|
})
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|