pool_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package redis_test
  2. import (
  3. "time"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. "gopkg.in/redis.v5"
  7. )
  8. var _ = Describe("pool", func() {
  9. var client *redis.Client
  10. BeforeEach(func() {
  11. client = redis.NewClient(redisOptions())
  12. Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
  13. })
  14. AfterEach(func() {
  15. Expect(client.Close()).NotTo(HaveOccurred())
  16. })
  17. It("respects max size", func() {
  18. perform(1000, func(id int) {
  19. val, err := client.Ping().Result()
  20. Expect(err).NotTo(HaveOccurred())
  21. Expect(val).To(Equal("PONG"))
  22. })
  23. pool := client.Pool()
  24. Expect(pool.Len()).To(BeNumerically("<=", 10))
  25. Expect(pool.FreeLen()).To(BeNumerically("<=", 10))
  26. Expect(pool.Len()).To(Equal(pool.FreeLen()))
  27. })
  28. It("respects max size on multi", func() {
  29. perform(1000, func(id int) {
  30. var ping *redis.StatusCmd
  31. err := client.Watch(func(tx *redis.Tx) error {
  32. cmds, err := tx.Pipelined(func(pipe *redis.Pipeline) error {
  33. ping = pipe.Ping()
  34. return nil
  35. })
  36. Expect(err).NotTo(HaveOccurred())
  37. Expect(cmds).To(HaveLen(1))
  38. return err
  39. })
  40. Expect(err).NotTo(HaveOccurred())
  41. Expect(ping.Err()).NotTo(HaveOccurred())
  42. Expect(ping.Val()).To(Equal("PONG"))
  43. })
  44. pool := client.Pool()
  45. Expect(pool.Len()).To(BeNumerically("<=", 10))
  46. Expect(pool.FreeLen()).To(BeNumerically("<=", 10))
  47. Expect(pool.Len()).To(Equal(pool.FreeLen()))
  48. })
  49. It("respects max size on pipelines", func() {
  50. perform(1000, func(id int) {
  51. pipe := client.Pipeline()
  52. ping := pipe.Ping()
  53. cmds, err := pipe.Exec()
  54. Expect(err).NotTo(HaveOccurred())
  55. Expect(cmds).To(HaveLen(1))
  56. Expect(ping.Err()).NotTo(HaveOccurred())
  57. Expect(ping.Val()).To(Equal("PONG"))
  58. Expect(pipe.Close()).NotTo(HaveOccurred())
  59. })
  60. pool := client.Pool()
  61. Expect(pool.Len()).To(BeNumerically("<=", 10))
  62. Expect(pool.FreeLen()).To(BeNumerically("<=", 10))
  63. Expect(pool.Len()).To(Equal(pool.FreeLen()))
  64. })
  65. It("respects max size on pubsub", func() {
  66. connPool := client.Pool()
  67. perform(1000, func(id int) {
  68. pubsub, err := client.Subscribe()
  69. Expect(err).NotTo(HaveOccurred())
  70. Expect(pubsub.Close()).NotTo(HaveOccurred())
  71. })
  72. Expect(connPool.Len()).To(Equal(connPool.FreeLen()))
  73. Expect(connPool.Len()).To(BeNumerically("<=", 10))
  74. })
  75. It("removes broken connections", func() {
  76. cn, _, err := client.Pool().Get()
  77. Expect(err).NotTo(HaveOccurred())
  78. cn.SetNetConn(&badConn{})
  79. Expect(client.Pool().Put(cn)).NotTo(HaveOccurred())
  80. err = client.Ping().Err()
  81. Expect(err).To(MatchError("bad connection"))
  82. val, err := client.Ping().Result()
  83. Expect(err).NotTo(HaveOccurred())
  84. Expect(val).To(Equal("PONG"))
  85. pool := client.Pool()
  86. Expect(pool.Len()).To(Equal(1))
  87. Expect(pool.FreeLen()).To(Equal(1))
  88. stats := pool.Stats()
  89. Expect(stats.Requests).To(Equal(uint32(4)))
  90. Expect(stats.Hits).To(Equal(uint32(2)))
  91. Expect(stats.Timeouts).To(Equal(uint32(0)))
  92. })
  93. It("reuses connections", func() {
  94. for i := 0; i < 100; i++ {
  95. val, err := client.Ping().Result()
  96. Expect(err).NotTo(HaveOccurred())
  97. Expect(val).To(Equal("PONG"))
  98. }
  99. pool := client.Pool()
  100. Expect(pool.Len()).To(Equal(1))
  101. Expect(pool.FreeLen()).To(Equal(1))
  102. stats := pool.Stats()
  103. Expect(stats.Requests).To(Equal(uint32(101)))
  104. Expect(stats.Hits).To(Equal(uint32(100)))
  105. Expect(stats.Timeouts).To(Equal(uint32(0)))
  106. })
  107. It("removes idle connections", func() {
  108. stats := client.PoolStats()
  109. Expect(stats).To(Equal(&redis.PoolStats{
  110. Requests: 1,
  111. Hits: 0,
  112. Timeouts: 0,
  113. TotalConns: 1,
  114. FreeConns: 1,
  115. }))
  116. time.Sleep(2 * time.Second)
  117. stats = client.PoolStats()
  118. Expect(stats).To(Equal(&redis.PoolStats{
  119. Requests: 1,
  120. Hits: 0,
  121. Timeouts: 0,
  122. TotalConns: 0,
  123. FreeConns: 0,
  124. }))
  125. })
  126. })