jason810496 commented on code in PR #65367:
URL: https://github.com/apache/airflow/pull/65367#discussion_r3472277032


##########
airflow-core/tests/unit/models/test_trigger.py:
##########
@@ -234,6 +235,38 @@ def test_submit_event(mock_callback_handle_event, session, 
create_task_instance)
     mock_callback_handle_event.assert_called_once_with(event, session)
 
 
+@patch("airflow.models.trigger.AssetManager.register_asset_change")
+def test_submit_event_no_n_plus_one_for_assets(_, session):
+    """Ensure asset notifications do not trigger per-asset lazy-load 
queries."""
+
+    def _create_trigger_with_assets(asset_count: int) -> int:

Review Comment:
   Then we could collapse the these utility function as the test function 
itself.



##########
airflow-core/tests/unit/models/test_trigger.py:
##########
@@ -234,6 +235,38 @@ def test_submit_event(mock_callback_handle_event, session, 
create_task_instance)
     mock_callback_handle_event.assert_called_once_with(event, session)
 
 
+@patch("airflow.models.trigger.AssetManager.register_asset_change")
+def test_submit_event_no_n_plus_one_for_assets(_, session):
+    """Ensure asset notifications do not trigger per-asset lazy-load 
queries."""
+
+    def _create_trigger_with_assets(asset_count: int) -> int:
+        trigger = Trigger(classpath="airflow.triggers.testing.SuccessTrigger", 
kwargs={})
+        session.add(trigger)
+        session.flush()
+
+        for i in range(asset_count):
+            asset = AssetModel(name=f"asset_{asset_count}_{i}")
+            asset.add_trigger(trigger, f"watcher_{i}")
+            session.add(asset)
+
+        session.commit()
+        session.expire_all()
+        return trigger.id
+
+    def _submit_event_query_count(trigger_id: int) -> int:
+        with count_queries(session=session) as query_result:
+            Trigger.submit_event(trigger_id, TriggerEvent("payload"), 
session=session)
+        return sum(query_result.values())
+
+    small_query_count = 
_submit_event_query_count(_create_trigger_with_assets(1))
+    larger_query_count = 
_submit_event_query_count(_create_trigger_with_assets(5))
+
+    assert larger_query_count - small_query_count < 3, (
+        f"Added 4 assets but query count increased by {larger_query_count - 
small_query_count} "
+        f"({small_query_count} -> {larger_query_count}), suggesting n+1 
queries for trigger assets"
+    )

Review Comment:
   IIUC, we should parameterize the `asset_count` at test function level and 
get the exact same select count.



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