Skip to content

Commit

Permalink
Merge pull request #875 from roflcoopter/feature/shutdown-viseron-in-…
Browse files Browse the repository at this point in the history
…test

properly cleanup after test_setup_components
  • Loading branch information
roflcoopter authored Jan 2, 2025
2 parents 58d644f + f1aa40d commit 7e9af6e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion tests/components/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
)

from tests.common import MockComponent, return_any
from tests.conftest import MockViseron


def test_setup_components(vis, caplog):
def test_setup_components(vis: MockViseron, caplog):
"""Test setup of core and default components."""
setup_components(vis, {"logger": {}})
for component in CORE_COMPONENTS:
assert component in vis.data[LOADED]

for component in DEFAULT_COMPONENTS:
assert component in vis.data[LOADED]
vis.shutdown()


def test_setup_components_2(vis, caplog):
Expand Down
2 changes: 1 addition & 1 deletion tests/components/webserver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def setUp(self) -> None:
"viseron.components.webserver.create_application"
):
mocked_load_config.return_value = self.config
self.vis = setup_viseron()
self.vis = setup_viseron(start_background_scheduler=False)
self.webserver: Webserver = self.vis.data[COMPONENT]
return super().setUp()

Expand Down
12 changes: 8 additions & 4 deletions viseron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ def enable_logging() -> None:
)


def setup_viseron() -> Viseron:
def setup_viseron(start_background_scheduler=True) -> Viseron:
"""Set up and run Viseron."""
start = timer()
enable_logging()
viseron_version = os.getenv("VISERON_VERSION")
LOGGER.info("-------------------------------------------")
LOGGER.info(f"Initializing Viseron {viseron_version if viseron_version else ''}")

vis = Viseron()
vis = Viseron(start_background_scheduler=start_background_scheduler)

try:
config = load_config()
Expand Down Expand Up @@ -249,7 +249,7 @@ def setup_viseron() -> Viseron:
class Viseron:
"""Viseron."""

def __init__(self) -> None:
def __init__(self, start_background_scheduler=True) -> None:
self.states = States(self)

self.setup_threads: list[threading.Thread] = []
Expand All @@ -270,7 +270,8 @@ def __init__(self) -> None:
self.data[REGISTERED_DOMAINS] = {}

self.background_scheduler = BackgroundScheduler(timezone="UTC", daemon=True)
self.background_scheduler.start()
if start_background_scheduler:
self.background_scheduler.start()
self._thread_watchdog = ThreadWatchDog(self)
self._subprocess_watchdog = SubprocessWatchDog(self)
self._process_watchdog = ProcessWatchDog(self)
Expand Down Expand Up @@ -542,6 +543,7 @@ def shutdown(self) -> None:

try:
self.background_scheduler.shutdown(wait=False)
self.background_scheduler.remove_all_jobs()
except SchedulerNotRunningError as err:
LOGGER.warning(f"Failed to shutdown scheduler: {err}")

Expand All @@ -555,6 +557,8 @@ def shutdown(self) -> None:
self, data_stream, VISERON_SIGNAL_STOPPING
)

if data_stream:
data_stream.remove_all_subscriptions()
LOGGER.info("Shutdown complete in %.1f seconds", timer() - start)

def add_entity(self, component: str, entity: Entity):
Expand Down
6 changes: 6 additions & 0 deletions viseron/components/data_stream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def unsubscribe_data(data_topic: str, unique_id: uuid.UUID) -> None:

DataStream._subscribers[data_topic].pop(unique_id)

@staticmethod
def remove_all_subscriptions() -> None:
"""Remove all subscriptions."""
DataStream._subscribers.clear()
DataStream._wildcard_subscribers.clear()

def run_callbacks(
self,
callbacks: dict[uuid.UUID, DataSubscriber],
Expand Down

0 comments on commit 7e9af6e

Please sign in to comment.