2019-01-07 08:52:53 +01:00
|
|
|
import asyncio
|
|
|
|
from unittest import TestCase
|
|
|
|
from torba.testcase import AdvanceTimeTestCase
|
2018-07-24 18:44:37 +02:00
|
|
|
|
2019-01-21 21:55:50 +01:00
|
|
|
from tests import mocks
|
|
|
|
from lbrynet.conf import Config
|
2018-11-04 19:44:17 +01:00
|
|
|
from lbrynet.extras.daemon.ComponentManager import ComponentManager
|
2018-10-30 18:41:38 +01:00
|
|
|
from lbrynet.extras.daemon.Components import DATABASE_COMPONENT, DHT_COMPONENT
|
2018-11-04 19:44:17 +01:00
|
|
|
from lbrynet.extras.daemon.Components import HASH_ANNOUNCER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT
|
|
|
|
from lbrynet.extras.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT
|
|
|
|
from lbrynet.extras.daemon.Components import RATE_LIMITER_COMPONENT, HEADERS_COMPONENT, PAYMENT_RATE_COMPONENT
|
|
|
|
from lbrynet.extras.daemon import Components
|
2018-07-24 18:44:37 +02:00
|
|
|
|
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
class TestComponentManager(TestCase):
|
2018-07-24 18:44:37 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.default_components_sort = [
|
2018-08-02 21:54:43 +02:00
|
|
|
[
|
|
|
|
Components.HeadersComponent,
|
|
|
|
Components.DatabaseComponent,
|
|
|
|
Components.ExchangeRateManagerComponent,
|
|
|
|
Components.PaymentRateComponent,
|
|
|
|
Components.RateLimiterComponent,
|
|
|
|
Components.UPnPComponent
|
|
|
|
],
|
|
|
|
[
|
2018-10-30 18:41:38 +01:00
|
|
|
Components.BlobComponent,
|
2018-08-02 21:54:43 +02:00
|
|
|
Components.DHTComponent,
|
|
|
|
Components.WalletComponent
|
|
|
|
],
|
|
|
|
[
|
2018-10-30 18:41:38 +01:00
|
|
|
Components.FileManagerComponent,
|
|
|
|
Components.HashAnnouncerComponent,
|
|
|
|
Components.PeerProtocolServerComponent
|
2018-08-02 21:54:43 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
Components.ReflectorComponent
|
|
|
|
]
|
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 tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
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-07 08:52:53 +01:00
|
|
|
class TestComponentManagerOverrides(TestCase):
|
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")
|
|
|
|
# wallet should be an instance of FakeWallet and not WalletComponent from Components.py
|
|
|
|
self.assertIsInstance(fake_wallet, FakeWallet)
|
|
|
|
self.assertNotIsInstance(fake_wallet, Components.WalletComponent)
|
|
|
|
|
|
|
|
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-07 08:52:53 +01:00
|
|
|
class TestComponentManagerProperStart(AdvanceTimeTestCase):
|
|
|
|
|
2018-07-24 18:44:37 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.component_manager = ComponentManager(
|
2019-01-21 21:55:50 +01:00
|
|
|
Config(),
|
2018-10-30 18:41:38 +01:00
|
|
|
skip_components=[DATABASE_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT,
|
2018-07-24 18:44:37 +02:00
|
|
|
PEER_PROTOCOL_SERVER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT,
|
2018-08-02 21:54:43 +02:00
|
|
|
HEADERS_COMPONENT, PAYMENT_RATE_COMPONENT, RATE_LIMITER_COMPONENT,
|
2018-07-24 18:44:37 +02:00
|
|
|
EXCHANGE_RATE_MANAGER_COMPONENT],
|
|
|
|
wallet=mocks.FakeDelayedWallet,
|
2018-08-02 21:54:43 +02:00
|
|
|
file_manager=mocks.FakeDelayedFileManager,
|
|
|
|
blob_manager=mocks.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):
|
|
|
|
asyncio.create_task(self.component_manager.setup())
|
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)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('file_manager').running)
|
|
|
|
|
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)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('file_manager').running)
|
|
|
|
|
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)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertTrue(self.component_manager.get_component('file_manager').running)
|
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
async def test_proper_stopping_of_components(self):
|
|
|
|
asyncio.create_task(self.component_manager.setup())
|
|
|
|
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('file_manager').running)
|
|
|
|
|
|
|
|
asyncio.create_task(self.component_manager.stop())
|
|
|
|
await self.advance(0)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('file_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)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('file_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)
|
2018-07-24 18:44:37 +02:00
|
|
|
self.assertFalse(self.component_manager.get_component('file_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)
|