Skip to content

Commit 284f5af

Browse files
committed
Added unit tests
1 parent e077a23 commit 284f5af

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

tests/unittests/test_dispatcher.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
import asyncio
34
import collections as col
45
import os
56
import sys
@@ -8,18 +9,21 @@
89
from unittest.mock import patch
910

1011
from azure_functions_worker import protos
11-
from azure_functions_worker.version import VERSION
12-
from tests.utils import testutils
1312
from azure_functions_worker.constants import PYTHON_THREADPOOL_THREAD_COUNT, \
1413
PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT, \
1514
PYTHON_THREADPOOL_THREAD_COUNT_MAX_37, PYTHON_THREADPOOL_THREAD_COUNT_MIN
15+
from azure_functions_worker.version import VERSION
16+
from tests.utils import testutils
17+
from tests.utils.testutils import UNIT_TESTS_ROOT
1618

1719
SysVersionInfo = col.namedtuple("VersionInfo", ["major", "minor", "micro",
1820
"releaselevel", "serial"])
1921
DISPATCHER_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / 'dispatcher_functions'
2022
DISPATCHER_STEIN_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / \
2123
'dispatcher_functions' / \
2224
'dispatcher_functions_stein'
25+
FUNCTION_APP_DIRECTORY = UNIT_TESTS_ROOT / 'dispatcher_functions' / \
26+
'dispatcher_functions_stein'
2327

2428

2529
class TestThreadPoolSettingsPython37(testutils.AsyncTestCase):
@@ -668,8 +672,35 @@ async def test_dispatcher_load_azfunc_in_init(self):
668672
)]),
669673
1
670674
)
675+
self.assertEqual(
676+
len([log for log in r.logs if log.message.startswith(
677+
"Received WorkerMetadataRequest from _handle__worker_init_request"
678+
)]),
679+
0
680+
)
671681
self.assertIn("azure.functions", sys.modules)
672682

683+
async def test_dispatcher_indexing_in_init(self):
684+
"""Test if azure functions is loaded during init
685+
"""
686+
env = {"INIT_INDEXING": "1"}
687+
with patch.dict(os.environ, env):
688+
async with self._ctrl as host:
689+
r = await host.init_worker()
690+
self.assertEqual(
691+
len([log for log in r.logs if log.message.startswith(
692+
"Received WorkerInitRequest"
693+
)]),
694+
1
695+
)
696+
697+
self.assertEqual(
698+
len([log for log in r.logs if log.message.startswith(
699+
"Received WorkerMetadataRequest from _handle__worker_init_request"
700+
)]),
701+
1
702+
)
703+
673704
async def test_dispatcher_load_modules_dedicated_app(self):
674705
"""Test modules are loaded in dedicated apps
675706
"""
@@ -719,3 +750,48 @@ async def test_dispatcher_load_modules_con_app_placeholder_disabled(self):
719750
"worker_dependencies_path: , customer_dependencies_path: , "
720751
"working_directory: , Linux Consumption: True,"
721752
" Placeholder: False", logs)
753+
754+
755+
class DispatcherTests(unittest.TestCase):
756+
757+
def setUp(self):
758+
self.loop = asyncio.new_event_loop()
759+
asyncio.set_event_loop(self.loop)
760+
self.dispatcher = testutils.create_dummy_dispatcher()
761+
762+
def tearDown(self):
763+
self.loop.close()
764+
765+
@patch.dict(os.environ, {'INIT_INDEXING': 'true'})
766+
# @patch('azure_functions_worker.dispatcher.Dispatcher.get_function_metadata')
767+
def test_worker_init_request_with_indexing_enabled(self):
768+
# Set up any additional mocks needed for your test
769+
# mock_get_function_metadata.return_value = None
770+
import azure.functions
771+
772+
request = protos.StreamingMessage(
773+
worker_init_request=protos.WorkerInitRequest(
774+
host_version="2.3.4",
775+
function_app_directory=str(FUNCTION_APP_DIRECTORY)
776+
)
777+
)
778+
779+
# Execute the method under test
780+
result = self.loop.run_until_complete(
781+
self.dispatcher._handle__worker_init_request(request))
782+
783+
# Assert conditions based on INIT_INDEXING being true
784+
self.assertIsNotNone(self.dispatcher.function_metadata_result)
785+
786+
@patch.dict(os.environ, {'INIT_INDEXING': 'false'})
787+
@patch('yourmodule.dispatcher.Dispatcher.get_function_metadata')
788+
def test_worker_init_request_with_indexing_disabled(self, mock_get_function_metadata):
789+
# Execute the method under test
790+
# result = self.loop.run_until_complete(self.dispatcher._handle__worker_init_request(request))
791+
792+
# Assert conditions based on INIT_INDEXING being false
793+
# For example, you might expect function_metadata_result or function_metadata_exception to be set differently
794+
# Adjust your assertions accordingly
795+
pass
796+
797+

0 commit comments

Comments
 (0)