-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5350c18
commit a54ed5a
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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)) | ||
}) | ||
}) |