@@ -33,6 +33,60 @@ class TestSession:
3333 "enable_telemetry" : False ,
3434 }
3535
36+ @pytest .mark .parametrize (
37+ "backend_kwargs, backend_name" ,
38+ [
39+ ({}, "ThriftDatabricksClient" ),
40+ ({"use_kernel" : False }, "ThriftDatabricksClient" ),
41+ ({"use_sea" : True }, "SeaDatabricksClient" ),
42+ ],
43+ )
44+ @pytest .mark .parametrize ("proxy_uri" , [None , "http://proxy.example.com:8081" ])
45+ def test_python_backends_keep_http_client (
46+ self , backend_kwargs , backend_name , proxy_uri
47+ ):
48+ with patch (f"{ self .PACKAGE_NAME } .session.{ backend_name } " ) as backend , patch (
49+ f"{ self .PACKAGE_NAME } .client.UnifiedHttpClient"
50+ ) as http_client , patch (
51+ f"{ self .PACKAGE_NAME } .client.TelemetryClientFactory.get_telemetry_client"
52+ ) as telemetry :
53+ http_client .return_value .using_proxy .return_value = proxy_uri is not None
54+ http_client .return_value .proxy_uri = proxy_uri
55+ conn = databricks .sql .connect (
56+ ** self .DUMMY_CONNECTION_ARGS , ** backend_kwargs
57+ )
58+ try :
59+ http_client .assert_called_once ()
60+ assert conn .http_client is http_client .return_value
61+ assert conn .session .http_client is conn .http_client
62+ assert backend .call_args .kwargs ["http_client" ] is conn .http_client
63+ params = telemetry .return_value .export_initial_telemetry_log .call_args .kwargs [
64+ "driver_connection_params"
65+ ]
66+ assert params .use_proxy is (proxy_uri is not None )
67+ assert params .use_system_proxy is (proxy_uri is not None )
68+ if proxy_uri :
69+ assert params .proxy_host_info .host_url == "proxy.example.com"
70+ assert params .proxy_host_info .port == 8081
71+ else :
72+ assert params .proxy_host_info is None
73+ finally :
74+ conn .close ()
75+ backend .return_value .close_session .assert_called_once ()
76+ http_client .return_value .close .assert_called_once ()
77+
78+ def test_conflicting_kernel_and_sea_flags_preserve_error (self ):
79+ with patch (
80+ f"{ self .PACKAGE_NAME } .client.UnifiedHttpClient" ,
81+ side_effect = AssertionError (
82+ "Explicit kernel must not create a Python client"
83+ ),
84+ ) as http_client , pytest .raises (ValueError , match = "mutually exclusive" ):
85+ databricks .sql .connect (
86+ ** self .DUMMY_CONNECTION_ARGS , use_kernel = True , use_sea = True
87+ )
88+ http_client .assert_not_called ()
89+
3690 @patch ("%s.session.ThriftDatabricksClient" % PACKAGE_NAME )
3791 def test_close_uses_the_correct_session_id (self , mock_client_class ):
3892 instance = mock_client_class .return_value
@@ -879,6 +933,92 @@ def _connect(self, **overrides):
879933 args .update (overrides )
880934 return databricks .sql .connect (** args )
881935
936+ @pytest .mark .parametrize ("route" , ["explicit" , "recovery" , "cached" ])
937+ @patch ("%s.session.ThriftDatabricksClient" % PACKAGE )
938+ def test_kernel_http_client_lifecycle (self , mock_thrift , route ):
939+ from databricks .sql .backend import reyden_warehouse_cache
940+ from databricks .sql .result_set import ResultSet
941+
942+ explicit = route == "explicit"
943+ mock_thrift .return_value .open_session .side_effect = self ._reject ()
944+ if route == "cached" :
945+ reyden_warehouse_cache .mark_reyden (self .HOST , "wh-reyden" )
946+
947+ with self ._fake_kernel () as kernel , patch (
948+ f"{ self .PACKAGE } .client.UnifiedHttpClient"
949+ ) as http_client , patch (
950+ f"{ self .PACKAGE } .client.TelemetryClientFactory.get_telemetry_client"
951+ ) as telemetry :
952+ if explicit :
953+ http_client .side_effect = AssertionError (
954+ "Explicit kernel must not create a Python client"
955+ )
956+ http_client .return_value .using_proxy .return_value = True
957+ http_client .return_value .proxy_uri = "http://proxy.example.com:8081"
958+ result = Mock (spec = ResultSet , is_staging_operation = False )
959+ result .fetchall .return_value = [(1 ,)]
960+ kernel .return_value .execute_command .return_value = result
961+ conn = self ._connect (
962+ ** ({"use_kernel" : True } if explicit else {}),
963+ enable_telemetry = True ,
964+ force_enable_telemetry = True ,
965+ )
966+ try :
967+ assert conn .session .use_kernel is True
968+ assert conn .telemetry_enabled is False
969+ assert conn .http_client is (
970+ None if explicit else http_client .return_value
971+ )
972+ assert conn .session .http_client is conn .http_client
973+ assert kernel .call_args .kwargs ["http_client" ] is conn .http_client
974+ params = telemetry .return_value .export_initial_telemetry_log .call_args .kwargs [
975+ "driver_connection_params"
976+ ]
977+ assert params .use_proxy is (not explicit )
978+ assert params .use_system_proxy is (not explicit )
979+ if explicit :
980+ assert params .proxy_host_info is None
981+ with conn .cursor () as cursor :
982+ cursor .execute ("SELECT 1" )
983+ assert cursor .fetchall () == [(1 ,)]
984+ kernel .return_value .execute_command .assert_called_once ()
985+ finally :
986+ conn .close ()
987+ kernel .return_value .close_session .assert_called_once ()
988+ if explicit :
989+ http_client .assert_not_called ()
990+ else :
991+ http_client .assert_called_once ()
992+ http_client .return_value .close .assert_called_once ()
993+ assert mock_thrift .return_value .open_session .call_count == (
994+ 1 if route == "recovery" else 0
995+ )
996+
997+ @pytest .mark .parametrize ("failure_stage" , ["backend" , "open" ])
998+ def test_explicit_kernel_failure_preserves_error_and_telemetry (self , failure_stage ):
999+ with self ._fake_kernel () as kernel , patch (
1000+ f"{ self .PACKAGE } .client.UnifiedHttpClient" ,
1001+ side_effect = AssertionError (
1002+ "Explicit kernel must not create a Python client"
1003+ ),
1004+ ) as http_client , patch (
1005+ f"{ self .PACKAGE } .client.TelemetryClientFactory.connection_failure_log"
1006+ ) as failure_log :
1007+ if failure_stage == "backend" :
1008+ error = ImportError ("kernel package unavailable" )
1009+ kernel .side_effect = error
1010+ else :
1011+ error = OperationalError ("kernel could not open session" )
1012+ kernel .return_value .open_session .side_effect = error
1013+ with pytest .raises (type (error )) as excinfo :
1014+ self ._connect (use_kernel = True , enable_telemetry = True )
1015+ assert excinfo .value is error
1016+ http_client .assert_not_called ()
1017+ failure_log .assert_called_once ()
1018+ assert failure_log .call_args .kwargs ["enable_telemetry" ] is (
1019+ failure_stage == "backend"
1020+ )
1021+
8821022 @pytest .mark .parametrize ("route" , ["explicit" , "recovery" , "cached" ])
8831023 @pytest .mark .parametrize (
8841024 "geospatial_kwargs, expected" ,
0 commit comments