@@ -1736,3 +1736,50 @@ def test_handle_endpoint_invocation_invalid_events(settings):
17361736
17371737 assert invocation .status == InvocationStatusChoices .FAILURE
17381738 assert invocation .error_message == SystemErrorMessages .UNEXPECTED_ERROR
1739+
1740+
1741+ @pytest .mark .parametrize (
1742+ "status" ,
1743+ set (InvocationStatusChoices ).difference (
1744+ [InvocationStatusChoices .EXECUTING , InvocationStatusChoices .CANCELLED ]
1745+ ),
1746+ )
1747+ @pytest .mark .django_db
1748+ def test_handle_endpoint_invocation_wrong_state_raises (
1749+ mocker , settings , status
1750+ ):
1751+ invocation = InvocationFactory (status = status )
1752+ event = {
1753+ "invocationStatus" : "Completed" ,
1754+ "inferenceId" : f"{ settings .COMPONENTS_REGISTRY_PREFIX } -AEI-{ invocation .pk } " ,
1755+ }
1756+ mock_handle_event = mocker .patch .object (
1757+ EndpointOrchestrator ,
1758+ "handle_event" ,
1759+ )
1760+
1761+ with pytest .raises (RuntimeError , match = "not in the expected state" ):
1762+ handle_endpoint_invocation_event (event = event )
1763+ invocation .refresh_from_db ()
1764+
1765+ mock_handle_event .assert_not_called ()
1766+ assert invocation .status == status
1767+
1768+
1769+ @pytest .mark .django_db
1770+ def test_handle_endpoint_invocation_cancelled_skipped (mocker , settings ):
1771+ invocation = InvocationFactory (status = InvocationStatusChoices .CANCELLED )
1772+ event = {
1773+ "invocationStatus" : "Completed" ,
1774+ "inferenceId" : f"{ settings .COMPONENTS_REGISTRY_PREFIX } -AEI-{ invocation .pk } " ,
1775+ }
1776+ mock_handle_event = mocker .patch .object (
1777+ EndpointOrchestrator ,
1778+ "handle_event" ,
1779+ )
1780+
1781+ handle_endpoint_invocation_event (event = event )
1782+ invocation .refresh_from_db ()
1783+
1784+ mock_handle_event .assert_not_called ()
1785+ assert invocation .status == InvocationStatusChoices .CANCELLED
0 commit comments