|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/go-redis/redis/v9/internal/pool" |
| 7 | + "github.com/go-redis/redis/v9/internal/proto" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | +) |
| 12 | + |
| 13 | +type timeoutErr struct { |
| 14 | + error |
| 15 | +} |
| 16 | + |
| 17 | +func (e timeoutErr) Timeout() bool { |
| 18 | + return true |
| 19 | +} |
| 20 | + |
| 21 | +func (e timeoutErr) Temporary() bool { |
| 22 | + return true |
| 23 | +} |
| 24 | + |
| 25 | +func (e timeoutErr) Error() string { |
| 26 | + return "i/o timeout" |
| 27 | +} |
| 28 | + |
| 29 | +var _ = Describe("withConn", func() { |
| 30 | + var client *Client |
| 31 | + |
| 32 | + BeforeEach(func() { |
| 33 | + client = NewClient(&Options{ |
| 34 | + PoolSize: 1, |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + AfterEach(func() { |
| 39 | + client.Close() |
| 40 | + }) |
| 41 | + |
| 42 | + It("should replace the connection in the pool when there is no error", func() { |
| 43 | + var conn *pool.Conn |
| 44 | + |
| 45 | + client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error { |
| 46 | + conn = c |
| 47 | + return nil |
| 48 | + }) |
| 49 | + |
| 50 | + newConn, err := client.connPool.Get(ctx) |
| 51 | + Expect(err).To(BeNil()) |
| 52 | + Expect(newConn).To(Equal(conn)) |
| 53 | + }) |
| 54 | + |
| 55 | + It("should replace the connection in the pool when there is an error not related to a bad connection", func() { |
| 56 | + var conn *pool.Conn |
| 57 | + |
| 58 | + client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error { |
| 59 | + conn = c |
| 60 | + return proto.RedisError("LOADING") |
| 61 | + }) |
| 62 | + |
| 63 | + newConn, err := client.connPool.Get(ctx) |
| 64 | + Expect(err).To(BeNil()) |
| 65 | + Expect(newConn).To(Equal(conn)) |
| 66 | + }) |
| 67 | + |
| 68 | + It("should remove the connection from the pool when it times out", func() { |
| 69 | + var conn *pool.Conn |
| 70 | + |
| 71 | + client.withConn(ctx, func(ctx context.Context, c *pool.Conn) error { |
| 72 | + conn = c |
| 73 | + return timeoutErr{} |
| 74 | + }) |
| 75 | + |
| 76 | + newConn, err := client.connPool.Get(ctx) |
| 77 | + Expect(err).To(BeNil()) |
| 78 | + Expect(newConn).NotTo(Equal(conn)) |
| 79 | + Expect(client.connPool.Len()).To(Equal(1)) |
| 80 | + }) |
| 81 | +}) |
0 commit comments