-
Notifications
You must be signed in to change notification settings - Fork 21
/
backoff_test.go
144 lines (132 loc) · 3.64 KB
/
backoff_test.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package utils_test
import (
"context"
"time"
. "github.com/pingcap/check"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/mock"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/util/testleak"
"go.uber.org/multierr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var _ = Suite(&testBackofferSuite{})
type testBackofferSuite struct {
mock *mock.Cluster
}
func (s *testBackofferSuite) SetUpSuite(c *C) {
var err error
s.mock, err = mock.NewCluster()
c.Assert(err, IsNil)
}
func (s *testBackofferSuite) TearDownSuite(c *C) {
testleak.AfterTest(c)()
}
func (s *testBackofferSuite) TestBackoffWithSuccess(c *C) {
var counter int
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
switch counter {
case 0:
return status.Error(codes.Unavailable, "transport is closing")
case 1:
return berrors.ErrKVEpochNotMatch
case 2:
return nil
}
return nil
}, backoffer)
c.Assert(counter, Equals, 3)
c.Assert(err, IsNil)
}
func (s *testBackofferSuite) TestBackoffWithFatalError(c *C) {
var counter int
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
gRPCError := status.Error(codes.Unavailable, "transport is closing")
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
switch counter {
case 0:
return gRPCError // nolint:wrapcheck
case 1:
return berrors.ErrKVEpochNotMatch
case 2:
return berrors.ErrKVDownloadFailed
case 3:
return berrors.ErrKVRangeIsEmpty
}
return nil
}, backoffer)
c.Assert(counter, Equals, 4)
c.Assert(multierr.Errors(err), DeepEquals, []error{
gRPCError,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVDownloadFailed,
berrors.ErrKVRangeIsEmpty,
})
}
func (s *testBackofferSuite) TestBackoffWithFatalRawGRPCError(c *C) {
var counter int
canceledError := status.Error(codes.Canceled, "context canceled")
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
return canceledError // nolint:wrapcheck
}, backoffer)
c.Assert(counter, Equals, 1)
c.Assert(multierr.Errors(err), DeepEquals, []error{
canceledError,
})
}
func (s *testBackofferSuite) TestBackoffWithRetryableError(c *C) {
var counter int
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
return berrors.ErrKVEpochNotMatch
}, backoffer)
c.Assert(counter, Equals, 10)
c.Assert(multierr.Errors(err), DeepEquals, []error{
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
})
}
func (s *testBackofferSuite) TestPdBackoffWithRetryableError(c *C) {
var counter int
backoffer := utils.NewPDReqBackoffer()
gRPCError := status.Error(codes.Unavailable, "transport is closing")
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
return gRPCError
}, backoffer)
c.Assert(counter, Equals, 16)
c.Assert(multierr.Errors(err), DeepEquals, []error{
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
})
}