1919
2020
2121@pytest .fixture
22- @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
23- def mock_telemetry_client (mock_singleton_class ):
22+ def mock_telemetry_client ():
2423 """Create a mock telemetry client for testing."""
2524 session_id = str (uuid .uuid4 ())
2625 auth_provider = AccessTokenAuthProvider ("test-token" )
2726 executor = 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 ()
27+ mock_http_client = MagicMock ()
3328
3429 return TelemetryClient (
3530 telemetry_enabled = True ,
@@ -38,7 +33,7 @@ def mock_telemetry_client(mock_singleton_class):
3833 host_url = "test-host.com" ,
3934 executor = executor ,
4035 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
41- client_context = mock_client_context ,
36+ http_client = mock_http_client ,
4237 )
4338
4439
@@ -217,16 +212,11 @@ def telemetry_system_reset(self):
217212 TelemetryClientFactory ._executor = None
218213 TelemetryClientFactory ._initialized = False
219214
220- @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
221- def test_client_lifecycle_flow (self , mock_singleton_class ):
215+ def test_client_lifecycle_flow (self ):
222216 """Test complete client lifecycle: initialize -> use -> close."""
223217 session_id_hex = "test-session"
224218 auth_provider = AccessTokenAuthProvider ("token" )
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 ()
219+ mock_http_client = MagicMock ()
230220
231221 # Initialize enabled client
232222 TelemetryClientFactory .initialize_telemetry_client (
@@ -235,7 +225,7 @@ def test_client_lifecycle_flow(self, mock_singleton_class):
235225 auth_provider = auth_provider ,
236226 host_url = "test-host.com" ,
237227 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
238- client_context = mock_client_context ,
228+ http_client = mock_http_client ,
239229 )
240230
241231 client = TelemetryClientFactory .get_telemetry_client (session_id_hex )
@@ -248,40 +238,28 @@ def test_client_lifecycle_flow(self, mock_singleton_class):
248238 mock_close .assert_called_once ()
249239
250240 # Should get NoopTelemetryClient after close
251- client = TelemetryClientFactory .get_telemetry_client (session_id_hex )
252- assert isinstance (client , NoopTelemetryClient )
253241
254- @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
255- def test_disabled_telemetry_flow (self , mock_singleton_class ):
256- """Test that disabled telemetry uses NoopTelemetryClient."""
242+ def test_disabled_telemetry_creates_noop_client (self ):
243+ """Test that disabled telemetry creates NoopTelemetryClient."""
257244 session_id_hex = "test-session"
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 ()
245+ mock_http_client = MagicMock ()
263246
264247 TelemetryClientFactory .initialize_telemetry_client (
265248 telemetry_enabled = False ,
266249 session_id_hex = session_id_hex ,
267250 auth_provider = None ,
268251 host_url = "test-host.com" ,
269252 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
270- client_context = mock_client_context ,
253+ http_client = mock_http_client ,
271254 )
272255
273256 client = TelemetryClientFactory .get_telemetry_client (session_id_hex )
274257 assert isinstance (client , NoopTelemetryClient )
275258
276- @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
277- def test_factory_error_handling (self , mock_singleton_class ):
259+ def test_factory_error_handling (self ):
278260 """Test that factory errors fall back to NoopTelemetryClient."""
279261 session_id = "test-session"
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 ()
262+ mock_http_client = MagicMock ()
285263
286264 # Simulate initialization error
287265 with patch (
@@ -294,23 +272,18 @@ def test_factory_error_handling(self, mock_singleton_class):
294272 auth_provider = AccessTokenAuthProvider ("token" ),
295273 host_url = "test-host.com" ,
296274 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
297- client_context = mock_client_context ,
275+ http_client = mock_http_client ,
298276 )
299277
300278 # Should fall back to NoopTelemetryClient
301279 client = TelemetryClientFactory .get_telemetry_client (session_id )
302280 assert isinstance (client , NoopTelemetryClient )
303281
304- @patch ("databricks.sql.telemetry.telemetry_client.TelemetryHttpClientSingleton" )
305- def test_factory_shutdown_flow (self , mock_singleton_class ):
282+ def test_factory_shutdown_flow (self ):
306283 """Test factory shutdown when last client is removed."""
307284 session1 = "session-1"
308285 session2 = "session-2"
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 ()
286+ mock_http_client = MagicMock ()
314287
315288 # Initialize multiple clients
316289 for session in [session1 , session2 ]:
@@ -320,7 +293,7 @@ def test_factory_shutdown_flow(self, mock_singleton_class):
320293 auth_provider = AccessTokenAuthProvider ("token" ),
321294 host_url = "test-host.com" ,
322295 batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
323- client_context = mock_client_context ,
296+ http_client = mock_http_client ,
324297 )
325298
326299 # Factory should be initialized
0 commit comments