kaxil commented on code in PR #63614: URL: https://github.com/apache/airflow/pull/63614#discussion_r3144426356
########## providers/microsoft/azure/src/airflow/providers/microsoft/azure/triggers/synapse.py: ########## @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import asyncio +import time +from collections.abc import AsyncIterator +from typing import Any + +from azure.core.exceptions import ServiceRequestError + +from airflow.providers.microsoft.azure.hooks.synapse import ( + AzureSynapsePipelineAsyncHook, + AzureSynapsePipelineRunStatus, +) +from airflow.triggers.base import BaseTrigger, TriggerEvent + + +class AzureSynapsePipelineTrigger(BaseTrigger): + """ + Trigger when an Azure Synapse pipeline run reaches a terminal state. + + If an unexpected exception occurs while polling, the trigger attempts to cancel + the pipeline run to avoid leaving orphaned executions. + + :param run_id: Pipeline run identifier. + :param azure_synapse_conn_id: Azure Synapse connection id. + :param azure_synapse_workspace_dev_endpoint: Synapse workspace dev endpoint. + :param end_time: Epoch timestamp when the trigger should timeout. + :param check_interval: Poll interval in seconds. + """ + + def __init__( + self, + run_id: str, + azure_synapse_conn_id: str, + azure_synapse_workspace_dev_endpoint: str, + end_time: float, + check_interval: int = 60, + ): + super().__init__() + + self.run_id = run_id + self.azure_synapse_conn_id = azure_synapse_conn_id + self.azure_synapse_workspace_dev_endpoint = azure_synapse_workspace_dev_endpoint + self.end_time = end_time + self.check_interval = check_interval + + def serialize(self) -> tuple[str, dict[str, Any]]: + """Serialize trigger arguments and classpath.""" + return ( + f"{self.__class__.__module__}.{self.__class__.__name__}", + { + "run_id": self.run_id, + "azure_synapse_conn_id": self.azure_synapse_conn_id, + "azure_synapse_workspace_dev_endpoint": self.azure_synapse_workspace_dev_endpoint, + "end_time": self.end_time, + "check_interval": self.check_interval, + }, + ) + + def _build_trigger_event(self, pipeline_status: str) -> TriggerEvent | None: + """ + Convert Synapse pipeline status to TriggerEvent. + + Returns None if the pipeline is still running. + """ + if pipeline_status == AzureSynapsePipelineRunStatus.SUCCEEDED: + return TriggerEvent( + { + "status": "success", + "message": f"Pipeline run {self.run_id} succeeded.", + "run_id": self.run_id, + } + ) + + if pipeline_status in AzureSynapsePipelineRunStatus.FAILURE_STATES: + return TriggerEvent( + { + "status": "error", + "message": f"Pipeline run {self.run_id} finished with state {pipeline_status}.", + "run_id": self.run_id, + } + ) + + return None + + async def run(self) -> AsyncIterator[TriggerEvent]: + """Poll Synapse pipeline status until completion or timeout.""" + async with AzureSynapsePipelineAsyncHook( + azure_synapse_conn_id=self.azure_synapse_conn_id, + azure_synapse_workspace_dev_endpoint=self.azure_synapse_workspace_dev_endpoint, + ) as hook: + try: + executed_after_token_refresh = True + + while time.time() < self.end_time: + try: + pipeline_status = await hook.get_pipeline_run_status(self.run_id) + + executed_after_token_refresh = True + + event = self._build_trigger_event(pipeline_status) + + if event: + yield event + return + + self.log.info( + "Pipeline %s not finished yet (state=%s). Sleeping %s seconds.", + self.run_id, + pipeline_status, + self.check_interval, + ) + + await asyncio.sleep(self.check_interval) + + except ServiceRequestError: + # Azure token may expire during long pipeline runs. + if not executed_after_token_refresh: + raise + + await hook.refresh_conn() + executed_after_token_refresh = False + + await hook.cancel_pipeline_run(self.run_id) Review Comment: The timeout-path cancel on line 141 isn't wrapped in its own try/except, so a transient failure on that single call falls through to the outer `except Exception`, which retries the cancel on line 156 and yields the cancel error as the `message` instead of "Timeout waiting for pipeline run...". Could you wrap the line-141 cancel in its own try/except (logging if it fails) and unconditionally yield the timeout event so the user sees the real reason? ########## providers/microsoft/azure/src/airflow/providers/microsoft/azure/triggers/synapse.py: ########## @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import asyncio +import time +from collections.abc import AsyncIterator +from typing import Any + +from azure.core.exceptions import ServiceRequestError + +from airflow.providers.microsoft.azure.hooks.synapse import ( + AzureSynapsePipelineAsyncHook, + AzureSynapsePipelineRunStatus, +) +from airflow.triggers.base import BaseTrigger, TriggerEvent + + +class AzureSynapsePipelineTrigger(BaseTrigger): + """ + Trigger when an Azure Synapse pipeline run reaches a terminal state. + + If an unexpected exception occurs while polling, the trigger attempts to cancel + the pipeline run to avoid leaving orphaned executions. + + :param run_id: Pipeline run identifier. + :param azure_synapse_conn_id: Azure Synapse connection id. + :param azure_synapse_workspace_dev_endpoint: Synapse workspace dev endpoint. + :param end_time: Epoch timestamp when the trigger should timeout. + :param check_interval: Poll interval in seconds. + """ + + def __init__( + self, + run_id: str, + azure_synapse_conn_id: str, + azure_synapse_workspace_dev_endpoint: str, + end_time: float, + check_interval: int = 60, + ): + super().__init__() + + self.run_id = run_id + self.azure_synapse_conn_id = azure_synapse_conn_id + self.azure_synapse_workspace_dev_endpoint = azure_synapse_workspace_dev_endpoint + self.end_time = end_time + self.check_interval = check_interval + + def serialize(self) -> tuple[str, dict[str, Any]]: + """Serialize trigger arguments and classpath.""" + return ( + f"{self.__class__.__module__}.{self.__class__.__name__}", + { + "run_id": self.run_id, + "azure_synapse_conn_id": self.azure_synapse_conn_id, + "azure_synapse_workspace_dev_endpoint": self.azure_synapse_workspace_dev_endpoint, + "end_time": self.end_time, + "check_interval": self.check_interval, + }, + ) + + def _build_trigger_event(self, pipeline_status: str) -> TriggerEvent | None: + """ + Convert Synapse pipeline status to TriggerEvent. + + Returns None if the pipeline is still running. + """ + if pipeline_status == AzureSynapsePipelineRunStatus.SUCCEEDED: + return TriggerEvent( + { + "status": "success", + "message": f"Pipeline run {self.run_id} succeeded.", + "run_id": self.run_id, + } + ) + + if pipeline_status in AzureSynapsePipelineRunStatus.FAILURE_STATES: + return TriggerEvent( + { + "status": "error", + "message": f"Pipeline run {self.run_id} finished with state {pipeline_status}.", + "run_id": self.run_id, + } + ) + + return None + + async def run(self) -> AsyncIterator[TriggerEvent]: + """Poll Synapse pipeline status until completion or timeout.""" + async with AzureSynapsePipelineAsyncHook( + azure_synapse_conn_id=self.azure_synapse_conn_id, + azure_synapse_workspace_dev_endpoint=self.azure_synapse_workspace_dev_endpoint, + ) as hook: + try: + executed_after_token_refresh = True + + while time.time() < self.end_time: Review Comment: There's a race between the deadline check at line 112 and the final `await asyncio.sleep(self.check_interval)` at line 131: if the pipeline reaches `SUCCEEDED` during that last sleep AND the deadline expires in the same window, the loop exits without one more poll and we cancel a run that already succeeded. Doing one final `get_pipeline_run_status` after leaving the loop and only canceling if it's still non-terminal would close that gap. ########## providers/microsoft/azure/src/airflow/providers/microsoft/azure/operators/synapse.py: ########## @@ -194,11 +200,12 @@ class AzureSynapseRunPipelineOperator(BaseOperator): :param parameters: Parameters of the pipeline run. These parameters are referenced in a pipeline via ``@pipeline().parameters.parameterName`` and will be used only if the ``reference_pipeline_run_id`` is not specified. - :param timeout: Time in seconds to wait for a pipeline to reach a terminal status for non-asynchronous - waits. Used only if ``wait_for_termination`` is True. + :param timeout: Maximum time in seconds to wait for the pipeline run to reach a terminal state. In synchronous + mode this controls how long the operator polls for completion. In deferrable mode it determines the maximum + time the trigger will wait before emitting a timeout event. Used only if ``wait_for_termination`` is True. :param check_interval: Time in seconds to check on a pipeline run's status for non-asynchronous waits. Used only if ``wait_for_termination`` is True. - + :param deferrable: Run operator in deferrable mode. Used only if ``wait_for_termination`` is True. Default is False. Review Comment: The docstring says "Default is False" but the actual default at line 227 is `conf.getboolean("operators", "default_deferrable", fallback=False)`, so users who set `[operators] default_deferrable = True` globally get True here. Sibling `AzureDataFactoryRunPipelineOperator` just says "Run operator in deferrable mode." Either mirror that, or rephrase to something like `Defaults to [operators] default_deferrable (False if unset).` ########## providers/microsoft/azure/tests/unit/microsoft/azure/triggers/test_synapse.py: ########## @@ -0,0 +1,143 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import asyncio +import time +from unittest import mock + +import pytest + +from airflow.providers.microsoft.azure.triggers.synapse import ( + AzureSynapsePipelineRunStatus, + AzureSynapsePipelineTrigger, +) +from airflow.triggers.base import TriggerEvent + +AZURE_SYNAPSE_CONN_ID = "azure_synapse_default" +AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT = "azure_synapse_workspace_dev_endpoint" +RUN_ID = "7f8c6c72-c093-11ec-a83d-0242ac120007" +POKE_INTERVAL = 5 +AZ_PIPELINE_END_TIME = time.time() + 60 * 60 * 24 * 7 +MODULE = "airflow.providers.microsoft.azure" + + +class TestAzureSynapsePipelineTrigger: + TRIGGER = AzureSynapsePipelineTrigger( + run_id=RUN_ID, + azure_synapse_conn_id=AZURE_SYNAPSE_CONN_ID, + azure_synapse_workspace_dev_endpoint=AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT, + check_interval=POKE_INTERVAL, + end_time=AZ_PIPELINE_END_TIME, + ) + + def test_synapse_trigger_serialization(self): + classpath, kwargs = self.TRIGGER.serialize() + + assert classpath == f"{MODULE}.triggers.synapse.AzureSynapsePipelineTrigger" + + assert kwargs == { + "run_id": RUN_ID, + "azure_synapse_conn_id": AZURE_SYNAPSE_CONN_ID, + "azure_synapse_workspace_dev_endpoint": AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT, + "check_interval": POKE_INTERVAL, + "end_time": AZ_PIPELINE_END_TIME, + } + + @pytest.mark.parametrize( + ("status", "expected"), + [ + ("Succeeded", "success"), + ("Failed", "error"), + ("Cancelled", "error"), + ], + ) + def test_build_trigger_event_terminal_states(self, status, expected): + event = self.TRIGGER._build_trigger_event(status) + + assert event is not None + assert event.payload["status"] == expected + assert event.payload["run_id"] == RUN_ID + + @pytest.mark.parametrize( + "status", + ["Queued", "InProgress", "Canceling"], + ) + def test_build_trigger_event_non_terminal_states(self, status): + event = self.TRIGGER._build_trigger_event(status) + assert event is None + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_run_success(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.SUCCEEDED + + generator = self.TRIGGER.run() + actual = await generator.asend(None) + + assert actual == TriggerEvent( + { + "status": "success", + "message": f"Pipeline run {RUN_ID} succeeded.", + "run_id": RUN_ID, + } + ) + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_run_failed(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.FAILED + + generator = self.TRIGGER.run() + actual = await generator.asend(None) + + assert actual == TriggerEvent( + { + "status": "error", + "message": f"Pipeline run {RUN_ID} finished with state Failed.", + "run_id": RUN_ID, + } + ) + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_waiting(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.QUEUED + + task = asyncio.create_task(self.TRIGGER.run().__anext__()) + + await asyncio.sleep(0.5) + + assert task.done() is False + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_exception(self, mock_status): + mock_status.side_effect = Exception("API failure") + + events = [event async for event in self.TRIGGER.run()] + + assert events == [ + TriggerEvent( + { + "status": "error", + "message": "API failure", + "run_id": RUN_ID, + } + ) + ] Review Comment: The timeout cancel-and-yield branch on `triggers/synapse.py:141-148` has no coverage. Could you add a test that fast-forwards `time.time()` past `end_time`, mocks the async hook, and asserts both that `cancel_pipeline_run` was awaited and that the yielded event message starts with "Timeout"? The cancel is the trigger's main safety net against orphan Synapse runs, so it's worth locking in. ########## providers/microsoft/azure/tests/unit/microsoft/azure/triggers/test_synapse.py: ########## @@ -0,0 +1,143 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import asyncio +import time +from unittest import mock + +import pytest + +from airflow.providers.microsoft.azure.triggers.synapse import ( + AzureSynapsePipelineRunStatus, + AzureSynapsePipelineTrigger, +) +from airflow.triggers.base import TriggerEvent + +AZURE_SYNAPSE_CONN_ID = "azure_synapse_default" +AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT = "azure_synapse_workspace_dev_endpoint" +RUN_ID = "7f8c6c72-c093-11ec-a83d-0242ac120007" +POKE_INTERVAL = 5 +AZ_PIPELINE_END_TIME = time.time() + 60 * 60 * 24 * 7 +MODULE = "airflow.providers.microsoft.azure" + + +class TestAzureSynapsePipelineTrigger: + TRIGGER = AzureSynapsePipelineTrigger( + run_id=RUN_ID, + azure_synapse_conn_id=AZURE_SYNAPSE_CONN_ID, + azure_synapse_workspace_dev_endpoint=AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT, + check_interval=POKE_INTERVAL, + end_time=AZ_PIPELINE_END_TIME, + ) + + def test_synapse_trigger_serialization(self): + classpath, kwargs = self.TRIGGER.serialize() + + assert classpath == f"{MODULE}.triggers.synapse.AzureSynapsePipelineTrigger" + + assert kwargs == { + "run_id": RUN_ID, + "azure_synapse_conn_id": AZURE_SYNAPSE_CONN_ID, + "azure_synapse_workspace_dev_endpoint": AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT, + "check_interval": POKE_INTERVAL, + "end_time": AZ_PIPELINE_END_TIME, + } + + @pytest.mark.parametrize( + ("status", "expected"), + [ + ("Succeeded", "success"), + ("Failed", "error"), + ("Cancelled", "error"), + ], + ) + def test_build_trigger_event_terminal_states(self, status, expected): + event = self.TRIGGER._build_trigger_event(status) + + assert event is not None + assert event.payload["status"] == expected + assert event.payload["run_id"] == RUN_ID + + @pytest.mark.parametrize( + "status", + ["Queued", "InProgress", "Canceling"], + ) + def test_build_trigger_event_non_terminal_states(self, status): + event = self.TRIGGER._build_trigger_event(status) + assert event is None + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_run_success(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.SUCCEEDED + + generator = self.TRIGGER.run() + actual = await generator.asend(None) + + assert actual == TriggerEvent( + { + "status": "success", + "message": f"Pipeline run {RUN_ID} succeeded.", + "run_id": RUN_ID, + } + ) + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_run_failed(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.FAILED + + generator = self.TRIGGER.run() + actual = await generator.asend(None) + + assert actual == TriggerEvent( + { + "status": "error", + "message": f"Pipeline run {RUN_ID} finished with state Failed.", + "run_id": RUN_ID, + } + ) + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_waiting(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.QUEUED + + task = asyncio.create_task(self.TRIGGER.run().__anext__()) + + await asyncio.sleep(0.5) Review Comment: This test asserts only that the task hasn't finished after 0.5s of sleeping inside `asyncio.sleep(self.check_interval)`, which is effectively verifying that `asyncio.sleep` doesn't return early. It also adds wall-clock latency to every CI run for no real coverage. Either drop it, or with `time_machine` assert the trigger calls `get_pipeline_run_status` on the expected cadence. ########## providers/microsoft/azure/tests/unit/microsoft/azure/triggers/test_synapse.py: ########## @@ -0,0 +1,143 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import asyncio +import time +from unittest import mock + +import pytest + +from airflow.providers.microsoft.azure.triggers.synapse import ( + AzureSynapsePipelineRunStatus, + AzureSynapsePipelineTrigger, +) +from airflow.triggers.base import TriggerEvent + +AZURE_SYNAPSE_CONN_ID = "azure_synapse_default" +AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT = "azure_synapse_workspace_dev_endpoint" +RUN_ID = "7f8c6c72-c093-11ec-a83d-0242ac120007" +POKE_INTERVAL = 5 +AZ_PIPELINE_END_TIME = time.time() + 60 * 60 * 24 * 7 +MODULE = "airflow.providers.microsoft.azure" + + +class TestAzureSynapsePipelineTrigger: + TRIGGER = AzureSynapsePipelineTrigger( + run_id=RUN_ID, + azure_synapse_conn_id=AZURE_SYNAPSE_CONN_ID, + azure_synapse_workspace_dev_endpoint=AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT, + check_interval=POKE_INTERVAL, + end_time=AZ_PIPELINE_END_TIME, + ) + + def test_synapse_trigger_serialization(self): + classpath, kwargs = self.TRIGGER.serialize() + + assert classpath == f"{MODULE}.triggers.synapse.AzureSynapsePipelineTrigger" + + assert kwargs == { + "run_id": RUN_ID, + "azure_synapse_conn_id": AZURE_SYNAPSE_CONN_ID, + "azure_synapse_workspace_dev_endpoint": AZURE_SYNAPSE_WORKSPACE_DEV_ENDPOINT, + "check_interval": POKE_INTERVAL, + "end_time": AZ_PIPELINE_END_TIME, + } + + @pytest.mark.parametrize( + ("status", "expected"), + [ + ("Succeeded", "success"), + ("Failed", "error"), + ("Cancelled", "error"), + ], + ) + def test_build_trigger_event_terminal_states(self, status, expected): + event = self.TRIGGER._build_trigger_event(status) + + assert event is not None + assert event.payload["status"] == expected + assert event.payload["run_id"] == RUN_ID + + @pytest.mark.parametrize( + "status", + ["Queued", "InProgress", "Canceling"], + ) + def test_build_trigger_event_non_terminal_states(self, status): + event = self.TRIGGER._build_trigger_event(status) + assert event is None + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_run_success(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.SUCCEEDED + + generator = self.TRIGGER.run() + actual = await generator.asend(None) + + assert actual == TriggerEvent( + { + "status": "success", + "message": f"Pipeline run {RUN_ID} succeeded.", + "run_id": RUN_ID, + } + ) + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_run_failed(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.FAILED + + generator = self.TRIGGER.run() + actual = await generator.asend(None) + + assert actual == TriggerEvent( + { + "status": "error", + "message": f"Pipeline run {RUN_ID} finished with state Failed.", + "run_id": RUN_ID, + } + ) + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_waiting(self, mock_status): + mock_status.return_value = AzureSynapsePipelineRunStatus.QUEUED + + task = asyncio.create_task(self.TRIGGER.run().__anext__()) + + await asyncio.sleep(0.5) + + assert task.done() is False + + @pytest.mark.asyncio + @mock.patch(f"{MODULE}.hooks.synapse.AzureSynapsePipelineAsyncHook.get_pipeline_run_status") + async def test_trigger_exception(self, mock_status): Review Comment: This test only mocks `get_pipeline_run_status`, but the trigger's outer `except Exception` at `triggers/synapse.py:153-158` also calls `cancel_pipeline_run`. The test passes today only because that unmocked cancel call also throws (via `get_async_conn` -> `get_connection`), and the inner try/except swallows it. Mocking `cancel_pipeline_run` as an `AsyncMock` and asserting it was awaited once would make the test deterministic and lock in the cancel-on-failure invariant. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
