2019-01-07 08:52:53 +01:00
|
|
|
import asyncio
|
2019-12-31 21:30:13 +01:00
|
|
|
from lbry.testcase import AsyncioTestCase, AdvanceTimeTestCase
|
2018-07-24 18:44:37 +02:00
|
|
|
|
2019-06-21 03:02:58 +02:00
|
|
|
from lbry.conf import Config
|
2020-01-03 07:42:54 +01:00
|
|
|
from lbry.extras.daemon.componentmanager import ComponentManager
|
|
|
|
from lbry.extras.daemon.components import DATABASE_COMPONENT, DHT_COMPONENT
|
|
|
|
from lbry.extras.daemon.components import HASH_ANNOUNCER_COMPONENT, UPNP_COMPONENT
|
|
|
|
from lbry.extras.daemon.components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT
|
|
|
|
from lbry.extras.daemon import components
|
2018-07-24 18:44:37 +02:00
|
|
|
|
|
|
|
|
2019-01-24 02:57:56 +01:00
|
|
|
class TestComponentManager(AsyncioTestCase):
|
2019-01-24 02:26:29 +01:00
|
|
|
|
2018-07-24 18:44:37 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.default_components_sort = [
|
2018-08-02 21:54:43 +02:00
|
|
|
[
|
2020-01-03 07:42:54 +01:00
|
|
|
components.DatabaseComponent,
|
|
|
|
components.ExchangeRateManagerComponent,
|
|
|
|
components.UPnPComponent
|
2018-08-02 21:54:43 +02:00
|
|
|
],
|
|
|
|
[
|
2020-01-03 07:42:54 +01:00
|
|
|
components.BlobComponent,
|
|
|
|
components.DHTComponent,
|
|
|
|
components.WalletComponent
|
2018-08-02 21:54:43 +02:00
|
|
|
],
|
|
|
|
[
|
2020-01-03 07:42:54 +01:00
|
|
|
components.HashAnnouncerComponent,
|
|
|
|
components.PeerProtocolServerComponent,
|
|
|
|
components.StreamManagerComponent,
|
2018-08-02 21:54:43 +02:00
|
|
|
]
|
2018-07-24 18:44:37 +02:00
|
|
|
]
|
2019-01-21 21:55:50 +01:00
|
|
|
self.component_manager = ComponentManager(Config())
|
2018-07-24 18:44:37 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
2019-01-24 02:57:56 +01:00
|
|
|
class TestComponentManagerOverrides(AsyncioTestCase):
|
2019-01-24 02:26:29 +01:00
|
|
|
|
2018-07-24 18:44:37 +02:00
|
|
|
def test_init_with_overrides(self):
|
2018-10-18 12:42:45 +02:00
|
|
|
class FakeWallet:
|
2018-07-24 18:44:37 +02:00
|
|
|
component_name = "wallet"
|
|
|
|
depends_on = []
|
|
|
|
|
|
|
|
def __init__(self, component_manager):
|
|
|
|
self.component_manager = component_manager
|
|
|
|
|
|
|
|
@property
|
|
|
|
def component(self):
|
|
|
|
return self
|
|
|
|
|
2019-01-21 21:55:50 +01:00
|
|
|
new_component_manager = ComponentManager(Config(), wallet=FakeWallet)
|
2018-07-24 18:44:37 +02:00
|
|
|
fake_wallet = new_component_manager.get_component("wallet")
|
2020-01-03 07:42:54 +01:00
|
|
|
# wallet should be an instance of FakeWallet and not WalletComponent from components.py
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertIsInstance(fake_wallet, FakeWallet)
|
2020-01-03 07:42:54 +01:00
|
|
|
self.assertNotIsInstance(fake_wallet, components.WalletComponent)
|
2018-07-24 18:44:37 +02:00
|
|
|
|
|
|
|
def test_init_with_wrong_overrides(self):
|
2018-10-18 12:42:45 +02:00
|
|
|
class FakeRandomComponent:
|
2018-07-24 18:44:37 +02:00
|
|
|
component_name = "someComponent"
|
|
|
|
depends_on = []
|
|
|
|
|
|
|
|
with self.assertRaises(SyntaxError):
|
2019-01-21 21:55:50 +01:00
|
|
|
ComponentManager(Config(), randomComponent=FakeRandomComponent)
|
2018-07-24 18:44:37 +02:00
|
|
|
|
|
|
|
|
2019-01-24 02:26:29 +01:00
|
|
|
class FakeComponent:
|
|
|
|
depends_on = []
|
|
|
|
component_name = None
|
|
|
|
|
|
|
|
def __init__(self, component_manager):
|
|
|
|
self.component_manager = component_manager
|
|
|
|
self._running = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def running(self):
|
|
|
|
return self._running
|
|
|
|
|
|
|
|
async def start(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@property
|
|
|
|
def component(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def _setup(self):
|
|
|
|
result = await self.start()
|
|
|
|
self._running = True
|
|
|
|
return result
|
|
|
|
|
|
|
|
async def _stop(self):
|
|
|
|
result = await self.stop()
|
|
|
|
self._running = False
|
|
|
|
return result
|
|
|
|
|
|
|
|
async def get_status(self):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.component_name < other.component_name
|
|
|
|
|
|
|
|
|
|
|
|
class FakeDelayedWallet(FakeComponent):
|
|
|
|
component_name = "wallet"
|
|
|
|
depends_on = []
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
class FakeDelayedBlobManager(FakeComponent):
|
|
|
|
component_name = "blob_manager"
|
|
|
|
depends_on = [FakeDelayedWallet.component_name]
|
|
|
|
|
|
|
|
async def start(self):
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
class FakeDelayedStreamManager(FakeComponent):
|
|
|
|
component_name = "stream_manager"
|
|
|
|
depends_on = [FakeDelayedBlobManager.component_name]
|
|
|
|
|
|
|
|
async def start(self):
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
class TestComponentManagerProperStart(AdvanceTimeTestCase):
|
2019-01-24 02:26:29 +01:00
|
|
|
|
2018-07-24 18:44:37 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.component_manager = ComponentManager(
|
2019-01-21 21:55:50 +01:00
|
|
|
Config(),
|
2019-01-24 02:26:29 +01:00
|
|
|
skip_components=[
|
|
|
|
DATABASE_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT,
|
|
|
|
PEER_PROTOCOL_SERVER_COMPONENT, UPNP_COMPONENT,
|
2019-09-27 14:26:35 +02:00
|
|
|
EXCHANGE_RATE_MANAGER_COMPONENT],
|
2019-01-24 02:26:29 +01:00
|
|
|
wallet=FakeDelayedWallet,
|
|
|
|
stream_manager=FakeDelayedStreamManager,
|
|
|
|
blob_manager=FakeDelayedBlobManager
|
2018-07-24 18:44:37 +02:00
|
|
|
)
|
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
async def test_proper_starting_of_components(self):
|
2019-01-24 02:26:29 +01:00
|
|
|
asyncio.create_task(self.component_manager.start())
|
2018-07-24 18:44:37 +02:00
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
await self.advance(0)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('wallet').running)
|
2018-08-02 21:54:43 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('blob_manager').running)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertFalse(self.component_manager.get_component('stream_manager').running)
|
2018-07-24 18:44:37 +02:00
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
await self.advance(1)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('wallet').running)
|
2018-08-02 21:54:43 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('blob_manager').running)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertFalse(self.component_manager.get_component('stream_manager').running)
|
2018-07-24 18:44:37 +02:00
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
await self.advance(1)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('wallet').running)
|
2018-08-02 21:54:43 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('blob_manager').running)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertTrue(self.component_manager.get_component('stream_manager').running)
|
2018-07-24 18:44:37 +02:00
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
async def test_proper_stopping_of_components(self):
|
2019-01-24 02:26:29 +01:00
|
|
|
asyncio.create_task(self.component_manager.start())
|
2019-01-07 08:52:53 +01:00
|
|
|
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)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertTrue(self.component_manager.get_component('stream_manager').running)
|
2019-01-07 08:52:53 +01:00
|
|
|
|
|
|
|
asyncio.create_task(self.component_manager.stop())
|
|
|
|
await self.advance(0)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertFalse(self.component_manager.get_component('stream_manager').running)
|
2018-08-02 21:54:43 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('blob_manager').running)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('wallet').running)
|
2019-01-07 08:52:53 +01:00
|
|
|
await self.advance(1)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertFalse(self.component_manager.get_component('stream_manager').running)
|
2018-08-02 21:54:43 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('blob_manager').running)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('wallet').running)
|
2019-01-07 08:52:53 +01:00
|
|
|
await self.advance(1)
|
2019-01-24 02:26:29 +01:00
|
|
|
self.assertFalse(self.component_manager.get_component('stream_manager').running)
|
2018-08-02 21:54:43 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('blob_manager').running)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('wallet').running)
|