ferruzzi commented on code in PR #70421: URL: https://github.com/apache/airflow/pull/70421#discussion_r3778890432
########## airflow-core/tests/unit/models/test_prune_deadlines.py: ########## @@ -0,0 +1,137 @@ +# 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. +""" +Adversarial QA coverage for ``Deadline.prune_deadlines``. + +``prune_deadlines`` is the batch-delete path the scheduler invokes (via +``DagRun.update_state`` -> ``dagrun.py:1237``) when a DagRun completes on time: +deadlines that no longer need to fire are removed. These tests drill into the +on-time/overdue/pending selection logic, the (lack of) batching, callback +cascade behaviour, and concurrent-mutation edge cases against a real DB. +""" + +from __future__ import annotations + +from datetime import timedelta +from typing import TYPE_CHECKING + +import pytest +import time_machine +from sqlalchemy import select + +from airflow.models import DagRun +from airflow.models.callback import Callback +from airflow.models.deadline import Deadline +from airflow.providers.standard.operators.empty import EmptyOperator +from airflow.sdk.definitions.callback import AsyncCallback +from airflow.utils.state import DagRunState + +from tests_common.test_utils import db +from unit.models import DEFAULT_DATE + +if TYPE_CHECKING: + from sqlalchemy.orm import Session + +DAG_ID = "qaw18_prune_dag" + + +async def _qaw18_callback(): + pass + + +CALLBACK_PATH = f"{__name__}.{_qaw18_callback.__name__}" + + +def _clean_db(): + db.clear_db_dags() + db.clear_db_runs() + db.clear_db_deadline() + + [email protected] +def dagrun(session, dag_maker): + with dag_maker(DAG_ID): + EmptyOperator(task_id="task_id") + with time_machine.travel(DEFAULT_DATE): + dag_maker.create_dagrun(state=DagRunState.QUEUED, logical_date=DEFAULT_DATE) + session.commit() + return session.scalars(select(DagRun)).one() + + +def _make_deadline(session: Session, *, dagrun_id: int, deadline_time, state=None) -> Deadline: + deadline = Deadline( + deadline_time=deadline_time, + callback=AsyncCallback(CALLBACK_PATH), + dagrun_id=dagrun_id, + dag_id=DAG_ID, + deadline_alert_id=None, + ) + session.add(deadline) + session.flush() + if state is not None: + deadline.callback.state = state + session.add(deadline.callback) + session.flush() + return deadline + + [email protected]_test +class TestPruneDeadlines: + @staticmethod + def setup_method(): + _clean_db() + Review Comment: In fact, I just set Claude to search the repo and it says we missed a LOT. It counts around 85 uses over 50 files that my initial scope missed... -- 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]
