-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathbackoff_test.go
161 lines (150 loc) · 4.07 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package utils_test
import (
"context"
"testing"
"time"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestBackoffWithSuccess(t *testing.T) {
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)
require.Equal(t, 3, counter)
require.NoError(t, err)
}
func TestBackoffWithFatalError(t *testing.T) {
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)
require.Equal(t, 4, counter)
require.Equal(t, []error{
gRPCError,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVDownloadFailed,
berrors.ErrKVRangeIsEmpty,
}, multierr.Errors(err))
}
func TestBackoffWithFatalRawGRPCError(t *testing.T) {
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)
require.Equal(t, 1, counter)
require.Equal(t, []error{canceledError}, multierr.Errors(err))
}
func TestBackoffWithRetryableError(t *testing.T) {
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)
require.Equal(t, 10, counter)
require.Equal(t, []error{
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
}, multierr.Errors(err))
}
func TestPdBackoffWithRetryableError(t *testing.T) {
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)
require.Equal(t, 16, counter)
require.Equal(t, []error{
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
gRPCError,
}, multierr.Errors(err))
}
func TestNewImportSSTBackofferWithSucess(t *testing.T) {
var counter int
backoffer := utils.NewImportSSTBackoffer()
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
if counter == 15 {
return nil
} else {
return berrors.ErrKVDownloadFailed
}
}, backoffer)
require.Equal(t, 16, counter)
require.NoError(t, err)
}
func TestNewDownloadSSTBackofferWithCancel(t *testing.T) {
var counter int
backoffer := utils.NewDownloadSSTBackoffer()
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
if counter == 3 {
return context.Canceled
} else {
return berrors.ErrKVIngestFailed
}
}, backoffer)
require.Equal(t, 4, counter)
require.Equal(t, []error{
berrors.ErrKVIngestFailed,
berrors.ErrKVIngestFailed,
berrors.ErrKVIngestFailed,
context.Canceled,
}, multierr.Errors(err))
}