-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
retry_errors_spec.rb
285 lines (225 loc) · 10.4 KB
/
retry_errors_spec.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
require_relative '../../spec_helper'
module Aws
module Plugins
describe RetryErrors do
RetryErrorsSvc = ApiHelper.sample_service
let(:client) { RetryErrorsSvc::Client.new(stub_responses: true) }
it 'defaults config.retry_limit to 3' do
expect(client.config.retry_limit).to eq(3)
end
describe 'ErrorInspector' do
def inspector(error, http_status_code = 404)
RetryErrors::ErrorInspector.new(error, http_status_code)
end
describe '#expired_credentials?' do
it 'returns true if the error code is InvalidClientTokenId' do
error = RetryErrorsSvc::Errors::InvalidClientTokenId.new(nil,nil)
expect(inspector(error).expired_credentials?).to be(true)
end
it 'returns true if the error code is UnrecognizedClientException' do
error = RetryErrorsSvc::Errors::UnrecognizedClientException.new(nil,nil)
expect(inspector(error).expired_credentials?).to be(true)
end
it 'returns true if the error code is InvalidAccessKeyId' do
error = RetryErrorsSvc::Errors::InvalidAccessKeyId.new(nil,nil)
expect(inspector(error).expired_credentials?).to be(true)
end
it 'returns true if the error code is AuthFailure' do
error = RetryErrorsSvc::Errors::AuthFailure.new(nil,nil)
expect(inspector(error).expired_credentials?).to be(true)
end
it 'returns true if the error code matches /expired/' do
error = RetryErrorsSvc::Errors::SomethingExpiredError.new(nil,nil)
expect(inspector(error).expired_credentials?).to be(true)
end
it 'returns false for other errors' do
error = RetryErrorsSvc::Errors::SomeRandomError.new(nil,nil)
expect(inspector(error).expired_credentials?).to be(false)
end
end
describe '#throttling_error?' do
it 'returns true for Throttling' do
error = RetryErrorsSvc::Errors::Throttling.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for ThrottlingException' do
error = RetryErrorsSvc::Errors::ThrottlingException.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for RequestThrottled' do
error = RetryErrorsSvc::Errors::RequestThrottled.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for ProvisionedThroughputExceededException' do
error = RetryErrorsSvc::Errors::ProvisionedThroughputExceededException.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for RequestLimitExceeded' do
error = RetryErrorsSvc::Errors::RequestLimitExceeded.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for BandwidthLimitExceeded' do
error = RetryErrorsSvc::Errors::BandwidthLimitExceeded.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for LimitExceededException' do
error = RetryErrorsSvc::Errors::LimitExceededException.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns true for error codes that match /throttl/' do
error = RetryErrorsSvc::Errors::Throttled.new(nil,nil)
expect(inspector(error).throttling_error?).to be(true)
end
it 'returns false for other errors' do
error = RetryErrorsSvc::Errors::SomeRandomError.new(nil,nil)
expect(inspector(error).throttling_error?).to be(false)
end
end
describe '#checksum?' do
it 'returns true if the error extends Errors::ChecksumError' do
error = Errors::ChecksumError.new
expect(inspector(error).checksum?).to be(true)
end
it 'returns true if the error is a crc32 error' do
error = RetryErrorsSvc::Errors::CRC32CheckFailed.new(nil,nil)
expect(inspector(error).checksum?).to be(true)
end
it 'returns false if the error does not exend ChecksumError' do
error = double('error')
expect(inspector(error).checksum?).to be(false)
end
end
describe '#server?' do
it 'returns true if the error is a 500 level error' do
error = RetryErrorsSvc::Errors::RandomError.new(nil,nil)
expect(inspector(error, 500).server?).to be(true)
end
it 'returns false if the error is not a 500 level error' do
error = RetryErrorsSvc::Errors::RandomError.new(nil,nil)
expect(inspector(error, 404).server?).to be(false)
end
end
describe '#networking?' do
it 'returns true if the error code is RequestTimeout' do
error = RetryErrorsSvc::Errors::RequestTimeout.new(nil,nil)
expect(inspector(error).networking?).to be(true)
end
it 'does not assume a networking error for 0 status code' do
error = double('error')
expect(inspector(error, 0).networking?).to be(false)
end
it 'returns false if the http status code is not 0' do
error = double('error')
expect(inspector(error, 307).networking?).to be(false)
end
it 'returns true if the error is wrapped in a NetworkingError' do
error = StandardError.new('oops')
error = Seahorse::Client::NetworkingError.new(error)
expect(inspector(error, 200).networking?).to be(true)
end
end
end
describe 'Handler' do
let(:credentials) { Credentials.new('akid', 'secret') }
let(:config) {
cfg = Seahorse::Client::Configuration.new
cfg.add_option(:credentials, credentials)
RetryErrors.new.add_options(cfg)
cfg.build!
}
let(:resp) { Seahorse::Client::Response.new }
let(:handler) { RetryErrors::Handler.new }
before(:each) do
resp.context.config = config
resp.context.http_response.status_code = 400
end
def handle(send_handler = nil, &block)
allow(Kernel).to receive(:sleep)
handler.handler = send_handler || block
handler.call(resp.context)
end
it 'does not retry responses that have no error' do
resp.error = nil
send_handler = double('send-handler')
expect(send_handler).to receive(:call).once.and_return(resp)
handle(send_handler)
end
it 'reties 3 times for a total of 4 attemps' do
resp.error = RetryErrorsSvc::Errors::RequestLimitExceeded.new(nil,nil)
send_handler = double('send-handler')
expect(send_handler).to receive(:call).
exactly(4).times.
with(resp.context).
and_return(resp)
handle(send_handler)
end
it 'backs off exponentially between each retry attempt' do
expect(Kernel).to receive(:sleep).with(0.3).ordered
expect(Kernel).to receive(:sleep).with(0.6).ordered
expect(Kernel).to receive(:sleep).with(1.2).ordered
resp.error = RetryErrorsSvc::Errors::RequestLimitExceeded.new(nil,nil)
handle { |context| resp }
end
it 'increments the retry count on the context' do
resp.error = RetryErrorsSvc::Errors::RequestLimitExceeded.new(nil,nil)
handle { |context| resp }
expect(resp.context.retries).to eq(3)
end
it 'rewinds the request body before each retry attempt' do
body = resp.context.http_request.body
expect(body).to receive(:rewind).exactly(3).times
resp.error = RetryErrorsSvc::Errors::RequestLimitExceeded.new(nil,nil)
handle { |context| resp }
end
it 'truncates the response body before retry attempt of invalid bytes' do
body = double('truncatable-body', pos: 100, truncate: 0)
resp.context.http_response.body = body
expect(body).to receive(:truncate).with(0).exactly(3).times
resp.error = Seahorse::Client::NetworkingError.new(
Seahorse::Client::NetHttp::Handler::TruncatedBodyError.new(11, 10))
handle { |context| resp }
end
it 'does not truncate response body before retry attempt of other errors' do
body = double('truncatable-body', pos: 100, truncate: 0)
resp.context.http_response.body = body
expect(body).to receive(:truncate).never
resp.error = RetryErrorsSvc::Errors::RequestLimitExceeded.new(nil,nil)
handle { |context| resp }
end
it 'skips retry on invalid bytes if response body is un-truncatable' do
resp.context.http_response.body = double('write-once-body', pos: 100)
resp.error = Seahorse::Client::NetworkingError.new(
Seahorse::Client::NetHttp::Handler::TruncatedBodyError.new(11, 10))
handle { |context| resp }
expect(resp.context.retries).to eq(0)
end
it 'retries if creds expire and are refreshable' do
expect(credentials).to receive(:refresh!).exactly(3).times
resp.error = RetryErrorsSvc::Errors::AuthFailure.new(nil,nil)
handle { |context| resp }
expect(resp.context.retries).to eq(3)
end
it 'skips retry if creds expire and are not refreshable' do
resp.error = RetryErrorsSvc::Errors::AuthFailure.new(nil,nil)
handle { |context| resp }
expect(resp.context.retries).to eq(0)
end
it 'retries 500 level errors' do
error = RuntimeError.new('random-runtime-error')
handle do |context|
context.http_response.signal_headers(500, {})
context.http_response.signal_error(error)
resp
end
expect(resp.context.retries).to eq(3)
end
it 'retries Seahorse::Client::NetworkingErrors' do
error = RuntimeError.new('random-runtime-error')
resp.error = Seahorse::Client::NetworkingError.new(error)
handle { |context| resp }
expect(resp.context.retries).to eq(3)
end
end
end
end
end