fixed component manager tests

This commit is contained in:
Lex Berezhny 2019-01-23 20:26:29 -05:00
parent 3d9ea92118
commit 2dcd187d69
4 changed files with 93 additions and 52 deletions

View file

@ -111,26 +111,11 @@ class ComponentManager:
steps.reverse()
return steps
async def setup(self, **callbacks):
async def start(self):
""" Start Components in sequence sorted by requirements """
for component_name, cb in callbacks.items():
if component_name not in self.component_classes:
if component_name not in self.skip_components:
raise NameError("unknown component: %s" % component_name)
if not callable(cb):
raise ValueError("%s is not callable" % cb)
async def _setup(component):
await component._setup()
if component.component_name in callbacks:
maybe_coro = callbacks[component.component_name](component)
if asyncio.iscoroutine(maybe_coro):
await asyncio.create_task(maybe_coro)
stages = self.sort_components()
for stage in stages:
for stage in self.sort_components():
needing_start = [
_setup(component) for component in stage if not component.running
component._setup() for component in stage if not component.running
]
if needing_start:
await asyncio.wait(needing_start)
@ -146,7 +131,7 @@ class ComponentManager:
component._stop() for component in stage if component.running
]
if needing_stop:
await asyncio.wait(needing_stop, loop=self.loop)
await asyncio.wait(needing_stop)
def all_components_running(self, *component_names):
"""

View file

@ -413,7 +413,7 @@ class Daemon(metaclass=JSONRPCServerType):
if self.analytics_manager:
self.analytics_manager.start()
self._component_setup_task = self.component_manager.setup()
self._component_setup_task = self.component_manager.start()
await self._component_setup_task
await self.analytics_manager.send_server_startup()
log.info("Started lbrynet-daemon")

View file

@ -11,16 +11,14 @@ from lbrynet.extras.daemon.Components import HEADERS_COMPONENT
from lbrynet.extras.daemon import Components
@unittest.SkipTest
class TestComponentManager(unittest.TestCase):
def setUp(self):
self.default_components_sort = [
[
Components.HeadersComponent,
Components.DatabaseComponent,
Components.ExchangeRateManagerComponent,
Components.PaymentRateComponent,
Components.RateLimiterComponent,
Components.UPnPComponent
],
[
@ -29,19 +27,13 @@ class TestComponentManager(unittest.TestCase):
Components.WalletComponent
],
[
Components.FileManagerComponent,
Components.HashAnnouncerComponent,
Components.PeerProtocolServerComponent
],
[
Components.ReflectorComponent
Components.PeerProtocolServerComponent,
Components.StreamManagerComponent,
]
]
self.component_manager = ComponentManager(Config())
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):
@ -50,18 +42,16 @@ class TestComponentManager(unittest.TestCase):
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")
@unittest.SkipTest
class TestComponentManagerOverrides(unittest.TestCase):
def test_init_with_overrides(self):
class FakeWallet:
component_name = "wallet"
@ -89,57 +79,123 @@ class TestComponentManagerOverrides(unittest.TestCase):
ComponentManager(Config(), randomComponent=FakeRandomComponent)
@unittest.SkipTest
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)
class TestComponentManagerProperStart(AdvanceTimeTestCase):
def setUp(self):
self.component_manager = ComponentManager(
Config(),
skip_components=[DATABASE_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT,
PEER_PROTOCOL_SERVER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT,
HEADERS_COMPONENT, PAYMENT_RATE_COMPONENT, RATE_LIMITER_COMPONENT,
EXCHANGE_RATE_MANAGER_COMPONENT],
wallet=mocks.FakeDelayedWallet,
file_manager=mocks.FakeDelayedFileManager,
blob_manager=mocks.FakeDelayedBlobManager
skip_components=[
DATABASE_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT,
PEER_PROTOCOL_SERVER_COMPONENT, UPNP_COMPONENT,
HEADERS_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.setup())
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('file_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('file_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('file_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.setup())
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('file_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('file_manager').running)
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('file_manager').running)
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('file_manager').running)
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)

View file

@ -149,7 +149,7 @@ class TestFileListSorting(unittest.TestCase):
'ashlee27', 'bfrederick', 'brittanyhicks', 'davidsonjeffrey', 'heidiherring',
'jlewis', 'kswanson', 'michelle50', 'richard64', 'xsteele'
]
return f2d(self.test_daemon.component_manager.setup())
return f2d(self.test_daemon.component_manager.start())
def test_sort_by_points_paid_no_direction_specified(self):
sort_options = ['points_paid']