chore: cleanup

This commit is contained in:
Vladimir Mihailenco 2022-11-21 11:31:38 +02:00
parent dadd6d2785
commit 7335ef65dc
4 changed files with 83 additions and 85 deletions

View File

@ -1336,6 +1336,9 @@ var _ = Describe("ClusterClient timeout", func() {
}, 2*pause).ShouldNot(HaveOccurred()) }, 2*pause).ShouldNot(HaveOccurred())
return nil return nil
}) })
err := client.Close()
Expect(err).NotTo(HaveOccurred())
}) })
testTimeout() testTimeout()

View File

@ -94,7 +94,7 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
conn, err := hook(ctx, network, addr) conn, err := hook(ctx, network, addr)
if err != nil { if err != nil {
recordError(ctx, span, err) recordError(span, err)
return nil, err return nil, err
} }
return conn, nil return conn, nil
@ -118,7 +118,7 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
defer span.End() defer span.End()
if err := hook(ctx, cmd); err != nil { if err := hook(ctx, cmd); err != nil {
recordError(ctx, span, err) recordError(span, err)
return err return err
} }
return nil return nil
@ -147,14 +147,14 @@ func (th *tracingHook) ProcessPipelineHook(
defer span.End() defer span.End()
if err := hook(ctx, cmds); err != nil { if err := hook(ctx, cmds); err != nil {
recordError(ctx, span, err) recordError(span, err)
return err return err
} }
return nil return nil
} }
} }
func recordError(ctx context.Context, span trace.Span, err error) { func recordError(span trace.Span, err error) {
if err != redis.Nil { if err != redis.Nil {
span.RecordError(err) span.RecordError(err)
span.SetStatus(codes.Error, err.Error()) span.SetStatus(codes.Error, err.Error())

View File

@ -1,10 +1,14 @@
package redis package redis
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
"time" "time"
"github.com/go-redis/redis/v9/internal/pool"
"github.com/go-redis/redis/v9/internal/proto"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -158,3 +162,75 @@ func BenchmarkRingShardingRebalanceLocked(b *testing.B) {
ring.sharding.rebalanceLocked() ring.sharding.rebalanceLocked()
} }
} }
//------------------------------------------------------------------------------
type timeoutErr struct {
error
}
func (e timeoutErr) Timeout() bool {
return true
}
func (e timeoutErr) Temporary() bool {
return true
}
func (e timeoutErr) Error() string {
return "i/o timeout"
}
var _ = Describe("withConn", func() {
var client *Client
BeforeEach(func() {
client = NewClient(&Options{
PoolSize: 1,
})
})
AfterEach(func() {
client.Close()
})
It("should replace the connection in the pool when there is no error", func() {
var conn *pool.Conn
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
conn = c
return nil
})
newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).To(Equal(conn))
})
It("should replace the connection in the pool when there is an error not related to a bad connection", func() {
var conn *pool.Conn
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
conn = c
return proto.RedisError("LOADING")
})
newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).To(Equal(conn))
})
It("should remove the connection from the pool when it times out", func() {
var conn *pool.Conn
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
conn = c
return timeoutErr{}
})
newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).NotTo(Equal(conn))
Expect(client.connPool.Len()).To(Equal(1))
})
})

View File

@ -1,81 +0,0 @@
package redis
import (
"context"
"github.com/go-redis/redis/v9/internal/pool"
"github.com/go-redis/redis/v9/internal/proto"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type timeoutErr struct {
error
}
func (e timeoutErr) Timeout() bool {
return true
}
func (e timeoutErr) Temporary() bool {
return true
}
func (e timeoutErr) Error() string {
return "i/o timeout"
}
var _ = Describe("withConn", func() {
var client *Client
BeforeEach(func() {
client = NewClient(&Options{
PoolSize: 1,
})
})
AfterEach(func() {
client.Close()
})
It("should replace the connection in the pool when there is no error", func() {
var conn *pool.Conn
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
conn = c
return nil
})
newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).To(Equal(conn))
})
It("should replace the connection in the pool when there is an error not related to a bad connection", func() {
var conn *pool.Conn
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
conn = c
return proto.RedisError("LOADING")
})
newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).To(Equal(conn))
})
It("should remove the connection from the pool when it times out", func() {
var conn *pool.Conn
client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error {
conn = c
return timeoutErr{}
})
newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).NotTo(Equal(conn))
Expect(client.connPool.Len()).To(Equal(1))
})
})