Skip to content

Commit

Permalink
Rename events to avoid misunderstanding them as bool attributes: star…
Browse files Browse the repository at this point in the history
…ted -> started_event, unused -> unused_event

This prevents errors like: `if component.started: ...`
(should be: `if component.started_event.is_set(): ...`)
  • Loading branch information
kozlovsky committed Oct 4, 2021
1 parent bb39879 commit 2730ab8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def test_bandwidth_accounting_component(tribler_config):
await session.start()

comp = BandwidthAccountingComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.community
assert comp._ipv8

Expand Down
18 changes: 9 additions & 9 deletions src/tribler-core/tribler_core/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def __init__(self):
self.session: Optional[Session] = None
self.dependencies: Set[Component] = set()
self.reverse_dependencies: Set[Component] = set()
self.started = Event()
self.started_event = Event()
self.failed = False
self.unused = Event()
self.unused_event = Event()
self.stopped = False
# Every component starts unused, so it does not lock the whole system on shutdown
self.unused.set()
self.unused_event.set()

@classmethod
def instance(cls: Type[T]) -> T:
Expand All @@ -132,14 +132,14 @@ async def start(self):
sys.stderr.write(f'\nException in {self.__class__.__name__}.start(): {type(e).__name__}:{e}\n')
self.logger.exception(f'Exception in {self.__class__.__name__}.start(): {type(e).__name__}:{e}')
self.failed = True
self.started.set()
self.started_event.set()
raise
self.started.set()
self.started_event.set()

async def stop(self):
self.logger.info(f'Stop: {self.__class__.__name__}')
self.logger.info("Waiting for other components to release me")
await self.unused.wait()
await self.unused_event.wait()
self.logger.info("Component free, shutting down")
try:
await self.shutdown()
Expand Down Expand Up @@ -182,7 +182,7 @@ async def get_component(self, dependency: Type[T]) -> Optional[T]:
if not dep:
return None

await dep.started.wait()
await dep.started_event.wait()
if dep.failed:
self.logger.warning(f'Component {self.__class__.__name__} has failed dependency {dependency.__name__}')
return None
Expand All @@ -207,10 +207,10 @@ def _use_by(self, component: Component):
assert component not in self.reverse_dependencies
self.reverse_dependencies.add(component)
if len(self.reverse_dependencies) == 1:
self.unused.clear()
self.unused_event.clear()

def _unuse_by(self, component: Component):
assert component in self.reverse_dependencies
self.reverse_dependencies.remove(component)
if not self.reverse_dependencies:
self.unused.set()
self.unused_event.set()
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def test_giga_channel_component(tribler_config):
await session.start()

comp = GigaChannelComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.community
assert comp._ipv8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def test_gigachannel_manager_component(tribler_config):
comp = GigachannelManagerComponent.instance()
await session.start()

assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.gigachannel_manager

await session.shutdown()
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def test_metadata_store_component(tribler_config):
comp = MetadataStoreComponent.instance()
await session.start()

assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.mds

await session.shutdown()
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ComponentB(TestComponent):

for component in a, b:
assert not component.run_was_executed
assert not component.started.is_set()
assert not component.started_event.is_set()
assert not component.shutdown_was_executed
assert not component.stopped

Expand All @@ -43,7 +43,7 @@ class ComponentB(TestComponent):
assert ComponentA.instance() is a and ComponentB.instance() is b
for component in a, b:
assert component.run_was_executed
assert component.started.is_set()
assert component.started_event.is_set()
assert not component.shutdown_was_executed
assert not component.stopped

Expand All @@ -53,7 +53,7 @@ class ComponentB(TestComponent):
assert ComponentA.instance() is a and ComponentB.instance() is b
for component in a, b:
assert component.run_was_executed
assert component.started.is_set()
assert component.started_event.is_set()
assert component.shutdown_was_executed
assert component.stopped

Expand All @@ -73,20 +73,20 @@ async def run(self):

for component in a, b:
assert not component.dependencies and not component.reverse_dependencies
assert component.unused.is_set()
assert component.unused_event.is_set()

await session.start()

assert a in b.dependencies and not b.reverse_dependencies
assert not a.dependencies and b in a.reverse_dependencies
assert b.unused.is_set() and not a.unused.is_set()
assert b.unused_event.is_set() and not a.unused_event.is_set()

session.shutdown_event.set()
await session.shutdown()

for component in a, b:
assert not component.dependencies and not component.reverse_dependencies
assert component.unused.is_set()
assert component.unused_event.is_set()


async def test_required_dependency_missed(tribler_config):
Expand All @@ -112,7 +112,7 @@ async def run(self):
await session.start(failfast=False)

assert ComponentB.instance() is b
assert b.started.is_set()
assert b.started_event.is_set()
assert b.failed


Expand All @@ -134,13 +134,13 @@ async def shutdown(self):

await session.start()

assert not a.unused.is_set()
assert not a.unused_event.is_set()

with pytest.raises(TestException):
await session.shutdown()

for component in a, b:
assert not component.dependencies
assert not component.reverse_dependencies
assert component.unused.is_set()
assert component.unused_event.is_set()
assert component.stopped
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def test_ipv8_component(tribler_config):
await session.start()

comp = Ipv8Component.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.ipv8
assert comp.peer
assert not comp.dht_discovery_community
Expand All @@ -74,7 +74,7 @@ async def test_libtorrent_component(tribler_config):
await session.start()

comp = LibtorrentComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.download_manager

await session.shutdown()
Expand All @@ -89,7 +89,7 @@ async def test_payout_component(tribler_config):
await session.start()

comp = PayoutComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.payout_manager

await session.shutdown()
Expand Down Expand Up @@ -119,7 +119,7 @@ async def test_reporter_component(tribler_config):
await session.start()

comp = ReporterComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed

await session.shutdown()

Expand All @@ -134,7 +134,7 @@ async def test_resource_monitor_component(tribler_config):
await session.start()

comp = ResourceMonitorComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.resource_monitor

await session.shutdown()
Expand All @@ -147,7 +147,7 @@ async def test_REST_component(tribler_config):
await session.start()

comp = RESTComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.rest_manager

await session.shutdown()
Expand All @@ -160,7 +160,7 @@ async def test_socks_servers_component(tribler_config):
await session.start()

comp = SocksServersComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.socks_ports
assert comp.socks_servers

Expand All @@ -177,7 +177,7 @@ async def test_torrent_checker_component(tribler_config):
await session.start()

comp = TorrentCheckerComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.torrent_checker

await session.shutdown()
Expand All @@ -193,7 +193,7 @@ async def test_tunnels_component(tribler_config):
await session.start()

comp = TunnelsComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.community
assert comp._ipv8

Expand All @@ -207,7 +207,7 @@ async def test_upgrade_component(tribler_config):
await session.start()

comp = UpgradeComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.upgrader

await session.shutdown()
Expand All @@ -220,7 +220,7 @@ async def test_version_check_component(tribler_config):
await session.start()

comp = VersionCheckComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.version_check_manager

await session.shutdown()
Expand All @@ -236,7 +236,7 @@ async def test_watch_folder_component(tribler_config):
await session.start()

comp = WatchFolderComponent.instance()
assert comp.started.is_set() and not comp.failed
assert comp.started_event.is_set() and not comp.failed
assert comp.watch_folder

await session.shutdown()

0 comments on commit 2730ab8

Please sign in to comment.