From 17b0542fdf418a2836fae41f24b19bd2ac2da06e Mon Sep 17 00:00:00 2001 From: Mieszko Banczerowski Date: Wed, 16 Sep 2020 13:33:17 +0200 Subject: [PATCH] GPI-1232 Synchronous importer for local sizes --- src/galaxy/api/importer.py | 13 +++++++++++++ src/galaxy/api/plugin.py | 4 ++-- tests/test_local_size.py | 19 +++++++++++-------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/galaxy/api/importer.py b/src/galaxy/api/importer.py index c78317b..f958f32 100644 --- a/src/galaxy/api/importer.py +++ b/src/galaxy/api/importer.py @@ -87,3 +87,16 @@ async def _import_element(self, id_, context_): self._notification_failure(id_, UnknownError()) finally: self._notification_partially_finished(id_) + + +class SynchroneousImporter(Importer): + async def _import_elements(self, ids_, context_): + try: + for id_ in ids_: + await self._import_element(id_, context_) + self._notification_finished() + self._complete() + except asyncio.CancelledError: + logger.debug("Importing %s cancelled", self._name) + finally: + self._import_in_progress = False diff --git a/src/galaxy/api/plugin.py b/src/galaxy/api/plugin.py index 04dd176..9a746d2 100644 --- a/src/galaxy/api/plugin.py +++ b/src/galaxy/api/plugin.py @@ -13,7 +13,7 @@ Subscription, SubscriptionGame ) from galaxy.task_manager import TaskManager -from galaxy.api.importer import Importer, CollectionImporter +from galaxy.api.importer import Importer, CollectionImporter, SynchroneousImporter logger = logging.getLogger(__name__) @@ -104,7 +104,7 @@ def __init__(self, platform, version, reader, writer, handshake_token): self._user_presence_import_finished, self.user_presence_import_complete ) - self._local_size_importer = Importer( + self._local_size_importer = SynchroneousImporter( self._external_task_manager, "local size", self.get_local_size, diff --git a/tests/test_local_size.py b/tests/test_local_size.py index 42c9946..2d03079 100644 --- a/tests/test_local_size.py +++ b/tests/test_local_size.py @@ -19,7 +19,7 @@ async def test_get_local_size_success(plugin, read, write): } read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)] plugin.get_local_size.side_effect = [ - async_return_value(100000000000), + async_return_value(100000000000, 1), async_return_value(None), async_return_value(3333333) ] @@ -89,12 +89,15 @@ async def test_get_local_size_error(exception, code, message, plugin, read, writ plugin.get_local_size.assert_called() plugin.local_size_import_complete.assert_called_once_with() - assert get_messages(write) == [ - { - "jsonrpc": "2.0", - "id": request_id, - "result": None - }, + direct_response = { + "jsonrpc": "2.0", + "id": request_id, + "result": None + } + responses = get_messages(write) + assert direct_response in responses + responses.remove(direct_response) + assert responses == [ { "jsonrpc": "2.0", "method": "local_size_import_failure", @@ -145,6 +148,7 @@ async def test_prepare_get_local_size_context_error(plugin, read, write): @pytest.mark.asyncio async def test_import_already_in_progress_error(plugin, read, write): plugin.prepare_local_size_context.return_value = async_return_value(None) + plugin.get_local_size.return_value = async_return_value(100, 5) requests = [ { "jsonrpc": "2.0", @@ -185,4 +189,3 @@ async def test_import_already_in_progress_error(plugin, read, write): "message": "Import already in progress" } } in responses -