File: error_test.go

package info (click to toggle)
golang-github-redis-go-redis 9.17.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,080 kB
  • sloc: sh: 289; makefile: 99
file content (66 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package redis_test

import (
	"context"
	"io"

	. "github.com/bsm/ginkgo/v2"
	. "github.com/bsm/gomega"
	"github.com/redis/go-redis/v9"
	"github.com/redis/go-redis/v9/internal/proto"
)

type testTimeout struct {
	timeout bool
}

func (t testTimeout) Timeout() bool {
	return t.timeout
}

func (t testTimeout) Error() string {
	return "test timeout"
}

var _ = Describe("error", func() {
	BeforeEach(func() {

	})

	AfterEach(func() {

	})

	It("should retry", func() {
		data := map[error]bool{
			io.EOF:                   true,
			io.ErrUnexpectedEOF:      true,
			nil:                      false,
			context.Canceled:         false,
			context.DeadlineExceeded: false,
			redis.ErrPoolTimeout:     true,
			// Use typed errors instead of plain errors.New()
			proto.ParseErrorReply([]byte("-ERR max number of clients reached")):                      true,
			proto.ParseErrorReply([]byte("-LOADING Redis is loading the dataset in memory")):         true,
			proto.ParseErrorReply([]byte("-READONLY You can't write against a read only replica")):   true,
			proto.ParseErrorReply([]byte("-CLUSTERDOWN The cluster is down")):                        true,
			proto.ParseErrorReply([]byte("-TRYAGAIN Command cannot be processed, please try again")): true,
			proto.ParseErrorReply([]byte("-ERR other")): false,
		}

		for err, expected := range data {
			Expect(redis.ShouldRetry(err, false)).To(Equal(expected))
			Expect(redis.ShouldRetry(err, true)).To(Equal(expected))
		}
	})

	It("should retry timeout", func() {
		t1 := testTimeout{timeout: true}
		Expect(redis.ShouldRetry(t1, true)).To(Equal(true))
		Expect(redis.ShouldRetry(t1, false)).To(Equal(false))

		t2 := testTimeout{timeout: false}
		Expect(redis.ShouldRetry(t2, true)).To(Equal(true))
		Expect(redis.ShouldRetry(t2, false)).To(Equal(true))
	})
})