diff --git a/lbry/extras/daemon/componentmanager.py b/lbry/extras/daemon/componentmanager.py index 811f58561..f9f7903ae 100644 --- a/lbry/extras/daemon/componentmanager.py +++ b/lbry/extras/daemon/componentmanager.py @@ -158,11 +158,14 @@ class ComponentManager: for component in self.components } - def get_component(self, component_name): + def get_actual_component(self, component_name): for component in self.components: if component.component_name == component_name: - return component.component + return component raise NameError(component_name) + def get_component(self, component_name): + return self.get_actual_component(component_name).component + def has_component(self, component_name): return any(component for component in self.components if component_name == component.component_name) diff --git a/lbry/extras/daemon/daemon.py b/lbry/extras/daemon/daemon.py index 927650dd8..edab45785 100644 --- a/lbry/extras/daemon/daemon.py +++ b/lbry/extras/daemon/daemon.py @@ -1341,6 +1341,8 @@ class Daemon(metaclass=JSONRPCServerType): Returns: Dictionary of wallet status information. """ + if self.wallet_manager is None: + return {'is_encrypted': None, 'is_syncing': True, 'is_locked': None} wallet = self.wallet_manager.get_wallet_or_default(wallet_id) return { 'is_encrypted': wallet.is_encrypted, diff --git a/lbry/wallet/tasks.py b/lbry/wallet/tasks.py index f57b55f5f..2ec60709e 100644 --- a/lbry/wallet/tasks.py +++ b/lbry/wallet/tasks.py @@ -7,6 +7,7 @@ class TaskGroup: self._loop = loop or get_event_loop() self._tasks = set() self.done = Event() + self.started = Event() def __len__(self): return len(self._tasks) @@ -14,6 +15,7 @@ class TaskGroup: def add(self, coro): task = self._loop.create_task(coro) self._tasks.add(task) + self.started.set() self.done.clear() task.add_done_callback(self._remove) return task @@ -22,8 +24,10 @@ class TaskGroup: self._tasks.remove(task) if len(self._tasks) < 1: self.done.set() + self.started.clear() def cancel(self): for task in self._tasks: task.cancel() self.done.set() + self.started.clear() diff --git a/tests/integration/blockchain/test_wallet_commands.py b/tests/integration/blockchain/test_wallet_commands.py index cd86fa51d..455f8b4e0 100644 --- a/tests/integration/blockchain/test_wallet_commands.py +++ b/tests/integration/blockchain/test_wallet_commands.py @@ -1,6 +1,5 @@ import asyncio import json -import os from lbry.wallet import ENCRYPT_ON_DISK from lbry.error import InvalidPasswordError @@ -22,14 +21,26 @@ class WalletCommands(CommandTestCase): async def test_wallet_syncing_status(self): address = await self.daemon.jsonrpc_address_unused() - sendtxid = await self.blockchain.send_to_address(address, 1) + self.assertFalse(self.daemon.jsonrpc_wallet_status()['is_syncing']) + await self.blockchain.send_to_address(address, 1) + await self.ledger._update_tasks.started.wait() + self.assertTrue(self.daemon.jsonrpc_wallet_status()['is_syncing']) + await self.ledger._update_tasks.done.wait() + self.assertFalse(self.daemon.jsonrpc_wallet_status()['is_syncing']) - async def eventually_will_sync(): - while not self.daemon.jsonrpc_wallet_status()['is_syncing']: - await asyncio.sleep(0) - check_sync = asyncio.create_task(eventually_will_sync()) - await self.confirm_tx(sendtxid, self.ledger) - await asyncio.wait_for(check_sync, timeout=10) + wallet = self.daemon.component_manager.get_actual_component('wallet') + wallet_manager = wallet.wallet_manager + # when component manager hasn't started yet + wallet.wallet_manager = None + self.assertEqual( + {'is_encrypted': None, 'is_syncing': True, 'is_locked': None}, + self.daemon.jsonrpc_wallet_status() + ) + wallet.wallet_manager = wallet_manager + self.assertEqual( + {'is_encrypted': False, 'is_syncing': False, 'is_locked': False}, + self.daemon.jsonrpc_wallet_status() + ) async def test_wallet_reconnect(self): await self.conductor.spv_node.stop(True)