From a358c6eef1b00c82960513a33d150b2c835cb4c3 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Tue, 14 May 2024 16:14:26 +0400 Subject: [PATCH] fix (vscode) - schedule vscode status check using `kivy.clock.Clock` instead of `asyncio` - closes #101 --- CHANGELOG.md | 2 + tests/fixtures/stability.py | 64 +++++++++++++++++------ tests/integration/test_services.py | 22 ++------ ubo_app/services/050-vscode/setup.py | 7 ++- ubo_app/services/050-vscode/ubo_handle.py | 2 +- 5 files changed, 57 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53ba2ac5..fd809108 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ is set/cleared - closes #105 - fix(camera) - back button in the camera viewfinder doesn't cancel the parent application/menu - closes #106 +- fix (vscode) - schedule vscode status check using `kivy.clock.Clock` instead of + `asyncio` - closes #101 ## Version 0.14.1 diff --git a/tests/fixtures/stability.py b/tests/fixtures/stability.py index 269fd049..8be5c386 100644 --- a/tests/fixtures/stability.py +++ b/tests/fixtures/stability.py @@ -2,6 +2,7 @@ from __future__ import annotations +import asyncio from typing import TYPE_CHECKING, Protocol import pytest @@ -10,6 +11,9 @@ from tests.fixtures.snapshot import write_image if TYPE_CHECKING: + from collections.abc import Callable, Coroutine + + from numpy._typing import NDArray from redux_pytest.fixtures import StoreSnapshot from redux_pytest.fixtures.wait_for import AsyncWaiter, WaitFor @@ -27,10 +31,46 @@ async def __call__( ... +async def _run( + *, + check: Callable[[], Coroutine], + store_snapshot: StoreSnapshot, + window_snapshot: WindowSnapshot, + store_snapshots: list[str], + window_snapshots: list[NDArray], +) -> None: + await asyncio.sleep(2) + for _ in range(3): + try: + await check() + except RetryError as exception: + if isinstance(exception.last_attempt.exception(), AssertionError): + continue + raise + except AssertionError: + continue + + try: + await check() + except RetryError: + for i, snapshot in enumerate(store_snapshots): + ( + store_snapshot.results_dir + / f'store-unstability_snapshot_{i}.mismatch.jsonc' + ).write_text(snapshot) + for i, snapshot in enumerate(window_snapshots): + write_image( + window_snapshot.results_dir + / f'window-unstability_snapshot_{i}.mismatch.png', + snapshot, + ) + raise + + @pytest.fixture() async def stability( - window_snapshot: WindowSnapshot, store_snapshot: StoreSnapshot, + window_snapshot: WindowSnapshot, wait_for: WaitFor, ) -> AsyncWaiter: """Wait for the screen and store to stabilize.""" @@ -72,20 +112,12 @@ def check() -> None: assert is_window_stable, 'The content of the screen is not stable yet' assert is_store_stable, 'The content of the store is not stable yet' - try: - await check() - except RetryError: - for i, snapshot in enumerate(store_snapshots): - ( - store_snapshot.results_dir - / f'store-unstability_snapshot_{i}.mismatch.jsonc' - ).write_text(snapshot) - for i, snapshot in enumerate(window_snapshots): - write_image( - window_snapshot.results_dir - / f'window-unstability_snapshot_{i}.mismatch.png', - snapshot, - ) - raise + await _run( + check=check, + store_snapshot=store_snapshot, + window_snapshot=window_snapshot, + store_snapshots=store_snapshots, + window_snapshots=window_snapshots, + ) return wrapper diff --git a/tests/integration/test_services.py b/tests/integration/test_services.py index dbe1864a..e26d5cd9 100644 --- a/tests/integration/test_services.py +++ b/tests/integration/test_services.py @@ -2,11 +2,8 @@ from __future__ import annotations -import asyncio from typing import TYPE_CHECKING -from tenacity import RetryError - if TYPE_CHECKING: from redux_pytest.fixtures import StoreSnapshot @@ -45,19 +42,6 @@ async def test_all_services_register( app = MenuApp() app_context.set_app(app) load_services(ALL_SERVICES_IDS, timeout=10) - for _ in range(3): - try: - await asyncio.sleep(3) - await stability(timeout=3) - store_snapshot.take() - window_snapshot.take() - break - except RetryError as exception: - if isinstance(exception.last_attempt.exception(), AssertionError): - continue - raise - except AssertionError: - continue - else: - store_snapshot.take() - window_snapshot.take() + await stability() + store_snapshot.take() + window_snapshot.take() diff --git a/ubo_app/services/050-vscode/setup.py b/ubo_app/services/050-vscode/setup.py index dfcf8f23..a4264789 100644 --- a/ubo_app/services/050-vscode/setup.py +++ b/ubo_app/services/050-vscode/setup.py @@ -8,6 +8,7 @@ from commands import check_status, install_service, uninstall_service from constants import CODE_BINARY_PATH, CODE_BINARY_URL, DOWNLOAD_PATH +from kivy.clock import Clock from kivy.lang.builder import Builder from login_page import LoginPage from ubo_gui.constants import DANGER_COLOR @@ -231,16 +232,14 @@ def generate_vscode_menu() -> Callable[[], HeadedMenu]: return vscode_menu -async def init_service() -> None: +def init_service() -> None: dispatch( RegisterSettingAppAction( menu_item=ActionItem(label='VSCode', icon='󰨞', action=generate_vscode_menu), category=SettingsCategory.REMOTE, ), ) - while True: - await asyncio.sleep(5) - await check_status() + Clock.schedule_interval(lambda _: create_task(check_status()), 5) Builder.load_file( diff --git a/ubo_app/services/050-vscode/ubo_handle.py b/ubo_app/services/050-vscode/ubo_handle.py index d5676324..6b1fe6cc 100644 --- a/ubo_app/services/050-vscode/ubo_handle.py +++ b/ubo_app/services/050-vscode/ubo_handle.py @@ -12,7 +12,7 @@ async def setup(service: Service) -> None: from setup import init_service service.register_reducer(reducer) - await init_service() + init_service() register(