internal/pool: improve tests.

This commit is contained in:
Vladimir Mihailenco 2016-09-10 14:09:39 +00:00
parent 5f82b2acc7
commit 4b0862b5fd
1 changed files with 12 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package pool_test
import ( import (
"errors" "errors"
"net"
"testing" "testing"
"time" "time"
@ -97,7 +96,7 @@ var _ = Describe("conns reaper", func() {
const idleTimeout = time.Minute const idleTimeout = time.Minute
var connPool *pool.ConnPool var connPool *pool.ConnPool
var idleConns, closedConns []*pool.Conn var conns, idleConns, closedConns []*pool.Conn
BeforeEach(func() { BeforeEach(func() {
connPool = pool.NewConnPool( connPool = pool.NewConnPool(
@ -109,7 +108,7 @@ var _ = Describe("conns reaper", func() {
return nil return nil
} }
var cns []*pool.Conn conns = nil
// add stale connections // add stale connections
idleConns = nil idleConns = nil
@ -117,19 +116,18 @@ var _ = Describe("conns reaper", func() {
cn, err := connPool.Get() cn, err := connPool.Get()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
cn.UsedAt = time.Now().Add(-2 * idleTimeout) cn.UsedAt = time.Now().Add(-2 * idleTimeout)
cns = append(cns, cn) conns = append(conns, cn)
idleConns = append(idleConns, cn) idleConns = append(idleConns, cn)
} }
// add fresh connections // add fresh connections
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
cn := pool.NewConn(&net.TCPConn{})
cn, err := connPool.Get() cn, err := connPool.Get()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
cns = append(cns, cn) conns = append(conns, cn)
} }
for _, cn := range cns { for _, cn := range conns {
Expect(connPool.Put(cn)).NotTo(HaveOccurred()) Expect(connPool.Put(cn)).NotTo(HaveOccurred())
} }
@ -142,7 +140,11 @@ var _ = Describe("conns reaper", func() {
}) })
AfterEach(func() { AfterEach(func() {
connPool.Close() _ = connPool.Close()
Expect(connPool.Len()).To(Equal(0))
Expect(connPool.FreeLen()).To(Equal(0))
Expect(len(closedConns)).To(Equal(len(conns)))
Expect(closedConns).To(ConsistOf(conns))
}) })
It("reaps stale connections", func() { It("reaps stale connections", func() {
@ -157,7 +159,7 @@ var _ = Describe("conns reaper", func() {
}) })
It("stale connections are closed", func() { It("stale connections are closed", func() {
Expect(closedConns).To(HaveLen(3)) Expect(len(closedConns)).To(Equal(len(idleConns)))
Expect(closedConns).To(ConsistOf(idleConns)) Expect(closedConns).To(ConsistOf(idleConns))
}) })
@ -177,6 +179,7 @@ var _ = Describe("conns reaper", func() {
cn, err := connPool.Get() cn, err := connPool.Get()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(cn).NotTo(BeNil()) Expect(cn).NotTo(BeNil())
conns = append(conns, cn)
Expect(connPool.Len()).To(Equal(4)) Expect(connPool.Len()).To(Equal(4))
Expect(connPool.FreeLen()).To(Equal(0)) Expect(connPool.FreeLen()).To(Equal(0))