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