1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

internal/pool: fix reaper to properly close connections.

This commit is contained in:
Vladimir Mihailenco
2016-09-10 13:54:49 +00:00
parent 788a36eee7
commit aab4e040c6
2 changed files with 64 additions and 32 deletions

View File

@ -94,20 +94,31 @@ var _ = Describe("ConnPool", func() {
})
var _ = Describe("conns reaper", func() {
const idleTimeout = time.Minute
var connPool *pool.ConnPool
var idleConns, closedConns []*pool.Conn
BeforeEach(func() {
connPool = pool.NewConnPool(
dummyDialer, 10, time.Second, time.Millisecond, time.Hour)
dummyDialer, 10, time.Second, idleTimeout, time.Hour)
closedConns = nil
connPool.OnClose = func(cn *pool.Conn) error {
closedConns = append(closedConns, cn)
return nil
}
var cns []*pool.Conn
// add stale connections
idleConns = nil
for i := 0; i < 3; i++ {
cn, err := connPool.Get()
Expect(err).NotTo(HaveOccurred())
cn.UsedAt = time.Now().Add(-2 * time.Minute)
cn.UsedAt = time.Now().Add(-2 * idleTimeout)
cns = append(cns, cn)
idleConns = append(idleConns, cn)
}
// add fresh connections
@ -139,6 +150,17 @@ var _ = Describe("conns reaper", func() {
Expect(connPool.FreeLen()).To(Equal(3))
})
It("does not reap fresh connections", func() {
n, err := connPool.ReapStaleConns()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(0))
})
It("stale connections are closed", func() {
Expect(closedConns).To(HaveLen(3))
Expect(closedConns).To(ConsistOf(idleConns))
})
It("pool is functional", func() {
for j := 0; j < 3; j++ {
var freeCns []*pool.Conn