1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-31 05:04:23 +03:00

Port pool fixes

This commit is contained in:
Vladimir Mihailenco
2020-08-15 15:36:02 +03:00
parent 0ddc3abd36
commit 6db422f051
10 changed files with 244 additions and 303 deletions

View File

@ -13,7 +13,7 @@ import (
)
var _ = Describe("ConnPool", func() {
c := context.Background()
ctx := context.Background()
var connPool *pool.ConnPool
BeforeEach(func() {
@ -32,13 +32,13 @@ var _ = Describe("ConnPool", func() {
It("should unblock client when conn is removed", func() {
// Reserve one connection.
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
// Reserve all other connections.
var cns []*pool.Conn
for i := 0; i < 9; i++ {
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
cns = append(cns, cn)
}
@ -49,11 +49,11 @@ var _ = Describe("ConnPool", func() {
defer GinkgoRecover()
started <- true
_, err := connPool.Get(c)
_, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
done <- true
connPool.Put(cn)
connPool.Put(ctx, cn)
}()
<-started
@ -65,7 +65,7 @@ var _ = Describe("ConnPool", func() {
// ok
}
connPool.Remove(cn, nil)
connPool.Remove(ctx, cn, nil)
// Check that Get is unblocked.
select {
@ -76,14 +76,14 @@ var _ = Describe("ConnPool", func() {
}
for _, cn := range cns {
connPool.Put(cn)
connPool.Put(ctx, cn)
}
})
})
var _ = Describe("MinIdleConns", func() {
c := context.Background()
const poolSize = 100
ctx := context.Background()
var minIdleConns int
var connPool *pool.ConnPool
@ -113,7 +113,7 @@ var _ = Describe("MinIdleConns", func() {
BeforeEach(func() {
var err error
cn, err = connPool.Get(c)
cn, err = connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
Eventually(func() int {
@ -128,7 +128,7 @@ var _ = Describe("MinIdleConns", func() {
Context("after Remove", func() {
BeforeEach(func() {
connPool.Remove(cn, nil)
connPool.Remove(ctx, cn, nil)
})
It("has idle connections", func() {
@ -148,7 +148,7 @@ var _ = Describe("MinIdleConns", func() {
perform(poolSize, func(_ int) {
defer GinkgoRecover()
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
mu.Lock()
cns = append(cns, cn)
@ -163,7 +163,7 @@ var _ = Describe("MinIdleConns", func() {
It("Get is blocked", func() {
done := make(chan struct{})
go func() {
connPool.Get(c)
connPool.Get(ctx)
close(done)
}()
@ -186,7 +186,7 @@ var _ = Describe("MinIdleConns", func() {
BeforeEach(func() {
perform(len(cns), func(i int) {
mu.RLock()
connPool.Put(cns[i])
connPool.Put(ctx, cns[i])
mu.RUnlock()
})
@ -205,7 +205,7 @@ var _ = Describe("MinIdleConns", func() {
BeforeEach(func() {
perform(len(cns), func(i int) {
mu.RLock()
connPool.Remove(cns[i], nil)
connPool.Remove(ctx, cns[i], nil)
mu.RUnlock()
})
@ -250,11 +250,10 @@ var _ = Describe("MinIdleConns", func() {
})
var _ = Describe("conns reaper", func() {
c := context.Background()
const idleTimeout = time.Minute
const maxAge = time.Hour
ctx := context.Background()
var connPool *pool.ConnPool
var conns, staleConns, closedConns []*pool.Conn
@ -279,7 +278,7 @@ var _ = Describe("conns reaper", func() {
// add stale connections
staleConns = nil
for i := 0; i < 3; i++ {
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
switch typ {
case "idle":
@ -293,13 +292,13 @@ var _ = Describe("conns reaper", func() {
// add fresh connections
for i := 0; i < 3; i++ {
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
conns = append(conns, cn)
}
for _, cn := range conns {
connPool.Put(cn)
connPool.Put(ctx, cn)
}
Expect(connPool.Len()).To(Equal(6))
@ -338,7 +337,7 @@ var _ = Describe("conns reaper", func() {
for j := 0; j < 3; j++ {
var freeCns []*pool.Conn
for i := 0; i < 3; i++ {
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(cn).NotTo(BeNil())
freeCns = append(freeCns, cn)
@ -347,7 +346,7 @@ var _ = Describe("conns reaper", func() {
Expect(connPool.Len()).To(Equal(3))
Expect(connPool.IdleLen()).To(Equal(0))
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(cn).NotTo(BeNil())
conns = append(conns, cn)
@ -355,13 +354,13 @@ var _ = Describe("conns reaper", func() {
Expect(connPool.Len()).To(Equal(4))
Expect(connPool.IdleLen()).To(Equal(0))
connPool.Remove(cn, nil)
connPool.Remove(ctx, cn, nil)
Expect(connPool.Len()).To(Equal(3))
Expect(connPool.IdleLen()).To(Equal(0))
for _, cn := range freeCns {
connPool.Put(cn)
connPool.Put(ctx, cn)
}
Expect(connPool.Len()).To(Equal(3))
@ -375,7 +374,7 @@ var _ = Describe("conns reaper", func() {
})
var _ = Describe("race", func() {
c := context.Background()
ctx := context.Background()
var connPool *pool.ConnPool
var C, N int
@ -402,18 +401,18 @@ var _ = Describe("race", func() {
perform(C, func(id int) {
for i := 0; i < N; i++ {
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
if err == nil {
connPool.Put(cn)
connPool.Put(ctx, cn)
}
}
}, func(id int) {
for i := 0; i < N; i++ {
cn, err := connPool.Get(c)
cn, err := connPool.Get(ctx)
Expect(err).NotTo(HaveOccurred())
if err == nil {
connPool.Remove(cn, nil)
connPool.Remove(ctx, cn, nil)
}
}
})