Skip to content

Commit 7b61ad8

Browse files
committed
ngclient: use mock instead of slow_retrieval_server
Instead of starting a dedicated slow_retrieval_server to test for read timeout in RequestsFetcher, use unittest.mock to mock the response.raw.read call. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 119693f commit 7b61ad8

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

tests/test_fetcher_ng.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from tests import utils
2020
from tuf import exceptions, unittest_toolbox
2121
from tuf.ngclient._internal.requests_fetcher import RequestsFetcher
22-
from unittest.mock import patch
22+
from unittest.mock import Mock, patch
2323

2424
logger = logging.getLogger(__name__)
2525

@@ -110,19 +110,17 @@ def test_http_error(self):
110110
self.fetcher.fetch(self.url)
111111
self.assertEqual(cm.exception.status_code, 404)
112112

113-
# Read timeout error
114-
def test_read_timeout(self):
115-
# Reduce the read socket timeout to speed up the test
116-
# while keeping the connect timeout
117-
default_socket_timeout = self.fetcher.socket_timeout
118-
self.fetcher.socket_timeout = (default_socket_timeout, 0.1)
119-
# Launch a new "slow retrieval" server sending one byte each 40s
120-
slow_server_process_handler = utils.TestServerProcess(log=logger, server='slow_retrieval_server.py')
121-
self.url = f"http://{utils.TEST_HOST_ADDRESS}:{str(slow_server_process_handler.port)}/{self.rel_target_filepath}"
113+
# Response read timeout error
114+
@patch.object(requests.Session, 'get')
115+
def test_response_read_timeout(self, mock_session_get):
116+
mock_response = Mock()
117+
attr = {'raw.read.side_effect': urllib3.exceptions.ReadTimeoutError(None, None, "Read timed out.")}
118+
mock_response.configure_mock(**attr)
119+
mock_session_get.return_value = mock_response
120+
122121
with self.assertRaises(exceptions.SlowRetrievalError):
123122
next(self.fetcher.fetch(self.url))
124-
125-
slow_server_process_handler.clean()
123+
mock_response.raw.read.assert_called_once()
126124

127125
# Read/connect session timeout error
128126
@patch.object(requests.Session, 'get', side_effect=urllib3.exceptions.TimeoutError)

0 commit comments

Comments
 (0)