File size: 14,607 Bytes
065fee7 |
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import http.client
from unittest import mock
import pytest # type: ignore
import requests.exceptions
import urllib3.exceptions # type: ignore
from google.resumable_media import common
from google.resumable_media.requests import _request_helpers
EXPECTED_TIMEOUT = (61, 60)
class TestRequestsMixin(object):
def test__get_status_code(self):
status_code = int(http.client.OK)
response = _make_response(status_code)
assert status_code == _request_helpers.RequestsMixin._get_status_code(response)
def test__get_headers(self):
headers = {"fruit": "apple"}
response = mock.Mock(headers=headers, spec=["headers"])
assert headers == _request_helpers.RequestsMixin._get_headers(response)
def test__get_body(self):
body = b"This is the payload."
response = mock.Mock(content=body, spec=["content"])
assert body == _request_helpers.RequestsMixin._get_body(response)
class TestRawRequestsMixin(object):
def test__get_body_wo_content_consumed(self):
body = b"This is the payload."
raw = mock.Mock(spec=["stream"])
raw.stream.return_value = iter([body])
response = mock.Mock(raw=raw, _content=False, spec=["raw", "_content"])
assert body == _request_helpers.RawRequestsMixin._get_body(response)
raw.stream.assert_called_once_with(
_request_helpers._SINGLE_GET_CHUNK_SIZE, decode_content=False
)
def test__get_body_w_content_consumed(self):
body = b"This is the payload."
response = mock.Mock(_content=body, spec=["_content"])
assert body == _request_helpers.RawRequestsMixin._get_body(response)
def _make_response(status_code):
return mock.Mock(status_code=status_code, spec=["status_code"])
def _get_status_code(response):
return response.status_code
class Test_wait_and_retry(object):
def test_success_no_retry(self):
truthy = http.client.OK
assert truthy not in common.RETRYABLE
response = _make_response(truthy)
func = mock.Mock(return_value=response, spec=[])
retry_strategy = common.RetryStrategy()
ret_val = _request_helpers.wait_and_retry(
func, _get_status_code, retry_strategy
)
assert ret_val is response
func.assert_called_once_with()
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_success_with_retry(self, randint_mock, sleep_mock):
randint_mock.side_effect = [125, 625, 375]
status_codes = (
http.client.INTERNAL_SERVER_ERROR,
http.client.BAD_GATEWAY,
http.client.SERVICE_UNAVAILABLE,
http.client.NOT_FOUND,
)
responses = [_make_response(status_code) for status_code in status_codes]
def raise_response():
raise common.InvalidResponse(responses.pop(0))
func = mock.Mock(side_effect=raise_response)
retry_strategy = common.RetryStrategy()
try:
_request_helpers.wait_and_retry(func, _get_status_code, retry_strategy)
except common.InvalidResponse as e:
ret_val = e.response
assert ret_val.status_code == status_codes[-1]
assert status_codes[-1] not in common.RETRYABLE
assert func.call_count == 4
assert func.mock_calls == [mock.call()] * 4
assert randint_mock.call_count == 3
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 3
assert sleep_mock.call_count == 3
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.375)
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_success_with_retry_custom_delay(self, randint_mock, sleep_mock):
randint_mock.side_effect = [125, 625, 375]
status_codes = (
http.client.INTERNAL_SERVER_ERROR,
http.client.BAD_GATEWAY,
http.client.SERVICE_UNAVAILABLE,
http.client.NOT_FOUND,
)
responses = [_make_response(status_code) for status_code in status_codes]
def raise_response():
raise common.InvalidResponse(responses.pop(0))
func = mock.Mock(side_effect=raise_response)
retry_strategy = common.RetryStrategy(initial_delay=3.0, multiplier=4)
try:
_request_helpers.wait_and_retry(func, _get_status_code, retry_strategy)
except common.InvalidResponse as e:
ret_val = e.response
assert ret_val.status_code == status_codes[-1]
assert status_codes[-1] not in common.RETRYABLE
assert func.call_count == 4
assert func.mock_calls == [mock.call()] * 4
assert randint_mock.call_count == 3
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 3
assert sleep_mock.call_count == 3
sleep_mock.assert_any_call(3.125) # initial delay 3 + jitter 0.125
sleep_mock.assert_any_call(
12.625
) # previous delay 3 * multiplier 4 + jitter 0.625
sleep_mock.assert_any_call(
48.375
) # previous delay 12 * multiplier 4 + jitter 0.375
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_success_http_standard_lib_connection_errors(
self, randint_mock, sleep_mock
):
randint_mock.side_effect = [125, 625, 500, 875, 375]
status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
http.client.BadStatusLine(""),
http.client.IncompleteRead(""),
http.client.ResponseNotReady,
ConnectionError,
response,
]
func = mock.Mock(side_effect=responses, spec=[])
retry_strategy = common.RetryStrategy()
ret_val = _request_helpers.wait_and_retry(
func, _get_status_code, retry_strategy
)
assert ret_val == responses[-1]
assert func.call_count == 5
assert func.mock_calls == [mock.call()] * 5
assert randint_mock.call_count == 4
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 4
assert sleep_mock.call_count == 4
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.500)
sleep_mock.assert_any_call(8.875)
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_success_requests_lib_connection_errors(
self, randint_mock, sleep_mock
):
randint_mock.side_effect = [125, 625, 500, 875]
status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
requests.exceptions.ConnectionError,
requests.exceptions.ChunkedEncodingError,
requests.exceptions.Timeout,
response,
]
func = mock.Mock(side_effect=responses, spec=[])
retry_strategy = common.RetryStrategy()
ret_val = _request_helpers.wait_and_retry(
func, _get_status_code, retry_strategy
)
assert ret_val == responses[-1]
assert func.call_count == 4
assert func.mock_calls == [mock.call()] * 4
assert randint_mock.call_count == 3
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 3
assert sleep_mock.call_count == 3
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.500)
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_success_urllib3_connection_errors(self, randint_mock, sleep_mock):
randint_mock.side_effect = [125, 625, 500, 875, 375]
status_code = int(http.client.OK)
response = _make_response(status_code)
responses = [
urllib3.exceptions.PoolError(None, ""),
urllib3.exceptions.ProtocolError,
urllib3.exceptions.SSLError,
urllib3.exceptions.TimeoutError,
response,
]
func = mock.Mock(side_effect=responses, spec=[])
retry_strategy = common.RetryStrategy()
ret_val = _request_helpers.wait_and_retry(
func, _get_status_code, retry_strategy
)
assert ret_val == responses[-1]
assert func.call_count == 5
assert func.mock_calls == [mock.call()] * 5
assert randint_mock.call_count == 4
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 4
assert sleep_mock.call_count == 4
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.500)
sleep_mock.assert_any_call(8.875)
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_exceeds_max_cumulative(self, randint_mock, sleep_mock):
randint_mock.side_effect = [875, 0, 375, 500, 500, 250, 125]
status_codes = (
http.client.SERVICE_UNAVAILABLE,
http.client.GATEWAY_TIMEOUT,
common.TOO_MANY_REQUESTS,
http.client.INTERNAL_SERVER_ERROR,
http.client.SERVICE_UNAVAILABLE,
http.client.BAD_GATEWAY,
common.TOO_MANY_REQUESTS,
)
responses = [_make_response(status_code) for status_code in status_codes]
def raise_response():
raise common.InvalidResponse(responses.pop(0))
func = mock.Mock(side_effect=raise_response)
retry_strategy = common.RetryStrategy(max_cumulative_retry=100.0)
try:
_request_helpers.wait_and_retry(func, _get_status_code, retry_strategy)
except common.InvalidResponse as e:
ret_val = e.response
assert ret_val.status_code == status_codes[-1]
assert status_codes[-1] in common.RETRYABLE
assert func.call_count == 7
assert func.mock_calls == [mock.call()] * 7
assert randint_mock.call_count == 7
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 7
assert sleep_mock.call_count == 6
sleep_mock.assert_any_call(1.875)
sleep_mock.assert_any_call(2.0)
sleep_mock.assert_any_call(4.375)
sleep_mock.assert_any_call(8.5)
sleep_mock.assert_any_call(16.5)
sleep_mock.assert_any_call(32.25)
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_exceeds_max_retries(self, randint_mock, sleep_mock):
randint_mock.side_effect = [875, 0, 375, 500, 500, 250, 125]
status_codes = (
http.client.SERVICE_UNAVAILABLE,
http.client.GATEWAY_TIMEOUT,
common.TOO_MANY_REQUESTS,
http.client.INTERNAL_SERVER_ERROR,
http.client.SERVICE_UNAVAILABLE,
http.client.BAD_GATEWAY,
common.TOO_MANY_REQUESTS,
)
responses = [_make_response(status_code) for status_code in status_codes]
def raise_response():
raise common.InvalidResponse(responses.pop(0))
func = mock.Mock(side_effect=raise_response)
retry_strategy = common.RetryStrategy(max_retries=6)
try:
_request_helpers.wait_and_retry(func, _get_status_code, retry_strategy)
except common.InvalidResponse as e:
ret_val = e.response
assert ret_val.status_code == status_codes[-1]
assert status_codes[-1] in common.RETRYABLE
assert func.call_count == 7
assert func.mock_calls == [mock.call()] * 7
assert randint_mock.call_count == 7
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 7
assert sleep_mock.call_count == 6
sleep_mock.assert_any_call(1.875)
sleep_mock.assert_any_call(2.0)
sleep_mock.assert_any_call(4.375)
sleep_mock.assert_any_call(8.5)
sleep_mock.assert_any_call(16.5)
sleep_mock.assert_any_call(32.25)
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_zero_max_retries(self, randint_mock, sleep_mock):
randint_mock.side_effect = [875, 0, 375]
status_codes = (
http.client.SERVICE_UNAVAILABLE,
http.client.GATEWAY_TIMEOUT,
common.TOO_MANY_REQUESTS,
)
responses = [_make_response(status_code) for status_code in status_codes]
def raise_response():
raise common.InvalidResponse(responses.pop(0))
func = mock.Mock(side_effect=raise_response)
retry_strategy = common.RetryStrategy(max_retries=0)
try:
_request_helpers.wait_and_retry(func, _get_status_code, retry_strategy)
except common.InvalidResponse as e:
ret_val = e.response
assert func.call_count == 1
assert func.mock_calls == [mock.call()] * 1
assert ret_val.status_code == status_codes[0]
assert randint_mock.call_count == 1
assert sleep_mock.call_count == 0
@mock.patch("time.sleep")
@mock.patch("random.randint")
def test_retry_exceeded_reraises_connection_error(self, randint_mock, sleep_mock):
randint_mock.side_effect = [875, 0, 375, 500, 500, 250, 125]
responses = [requests.exceptions.ConnectionError] * 7
func = mock.Mock(side_effect=responses, spec=[])
retry_strategy = common.RetryStrategy(max_cumulative_retry=100.0)
with pytest.raises(requests.exceptions.ConnectionError):
_request_helpers.wait_and_retry(func, _get_status_code, retry_strategy)
assert func.call_count == 7
assert func.mock_calls == [mock.call()] * 7
assert randint_mock.call_count == 7
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 7
assert sleep_mock.call_count == 6
sleep_mock.assert_any_call(1.875)
sleep_mock.assert_any_call(2.0)
sleep_mock.assert_any_call(4.375)
sleep_mock.assert_any_call(8.5)
sleep_mock.assert_any_call(16.5)
sleep_mock.assert_any_call(32.25)
|