Commit 790dde80 authored by Yannick's avatar Yannick
Browse files

backoff policy no longer configurable

parent 93019681
Pipeline #113084 failed with stages
in 49 minutes and 33 seconds
import backoff
from app.conf import Config
from httpx import (
RemoteProtocolError,
......@@ -8,6 +7,8 @@ from odes_storage.exceptions import ResponseHandlingException
_exceptions_type_to_retry = (RemoteProtocolError, TimeoutException, ResponseHandlingException)
_backoff_max_tries = 4
_backoff_max_wait = 5
def backoff_policy(on_backoff_handlers=None):
......@@ -21,8 +22,8 @@ def backoff_policy(on_backoff_handlers=None):
return backoff.on_exception(backoff.expo, # it will generate [1, 2, 4, 5, .. , 5]
_exceptions_type_to_retry,
max_tries=Config.de_client_backoff_max_tries.value,
max_tries=_backoff_max_tries,
on_backoff=on_backoff_handlers,
base=2,
factor=1,
max_value=Config.de_client_backoff_max_wait.value)
max_value=_backoff_max_wait)
......@@ -138,21 +138,6 @@ class ConfigurationContainer:
default='500',
factory=lambda x: int(x))
de_client_backoff_max_tries: EnvVar = EnvVar(
key='DE_CLIENT_BACKOFF_MAX_RETRIES',
description="""The maximum number of attempts to make before giving
up. Once exhausted, the exception will be allowed to escape.
The default value of None means their is no limit to the
number of tries.""",
default='4',
factory=lambda x: int(x))
de_client_backoff_max_wait: EnvVar = EnvVar(
key='DE_CLIENT_BACKOFF_MAX_WAIT',
description="""The maximum wait in second between retry. """,
default='5',
factory=lambda x: int(x))
build_details: EnvVar = EnvVar(
key='OS_WELLBORE_DDMS_BUILD_DETAILS',
description='contains optional extra information of the build, format is the multiple "key=value" separated'
......
......@@ -31,12 +31,12 @@ from app.clients.backoff_policy import backoff_policy, _exceptions_type_to_retry
from app.context import get_or_create_ctx
from tests.unit.test_utils import make_record
from odes_storage.exceptions import ResponseHandlingException
from app.conf import Config
from tests.unit.test_utils import ctx_fixture
@pytest.mark.asyncio
async def test_make_storage_client(local_dev_config, httpx_mock: HTTPXMock, ctx_fixture):
async def test_make_storage_client(local_dev_config, httpx_mock: HTTPXMock, ctx_fixture, nope_logger_fixture):
async with make_storage_record_client(host=local_dev_config.service_host_storage.value,
timeout=local_dev_config.de_client_config_timeout.value) as client:
assert isinstance(client, StorageRecordServiceClient)
......@@ -54,7 +54,7 @@ async def test_make_storage_client(local_dev_config, httpx_mock: HTTPXMock, ctx_
@pytest.mark.asyncio
async def test_make_search_client(local_dev_config, httpx_mock: HTTPXMock, ctx_fixture):
async def test_make_search_client(local_dev_config, httpx_mock: HTTPXMock, ctx_fixture, nope_logger_fixture):
async with make_search_client(host=local_dev_config.service_host_search.value,
timeout=local_dev_config.de_client_config_timeout.value) as client:
assert isinstance(client, SearchServiceClient)
......@@ -82,25 +82,25 @@ class MyException(Exception):
(RuntimeError, 4),
(MyException, 4),
])
def test_de_clients_backoff(exception_type, requested_retries_count):
def test_de_clients_backoff(exception_type, requested_retries_count, nope_logger_fixture):
"""
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 = requested_retries_count
mocky_func = mock.MagicMock(autospec=True, side_effect=exception_type(f'{exception_type} has raised!'))
mocky_func.__name__ = ''
decorator_func = backoff_policy()
decorated_mocked_func = decorator_func(mocky_func)
try:
decorated_mocked_func()
except BaseException:
# consume exception to let the test case pass
pass
finally:
retries_count = requested_retries_count if exception_type in _exceptions_type_to_retry else 1
assert mocky_func.call_count == retries_count
with mock.patch('app.clients.backoff_policy._backoff_max_wait', 1):
# assigned expected retries count to config, to be used by backoff decorator
with mock.patch('app.clients.backoff_policy._backoff_max_tries', requested_retries_count):
mocky_func = mock.MagicMock(autospec=True, side_effect=exception_type(f'{exception_type} has raised!'))
mocky_func.__name__ = ''
decorator_func = backoff_policy()
decorated_mocked_func = decorator_func(mocky_func)
try:
decorated_mocked_func()
except BaseException:
# consume exception to let the test case pass
pass
finally:
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