kaxil commented on code in PR #68218:
URL: https://github.com/apache/airflow/pull/68218#discussion_r3376037146


##########
airflow-core/tests/unit/utils/test_db_cleanup.py:
##########
@@ -878,6 +879,95 @@ def create_tis(base_date, num_tis, 
run_type=DagRunType.SCHEDULED):
         session.commit()
 
 
[email protected]_test
+class TestTaskStoreCleanup:
+    def test_expired_rows_deleted(self):
+        from datetime import timezone as dt_timezone
+
+        cfg = config_dict["task_store"]
+        now = pendulum.now(tz="UTC")
+        past = now.subtract(days=30)
+        future = now.add(days=30)
+
+        with create_session() as session:
+            bundle = DagBundleModel(name="ts_test_bundle")
+            session.add(bundle)
+            session.flush()
+
+            dag = DAG(dag_id="ts_test_dag")
+            dm = DagModel(dag_id="ts_test_dag", bundle_name="ts_test_bundle")
+            session.add(dm)
+            SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(dag), 
bundle_name="ts_test_bundle")
+
+            dag_run = DagRun(
+                "ts_test_dag",
+                run_id="ts_test_run",
+                run_type=DagRunType.SCHEDULED,
+                start_date=past,
+            )
+            session.add(dag_run)
+            session.flush()
+
+            expired = TaskStoreModel(
+                dag_run_id=dag_run.id,
+                task_id="t1",
+                map_index=-1,
+                key="job_id",
+                dag_id="ts_test_dag",
+                run_id="ts_test_run",
+                value="job-expired",
+                updated_at=past.in_timezone(dt_timezone.utc),
+                expires_at=past.subtract(days=1).in_timezone(dt_timezone.utc),
+            )
+            never_expire = TaskStoreModel(
+                dag_run_id=dag_run.id,
+                task_id="t1",
+                map_index=-1,
+                key="result",
+                dag_id="ts_test_dag",
+                run_id="ts_test_run",
+                value="job-never-expire",
+                updated_at=past.in_timezone(dt_timezone.utc),
+                expires_at=None,
+            )
+            not_yet_expired = TaskStoreModel(
+                dag_run_id=dag_run.id,
+                task_id="t1",
+                map_index=-1,
+                key="future_key",
+                dag_id="ts_test_dag",
+                run_id="ts_test_run",
+                value="job-future",
+                updated_at=past.in_timezone(dt_timezone.utc),
+                expires_at=future.in_timezone(dt_timezone.utc),
+            )
+            session.add_all([expired, never_expire, not_yet_expired])
+            session.commit()
+
+        cutoff = now.subtract(hours=1)
+        _cleanup_table(
+            **cfg.__dict__,
+            clean_before_timestamp=cutoff,
+            dry_run=False,
+            verbose=False,
+            confirm=False,
+            skip_archive=True,
+            session=create_session().__enter__(),

Review Comment:
   Could we wrap this in `with create_session() as session:` and pass 
`session=session`?
   
   Calling `.__enter__()` without a matching `.__exit__()` leaves the 
`create_session` generator (`utils/session.py`) suspended at its `yield`, so 
the `commit()` / `finally: session.close()` after the yield only run when it's 
garbage-collected. It works here only because `_cleanup_table` commits 
internally (`db_cleanup.py:443`) and `scoped=True` makes this the thread-local 
session that the later assertion block closes -- but that silently depends on 
the internal commit, and per scoped-session semantics it isn't the independent 
session it looks like.
   
   The same pattern is in `TestConnectionTestRequestCleanup` just below -- 
worth fixing there too while we're 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