Skip to content

Commit

Permalink
Changing modulenotfound to attribute error
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Aguiar authored and Gavin Aguiar committed Oct 9, 2023
1 parent 3844639 commit 6e3d652
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 3 additions & 1 deletion azure_functions_worker/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__'
Expand Down Expand Up @@ -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


Expand Down
23 changes: 15 additions & 8 deletions tests/unittests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand Down

0 comments on commit 6e3d652

Please sign in to comment.