1
1
from azure .durable_functions .models .ReplaySchema import ReplaySchema
2
2
from .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 , \
4
4
get_entity_state_result , assert_entity_state_equals
5
5
from tests .test_utils .ContextBuilder import ContextBuilder
6
6
from tests .test_utils .EntityContextBuilder import EntityContextBuilder
@@ -23,6 +23,14 @@ def generator_function_call_entity(context):
23
23
outputs .append (x )
24
24
return outputs
25
25
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
+
26
34
def generator_function_signal_entity (context ):
27
35
outputs = []
28
36
entityId = df .EntityId ("Counter" , "myCounter" )
@@ -53,6 +61,29 @@ def counter_entity_function(context):
53
61
context .set_state (current_value )
54
62
context .set_result (result )
55
63
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 )
56
87
57
88
def test_entity_signal_then_call ():
58
89
"""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
161
192
state .actions .append ([action ])
162
193
163
194
def 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 ):
165
196
context_builder .add_event_sent_event (instance_id , event_id )
166
197
context_builder .add_orchestrator_completed_event ()
167
198
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 )
169
200
170
201
def test_call_entity_sent ():
171
202
context_builder = ContextBuilder ('test_simple_function' )
@@ -233,4 +264,29 @@ def test_call_entity_raised():
233
264
234
265
#assert_valid_schema(result)
235
266
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
+
236
292
assert_orchestration_state_equals (expected , result )
0 commit comments