redis/redis_test.go

360 lines
7.7 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis_test
import (
2015-06-06 10:19:51 +03:00
"bytes"
"net"
2012-07-25 17:00:50 +04:00
"testing"
"time"
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2015-05-14 15:19:29 +03:00
"gopkg.in/redis.v3"
)
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() {
2015-05-02 16:19:22 +03:00
client = redis.NewClient(&redis.Options{
2015-01-15 18:51:22 +03:00
Addr: redisAddr,
})
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:0>"))
})
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
})
2015-01-15 18:51:22 +03:00
It("should support custom dialers", func() {
custom := redis.NewClient(&redis.Options{
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(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
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 pubsub without closing the connection", func() {
pubsub := client.PubSub()
Expect(pubsub.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
2015-01-15 18:51:22 +03:00
_, err := pubsub.Receive()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
Expect(client.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should close multi without closing the connection", func() {
multi := client.Multi()
Expect(multi.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
2015-01-15 18:51:22 +03:00
_, err := multi.Exec(func() error {
multi.Ping()
return nil
})
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
Expect(client.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should close pipeline without closing the connection", func() {
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(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
Expect(client.Ping().Err()).NotTo(HaveOccurred())
})
It("should close pubsub when client is closed", func() {
pubsub := client.PubSub()
Expect(client.Close()).NotTo(HaveOccurred())
Expect(pubsub.Close()).NotTo(HaveOccurred())
})
It("should close multi when client is closed", func() {
multi := client.Multi()
Expect(client.Close()).NotTo(HaveOccurred())
Expect(multi.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
2015-01-15 18:51:22 +03:00
It("should support idle-timeouts", func() {
2015-05-02 16:19:22 +03:00
idle := redis.NewClient(&redis.Options{
2015-01-15 18:51:22 +03:00
Addr: redisAddr,
IdleTimeout: 100 * time.Microsecond,
2015-01-15 18:51:22 +03:00
})
defer idle.Close()
2014-05-11 11:42:40 +04:00
2015-01-15 18:51:22 +03:00
Expect(idle.Ping().Err()).NotTo(HaveOccurred())
time.Sleep(time.Millisecond)
Expect(idle.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should support DB selection", func() {
2015-05-02 16:19:22 +03:00
db1 := redis.NewClient(&redis.Options{
2015-01-15 18:51:22 +03:00
Addr: redisAddr,
DB: 1,
})
defer db1.Close()
2015-01-15 18:51:22 +03:00
Expect(db1.Get("key").Err()).To(Equal(redis.Nil))
Expect(db1.Set("key", "value", 0).Err()).NotTo(HaveOccurred())
2012-08-26 13:18:42 +04:00
2015-01-15 18:51:22 +03:00
Expect(client.Get("key").Err()).To(Equal(redis.Nil))
Expect(db1.Get("key").Val()).To(Equal("value"))
Expect(db1.FlushDb().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2013-12-30 15:45:04 +04:00
It("should support DB selection with read timeout (issue #135)", func() {
for i := 0; i < 100; i++ {
db1 := redis.NewClient(&redis.Options{
Addr: redisAddr,
DB: 1,
ReadTimeout: time.Nanosecond,
})
err := db1.Ping().Err()
Expect(err).To(HaveOccurred())
Expect(err.(net.Error).Timeout()).To(BeTrue())
}
})
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.
cn, err := client.Pool().Get()
Expect(err).NotTo(HaveOccurred())
cn.SetNetConn(newBadNetConn())
Expect(client.Pool().Put(cn)).NotTo(HaveOccurred())
err = client.Ping().Err()
Expect(err).NotTo(HaveOccurred())
})
2015-01-15 18:51:22 +03:00
})
2014-05-11 11:42:40 +04:00
2012-08-17 22:36:48 +04:00
//------------------------------------------------------------------------------
2012-07-26 19:16:17 +04:00
2015-06-06 10:19:51 +03:00
const benchRedisAddr = ":6379"
func BenchmarkRedisPing(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Ping().Err(); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkRedisSet(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
2015-06-06 10:19:51 +03:00
value := string(bytes.Repeat([]byte{'1'}, 10000))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2015-06-06 10:19:51 +03:00
if err := client.Set("key", value, 0).Err(); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkRedisGetNil(b *testing.B) {
2015-06-06 10:19:51 +03:00
client := redis.NewClient(&redis.Options{
Addr: benchRedisAddr,
})
defer client.Close()
if err := client.FlushDb().Err(); err != nil {
b.Fatal(err)
}
2015-06-06 10:19:51 +03:00
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Get("key").Err(); err != redis.Nil {
b.Fatal(err)
}
}
})
}
func BenchmarkRedisGet(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
value := bytes.Repeat([]byte{'1'}, 10000)
if err := client.Set("key", value, 0).Err(); err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
s, err := client.Get("key").Result()
if err != nil {
b.Fatal(err)
}
if len(s) != 10000 {
panic("len(s) != 10000")
}
}
})
}
func BenchmarkRedisGetSetBytes(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
src := bytes.Repeat([]byte{'1'}, 10000)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Set("key", src, 0).Err(); err != nil {
b.Fatal(err)
}
dst, err := client.Get("key").Bytes()
if err != nil {
b.Fatal(err)
}
if !bytes.Equal(dst, src) {
panic("len(dst) != 10000")
}
}
})
}
func BenchmarkRedisMGet(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.MGet("key1", "key2").Err(); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkSetExpire(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.Set("key", "hello", 0).Err(); err != nil {
b.Fatal(err)
}
if err := client.Expire("key", time.Second).Err(); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkPipeline(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
pipe.Set("key", "hello", 0)
pipe.Expire("key", time.Second)
return nil
})
if err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkZAdd(b *testing.B) {
client := redis.NewClient(&redis.Options{
2015-06-06 10:19:51 +03:00
Addr: benchRedisAddr,
})
defer client.Close()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := client.ZAdd("key", redis.Z{float64(1), "hello"}).Err(); err != nil {
b.Fatal(err)
}
}
})
}