11from azure .durable_functions .models .ReplaySchema import ReplaySchema
22from .orchestrator_test_utils \
3- import assert_orchestration_state_equals , get_orchestration_state_result , assert_valid_schema , \
3+ import assert_orchestration_state_equals , assert_results_are_equal , get_orchestration_state_result , assert_valid_schema , \
44 get_entity_state_result , assert_entity_state_equals
55from tests .test_utils .ContextBuilder import ContextBuilder
66from tests .test_utils .EntityContextBuilder import EntityContextBuilder
@@ -23,6 +23,14 @@ def generator_function_call_entity(context):
2323 outputs .append (x )
2424 return outputs
2525
26+ def generator_function_catch_entity_exception (context ):
27+ entityId = df .EntityId ("Counter" , "myCounter" )
28+ try :
29+ yield context .call_entity (entityId , "add" , 3 )
30+ return "No exception thrown"
31+ except :
32+ return "Exception thrown"
33+
2634def generator_function_signal_entity (context ):
2735 outputs = []
2836 entityId = df .EntityId ("Counter" , "myCounter" )
@@ -53,6 +61,29 @@ def counter_entity_function(context):
5361 context .set_state (current_value )
5462 context .set_result (result )
5563
64+ def counter_entity_function_raises_exception (context ):
65+ raise Exception ("boom!" )
66+
67+ def test_entity_raises_exception ():
68+ # Create input batch
69+ batch = []
70+ add_to_batch (batch , name = "get" )
71+ context_builder = EntityContextBuilder (batch = batch )
72+
73+ # Run the entity, get observed result
74+ result = get_entity_state_result (
75+ context_builder ,
76+ counter_entity_function_raises_exception ,
77+ )
78+
79+ # Construct expected result
80+ expected_state = entity_base_expected_state ()
81+ apply_operation (expected_state , result = "boom!" , state = None , is_error = True )
82+ expected = expected_state .to_json ()
83+
84+ # Ensure expectation matches observed behavior
85+ #assert_valid_schema(result)
86+ assert_entity_state_equals (expected , result )
5687
5788def test_entity_signal_then_call ():
5889 """Tests that a simple counter entity outputs the correct value
@@ -161,11 +192,11 @@ def add_signal_entity_action(state: OrchestratorState, id_: df.EntityId, op: str
161192 state .actions .append ([action ])
162193
163194def add_call_entity_completed_events (
164- context_builder : ContextBuilder , op : str , instance_id = str , input_ = None , event_id = 0 ):
195+ context_builder : ContextBuilder , op : str , instance_id = str , input_ = None , event_id = 0 , is_error = False ):
165196 context_builder .add_event_sent_event (instance_id , event_id )
166197 context_builder .add_orchestrator_completed_event ()
167198 context_builder .add_orchestrator_started_event ()
168- context_builder .add_event_raised_event (name = "0000" , id_ = 0 , input_ = input_ , is_entity = True )
199+ context_builder .add_event_raised_event (name = "0000" , id_ = 0 , input_ = input_ , is_entity = True , is_error = is_error )
169200
170201def test_call_entity_sent ():
171202 context_builder = ContextBuilder ('test_simple_function' )
@@ -233,4 +264,29 @@ def test_call_entity_raised():
233264
234265 #assert_valid_schema(result)
235266
267+ assert_orchestration_state_equals (expected , result )
268+
269+ def test_call_entity_catch_exception ():
270+ entityId = df .EntityId ("Counter" , "myCounter" )
271+ context_builder = ContextBuilder ('catch exceptions' )
272+ add_call_entity_completed_events (
273+ context_builder ,
274+ "add" ,
275+ df .EntityId .get_scheduler_id (entityId ),
276+ input_ = "I am an error!" ,
277+ event_id = 0 ,
278+ is_error = True
279+ )
280+
281+ result = get_orchestration_state_result (
282+ context_builder , generator_function_catch_entity_exception )
283+
284+ expected_state = base_expected_state (
285+ "Exception thrown"
286+ )
287+
288+ add_call_entity_action (expected_state , entityId , "add" , 3 )
289+ expected_state ._is_done = True
290+ expected = expected_state .to_json ()
291+
236292 assert_orchestration_state_equals (expected , result )
0 commit comments