Commit 967f687a authored by Cyril Monmouton's avatar Cyril Monmouton
Browse files

Add documentation and simplify test case

parent 44dbb239
Pipeline #83974 failed with stages
in 31 minutes and 39 seconds
......@@ -4,10 +4,12 @@ from app.conf import Config
from httpx import (
RemoteProtocolError,
TimeoutException) # => ReadTimeout, WriteTimeout, ConnectTimeout, PoolTimeout
from odes_storage.exceptions import ResponseHandlingException
_exceptions_type_to_retry = (RemoteProtocolError, TimeoutException, ResponseHandlingException)
def backoff_policy(on_backoff_handlers=None):
"""
Returns: a retry decorator.
......@@ -18,7 +20,7 @@ def backoff_policy(on_backoff_handlers=None):
"""
return backoff.on_exception(backoff.expo, # it will generate [1, 2, 4, 5, .. , 5]
(RemoteProtocolError, TimeoutException, ResponseHandlingException),
_exceptions_type_to_retry,
max_tries=Config.de_client_backoff_max_tries.value,
on_backoff=on_backoff_handlers,
base=2,
......
......@@ -26,7 +26,7 @@ from app.clients import (
make_search_client,
StorageRecordServiceClient,
SearchServiceClient)
from app.clients.backoff_policy import backoff_policy
from app.clients.backoff_policy import backoff_policy, _exceptions_type_to_retry
from app.utils import get_or_create_ctx
from tests.unit.test_utils import make_record
......@@ -75,16 +75,21 @@ class MyException(Exception):
pass
@pytest.mark.parametrize("exception_type, expected_retries_count", [
@pytest.mark.parametrize("exception_type, requested_retries_count", [
(RemoteProtocolError, 3),
(TimeoutException, 4),
(ResponseHandlingException, 2),
(RuntimeError, 1),
(MyException, 1),
(RuntimeError, 4),
(MyException, 4),
])
def test_de_clients_backoff(exception_type, expected_retries_count):
def test_de_clients_backoff(exception_type, requested_retries_count):
"""
Ensure retry mechanism is based on config 'de_client_backoff_max_tries' value and
only specific Exceptions type trigger this retry.
"""
# assigned expected retries count to config, to be used by backoff decorator
Config.de_client_backoff_max_tries.value = expected_retries_count
Config.de_client_backoff_max_tries.value = requested_retries_count
mocky_func = mock.MagicMock(autospec=True, side_effect=exception_type(f'{exception_type} has raised!'))
mocky_func.__name__ = ''
......@@ -97,4 +102,5 @@ def test_de_clients_backoff(exception_type, expected_retries_count):
# consume exception to let the test case pass
pass
finally:
assert mocky_func.call_count == Config.de_client_backoff_max_tries.value
retries_count = requested_retries_count if exception_type in _exceptions_type_to_retry else 1
assert mocky_func.call_count == retries_count
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment