From aefdce6b47fe5c1606093c7cfc72dadff9510bd4 Mon Sep 17 00:00:00 2001 From: Abhinav Maurya Date: Tue, 3 Nov 2026 06:55:30 +0530 Subject: [PATCH] propagate function_invocation_id in purge requests --- .../models/DurableOrchestrationClient.py | 8 +- .../models/test_DurableOrchestrationClient.py | 85 ++++++++++++++++++- 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/azure/durable_functions/models/DurableOrchestrationClient.py b/azure/durable_functions/models/DurableOrchestrationClient.py index c3d32c6..a110e9c 100644 --- a/azure/durable_functions/models/DurableOrchestrationClient.py +++ b/azure/durable_functions/models/DurableOrchestrationClient.py @@ -410,7 +410,9 @@ async def purge_instance_history(self, instance_id: str) -> PurgeHistoryResult: The results of the request to delete the orchestration instance """ request_url = f"{self._orchestration_bindings.rpc_base_url}instances/{instance_id}" - response = await self._delete_async_request(request_url) + response = await self._delete_async_request( + request_url, + function_invocation_id=self._function_invocation_id) return self._parse_purge_instance_history_response(response) async def purge_instance_history_by( @@ -448,7 +450,9 @@ async def purge_instance_history_by( created_time_to=created_time_to, runtime_status=runtime_status) request_url = options.to_url(self._orchestration_bindings.rpc_base_url) - response = await self._delete_async_request(request_url) + response = await self._delete_async_request( + request_url, + function_invocation_id=self._function_invocation_id) return self._parse_purge_instance_history_response(response) async def terminate(self, instance_id: str, reason: str) -> None: diff --git a/tests/models/test_DurableOrchestrationClient.py b/tests/models/test_DurableOrchestrationClient.py index 27df446..5829f8a 100644 --- a/tests/models/test_DurableOrchestrationClient.py +++ b/tests/models/test_DurableOrchestrationClient.py @@ -64,7 +64,7 @@ async def get(self, url: str): assert url == self._expected_url return self._response - async def delete(self, url: str): + async def delete(self, url: str, function_invocation_id: str = None): assert url == self._expected_url return self._response @@ -861,6 +861,13 @@ async def post(self, url: str, data: Any = None, trace_parent: str = None, assert function_invocation_id == self._expected_invocation_id return self._response + async def delete(self, url: str, function_invocation_id: str = None): + assert url == self._expected_url + self._received_invocation_id = function_invocation_id + if self._expected_invocation_id is not None: + assert function_invocation_id == self._expected_invocation_id + return self._response + @pytest.mark.asyncio async def test_start_new_passes_invocation_id(binding_string): @@ -894,3 +901,79 @@ async def test_start_new_passes_none_when_no_invocation_id(binding_string): await client.start_new(function_name) assert mock_request.received_invocation_id is None + + +@pytest.mark.asyncio +async def test_purge_instance_history_passes_invocation_id(binding_string): + """Test that purge_instance_history passes the function_invocation_id to the HTTP request.""" + invocation_id = "test-invocation-456" + + mock_request = MockRequestWithInvocationId( + expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}", + response=[200, dict(instancesDeleted=1)], + expected_invocation_id=invocation_id) + + client = DurableOrchestrationClient( + binding_string, function_invocation_id=invocation_id) + client._delete_async_request = mock_request.delete + + result = await client.purge_instance_history(TEST_INSTANCE_ID) + assert result.instances_deleted == 1 + assert mock_request.received_invocation_id == invocation_id + + +@pytest.mark.asyncio +async def test_purge_instance_history_passes_none_when_no_invocation_id(binding_string): + """Test that purge_instance_history passes None when no invocation ID is provided.""" + mock_request = MockRequestWithInvocationId( + expected_url=f"{RPC_BASE_URL}instances/{TEST_INSTANCE_ID}", + response=[200, dict(instancesDeleted=1)]) + + client = DurableOrchestrationClient(binding_string) + client._delete_async_request = mock_request.delete + + result = await client.purge_instance_history(TEST_INSTANCE_ID) + assert result.instances_deleted == 1 + assert mock_request.received_invocation_id is None + + +@pytest.mark.asyncio +async def test_purge_instance_history_by_passes_invocation_id(binding_string): + """Test that purge_instance_history_by passes invocation_id to the HTTP request.""" + invocation_id = "test-invocation-456" + + mock_request = MockRequestWithInvocationId( + expected_url=f"{RPC_BASE_URL}instances/" + "?createdTimeFrom=2000-01-01T00:00:00.000000Z" + "&runtimeStatus=Completed", + response=[200, dict(instancesDeleted=1)], + expected_invocation_id=invocation_id) + + client = DurableOrchestrationClient( + binding_string, function_invocation_id=invocation_id) + client._delete_async_request = mock_request.delete + + result = await client.purge_instance_history_by( + created_time_from=datetime(2000, 1, 1), + runtime_status=[OrchestrationRuntimeStatus.Completed]) + assert result.instances_deleted == 1 + assert mock_request.received_invocation_id == invocation_id + + +@pytest.mark.asyncio +async def test_purge_instance_history_by_passes_none_when_no_invocation_id(binding_string): + """Test that purge_instance_history_by passes None when no invocation ID is given.""" + mock_request = MockRequestWithInvocationId( + expected_url=f"{RPC_BASE_URL}instances/" + "?createdTimeFrom=2000-01-01T00:00:00.000000Z" + "&runtimeStatus=Completed", + response=[200, dict(instancesDeleted=1)]) + + client = DurableOrchestrationClient(binding_string) + client._delete_async_request = mock_request.delete + + result = await client.purge_instance_history_by( + created_time_from=datetime(2000, 1, 1), + runtime_status=[OrchestrationRuntimeStatus.Completed]) + assert result.instances_deleted == 1 + assert mock_request.received_invocation_id is None