diff --git a/azure_functions_worker/loader.py b/azure_functions_worker/loader.py index 6fce3a4d..7277eb3e 100644 --- a/azure_functions_worker/loader.py +++ b/azure_functions_worker/loader.py @@ -19,6 +19,7 @@ from .bindings.retrycontext import RetryPolicy from .constants import MODULE_NOT_FOUND_TS_URL, SCRIPT_FILE_NAME, \ PYTHON_LANGUAGE_RUNTIME, RETRY_POLICY, CUSTOMER_PACKAGES_PATH +from .logging import logger from .utils.wrappers import attach_message_to_exception _AZURE_NAMESPACE = '__app__' @@ -85,7 +86,8 @@ def build_retry_protos(indexed_function) -> Dict: def get_retry_settings(indexed_function): try: return indexed_function.get_settings_dict(RETRY_POLICY) - except ModuleNotFoundError: + except AttributeError as e: + logger.warning("AttributeError while loading retry policy. %s", e) return None diff --git a/tests/unittests/test_loader.py b/tests/unittests/test_loader.py index b78aa99f..85ff1472 100644 --- a/tests/unittests/test_loader.py +++ b/tests/unittests/test_loader.py @@ -5,7 +5,7 @@ import subprocess import sys import textwrap -from unittest.mock import Mock +from unittest.mock import Mock, patch from azure.functions import Function from azure.functions.decorators.retry_policy import RetryPolicy @@ -26,7 +26,11 @@ def test_function(): self.func = Function(self.test_function, script_file="test.py") self.function_registry = functions.Registry() - def test_building_fixed_retry_protos(self): + @classmethod + def get_script_dir(cls): + return testutils.UNIT_TESTS_FOLDER / 'load_functions' + + def test_loader_building_fixed_retry_protos(self): trigger = TimerTrigger(schedule="*/1 * * * * *", arg_name="mytimer", name="mytimer") self.func.add_trigger(trigger=trigger) @@ -39,7 +43,7 @@ def test_building_fixed_retry_protos(self): self.assertEqual(protos.retry_strategy, 1) # 1 enum for fixed delay self.assertEqual(protos.delay_interval.seconds, 120) - def test_building_exponential_retry_protos(self): + def test_loader_building_exponential_retry_protos(self): trigger = TimerTrigger(schedule="*/1 * * * * *", arg_name="mytimer", name="mytimer") self.func.add_trigger(trigger=trigger) @@ -56,16 +60,19 @@ def test_building_exponential_retry_protos(self): self.assertEqual(protos.minimum_interval.seconds, 60) self.assertEqual(protos.maximum_interval.seconds, 120) - def test_retry_module_not_found(self): + @patch('azure_functions_worker.logging.logger.warning') + def test_loader_retry_policy_attribute_error(self, mock_logger): self.func = Mock() - self.func.get_settings_dict.side_effect = ModuleNotFoundError + self.func.get_settings_dict.side_effect = AttributeError('DummyError') result = build_retry_protos(self.func) self.assertIsNone(result) - @classmethod - def get_script_dir(cls): - return testutils.UNIT_TESTS_FOLDER / 'load_functions' + # Check if the logged message starts with the expected string + logged_message = mock_logger.call_args[0][ + 0] # Get the first argument of the logger.warning call + self.assertTrue(logged_message.startswith( + 'AttributeError while loading retry policy.')) def test_loader_simple(self): r = self.webhost.request('GET', 'simple')