2015-01-15 18:51:22 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2015-04-17 14:44:56 +03:00
|
|
|
|
2016-04-09 13:27:16 +03:00
|
|
|
"gopkg.in/redis.v4"
|
|
|
|
"gopkg.in/redis.v4/internal/pool"
|
2015-01-15 18:51:22 +03:00
|
|
|
)
|
|
|
|
|
2015-10-13 12:02:29 +03:00
|
|
|
var _ = Describe("pool", func() {
|
2015-01-15 18:51:22 +03:00
|
|
|
var client *redis.Client
|
2015-04-17 14:44:56 +03: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()).NotTo(HaveOccurred())
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should respect max size", func() {
|
2016-03-16 17:57:24 +03:00
|
|
|
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))
|
2015-05-02 16:11:18 +03:00
|
|
|
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() {
|
2016-03-16 17:57:24 +03:00
|
|
|
perform(1000, func(id int) {
|
2015-01-15 18:51:22 +03:00
|
|
|
var ping *redis.StatusCmd
|
|
|
|
|
2016-04-09 11:23:58 +03:00
|
|
|
tx, err := client.Watch()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
cmds, err := tx.Exec(func() error {
|
|
|
|
ping = tx.Ping()
|
2015-01-15 18:51:22 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
Expect(ping.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(ping.Val()).To(Equal("PONG"))
|
2016-04-09 11:23:58 +03:00
|
|
|
Expect(tx.Close()).NotTo(HaveOccurred())
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
|
2015-01-31 10:53:08 +03:00
|
|
|
pool := client.Pool()
|
|
|
|
Expect(pool.Len()).To(BeNumerically("<=", 10))
|
2015-05-02 16:11:18 +03:00
|
|
|
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() {
|
2016-03-16 17:57:24 +03:00
|
|
|
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))
|
2015-05-02 16:11:18 +03:00
|
|
|
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() {
|
2016-03-16 17:57:24 +03:00
|
|
|
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())
|
|
|
|
})
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
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())
|
2016-03-14 14:17:33 +03:00
|
|
|
cn.NetConn = &badConn{}
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(client.Pool().Put(cn)).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
err = client.Ping().Err()
|
2016-03-14 14:17:33 +03:00
|
|
|
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))
|
2015-05-02 16:11:18 +03:00
|
|
|
Expect(pool.FreeLen()).To(Equal(1))
|
2016-03-09 14:38:33 +03:00
|
|
|
|
|
|
|
stats := pool.Stats()
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(stats.Requests).To(Equal(uint32(4)))
|
2016-03-17 19:00:47 +03:00
|
|
|
Expect(stats.Hits).To(Equal(uint32(2)))
|
2016-03-09 14:38:33 +03:00
|
|
|
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))
|
2015-05-02 16:11:18 +03:00
|
|
|
Expect(pool.FreeLen()).To(Equal(1))
|
2016-03-09 14:38:33 +03:00
|
|
|
|
|
|
|
stats := pool.Stats()
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(stats.Requests).To(Equal(uint32(101)))
|
|
|
|
Expect(stats.Hits).To(Equal(uint32(100)))
|
2016-03-09 14:38:33 +03:00
|
|
|
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
})
|