Skip to content

Commit cd9d951

Browse files
Copilotberndverst
andcommitted
Add input validation to send_event following .NET reference implementation
Co-authored-by: berndverst <[email protected]>
1 parent 22d1459 commit cd9d951

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

durabletask/worker.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,12 @@ def wait_for_external_event(self, name: str) -> task.Task:
854854

855855
def send_event(self, instance_id: str, event_name: str, *,
856856
data: Optional[Any] = None) -> task.Task:
857+
# Validate inputs similar to .NET implementation
858+
if not instance_id:
859+
raise ValueError("instance_id cannot be None or empty")
860+
if not event_name:
861+
raise ValueError("event_name cannot be None or empty")
862+
857863
id = self.next_sequence_number()
858864
encoded_data = shared.to_json(data) if data is not None else None
859865
action = ph.new_send_event_action(id, instance_id, event_name, encoded_data)

tests/durabletask/test_send_event.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,40 @@ def orchestrator(ctx: task.OrchestrationContext, _):
204204
assert action.sendEvent.name == "event1"
205205
expected_data = json.dumps({"key": "value", "number": 42})
206206
assert action.sendEvent.data.value == expected_data
207+
208+
209+
def test_send_event_validation():
210+
"""Test send_event input validation"""
211+
212+
def orchestrator_empty_instance(ctx: task.OrchestrationContext, _):
213+
yield ctx.send_event("", "event1", data="test")
214+
return "completed"
215+
216+
registry = worker._Registry()
217+
218+
# Test empty instance_id
219+
name1 = registry.add_orchestrator(orchestrator_empty_instance)
220+
new_events = [
221+
helpers.new_orchestrator_started_event(),
222+
helpers.new_execution_started_event(name1, TEST_INSTANCE_ID, encoded_input=None),
223+
]
224+
executor = worker._OrchestrationExecutor(registry, TEST_LOGGER)
225+
226+
result = executor.execute(TEST_INSTANCE_ID, [], new_events)
227+
228+
# Check if the orchestration failed due to validation error
229+
actions = result.actions
230+
if len(actions) > 0:
231+
action = actions[0]
232+
if action.WhichOneof("orchestratorActionType") == "completeOrchestration":
233+
complete_action = action.completeOrchestration
234+
if complete_action.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED:
235+
# The orchestration should have failed with the validation error
236+
failure_details = complete_action.failureDetails
237+
assert "instance_id cannot be None or empty" in failure_details.errorMessage
238+
else:
239+
assert False, "Expected orchestration to fail with validation error"
240+
else:
241+
assert False, "Expected failure completion action, got different action type"
242+
else:
243+
assert False, "Expected at least one action (failure completion)"

0 commit comments

Comments
 (0)