This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new fe4481cc8e5 Fix Databricks async connection test on Airflow before 3.1 
(#74427)
fe4481cc8e5 is described below

commit fe4481cc8e52db0e9d19c84d52956975b1d55104
Author: Dheeraj Turaga <[email protected]>
AuthorDate: Wed Oct 7 18:20:20 2026 -0500

    Fix Databricks async connection test on Airflow before 3.1 (#74427)
    
    The test assumed the hook always awaits aget_connection, which BaseHook
    only has from Airflow 3.1. On older versions get_async_connection runs
    get_connection in a worker thread, so the Compat 2.11.1 and 3.0.6
    provider jobs fail on main and every open PR. Failing only when
    get_connection runs on the event loop, which is what broke on Airflow
    3.0, covers both paths.
    
    Co-authored-by: Jarek Potiuk <[email protected]>
---
 .../tests/unit/databricks/hooks/test_databricks.py | 24 +++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git 
a/providers/databricks/tests/unit/databricks/hooks/test_databricks.py 
b/providers/databricks/tests/unit/databricks/hooks/test_databricks.py
index 06c588b2df3..747a30ef885 100644
--- a/providers/databricks/tests/unit/databricks/hooks/test_databricks.py
+++ b/providers/databricks/tests/unit/databricks/hooks/test_databricks.py
@@ -17,6 +17,7 @@
 # under the License.
 from __future__ import annotations
 
+import asyncio
 import itertools
 import json
 import ssl
@@ -1526,6 +1527,18 @@ class 
TestDatabricksHookTokenWhenNoHostIsProvidedInExtra(TestDatabricksHookToken
         self.hook = DatabricksHook()
 
 
+_get_connection = DatabricksHook.get_connection
+
+
+def _get_connection_off_the_event_loop(conn_id):
+    """Fail like Airflow 3.0 does when the sync lookup runs on an event loop 
thread."""
+    try:
+        asyncio.get_running_loop()
+    except RuntimeError:
+        return _get_connection(conn_id)
+    raise RuntimeError("You cannot use AsyncToSync in the same thread as an 
async event loop")
+
+
 @pytest.mark.db_test
 class TestDatabricksHookConnSettings(TestDatabricksHookToken):
     """
@@ -1570,12 +1583,10 @@ class 
TestDatabricksHookConnSettings(TestDatabricksHookToken):
         assert mock_get.call_args.args == 
(f"http://{HOST}:7908/api/2.1/foo/bar";,)
 
     @pytest.mark.asyncio
-    @mock.patch.object(
-        DatabricksHook,
-        "get_connection",
-        autospec=True,
-        side_effect=RuntimeError("You cannot use AsyncToSync in the same 
thread as an async event loop"),
-    )
+    # Not autospec: an autospecced inherited classmethod gets the hook as 
``conn_id`` when called on
+    # an instance. Before Airflow 3.1 there is no ``aget_connection``, and 
``get_async_connection``
+    # runs ``get_connection`` in a worker thread instead.
+    @mock.patch.object(DatabricksHook, "get_connection", 
side_effect=_get_connection_off_the_event_loop)
     
@mock.patch("airflow.providers.databricks.hooks.databricks_base.aiohttp.ClientSession.get")
     async def test_async_do_api_call_fetches_connection_asynchronously(self, 
mock_get, mock_get_connection):
         mock_get.return_value.__aenter__.return_value.json = 
AsyncMock(return_value={"bar": "baz"})
@@ -1584,7 +1595,6 @@ class 
TestDatabricksHookConnSettings(TestDatabricksHookToken):
 
         assert run_page_url == {"bar": "baz"}
         assert mock_get.call_args.args == 
(f"http://{HOST}:7908/api/2.1/foo/bar";,)
-        mock_get_connection.assert_not_called()
 
     @pytest.mark.asyncio
     
@mock.patch("airflow.providers.databricks.hooks.databricks_base.aiohttp.ClientSession.get")

Reply via email to