henry3260 commented on code in PR #76424:
URL: https://github.com/apache/airflow/pull/76424#discussion_r4598389643


##########
airflow-core/tests/unit/api_fastapi/test_event_loop_blocking.py:
##########
@@ -0,0 +1,233 @@
+# 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.
+from __future__ import annotations
+
+import ast
+import contextlib
+import re
+from pathlib import Path
+
+import pytest
+from fastapi.testclient import TestClient
+
+from airflow.models.dag import clear_team_name_cache
+
+from tests_common.test_utils.api_fastapi import iter_api_routes
+from tests_common.test_utils.config import conf_vars
+from tests_common.test_utils.db import (
+    clear_db_backfills,
+    clear_db_connections,
+    clear_db_logs,
+    clear_db_pools,
+    clear_db_variables,
+)
+from tests_common.test_utils.paths import AIRFLOW_CORE_ROOT_PATH, 
AIRFLOW_ROOT_PATH
+
+KNOWN_SYNC_DB_FILE = AIRFLOW_ROOT_PATH / "generated" / 
"known_sync_db_on_event_loop.txt"
+KNOWN_OFFLOADS_FILE = AIRFLOW_ROOT_PATH / "generated" / 
"known_api_server_threadpool_offloads.txt"
+API_FASTAPI_PATH = AIRFLOW_CORE_ROOT_PATH / "src" / "airflow" / "api_fastapi"
+
+MODES = ("default", "multi_team")
+
+# The auth manager interface is sync, so these calls cannot be awaited 
directly.
+ALLOWED_OFFLOADS = frozenset(
+    {
+        
"airflow-core/src/airflow/api_fastapi/auth/middlewares/refresh_token.py::JWTRefreshMiddleware.refresh_user",
+        
"airflow-core/src/airflow/api_fastapi/auth/middlewares/team_authorization.py::TeamAuthorizationMiddleware.dispatch",
+    }
+)
+OFFLOAD_NAMES = frozenset({"run_in_threadpool", "run_in_executor", 
"to_thread"})
+OFFLOAD_IMPORTS = frozenset(
+    {
+        ("starlette.concurrency", "run_in_threadpool"),
+        ("fastapi.concurrency", "run_in_threadpool"),
+        ("anyio.to_thread", "run_sync"),
+        ("asyncio", "to_thread"),
+    }
+)
+
+
+def _read_known(path: Path) -> list[str]:
+    return [line.strip() for line in path.read_text().splitlines() if 
line.strip()]
+
+
+KNOWN_SYNC_DB = _read_known(KNOWN_SYNC_DB_FILE)
+KNOWN_OFFLOADS = _read_known(KNOWN_OFFLOADS_FILE)
+
+
+def _build_xfail_params(entries: list[str], reason: str) -> list:
+    """Make each known entry a strict xfail, so fixing it fails the test until 
the line is removed."""
+    return [
+        pytest.param(
+            entry, marks=pytest.mark.xfail(strict=True, raises=AssertionError, 
reason=reason), id=entry
+        )
+        for entry in entries
+    ]
+
+
+def _list_routes(app) -> list[tuple[str, str]]:
+    return sorted(
+        (method, route.path)
+        for route in iter_api_routes(app.routes)
+        for method in (route.methods or set()) - {"HEAD", "OPTIONS"}
+    )
+
+
+def _send_request(client: TestClient, queries: list, method: str, path: str, 
mode: str) -> list[str]:
+    """Send one request and return the sorted, unique sites that ran sync SQL 
on the event loop."""
+    # DagModel.get_team_name is cached, which would hide its query from every 
route after the first.
+    clear_team_name_cache()
+    queries.clear()
+    url = re.sub(r"\{[^}]+\}", "1", path)
+    body: dict | None = {} if method in {"POST", "PUT", "PATCH"} else None
+    with conf_vars({("core", "multi_team"): "True" if mode == "multi_team" 
else "False"}):
+        # The client re-raises server errors (anyio may group them). The 
queries recorded before the
+        # error are all we need.
+        with contextlib.suppress(BaseException):
+            client.request(method, url, json=body)

Review Comment:
   `suppress(BaseException)` also swallows the test timeout, 
`KeyboardInterrupt` and `SystemExit`. If a route hangs, the timeout is 
swallowed and the loop moves on, so the test can still pass and we never learn 
which route hung.
   
   Could we let those through, e.g. `except (Exception, BaseExceptionGroup)`?



##########
generated/known_api_server_threadpool_offloads.txt:
##########


Review Comment:
   Should we move these two lists next to the test instead? 



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