Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Tribler/tribler
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7a86309ac739b81fb81b4c7cc135cf76b4f9401b
Choose a base ref
..
head repository: Tribler/tribler
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a1b25050bc777ef953445eeb7ba0f6c25d4e6baa
Choose a head ref
Showing with 38 additions and 29 deletions.
  1. +12 −8 src/tribler-core/tribler_core/components/implementation/ipv8.py
  2. +26 −21 src/tribler-core/tribler_core/components/tests/test_base_component.py
20 changes: 12 additions & 8 deletions src/tribler-core/tribler_core/components/implementation/ipv8.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from ipv8.bootstrapping.dispersy.bootstrapper import DispersyBootstrapper
from ipv8.configuration import ConfigBuilder, DISPERSY_BOOTSTRAPPER
from ipv8.dht.churn import PingChurn
@@ -24,14 +26,16 @@

class Ipv8ComponentImp(Ipv8Component):
task_manager: TaskManager
rest_manager: RESTManager
rest_manager: Optional[RESTManager]

async def run(self):
await self.use(ReporterComponent, required=False)

config = self.session.config

self.rest_component = await self.use(RESTComponent, required=False)
rest_component = await self.use(RESTComponent, required=False)
rest_manager = self.rest_manager = rest_component.rest_manager if rest_component.enabled else None

self.task_manager = TaskManager()

port = config.ipv8.port
@@ -75,8 +79,8 @@ async def run(self):
config.ipv8.walk_interval,
config.ipv8.walk_scaling_upper_limit).start(self.task_manager)

if self.rest_component.enabled:
self.rest_component.rest_manager.get_endpoint('statistics').ipv8 = ipv8
if rest_manager:
rest_manager.get_endpoint('statistics').ipv8 = ipv8

self.peer_discovery_community = self.dht_discovery_community = None

@@ -93,8 +97,8 @@ async def run(self):
endpoints_to_init = ['/asyncio', '/attestation', '/dht', '/identity',
'/isolation', '/network', '/noblockdht', '/overlays']

if self.rest_component.enabled:
for path, endpoint in self.rest_component.rest_manager.get_endpoint('ipv8').endpoints.items():
if rest_manager:
for path, endpoint in rest_manager.get_endpoint('ipv8').endpoints.items():
if path in endpoints_to_init:
endpoint.initialize(ipv8)

@@ -125,8 +129,8 @@ def init_dht_discovery_community(self):
self.dht_discovery_community = community

async def shutdown(self):
if self.rest_component.enabled:
self.rest_component.rest_manager.get_endpoint('statistics').ipv8 = None
if self.rest_manager:
self.rest_manager.get_endpoint('statistics').ipv8 = None
await self.release(RESTComponent)

await self.unused.wait()
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re

from typing import List, Type
from tribler_core.components.base import Component, ComponentError, Session, SessionError, T
from tribler_core.components.base import Component, ComponentError, Session, T
from tribler_core.config.tribler_config import TriblerConfig

import pytest
@@ -10,11 +12,18 @@ class TestComponent(Component):
run_was_executed = False
shutdown_was_executed = False
should_be_enabled_result_value = True
default_implementation: T

@classmethod
def should_be_enabled(cls, config: TriblerConfig):
return cls.should_be_enabled_result_value

@classmethod
def make_implementation(cls: Type[T], config, enable) -> T:
result = cls.default_implementation(cls)
result.enabled = enable
return result

async def run(self):
self.run_was_executed = True

@@ -23,28 +32,20 @@ async def shutdown(self):


class ComponentA(TestComponent):
@classmethod
def make_implementation(cls: Type[T], config, enable) -> T:
result = ComponentAImp(cls)
result.enabled = enable
return result


class ComponentAImp(ComponentA):
pass


class ComponentB(TestComponent):
@classmethod
def make_implementation(cls: Type[T], config, enable) -> T:
result = ComponentBImp(cls)
result.enabled = enable
return result
pass

class ComponentAImp(ComponentA):
pass

class ComponentBImp(ComponentB):
pass

ComponentA.default_implementation = ComponentAImp
ComponentB.default_implementation = ComponentBImp

return ComponentA, ComponentB


@@ -53,7 +54,7 @@ def components_gen(config: TriblerConfig, component_list: List[T]):
yield cls.make_implementation(config, cls.should_be_enabled(config))


async def test_session_start_shutdown(loop, tribler_config):
async def test_session_start_shutdown(loop, tribler_config): # pylint: disable=unused-argument
ComponentA, ComponentB = make_test_components()

session = Session(tribler_config, list(components_gen(tribler_config, [ComponentA, ComponentB])))
@@ -87,7 +88,7 @@ async def test_session_start_shutdown(loop, tribler_config):
assert a.started.is_set() and b.started.is_set()


async def test_disabled_component(loop, tribler_config):
async def test_disabled_component(loop, tribler_config): # pylint: disable=unused-argument
ComponentA, ComponentB = make_test_components()
ComponentA.should_be_enabled_result_value = False

@@ -122,7 +123,7 @@ async def test_disabled_component(loop, tribler_config):
assert a.started.is_set() and b.started.is_set()


async def test_required_dependency_enabled(loop, tribler_config):
async def test_required_dependency_enabled(loop, tribler_config): # pylint: disable=unused-argument
ComponentA, ComponentB = make_test_components()
ComponentB.run = lambda self: self.use(ComponentA)

@@ -149,7 +150,7 @@ async def test_required_dependency_enabled(loop, tribler_config):
assert not b.components_used_by_me and not a.in_use_by


async def test_required_dependency_disabled(loop, tribler_config):
async def test_required_dependency_disabled(loop, tribler_config): # pylint: disable=unused-argument
ComponentA, ComponentB = make_test_components()
ComponentA.should_be_enabled_result_value = False
ComponentB.run = lambda self: self.use(ComponentA)
@@ -177,7 +178,7 @@ async def test_required_dependency_disabled(loop, tribler_config):
assert not b.components_used_by_me and not a.in_use_by


async def test_required_dependency_missed(loop, tribler_config):
async def test_required_dependency_missed(capsys, loop, tribler_config): # pylint: disable=unused-argument
ComponentA, ComponentB = make_test_components()
ComponentB.run = lambda self: self.use(ComponentA)

@@ -192,8 +193,12 @@ async def test_required_dependency_missed(loop, tribler_config):
with pytest.raises(ComponentError, match=r'ComponentA implementation not found in <Session:\d+>'):
await session.start()

captured = capsys.readouterr()
assert re.match(r'\nException in ComponentBImp.start\(\): '
r'ComponentError:ComponentA implementation not found in <Session:\d+>\n', captured.err)


async def test_optional_dependency_missed(loop, tribler_config):
async def test_optional_dependency_missed(loop, tribler_config): # pylint: disable=unused-argument
ComponentA, ComponentB = make_test_components()
ComponentB.run = lambda self: self.use(ComponentA, required=False)