|
| 1 | +package redis_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + |
| 8 | + . "github.com/bsm/ginkgo/v2" |
| 9 | + . "github.com/bsm/gomega" |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | +) |
| 12 | + |
| 13 | +type testTimeout struct { |
| 14 | + timeout bool |
| 15 | +} |
| 16 | + |
| 17 | +func (t testTimeout) Timeout() bool { |
| 18 | + return t.timeout |
| 19 | +} |
| 20 | + |
| 21 | +func (t testTimeout) Error() string { |
| 22 | + return "test timeout" |
| 23 | +} |
| 24 | + |
| 25 | +var _ = Describe("error", func() { |
| 26 | + BeforeEach(func() { |
| 27 | + |
| 28 | + }) |
| 29 | + |
| 30 | + AfterEach(func() { |
| 31 | + |
| 32 | + }) |
| 33 | + |
| 34 | + It("should retry", func() { |
| 35 | + data := map[error]bool{ |
| 36 | + io.EOF: true, |
| 37 | + io.ErrUnexpectedEOF: true, |
| 38 | + nil: false, |
| 39 | + context.Canceled: false, |
| 40 | + context.DeadlineExceeded: false, |
| 41 | + redis.ErrPoolTimeout: true, |
| 42 | + errors.New("ERR max number of clients reached"): true, |
| 43 | + errors.New("LOADING Redis is loading the dataset in memory"): true, |
| 44 | + errors.New("READONLY You can't write against a read only replica"): true, |
| 45 | + errors.New("CLUSTERDOWN The cluster is down"): true, |
| 46 | + errors.New("TRYAGAIN Command cannot be processed, please try again"): true, |
| 47 | + errors.New("other"): false, |
| 48 | + } |
| 49 | + |
| 50 | + for err, expected := range data { |
| 51 | + Expect(redis.ShouldRetry(err, false)).To(Equal(expected)) |
| 52 | + Expect(redis.ShouldRetry(err, true)).To(Equal(expected)) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + It("should retry timeout", func() { |
| 57 | + t1 := testTimeout{timeout: true} |
| 58 | + Expect(redis.ShouldRetry(t1, true)).To(Equal(true)) |
| 59 | + Expect(redis.ShouldRetry(t1, false)).To(Equal(false)) |
| 60 | + |
| 61 | + t2 := testTimeout{timeout: false} |
| 62 | + Expect(redis.ShouldRetry(t2, true)).To(Equal(true)) |
| 63 | + Expect(redis.ShouldRetry(t2, false)).To(Equal(true)) |
| 64 | + }) |
| 65 | +}) |
0 commit comments