Lee-W commented on code in PR #39230:
URL: https://github.com/apache/airflow/pull/39230#discussion_r1579150485


##########
airflow/providers/google/cloud/triggers/dataproc.py:
##########
@@ -91,20 +93,28 @@ def serialize(self):
                 "gcp_conn_id": self.gcp_conn_id,
                 "impersonation_chain": self.impersonation_chain,
                 "polling_interval_seconds": self.polling_interval_seconds,
+                "cancel_on_kill": self.cancel_on_kill,
             },
         )
 
     async def run(self):
-        while True:
-            job = await self.get_async_hook().get_job(
-                project_id=self.project_id, region=self.region, 
job_id=self.job_id
-            )
-            state = job.status.state
-            self.log.info("Dataproc job: %s is in state: %s", self.job_id, 
state)
-            if state in (JobStatus.State.DONE, JobStatus.State.CANCELLED, 
JobStatus.State.ERROR):
-                break
-            await asyncio.sleep(self.polling_interval_seconds)
-        yield TriggerEvent({"job_id": self.job_id, "job_state": state, "job": 
job})
+        try:
+            while True:
+                job = await self.get_async_hook().get_job(
+                    project_id=self.project_id, region=self.region, 
job_id=self.job_id
+                )
+                state = job.status.state
+                self.log.info("Dataproc job: %s is in state: %s", self.job_id, 
state)
+                if state in (JobStatus.State.DONE, JobStatus.State.CANCELLED, 
JobStatus.State.ERROR):
+                    break
+                await asyncio.sleep(self.polling_interval_seconds)
+            yield TriggerEvent({"job_id": self.job_id, "job_state": state, 
"job": job})
+        except asyncio.CancelledError:

Review Comment:
   Do we need to yield `TriggerEvent` or rereaise this exception?



##########
tests/providers/google/cloud/triggers/test_dataproc.py:
##########
@@ -375,3 +388,90 @@ async def test_async_operation_triggers_on_error(self, 
mock_hook, operation_trig
         )
         actual_event = await operation_trigger.run().asend(None)
         assert expected_event == actual_event
+
+
[email protected]_test
+class TestDataprocSubmitTrigger:
+    def test_submit_trigger_serialization(self, submit_trigger):
+        """Test that the trigger serializes its configuration correctly."""
+        classpath, kwargs = submit_trigger.serialize()
+        assert classpath == 
"airflow.providers.google.cloud.triggers.dataproc.DataprocSubmitTrigger"
+        assert kwargs == {
+            "job_id": TEST_JOB_ID,
+            "project_id": TEST_PROJECT_ID,
+            "region": TEST_REGION,
+            "gcp_conn_id": TEST_GCP_CONN_ID,
+            "polling_interval_seconds": TEST_POLL_INTERVAL,
+            "cancel_on_kill": True,
+            "impersonation_chain": None,
+        }
+
+    @pytest.mark.asyncio
+    
@mock.patch("airflow.providers.google.cloud.triggers.dataproc.DataprocSubmitTrigger.get_async_hook")
+    async def test_submit_trigger_run_success(self, mock_get_async_hook, 
submit_trigger):
+        """Test the trigger correctly handles a job completion."""
+        mock_hook = mock_get_async_hook.return_value
+        mock_hook.get_job = mock.AsyncMock(
+            
return_value=mock.AsyncMock(status=mock.AsyncMock(state=JobStatus.State.DONE))
+        )
+
+        async_gen = submit_trigger.run()
+        event = await async_gen.asend(None)
+        expected_event = TriggerEvent(
+            {"job_id": TEST_JOB_ID, "job_state": JobStatus.State.DONE, "job": 
mock_hook.get_job.return_value}
+        )
+        assert event.payload == expected_event.payload
+
+    @pytest.mark.asyncio
+    
@mock.patch("airflow.providers.google.cloud.triggers.dataproc.DataprocSubmitTrigger.get_async_hook")
+    async def test_submit_trigger_run_error(self, mock_get_async_hook, 
submit_trigger):
+        """Test the trigger correctly handles a job error."""
+        mock_hook = mock_get_async_hook.return_value
+        mock_hook.get_job = mock.AsyncMock(
+            
return_value=mock.AsyncMock(status=mock.AsyncMock(state=JobStatus.State.ERROR))
+        )
+
+        async_gen = submit_trigger.run()
+        event = await async_gen.asend(None)
+        expected_event = TriggerEvent(
+            {"job_id": TEST_JOB_ID, "job_state": JobStatus.State.ERROR, "job": 
mock_hook.get_job.return_value}
+        )
+        assert event.payload == expected_event.payload
+
+    import asyncio
+    from unittest.mock import AsyncMock, patch
+
+    import pytest

Review Comment:
   Why do we want to import them here?



-- 
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]

Reply via email to