Skip to content

Commit f5049ad

Browse files
cathleenyIsaac
andcommitted
test: isolate downloader clock from logging
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 22ad760 commit f5049ad

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎tests/unit/test_downloader.py‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import unittest
23
from unittest.mock import patch, MagicMock, Mock
34
import requests
@@ -45,8 +46,9 @@ def time_side_effect():
4546

4647
mock_time.side_effect = time_side_effect
4748

48-
@patch("time.time", return_value=1000)
49+
@patch("databricks.sql.cloudfetch.downloader.time")
4950
def test_run_link_expired(self, mock_time):
51+
mock_time.time.return_value = 1000
5052
mock_http_client = MagicMock()
5153
settings = Mock()
5254
result_link = Mock()
@@ -66,10 +68,11 @@ def test_run_link_expired(self, mock_time):
6668
d.run()
6769
self.assertTrue("link has expired" in context.exception.message)
6870

69-
mock_time.assert_called_once()
71+
mock_time.time.assert_called_once()
7072

71-
@patch("time.time", return_value=1000)
73+
@patch("databricks.sql.cloudfetch.downloader.time")
7274
def test_run_link_past_expiry_buffer(self, mock_time):
75+
mock_time.time.return_value = 1000
7376
mock_http_client = MagicMock()
7477
settings = Mock(link_expiry_buffer_secs=5)
7578
result_link = Mock()
@@ -85,11 +88,15 @@ def test_run_link_past_expiry_buffer(self, mock_time):
8588
http_client=mock_http_client,
8689
)
8790

91+
# Unrelated logging can occur while this test runs (e.g. when cyclic
92+
# GC closes a session). Its timestamp must not count as a download clock call.
93+
with self.assertLogs("databricks.sql.session", level="INFO"):
94+
logging.getLogger("databricks.sql.session").info("Closing session")
8895
with self.assertRaises(Error) as context:
8996
d.run()
9097
self.assertTrue("link has expired" in context.exception.message)
9198

92-
mock_time.assert_called_once()
99+
mock_time.time.assert_called_once()
93100

94101
@patch("time.time", return_value=1000)
95102
def test_run_get_response_not_ok(self, mock_time):
@@ -131,7 +138,7 @@ def test_run_uncompressed_successful(self, mock_time):
131138
self._setup_mock_http_response(mock_http_client, status=200, data=file_bytes)
132139

133140
# Patch the log metrics method to avoid division by zero
134-
with patch.object(downloader.ResultSetDownloadHandler, '_log_download_metrics'):
141+
with patch.object(downloader.ResultSetDownloadHandler, "_log_download_metrics"):
135142
d = downloader.ResultSetDownloadHandler(
136143
settings,
137144
result_link,
@@ -160,11 +167,16 @@ def test_run_compressed_successful(self, mock_time):
160167
result_link.fileLink = "https://s3.amazonaws.com/bucket/file.arrow?token=xyz789"
161168

162169
# Setup mock HTTP response using helper method
163-
self._setup_mock_http_response(mock_http_client, status=200, data=compressed_bytes)
170+
self._setup_mock_http_response(
171+
mock_http_client, status=200, data=compressed_bytes
172+
)
164173

165174
# Mock the decompression method and log metrics to avoid issues
166-
with patch.object(downloader.ResultSetDownloadHandler, '_decompress_data', return_value=file_bytes), \
167-
patch.object(downloader.ResultSetDownloadHandler, '_log_download_metrics'):
175+
with patch.object(
176+
downloader.ResultSetDownloadHandler,
177+
"_decompress_data",
178+
return_value=file_bytes,
179+
), patch.object(downloader.ResultSetDownloadHandler, "_log_download_metrics"):
168180
d = downloader.ResultSetDownloadHandler(
169181
settings,
170182
result_link,

0 commit comments

Comments
 (0)