1919
2020
2121@pytest .fixture
22- def mock_telemetry_client ():
22+ @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
23+ def mock_telemetry_client (mock_singleton_class ):
2324 """Create a mock telemetry client for testing."""
2425 session_id = str (uuid .uuid4 ())
2526 auth_provider = AccessTokenAuthProvider ("test-token" )
2627 executor = MagicMock ()
27- mock_http_client = MagicMock ()
28+ mock_client_context = MagicMock ()
29+
30+ # Mock the singleton to return a mock HTTP client
31+ mock_singleton = mock_singleton_class .return_value
32+ mock_singleton .get_http_client .return_value = MagicMock ()
2833
2934 return TelemetryClient (
3035 telemetry_enabled = True ,
@@ -33,7 +38,7 @@ def mock_telemetry_client():
3338 host_url = "test-host.com" ,
3439 executor = executor ,
3540 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
36- http_client = mock_http_client ,
41+ client_context = mock_client_context ,
3742 )
3843
3944
@@ -212,11 +217,16 @@ def telemetry_system_reset(self):
212217 TelemetryClientFactory ._executor = None
213218 TelemetryClientFactory ._initialized = False
214219
215- def test_client_lifecycle_flow (self ):
220+ @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
221+ def test_client_lifecycle_flow (self , mock_singleton_class ):
216222 """Test complete client lifecycle: initialize -> use -> close."""
217223 session_id_hex = "test-session"
218224 auth_provider = AccessTokenAuthProvider ("token" )
219- mock_http_client = MagicMock ()
225+ mock_client_context = MagicMock ()
226+
227+ # Mock the singleton to return a mock HTTP client
228+ mock_singleton = mock_singleton_class .return_value
229+ mock_singleton .get_http_client .return_value = MagicMock ()
220230
221231 # Initialize enabled client
222232 TelemetryClientFactory .initialize_telemetry_client (
@@ -225,7 +235,7 @@ def test_client_lifecycle_flow(self):
225235 auth_provider = auth_provider ,
226236 host_url = "test-host.com" ,
227237 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
228- http_client = mock_http_client ,
238+ client_context = mock_client_context ,
229239 )
230240
231241 client = TelemetryClientFactory .get_telemetry_client (session_id_hex )
@@ -241,27 +251,37 @@ def test_client_lifecycle_flow(self):
241251 client = TelemetryClientFactory .get_telemetry_client (session_id_hex )
242252 assert isinstance (client , NoopTelemetryClient )
243253
244- def test_disabled_telemetry_flow (self ):
254+ @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
255+ def test_disabled_telemetry_flow (self , mock_singleton_class ):
245256 """Test that disabled telemetry uses NoopTelemetryClient."""
246257 session_id_hex = "test-session"
247- mock_http_client = MagicMock ()
258+ mock_client_context = MagicMock ()
259+
260+ # Mock the singleton to return a mock HTTP client
261+ mock_singleton = mock_singleton_class .return_value
262+ mock_singleton .get_http_client .return_value = MagicMock ()
248263
249264 TelemetryClientFactory .initialize_telemetry_client (
250265 telemetry_enabled = False ,
251266 session_id_hex = session_id_hex ,
252267 auth_provider = None ,
253268 host_url = "test-host.com" ,
254269 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
255- http_client = mock_http_client ,
270+ client_context = mock_client_context ,
256271 )
257272
258273 client = TelemetryClientFactory .get_telemetry_client (session_id_hex )
259274 assert isinstance (client , NoopTelemetryClient )
260275
261- def test_factory_error_handling (self ):
276+ @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
277+ def test_factory_error_handling (self , mock_singleton_class ):
262278 """Test that factory errors fall back to NoopTelemetryClient."""
263279 session_id = "test-session"
264- mock_http_client = MagicMock ()
280+ mock_client_context = MagicMock ()
281+
282+ # Mock the singleton to return a mock HTTP client
283+ mock_singleton = mock_singleton_class .return_value
284+ mock_singleton .get_http_client .return_value = MagicMock ()
265285
266286 # Simulate initialization error
267287 with patch (
@@ -274,18 +294,23 @@ def test_factory_error_handling(self):
274294 auth_provider = AccessTokenAuthProvider ("token" ),
275295 host_url = "test-host.com" ,
276296 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
277- http_client = mock_http_client ,
297+ client_context = mock_client_context ,
278298 )
279299
280300 # Should fall back to NoopTelemetryClient
281301 client = TelemetryClientFactory .get_telemetry_client (session_id )
282302 assert isinstance (client , NoopTelemetryClient )
283303
284- def test_factory_shutdown_flow (self ):
304+ @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
305+ def test_factory_shutdown_flow (self , mock_singleton_class ):
285306 """Test factory shutdown when last client is removed."""
286307 session1 = "session-1"
287308 session2 = "session-2"
288- mock_http_client = MagicMock ()
309+ mock_client_context = MagicMock ()
310+
311+ # Mock the singleton to return a mock HTTP client
312+ mock_singleton = mock_singleton_class .return_value
313+ mock_singleton .get_http_client .return_value = MagicMock ()
289314
290315 # Initialize multiple clients
291316 for session in [session1 , session2 ]:
@@ -295,7 +320,7 @@ def test_factory_shutdown_flow(self):
295320 auth_provider = AccessTokenAuthProvider ("token" ),
296321 host_url = "test-host.com" ,
297322 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
298- http_client = mock_http_client ,
323+ client_context = mock_client_context ,
299324 )
300325
301326 # Factory should be initialized
0 commit comments