pharctor commented on code in PR #77305:
URL: https://github.com/apache/airflow/pull/77305#discussion_r4630993467


##########
airflow-core/tests/unit/jobs/test_scheduler_job.py:
##########
@@ -10528,6 +10528,41 @@ def test_update_dagrun_state_for_paused_dag(self, 
dag_maker, session):
         (scheduled_run,) = DagRun.find(dag_id=dag.dag_id, 
run_type=DagRunType.SCHEDULED, session=session)
         assert scheduled_run.state == State.SUCCESS
 
+    def test_update_dagrun_state_for_paused_dag_survives_failed_flush(self, 
dag_maker, session, caplog):
+        """A failed flush, as when a concurrent clear re-keys a task instance, 
must not escape the sweep."""
+        with dag_maker("testdag") as dag:
+            EmptyOperator(task_id="task1")
+
+        scheduled_run = dag_maker.create_dagrun(run_type=DagRunType.SCHEDULED)
+        scheduled_run.last_scheduling_decision = 
datetime.datetime.now(timezone.utc) - timedelta(minutes=1)
+        scheduled_run.get_task_instances(session=session)[0].set_state(
+            TaskInstanceState.RUNNING, session=session
+        )
+        DagModel.get_dagmodel(dag.dag_id, session=session).is_paused = True
+        session.commit()
+        run_id = scheduled_run.id
+
+        def update_state_after_concurrent_clear(dag_run, *, session, 
execute_callbacks=True):
+            ti = dag_run.get_task_instances(session=session)[0]
+            # What a clear committing between the sweep's read and its flush 
leaves behind:
+            # prepare_db_for_next_try gives the row a new primary key.
+            table = TaskInstance.__table__
+            session.execute(update(table).where(table.c.id == 
ti.id).values(id=uuid4()))
+            ti.state = TaskInstanceState.UPSTREAM_FAILED
+            session.flush()
+            return [], None
+
+        self.job_runner = SchedulerJobRunner(job=Job(), 
executors=[self.null_exec])
+        # No session is passed, so @provide_session commits on return, as it 
does on the scheduler's timer.
+        with (
+            mock.patch.object(DagRun, "update_state", 
update_state_after_concurrent_clear),
+            caplog.at_level(logging.ERROR, 
logger="airflow.jobs.scheduler_job_runner"),
+        ):
+            self.job_runner._update_dag_run_state_for_paused_dags()
+
+        assert "Failed to update dag run state for paused dags" in caplog.text

Review Comment:
   Thanks, switched to the structured assertion in 8058f04061.



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