From 2ab2b846b111c5d4528756940980dc830be9fb9e Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 14 Mar 2019 13:07:11 -0400 Subject: [PATCH] fix default download timeout not being set --- lbrynet/stream/stream_manager.py | 8 +++--- tests/unit/stream/test_stream_manager.py | 33 +++++++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lbrynet/stream/stream_manager.py b/lbrynet/stream/stream_manager.py index 181a69228..5c2107bf7 100644 --- a/lbrynet/stream/stream_manager.py +++ b/lbrynet/stream/stream_manager.py @@ -390,9 +390,8 @@ class StreamManager: self.streams.add(stream) return stream - async def _download_stream_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManager', - file_name: typing.Optional[str] = None, - timeout: typing.Optional[float] = None) -> ManagedStream: + async def _download_stream_from_uri(self, uri, timeout: float, exchange_rate_manager: 'ExchangeRateManager', + file_name: typing.Optional[str] = None) -> ManagedStream: start_time = self.loop.time() parsed_uri = parse_lbry_uri(uri) if parsed_uri.is_channel: @@ -496,12 +495,13 @@ class StreamManager: async def download_stream_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManager', file_name: typing.Optional[str] = None, timeout: typing.Optional[float] = None) -> ManagedStream: + timeout = timeout or self.config.download_timeout if uri in self.starting_streams: return await self.starting_streams[uri] fut = asyncio.Future(loop=self.loop) self.starting_streams[uri] = fut try: - stream = await self._download_stream_from_uri(uri, exchange_rate_manager, file_name, timeout) + stream = await self._download_stream_from_uri(uri, timeout, exchange_rate_manager, file_name) fut.set_result(stream) return stream except Exception as err: diff --git a/tests/unit/stream/test_stream_manager.py b/tests/unit/stream/test_stream_manager.py index cf0214248..8b42e5a41 100644 --- a/tests/unit/stream/test_stream_manager.py +++ b/tests/unit/stream/test_stream_manager.py @@ -17,12 +17,12 @@ from lbrynet.dht.node import Node from lbrynet.schema.claim import ClaimDict -def get_mock_node(peer): +def get_mock_node(peer=None): def mock_accumulate_peers(q1: asyncio.Queue, q2: asyncio.Queue): async def _task(): pass - - q2.put_nowait([peer]) + if peer: + q2.put_nowait([peer]) return q2, asyncio.create_task(_task()) mock_node = mock.Mock(spec=Node) @@ -132,6 +132,33 @@ class TestStreamManager(BlobExchangeTestBase): await asyncio.sleep(0, loop=self.loop) self.assertTrue(checked_post) + async def test_no_peers_timeout(self): + self.server_from_client = None + self.client_config.fixed_peer_delay = 9001.0 + self.client_config.download_timeout = 3.0 + await self.setup_stream_manager() + checked_post = False + + async def check_post(event): + self.assertEqual(event['event'], 'Time To First Bytes') + self.assertEqual(event['properties']['error'], 'DownloadSDTimeout') + self.assertEqual(event['properties']['tried_peers_count'], None) + self.assertEqual(event['properties']['active_peer_count'], None) + self.assertEqual(event['properties']['use_fixed_peers'], False) + self.assertEqual(event['properties']['added_fixed_peers'], False) + self.assertEqual(event['properties']['fixed_peer_delay'], 9001) + nonlocal checked_post + checked_post = True + + self.stream_manager.analytics_manager._post = check_post + start = self.loop.time() + with self.assertRaises(DownloadSDTimeout): + await self.stream_manager.download_stream_from_uri(self.uri, self.exchange_rate_manager) + duration = self.loop.time() - start + await asyncio.sleep(0, loop=self.loop) + self.assertTrue(4.0 >= duration >= 3.0) + self.assertTrue(checked_post) + async def test_download_stop_resume_delete(self): await self.setup_stream_manager() received = []