redis/redis_test.go

365 lines
8.6 KiB
Go
Raw Permalink Normal View History

2012-07-25 17:00:50 +04:00
package redis_test
import (
"bytes"
"net"
"time"
2017-02-18 17:42:34 +03:00
"github.com/go-redis/redis"
2012-07-25 17:00:50 +04:00
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
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() {
client = redis.NewClient(redisOptions())
2017-06-17 12:53:16 +03:00
Expect(client.FlushDB().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
AfterEach(func() {
client.Close()
2014-05-11 11:42:40 +04:00
})
2015-05-15 15:21:28 +03:00
It("should Stringer", func() {
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
})
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{
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
It("should close pubsub without closing the client", func() {
2017-04-11 16:53:55 +03:00
pubsub := client.Subscribe()
2015-01-15 18:51:22 +03:00
Expect(pubsub.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
2017-04-11 16:53:55 +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 {
2017-05-02 18:00:53 +03:00
_, err := tx.Pipelined(func(pipe redis.Pipeliner) 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())
2015-01-15 18:51:22 +03:00
Expect(client.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04: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"))
2015-01-15 18:51:22 +03:00
Expect(client.Ping().Err()).NotTo(HaveOccurred())
})
It("should close pubsub when client is closed", func() {
2017-04-11 16:53:55 +03:00
pubsub := client.Subscribe()
Expect(client.Close()).NotTo(HaveOccurred())
2017-04-11 16:53:55 +03:00
_, err := pubsub.Receive()
Expect(err).To(MatchError("redis: client is closed"))
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
It("should select DB", func() {
db2 := redis.NewClient(&redis.Options{
2015-01-15 18:51:22 +03:00
Addr: redisAddr,
DB: 2,
2015-01-15 18:51:22 +03:00
})
2017-06-17 12:53:16 +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())
n, err := db2.Get("db").Int64()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
2012-08-26 13:18:42 +04:00
Expect(client.Get("db").Err()).To(Equal(redis.Nil))
2013-12-30 15:45:04 +04:00
2017-06-17 12:53:16 +03:00
Expect(db2.FlushDB().Err()).NotTo(HaveOccurred())
Expect(db2.Close()).NotTo(HaveOccurred())
})
2016-04-12 19:41:56 +03:00
It("processes custom commands", func() {
cmd := redis.NewCmd("PING")
client.Process(cmd)
2016-04-12 19:41:56 +03:00
// Flush buffers.
Expect(client.Echo("hello").Err()).NotTo(HaveOccurred())
Expect(cmd.Err()).NotTo(HaveOccurred())
2016-04-12 19:41:56 +03:00
Expect(cmd.Val()).To(Equal("PONG"))
})
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()
Expect(err).NotTo(HaveOccurred())
2015-09-06 13:50:16 +03:00
cn.SetNetConn(&badConn{})
err = client.Pool().Put(cn)
Expect(err).NotTo(HaveOccurred())
err = client.Ping().Err()
Expect(err).NotTo(HaveOccurred())
})
2017-05-25 08:08:44 +03:00
It("should retry with backoff", func() {
Expect(client.Close()).NotTo(HaveOccurred())
// use up all the available connections to force a fail
connectionHogClient := redis.NewClient(&redis.Options{
Addr: redisAddr,
MaxRetries: 1,
})
defer connectionHogClient.Close()
for i := 0; i <= 1002; i++ {
connectionHogClient.Pool().NewConn()
}
clientNoRetry := redis.NewClient(&redis.Options{
Addr: redisAddr,
PoolSize: 1,
MaxRetryBackoff: -1,
})
defer clientNoRetry.Close()
clientRetry := redis.NewClient(&redis.Options{
Addr: redisAddr,
MaxRetries: 5,
PoolSize: 1,
MaxRetryBackoff: 128 * time.Millisecond,
})
defer clientRetry.Close()
startNoRetry := time.Now()
err := clientNoRetry.Ping().Err()
Expect(err).To(HaveOccurred())
elapseNoRetry := time.Since(startNoRetry)
startRetry := time.Now()
err = clientRetry.Ping().Err()
Expect(err).To(HaveOccurred())
elapseRetry := time.Since(startRetry)
Expect(elapseRetry > elapseNoRetry).To(BeTrue())
})
It("should update conn.UsedAt on read/write", func() {
2016-09-29 15:07:04 +03:00
cn, _, err := client.Pool().Get()
Expect(err).NotTo(HaveOccurred())
Expect(cn.UsedAt).NotTo(BeZero())
createdAt := cn.UsedAt()
err = client.Pool().Put(cn)
Expect(err).NotTo(HaveOccurred())
Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue())
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())
Expect(cn).NotTo(BeNil())
Expect(cn.UsedAt().After(createdAt)).To(BeTrue())
})
It("should process command with 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() {
2016-11-09 11:04:37 +03:00
bigVal := bytes.Repeat([]byte{'*'}, 2e6)
err := client.Set("key", bigVal, 0).Err()
Expect(err).NotTo(HaveOccurred())
// Reconnect to get new connection.
Expect(client.Close()).NotTo(HaveOccurred())
client = redis.NewClient(redisOptions())
2016-11-09 11:04:37 +03:00
got, err := client.Get("key").Bytes()
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(bigVal))
})
2016-12-30 13:58:04 +03:00
It("should call WrapProcess", func() {
var wrapperFnCalled bool
client.WrapProcess(func(oldProcess func(redis.Cmder) error) func(redis.Cmder) error {
return func(cmd redis.Cmder) error {
wrapperFnCalled = true
return oldProcess(cmd)
}
})
Expect(client.Ping().Err()).NotTo(HaveOccurred())
Expect(wrapperFnCalled).To(BeTrue())
})
})
var _ = Describe("Client timeout", func() {
var opt *redis.Options
var client *redis.Client
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
testTimeout := func() {
It("Ping timeouts", func() {
err := client.Ping().Err()
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
It("Pipeline timeouts", func() {
2017-05-02 18:00:53 +03:00
_, err := client.Pipelined(func(pipe redis.Pipeliner) error {
pipe.Ping()
return nil
})
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
It("Subscribe timeouts", func() {
if opt.WriteTimeout == 0 {
return
}
2017-04-11 16:53:55 +03:00
pubsub := client.Subscribe()
defer pubsub.Close()
2017-04-11 16:53:55 +03:00
err := pubsub.Subscribe("_")
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
It("Tx timeouts", func() {
err := client.Watch(func(tx *redis.Tx) error {
return tx.Ping().Err()
})
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
It("Tx Pipeline timeouts", func() {
err := client.Watch(func(tx *redis.Tx) error {
2017-05-02 18:00:53 +03:00
_, err := tx.Pipelined(func(pipe redis.Pipeliner) error {
pipe.Ping()
return nil
})
return err
})
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
})
}
Context("read timeout", func() {
BeforeEach(func() {
opt = redisOptions()
opt.ReadTimeout = time.Nanosecond
opt.WriteTimeout = -1
client = redis.NewClient(opt)
})
testTimeout()
})
Context("write timeout", func() {
BeforeEach(func() {
opt = redisOptions()
opt.ReadTimeout = -1
opt.WriteTimeout = time.Nanosecond
client = redis.NewClient(opt)
})
testTimeout()
})
2015-01-15 18:51:22 +03:00
})
2017-05-25 14:16:39 +03:00
var _ = Describe("Client OnConnect", func() {
var client *redis.Client
BeforeEach(func() {
opt := redisOptions()
opt.DB = 0
2017-05-25 14:16:39 +03:00
opt.OnConnect = func(cn *redis.Conn) error {
return cn.ClientSetName("on_connect").Err()
}
client = redis.NewClient(opt)
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("calls OnConnect", func() {
name, err := client.ClientGetName().Result()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("on_connect"))
})
})