From 7335ef65dc97c975471220a61cf790f626ed07be Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Mon, 21 Nov 2022 11:31:38 +0200 Subject: [PATCH] chore: cleanup --- cluster_test.go | 3 ++ extra/redisotel/tracing.go | 8 ++-- internal_test.go | 76 +++++++++++++++++++++++++++++++++++ with_conn_test.go | 81 -------------------------------------- 4 files changed, 83 insertions(+), 85 deletions(-) delete mode 100644 with_conn_test.go diff --git a/cluster_test.go b/cluster_test.go index 15c6a94..f9dc679 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -1336,6 +1336,9 @@ var _ = Describe("ClusterClient timeout", func() { }, 2*pause).ShouldNot(HaveOccurred()) return nil }) + + err := client.Close() + Expect(err).NotTo(HaveOccurred()) }) testTimeout() diff --git a/extra/redisotel/tracing.go b/extra/redisotel/tracing.go index be1a283..608d774 100644 --- a/extra/redisotel/tracing.go +++ b/extra/redisotel/tracing.go @@ -94,7 +94,7 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook { conn, err := hook(ctx, network, addr) if err != nil { - recordError(ctx, span, err) + recordError(span, err) return nil, err } return conn, nil @@ -118,7 +118,7 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook { defer span.End() if err := hook(ctx, cmd); err != nil { - recordError(ctx, span, err) + recordError(span, err) return err } return nil @@ -147,14 +147,14 @@ func (th *tracingHook) ProcessPipelineHook( defer span.End() if err := hook(ctx, cmds); err != nil { - recordError(ctx, span, err) + recordError(span, err) return err } return nil } } -func recordError(ctx context.Context, span trace.Span, err error) { +func recordError(span trace.Span, err error) { if err != redis.Nil { span.RecordError(err) span.SetStatus(codes.Error, err.Error()) diff --git a/internal_test.go b/internal_test.go index fcf1235..9f53ae6 100644 --- a/internal_test.go +++ b/internal_test.go @@ -1,10 +1,14 @@ package redis import ( + "context" "fmt" "testing" "time" + "github.com/go-redis/redis/v9/internal/pool" + "github.com/go-redis/redis/v9/internal/proto" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -158,3 +162,75 @@ func BenchmarkRingShardingRebalanceLocked(b *testing.B) { 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)) + }) +}) diff --git a/with_conn_test.go b/with_conn_test.go deleted file mode 100644 index 4c90d65..0000000 --- a/with_conn_test.go +++ /dev/null @@ -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)) - }) -})