class TestComponentManager(AsyncioTestCase): def setUp(self): self.default_components_sort = [ [ Components.DatabaseComponent, Components.ExchangeRateManagerComponent, Components.UPnPComponent ], [ Components.BlobComponent, Components.DHTComponent, Components.WalletComponent ], [ Components.HashAnnouncerComponent, Components.PeerProtocolServerComponent, Components.StreamManagerComponent, ] ] self.component_manager = ComponentManager(Config()) def test_sort_components(self): stages = self.component_manager.sort_components() for stage_list, sorted_stage_list in zip(stages, self.default_components_sort): self.assertEqual([type(stage) for stage in stage_list], sorted_stage_list) def test_sort_components_reverse(self): rev_stages = self.component_manager.sort_components(reverse=True) reverse_default_components_sort = reversed( self.default_components_sort) for stage_list, sorted_stage_list in zip( rev_stages, reverse_default_components_sort): self.assertEqual([type(stage) for stage in stage_list], sorted_stage_list) def test_get_component_not_exists(self): with self.assertRaises(NameError): self.component_manager.get_component("random_component")
def test_init_with_overrides(self): class FakeWallet: component_name = "wallet" depends_on = [] def __init__(self, component_manager): self.component_manager = component_manager @property def component(self): return self new_component_manager = ComponentManager(Config(), wallet=FakeWallet) fake_wallet = new_component_manager.get_component("wallet") # wallet should be an instance of FakeWallet and not WalletComponent from Components.py self.assertIsInstance(fake_wallet, FakeWallet) self.assertNotIsInstance(fake_wallet, Components.WalletComponent)
class TestComponentManagerProperStart(AdvanceTimeTestCase): def setUp(self): self.component_manager = ComponentManager( Config(), skip_components=[ DATABASE_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, UPNP_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT ], wallet=FakeDelayedWallet, stream_manager=FakeDelayedStreamManager, blob_manager=FakeDelayedBlobManager) async def test_proper_starting_of_components(self): asyncio.create_task(self.component_manager.start()) await self.advance(0) self.assertTrue(self.component_manager.get_component('wallet').running) self.assertFalse( self.component_manager.get_component('blob_manager').running) self.assertFalse( self.component_manager.get_component('stream_manager').running) await self.advance(1) self.assertTrue(self.component_manager.get_component('wallet').running) self.assertTrue( self.component_manager.get_component('blob_manager').running) self.assertFalse( self.component_manager.get_component('stream_manager').running) await self.advance(1) self.assertTrue(self.component_manager.get_component('wallet').running) self.assertTrue( self.component_manager.get_component('blob_manager').running) self.assertTrue( self.component_manager.get_component('stream_manager').running) async def test_proper_stopping_of_components(self): asyncio.create_task(self.component_manager.start()) await self.advance(0) await self.advance(1) await self.advance(1) self.assertTrue(self.component_manager.get_component('wallet').running) self.assertTrue( self.component_manager.get_component('blob_manager').running) self.assertTrue( self.component_manager.get_component('stream_manager').running) asyncio.create_task(self.component_manager.stop()) await self.advance(0) self.assertFalse( self.component_manager.get_component('stream_manager').running) self.assertTrue( self.component_manager.get_component('blob_manager').running) self.assertTrue(self.component_manager.get_component('wallet').running) await self.advance(1) self.assertFalse( self.component_manager.get_component('stream_manager').running) self.assertFalse( self.component_manager.get_component('blob_manager').running) self.assertTrue(self.component_manager.get_component('wallet').running) await self.advance(1) self.assertFalse( self.component_manager.get_component('stream_manager').running) self.assertFalse( self.component_manager.get_component('blob_manager').running) self.assertFalse( self.component_manager.get_component('wallet').running)
def get_test_daemon(conf: Config, with_fee=False): conf.data_dir = '/tmp' rates = { 'BTCLBC': { 'spot': 3.0, 'ts': test_utils.DEFAULT_ISO_TIME + 1 }, 'USDBTC': { 'spot': 2.0, 'ts': test_utils.DEFAULT_ISO_TIME + 2 } } component_manager = ComponentManager( conf, skip_components=[ DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT, UPNP_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, HASH_ANNOUNCER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT, BLOB_COMPONENT, HEADERS_COMPONENT, RATE_LIMITER_COMPONENT ], file_manager=FakeFileManager) daemon = LBRYDaemon(conf, component_manager=component_manager) daemon.payment_rate_manager = OnlyFreePaymentsManager() daemon.wallet_manager = mock.Mock(spec=LbryWalletManager) daemon.wallet_manager.wallet = mock.Mock(spec=Wallet) daemon.wallet_manager.use_encryption = False daemon.wallet_manager.network = FakeNetwork() daemon.storage = mock.Mock(spec=SQLiteStorage) market_feeds = [BTCLBCFeed(), USDBTCFeed()] daemon.exchange_rate_manager = DummyExchangeRateManager( market_feeds, rates) daemon.stream_manager = component_manager.get_component( FILE_MANAGER_COMPONENT) metadata = { "author": "fake author", "language": "en", "content_type": "fake/format", "description": "fake description", "license": "fake license", "license_url": "fake license url", "nsfw": False, "sources": { "lbry_sd_hash": 'd2b8b6e907dde95245fe6d144d16c2fdd60c4e0c6463ec98' 'b85642d06d8e9414e8fcfdcb7cb13532ec5454fb8fe7f280' }, "thumbnail": "fake thumbnail", "title": "fake title", "ver": "0.0.3" } if with_fee: metadata.update({ "fee": { "USD": { "address": "bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75 } } }) migrated = smart_decode(json.dumps(metadata)) daemon._resolve = daemon.resolve = lambda *_: defer.succeed( {"test": { 'claim': { 'value': migrated.claim_dict } }}) return daemon