Skip to content

Commit

Permalink
Merge pull request #7583 from xoriole/fix/speedup-large-torrent-test
Browse files Browse the repository at this point in the history
Optimize the large torrent creation test
  • Loading branch information
xoriole authored Aug 23, 2023
2 parents ff53155 + ae5e0af commit 7521758
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.components.libtorrent.download_manager.download_manager import DownloadManager
from tribler.core.components.libtorrent.torrentdef import TorrentDef
from tribler.core.components.restapi.rest.rest_endpoint import HTTP_BAD_REQUEST, RESTEndpoint, RESTResponse
from tribler.core.components.restapi.rest.rest_endpoint import HTTP_BAD_REQUEST, RESTEndpoint, RESTResponse, \
MAX_REQUEST_SIZE
from tribler.core.components.restapi.rest.schema import HandledErrorSchema
from tribler.core.components.restapi.rest.utils import return_handled_exception
from tribler.core.utilities.path_util import Path
Expand All @@ -25,8 +26,8 @@ class CreateTorrentEndpoint(RESTEndpoint):
"""
path = '/createtorrent'

def __init__(self, download_manager: DownloadManager):
super().__init__()
def __init__(self, download_manager: DownloadManager, client_max_size: int = MAX_REQUEST_SIZE):
super().__init__(client_max_size=client_max_size)
self.download_manager = download_manager

def setup_routes(self):
Expand Down
4 changes: 2 additions & 2 deletions src/tribler/core/components/restapi/rest/rest_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
class RESTEndpoint:
path = ''

def __init__(self, middlewares=()):
def __init__(self, middlewares=(), client_max_size=MAX_REQUEST_SIZE):
self._logger = logging.getLogger(self.__class__.__name__)
self.app = web.Application(middlewares=middlewares, client_max_size=MAX_REQUEST_SIZE)
self.app = web.Application(middlewares=middlewares, client_max_size=client_max_size)
self.endpoints: Dict[str, RESTEndpoint] = {}
self.async_group = AsyncGroup()
self.setup_routes()
Expand Down
4 changes: 2 additions & 2 deletions src/tribler/core/components/restapi/rest/rest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ async def error_middleware(request, handler):
'handled': True,
'message': f'Could not find {request.path}'
}}, status=HTTP_NOT_FOUND)
except HTTPRequestEntityTooLarge:
except HTTPRequestEntityTooLarge as http_error:
return RESTResponse({'error': {
'handled': True,
'message': f'Request size is larger than {MAX_REQUEST_SIZE} bytes'
'message': http_error.text,
}}, status=HTTP_REQUEST_ENTITY_TOO_LARGE)
except Exception as e:
logger.exception(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@

# pylint: disable=redefined-outer-name

@pytest.fixture
def client_max_size():
return 1024 # 1KB


@pytest.fixture
def endpoint(download_manager):
return CreateTorrentEndpoint(download_manager)
def endpoint(download_manager, client_max_size):
return CreateTorrentEndpoint(download_manager, client_max_size=client_max_size)


async def test_create_torrent(rest_api, tmp_path, download_manager):
Expand Down Expand Up @@ -75,13 +79,13 @@ def fake_create_torrent_file(*_, **__):
assert expected_response == error_response


async def test_create_torrent_of_large_size(rest_api):
async def test_create_torrent_of_large_size(client_max_size, rest_api):
"""
Testing whether the API returns a formatted 413 error if request size is above set client size.
"""

post_data = {
"description": ''.join(random.choice(string.ascii_letters) for _ in range(MAX_REQUEST_SIZE))
"description": ''.join(random.choice(string.ascii_letters) for _ in range(client_max_size))
}

error_response = await do_request(
Expand All @@ -91,13 +95,10 @@ async def test_create_torrent_of_large_size(rest_api):
post_data=post_data
)

expected_response = {
"error": {
"handled": True,
"message": f"Request size is larger than {MAX_REQUEST_SIZE} bytes"
}
}
assert expected_response == error_response
assert error_response["error"]["handled"] is True

expected_msg = f"Maximum request body size {client_max_size} exceeded"
assert expected_msg in error_response["error"]["message"]


async def test_create_torrent_missing_files_parameter(rest_api):
Expand Down
3 changes: 2 additions & 1 deletion src/tribler/core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def event_loop():
@pytest.fixture
async def rest_api(event_loop, aiohttp_client, endpoint: RESTEndpoint):
# In each test file that requires the use of this fixture, the endpoint fixture needs to be specified.
app = Application(middlewares=[error_middleware])
client_max_size: int = endpoint.app._client_max_size # pylint:disable=protected-access
app = Application(middlewares=[error_middleware], client_max_size=client_max_size)
app.add_subapp(endpoint.path, endpoint.app)

yield await aiohttp_client(app)
Expand Down

0 comments on commit 7521758

Please sign in to comment.