|
1 | 1 | # Copyright (c) Microsoft Corporation. All rights reserved.
|
2 | 2 | # Licensed under the MIT License.
|
| 3 | +import asyncio |
3 | 4 | import collections as col
|
4 | 5 | import os
|
5 | 6 | import sys
|
|
8 | 9 | from unittest.mock import patch
|
9 | 10 |
|
10 | 11 | from azure_functions_worker import protos
|
11 |
| -from azure_functions_worker.version import VERSION |
12 |
| -from tests.utils import testutils |
13 | 12 | from azure_functions_worker.constants import PYTHON_THREADPOOL_THREAD_COUNT, \
|
14 | 13 | PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT, \
|
15 | 14 | 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 |
16 | 18 |
|
17 | 19 | SysVersionInfo = col.namedtuple("VersionInfo", ["major", "minor", "micro",
|
18 | 20 | "releaselevel", "serial"])
|
19 | 21 | DISPATCHER_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / 'dispatcher_functions'
|
20 | 22 | DISPATCHER_STEIN_FUNCTIONS_DIR = testutils.UNIT_TESTS_FOLDER / \
|
21 | 23 | 'dispatcher_functions' / \
|
22 | 24 | 'dispatcher_functions_stein'
|
| 25 | +FUNCTION_APP_DIRECTORY = UNIT_TESTS_ROOT / 'dispatcher_functions' / \ |
| 26 | + 'dispatcher_functions_stein' |
23 | 27 |
|
24 | 28 |
|
25 | 29 | class TestThreadPoolSettingsPython37(testutils.AsyncTestCase):
|
@@ -668,8 +672,35 @@ async def test_dispatcher_load_azfunc_in_init(self):
|
668 | 672 | )]),
|
669 | 673 | 1
|
670 | 674 | )
|
| 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 | + ) |
671 | 681 | self.assertIn("azure.functions", sys.modules)
|
672 | 682 |
|
| 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 | + |
673 | 704 | async def test_dispatcher_load_modules_dedicated_app(self):
|
674 | 705 | """Test modules are loaded in dedicated apps
|
675 | 706 | """
|
@@ -719,3 +750,48 @@ async def test_dispatcher_load_modules_con_app_placeholder_disabled(self):
|
719 | 750 | "worker_dependencies_path: , customer_dependencies_path: , "
|
720 | 751 | "working_directory: , Linux Consumption: True,"
|
721 | 752 | " 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