11
11
from azure_functions_worker import protos
12
12
from azure_functions_worker .constants import PYTHON_THREADPOOL_THREAD_COUNT , \
13
13
PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT , \
14
- PYTHON_THREADPOOL_THREAD_COUNT_MAX_37 , PYTHON_THREADPOOL_THREAD_COUNT_MIN
14
+ PYTHON_THREADPOOL_THREAD_COUNT_MAX_37 , PYTHON_THREADPOOL_THREAD_COUNT_MIN , ENABLE_INIT_INDEXING
15
15
from azure_functions_worker .version import VERSION
16
16
from tests .utils import testutils
17
17
from tests .utils .testutils import UNIT_TESTS_ROOT
22
22
DISPATCHER_STEIN_FUNCTIONS_DIR = testutils .UNIT_TESTS_FOLDER / \
23
23
'dispatcher_functions' / \
24
24
'dispatcher_functions_stein'
25
- FUNCTION_APP_DIRECTORY = UNIT_TESTS_ROOT / 'dispatcher_functions' / \
26
- 'dispatcher_functions_stein'
25
+ FUNCTION_APP_DIRECTORY = UNIT_TESTS_ROOT / 'dispatcher_functions' / \
26
+ 'dispatcher_functions_stein'
27
27
28
28
29
29
class TestThreadPoolSettingsPython37 (testutils .AsyncTestCase ):
@@ -680,10 +680,10 @@ async def test_dispatcher_load_azfunc_in_init(self):
680
680
)
681
681
self .assertIn ("azure.functions" , sys .modules )
682
682
683
- async def test_dispatcher_indexing_in_init (self ):
683
+ async def test_dispatcher_indexing_in_init_request (self ):
684
684
"""Test if azure functions is loaded during init
685
685
"""
686
- env = {"INIT_INDEXING" : "1" }
686
+ env = {ENABLE_INIT_INDEXING : "1" }
687
687
with patch .dict (os .environ , env ):
688
688
async with self ._ctrl as host :
689
689
r = await host .init_worker ()
@@ -696,7 +696,8 @@ async def test_dispatcher_indexing_in_init(self):
696
696
697
697
self .assertEqual (
698
698
len ([log for log in r .logs if log .message .startswith (
699
- "Received WorkerMetadataRequest from _handle__worker_init_request"
699
+ "Received WorkerMetadataRequest from "
700
+ "_handle__worker_init_request"
700
701
)]),
701
702
1
702
703
)
@@ -752,46 +753,177 @@ async def test_dispatcher_load_modules_con_app_placeholder_disabled(self):
752
753
" Placeholder: False" , logs )
753
754
754
755
755
- class DispatcherTests (unittest .TestCase ):
756
+ class TestDispatcherIndexinginInit (unittest .TestCase ):
756
757
757
758
def setUp (self ):
758
759
self .loop = asyncio .new_event_loop ()
759
760
asyncio .set_event_loop (self .loop )
760
761
self .dispatcher = testutils .create_dummy_dispatcher ()
762
+ sys .path .append (str (FUNCTION_APP_DIRECTORY ))
761
763
762
764
def tearDown (self ):
763
765
self .loop .close ()
764
766
765
- @patch .dict (os .environ , {'INIT_INDEXING' : 'true' })
766
- # @patch('azure_functions_worker.dispatcher.Dispatcher.get_function_metadata')
767
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'true' })
767
768
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
769
772
770
request = protos .StreamingMessage (
773
- worker_init_request = protos .WorkerInitRequest (
774
- host_version = "2.3.4" ,
775
- function_app_directory = str (FUNCTION_APP_DIRECTORY )
776
- )
771
+ worker_init_request = protos .WorkerInitRequest (
772
+ host_version = "2.3.4" ,
773
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
777
774
)
775
+ )
778
776
779
- # Execute the method under test
780
- result = self .loop .run_until_complete (
777
+ self .loop .run_until_complete (
781
778
self .dispatcher ._handle__worker_init_request (request ))
782
779
783
- # Assert conditions based on INIT_INDEXING being true
784
780
self .assertIsNotNone (self .dispatcher .function_metadata_result )
781
+ self .assertIsNone (self .dispatcher .function_metadata_exception )
782
+
783
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'false' })
784
+ def test_worker_init_request_with_indexing_disabled (self ):
785
+ request = protos .StreamingMessage (
786
+ worker_init_request = protos .WorkerInitRequest (
787
+ host_version = "2.3.4" ,
788
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
789
+ )
790
+ )
791
+
792
+ self .loop .run_until_complete (
793
+ self .dispatcher ._handle__worker_init_request (request ))
794
+
795
+ self .assertIsNone (self .dispatcher .function_metadata_result )
796
+ self .assertIsNone (self .dispatcher .function_metadata_exception )
797
+
798
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'true' })
799
+ def test_worker_init_request_with_indexing_exception (self ):
800
+ sys .path .remove (str (FUNCTION_APP_DIRECTORY ))
801
+
802
+ request = protos .StreamingMessage (
803
+ worker_init_request = protos .WorkerInitRequest (
804
+ host_version = "2.3.4" ,
805
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
806
+ )
807
+ )
808
+
809
+ self .loop .run_until_complete (
810
+ self .dispatcher ._handle__worker_init_request (request ))
811
+
812
+ self .assertIsNone (self .dispatcher .function_metadata_result )
813
+ self .assertIsNotNone (self .dispatcher .function_metadata_exception )
814
+
815
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'true' })
816
+ def test_functions_metadata_request_with_init_indexing_enabled (self ):
817
+ init_request = protos .StreamingMessage (
818
+ worker_init_request = protos .WorkerInitRequest (
819
+ host_version = "2.3.4" ,
820
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
821
+ )
822
+ )
823
+
824
+ metadata_request = protos .StreamingMessage (
825
+ functions_metadata_request = protos .FunctionsMetadataRequest (
826
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
827
+ )
828
+ )
829
+
830
+ init_response = self .loop .run_until_complete (
831
+ self .dispatcher ._handle__worker_init_request (init_request ))
832
+ self .assertEqual (init_response .worker_init_response .result .status ,
833
+ protos .StatusResult .Success )
834
+
835
+ metadata_response = self .loop .run_until_complete (
836
+ self .dispatcher ._handle__functions_metadata_request (metadata_request ))
837
+
838
+ self .assertEqual (metadata_response .function_metadata_response .result .status ,
839
+ protos .StatusResult .Success )
840
+ self .assertIsNotNone (self .dispatcher .function_metadata_result )
841
+ self .assertIsNone (self .dispatcher .function_metadata_exception )
842
+
843
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'false' })
844
+ def test_functions_metadata_request_with_init_indexing_enabled (self ):
845
+ init_request = protos .StreamingMessage (
846
+ worker_init_request = protos .WorkerInitRequest (
847
+ host_version = "2.3.4" ,
848
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
849
+ )
850
+ )
851
+
852
+ metadata_request = protos .StreamingMessage (
853
+ functions_metadata_request = protos .FunctionsMetadataRequest (
854
+ function_app_directory = str (str (FUNCTION_APP_DIRECTORY ))
855
+ )
856
+ )
857
+
858
+ init_response = self .loop .run_until_complete (
859
+ self .dispatcher ._handle__worker_init_request (init_request ))
860
+ self .assertEqual (init_response .worker_init_response .result .status ,
861
+ protos .StatusResult .Success )
862
+ self .assertIsNone (self .dispatcher .function_metadata_result )
863
+ self .assertIsNone (self .dispatcher .function_metadata_exception )
864
+
865
+
866
+ metadata_response = self .loop .run_until_complete (
867
+ self .dispatcher ._handle__functions_metadata_request (metadata_request ))
868
+
869
+ self .assertEqual (metadata_response .function_metadata_response .result .status ,
870
+ protos .StatusResult .Success )
871
+ self .assertIsNotNone (self .dispatcher .function_metadata_result )
872
+ self .assertIsNone (self .dispatcher .function_metadata_exception )
785
873
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))
874
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'true' })
875
+ def test_functions_metadata_request_with_indexing_exception (self ):
876
+ sys .path .remove (str (FUNCTION_APP_DIRECTORY ))
791
877
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
878
+ request = protos .StreamingMessage (
879
+ worker_init_request = protos .WorkerInitRequest (
880
+ host_version = "2.3.4" ,
881
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
882
+ )
883
+ )
796
884
885
+ metadata_request = protos .StreamingMessage (
886
+ functions_metadata_request = protos .FunctionsMetadataRequest (
887
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
888
+ )
889
+ )
797
890
891
+ self .loop .run_until_complete (
892
+ self .dispatcher ._handle__worker_init_request (request ))
893
+
894
+ self .assertIsNone (self .dispatcher .function_metadata_result )
895
+ self .assertIsNotNone (self .dispatcher .function_metadata_exception )
896
+
897
+ metadata_response = self .loop .run_until_complete (
898
+ self .dispatcher ._handle__functions_metadata_request (metadata_request ))
899
+
900
+ self .assertEqual (metadata_response .function_metadata_response .result .status ,
901
+ protos .StatusResult .Failure )
902
+
903
+ @patch .dict (os .environ , {ENABLE_INIT_INDEXING : 'false' })
904
+ def test_dispatcher_indexing_in_load_request (self ):
905
+ init_request = protos .StreamingMessage (
906
+ worker_init_request = protos .WorkerInitRequest (
907
+ host_version = "2.3.4" ,
908
+ function_app_directory = str (FUNCTION_APP_DIRECTORY )
909
+ )
910
+ )
911
+
912
+ self .loop .run_until_complete (
913
+ self .dispatcher ._handle__worker_init_request (init_request ))
914
+
915
+ self .assertIsNone (self .dispatcher .function_metadata_result )
916
+
917
+ load_request = protos .StreamingMessage (
918
+ function_load_request = protos .FunctionLoadRequest (
919
+ function_id = "http_trigger" ,
920
+ metadata = protos .RpcFunctionMetadata (
921
+ directory = str (FUNCTION_APP_DIRECTORY ),
922
+ properties = {"worker_indexed" : "True" }
923
+ )))
924
+
925
+ self .loop .run_until_complete (
926
+ self .dispatcher ._handle__function_load_request (load_request ))
927
+
928
+ self .assertIsNotNone (self .dispatcher .function_metadata_result )
929
+ self .assertIsNone (self .dispatcher .function_metadata_exception )
0 commit comments