redis/pool_test.go

132 lines
3.4 KiB
Go
Raw Normal View History

2015-01-15 18:51:22 +03:00
package redis_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2015-05-14 15:19:29 +03:00
"gopkg.in/redis.v3"
"gopkg.in/redis.v3/internal/pool"
2015-01-15 18:51:22 +03:00
)
var _ = Describe("pool", func() {
2015-01-15 18:51:22 +03:00
var client *redis.Client
2015-01-15 18:51:22 +03:00
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("should respect max size", func() {
perform(1000, func(id int) {
2015-01-15 18:51:22 +03:00
val, err := client.Ping().Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("PONG"))
})
2015-01-31 10:53:08 +03:00
2015-01-30 17:58:26 +03:00
pool := client.Pool()
Expect(pool.Len()).To(BeNumerically("<=", 10))
Expect(pool.FreeLen()).To(BeNumerically("<=", 10))
Expect(pool.Len()).To(Equal(pool.FreeLen()))
2015-01-15 18:51:22 +03:00
})
It("should respect max on multi", func() {
perform(1000, func(id int) {
2015-01-15 18:51:22 +03:00
var ping *redis.StatusCmd
multi := client.Multi()
cmds, err := multi.Exec(func() error {
ping = multi.Ping()
return nil
})
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(1))
Expect(ping.Err()).NotTo(HaveOccurred())
Expect(ping.Val()).To(Equal("PONG"))
Expect(multi.Close()).NotTo(HaveOccurred())
})
2015-01-31 10:53:08 +03:00
pool := client.Pool()
Expect(pool.Len()).To(BeNumerically("<=", 10))
Expect(pool.FreeLen()).To(BeNumerically("<=", 10))
Expect(pool.Len()).To(Equal(pool.FreeLen()))
2015-01-15 18:51:22 +03:00
})
It("should respect max on pipelines", func() {
perform(1000, func(id int) {
2015-01-15 18:51:22 +03:00
pipe := client.Pipeline()
ping := pipe.Ping()
cmds, err := pipe.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(1))
Expect(ping.Err()).NotTo(HaveOccurred())
Expect(ping.Val()).To(Equal("PONG"))
Expect(pipe.Close()).NotTo(HaveOccurred())
})
2015-01-31 10:53:08 +03:00
pool := client.Pool()
Expect(pool.Len()).To(BeNumerically("<=", 10))
Expect(pool.FreeLen()).To(BeNumerically("<=", 10))
Expect(pool.Len()).To(Equal(pool.FreeLen()))
2015-01-15 18:51:22 +03:00
})
It("should respect max on pubsub", func() {
connPool := client.Pool()
connPool.(*pool.ConnPool).DialLimiter = nil
perform(1000, func(id int) {
2015-01-15 18:51:22 +03:00
pubsub := client.PubSub()
Expect(pubsub.Subscribe()).NotTo(HaveOccurred())
Expect(pubsub.Close()).NotTo(HaveOccurred())
})
Expect(connPool.Len()).To(Equal(connPool.FreeLen()))
Expect(connPool.Len()).To(BeNumerically("<=", 10))
2015-01-15 18:51:22 +03:00
})
It("should remove broken connections", func() {
2016-03-15 15:04:35 +03:00
cn, err := client.Pool().Get()
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
cn.NetConn = &badConn{}
2015-01-15 18:51:22 +03:00
Expect(client.Pool().Put(cn)).NotTo(HaveOccurred())
err = client.Ping().Err()
Expect(err).To(MatchError("bad connection"))
2015-01-15 18:51:22 +03:00
val, err := client.Ping().Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("PONG"))
2015-01-31 10:53:08 +03:00
pool := client.Pool()
Expect(pool.Len()).To(Equal(1))
Expect(pool.FreeLen()).To(Equal(1))
stats := pool.Stats()
Expect(stats.Requests).To(Equal(uint32(4)))
Expect(stats.Hits).To(Equal(uint32(3)))
Expect(stats.Waits).To(Equal(uint32(0)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
2015-01-15 18:51:22 +03:00
})
It("should reuse connections", func() {
for i := 0; i < 100; i++ {
val, err := client.Ping().Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("PONG"))
}
2015-01-31 10:53:08 +03:00
pool := client.Pool()
Expect(pool.Len()).To(Equal(1))
Expect(pool.FreeLen()).To(Equal(1))
stats := pool.Stats()
Expect(stats.Requests).To(Equal(uint32(101)))
Expect(stats.Hits).To(Equal(uint32(100)))
Expect(stats.Waits).To(Equal(uint32(0)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
2015-01-15 18:51:22 +03:00
})
})