forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errmap_test.go
185 lines (160 loc) · 5.65 KB
/
errmap_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package gocbcore
import (
"github.com/couchbase/gocbcore/v10/memd"
"testing"
"time"
)
func TestKvErrorConstantRetry(t *testing.T) {
constant := kvErrorMapRetry{
Strategy: "constant",
Interval: 1000,
After: 2000,
Ceil: 4000,
MaxDuration: 3000,
}
if constant.CalculateRetryDelay(0) != 2000*time.Millisecond {
t.Fatalf("failed to respect after for first retry")
}
if constant.CalculateRetryDelay(1) != 1000*time.Millisecond {
t.Fatalf("should respect interval for second retry")
}
if constant.CalculateRetryDelay(3) != 1000*time.Millisecond {
t.Fatalf("should respect interval for minimal retries")
}
if constant.CalculateRetryDelay(15000) != 1000*time.Millisecond {
t.Fatalf("should respect interval for large retry counts")
}
}
func TestKvErrorLinearRetry(t *testing.T) {
linear := kvErrorMapRetry{
Strategy: "linear",
Interval: 1000,
After: 2000,
Ceil: 60000,
MaxDuration: 100000,
}
if linear.CalculateRetryDelay(0) != 2000*time.Millisecond {
t.Fatalf("failed to respect after for first retry")
}
if linear.CalculateRetryDelay(1) != 1000*time.Millisecond {
t.Fatalf("should respect interval for second retry")
}
if linear.CalculateRetryDelay(3) != 3000*time.Millisecond {
t.Fatalf("should respect interval for minimal retries")
}
if linear.CalculateRetryDelay(150) != 60000*time.Millisecond {
t.Fatalf("should respect ceiling for large retry counts")
}
}
func TestKvErrorExponentialRetry(t *testing.T) {
exponential := kvErrorMapRetry{
Strategy: "exponential",
Interval: 10,
After: 1000,
Ceil: 60000,
MaxDuration: 100000,
}
if exponential.CalculateRetryDelay(0) != 1000*time.Millisecond {
t.Fatalf("failed to respect after for first retry")
}
if exponential.CalculateRetryDelay(1) != 10*time.Millisecond {
t.Fatalf("should respect interval for second retry")
}
if exponential.CalculateRetryDelay(3) != 1000*time.Millisecond {
t.Fatalf("should respect interval for minimal retries")
}
if exponential.CalculateRetryDelay(400) != 60000*time.Millisecond {
t.Fatalf("should respect ceiling for large retry counts")
}
}
type errMapTestRetryStrategy struct {
reasons []RetryReason
retries int
}
func (lrs *errMapTestRetryStrategy) RetryAfter(request RetryRequest, reason RetryReason) RetryAction {
lrs.retries++
lrs.reasons = append(lrs.reasons, reason)
return &WithDurationRetryAction{50 * time.Millisecond}
}
func (suite *StandardTestSuite) testKvErrorMapGeneric(checkName TestName) {
suite.EnsureSupportsFeature(TestFeatureErrMap)
if !suite.IsMockServer() {
suite.T().Skipf("only supported when testing against mock server")
}
testKey := "hello"
spec := suite.StartTest(checkName)
h := suite.GetHarness()
agent := spec.Agent
strategy := &errMapTestRetryStrategy{}
h.PushOp(agent.Get(GetOptions{
Key: []byte(testKey),
RetryStrategy: strategy,
CollectionName: spec.Collection,
ScopeName: spec.Scope,
}, func(res *GetResult, err error) {
h.Wrap(func() {})
}))
h.Wait(0)
if strategy.retries != 3 {
suite.T().Fatalf("Expected retries to be 3 but was %d", strategy.retries)
}
if len(strategy.reasons) != 3 {
suite.T().Fatalf("Expected 3 retry reasons but was %v", strategy.reasons)
}
for _, reason := range strategy.reasons {
if reason != KVErrMapRetryReason {
suite.T().Fatalf("Expected reason to be KVErrMapRetryReason but was %s", reason.Description())
}
}
suite.VerifyKVMetrics(spec.Meter, "Get", 1, false, false)
suite.EndTest(spec)
}
// It doesn't actually matter what strategy the error map specifies, we just test that retries happen as the
// strategy dictates no matter what.
func (suite *StandardTestSuite) TestKvErrorMap7ff0() {
suite.testKvErrorMapGeneric(TestNameErrMapLinearRetry)
}
func (suite *StandardTestSuite) TestKvErrorMap7ff1() {
suite.testKvErrorMapGeneric(TestNameErrMapConstantRetry)
}
func (suite *StandardTestSuite) TestKvErrorMap7ff2() {
suite.testKvErrorMapGeneric(TestNameErrMapExponentialRetry)
}
func (suite *UnitTestSuite) TestStoreKVErrorMapV1() {
data, err := loadRawTestDataset("err_map70_v1")
suite.Require().Nil(err, err)
errMgr := newErrMapManager("test")
errMgr.StoreErrorMap(data)
errMap := errMgr.kvErrorMap.Get()
suite.Require().NotNil(errMap)
suite.Assert().Equal(1, errMap.Version)
suite.Assert().Equal(2, errMap.Revision)
suite.Assert().Len(errMap.Errors, 58)
entry := errMgr.getKvErrMapData(memd.StatusLocked)
suite.Require().NotNil(entry)
suite.Assert().Equal("LOCKED", entry.Name)
suite.Assert().Equal("Requested resource is locked", entry.Description)
suite.Assert().Len(entry.Attributes, 3)
suite.Assert().Contains(entry.Attributes, kvErrorMapAttribute("item-locked"))
suite.Assert().Contains(entry.Attributes, kvErrorMapAttribute("item-only"))
suite.Assert().Contains(entry.Attributes, kvErrorMapAttribute("retry-now"))
}
func (suite *UnitTestSuite) TestStoreKVErrorMapV2() {
data, err := loadRawTestDataset("err_map71_v2")
suite.Require().Nil(err, err)
errMgr := newErrMapManager("test")
errMgr.StoreErrorMap(data)
errMap := errMgr.kvErrorMap.Get()
suite.Require().NotNil(errMap)
suite.Assert().Equal(2, errMap.Version)
suite.Assert().Equal(1, errMap.Revision)
suite.Assert().Len(errMap.Errors, 65)
entry := errMgr.getKvErrMapData(memd.StatusLocked)
suite.Require().NotNil(entry)
suite.Assert().Equal("LOCKED", entry.Name)
suite.Assert().Equal("Requested resource is locked", entry.Description)
suite.Assert().Len(entry.Attributes, 3)
suite.Assert().Contains(entry.Attributes, kvErrorMapAttribute("item-locked"))
suite.Assert().Contains(entry.Attributes, kvErrorMapAttribute("item-only"))
suite.Assert().Contains(entry.Attributes, kvErrorMapAttribute("retry-now"))
}