From 311ed71ad02a626472ee2d779493220b97d2372a Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Fri, 16 Jun 2023 10:15:04 +0200 Subject: [PATCH] 1.1.1 (#393) --- readme.md | 5 ++- .../lib/HABAppTests/test_rule/test_rule.py | 6 ++- .../rules/habapp/test_rule_context.py | 38 +++++++++++++++++++ src/HABApp/__version__.py | 2 +- .../wrapped_function/wrapped_thread.py | 13 ++++--- src/HABApp/rule/rule.py | 1 + 6 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 run/conf_testing/rules/habapp/test_rule_context.py diff --git a/readme.md b/readme.md index 7db4bc65..ae3c2634 100644 --- a/readme.md +++ b/readme.md @@ -128,8 +128,11 @@ MyOpenhabRule() ``` # Changelog +#### 1.1.1 (2023-06-16) +- Fixed a bug where the rule context was not found + #### 1.1.0 (2023-06-15) -- This is a breaking change! +This is a breaking change! - Renamed `GroupItemStateChangedEvent` to `GroupStateChangedEvent` - Groups issue a `GroupStateUpdateEvent` when the state updates on OH3 (consistent with OH4 behavior) - Groups work now with `ValueUpdateEvent` and `ValueChangedEvent` as expected diff --git a/run/conf_testing/lib/HABAppTests/test_rule/test_rule.py b/run/conf_testing/lib/HABAppTests/test_rule/test_rule.py index 05b9c709..4b124c80 100644 --- a/run/conf_testing/lib/HABAppTests/test_rule/test_rule.py +++ b/run/conf_testing/lib/HABAppTests/test_rule/test_rule.py @@ -1,4 +1,5 @@ import logging +from pathlib import Path from typing import Dict, Callable from typing import List @@ -179,8 +180,11 @@ def __run_tests(self) -> List[TestResult]: TestResult(c_name, tc.name, f'{i + 1:{width}d}/{count}') for i, tc in enumerate(self._tests.values()) ] + module_of_class = Path(self.__class__.__module__) + relative_path = module_of_class.relative_to(HABApp.CONFIG.directories.rules) + log.info('') - log.info(f'Running {count} tests for {c_name}') + log.info(f'Running {count} tests for {c_name} (from "{relative_path}")') for res, tc in zip(results, self._tests.values()): if self.config.skip_on_failure and self.__worst_result >= TestResultStatus.FAILED: diff --git a/run/conf_testing/rules/habapp/test_rule_context.py b/run/conf_testing/rules/habapp/test_rule_context.py new file mode 100644 index 00000000..795a7e2f --- /dev/null +++ b/run/conf_testing/rules/habapp/test_rule_context.py @@ -0,0 +1,38 @@ +from time import sleep + +from HABApp import Rule +from HABApp.core.internals import get_current_context +from HABAppTests import TestBaseRule + + +class OtherRule(Rule): + + def check_context(self): + assert get_current_context() is self._habapp_ctx + + +other_rule = OtherRule() + + +def func_no_context(target_context): + assert get_current_context() is target_context + + +class TestContext(TestBaseRule): + + def __init__(self): + super().__init__() + self.add_test('TestGetContext', self.test_get_context) + self.add_test('TestSchedulerContext', self.test_scheduler_context) + + def test_get_context(self): + assert get_current_context() is self._habapp_ctx + other_rule.check_context() + + def test_scheduler_context(self): + self.run.soon(func_no_context, self._habapp_ctx) + self.run.soon(other_rule.check_context) + sleep(0.1) + + +TestContext() diff --git a/src/HABApp/__version__.py b/src/HABApp/__version__.py index 61d06957..771d5726 100644 --- a/src/HABApp/__version__.py +++ b/src/HABApp/__version__.py @@ -1,4 +1,4 @@ # Version scheme: # X.X[.X] or X.X[.X].DEV-X -__version__ = '1.1.0' +__version__ = '1.1.1' diff --git a/src/HABApp/core/internals/wrapped_function/wrapped_thread.py b/src/HABApp/core/internals/wrapped_function/wrapped_thread.py index 2e669465..666278b9 100644 --- a/src/HABApp/core/internals/wrapped_function/wrapped_thread.py +++ b/src/HABApp/core/internals/wrapped_function/wrapped_thread.py @@ -10,7 +10,7 @@ from typing import Optional from HABApp.core.const import loop -from HABApp.core.internals import HINT_CONTEXT_OBJ +from HABApp.core.internals import HINT_CONTEXT_OBJ, ContextProvidingObj from .base import WrappedFunctionBase, default_logger POOL: Optional[ThreadPoolExecutor] = None @@ -48,9 +48,10 @@ async def run_in_thread_pool(func: Callable): HINT_FUNC_SYNC = Callable[..., Any] -class PoolFunc: - def __init__(self, parent: 'WrappedThreadFunction', - func_obj: HINT_FUNC_SYNC, func_args: Tuple[Any, ...], func_kwargs: Dict[str, Any]): +class PoolFunc(ContextProvidingObj): + def __init__(self, parent: 'WrappedThreadFunction', func_obj: HINT_FUNC_SYNC, func_args: Tuple[Any, ...], + func_kwargs: Dict[str, Any], context: Optional[HINT_CONTEXT_OBJ] = None, **kwargs): + super().__init__(context=context, **kwargs) self.parent: Final = parent self.func_obj: Final = func_obj self.func_args: Final = func_args @@ -133,4 +134,6 @@ def __init__(self, func: HINT_FUNC_SYNC, self.warn_too_long: bool = warn_too_long def run(self, *args, **kwargs): - POOL.submit(PoolFunc(self, self.func, args, kwargs).run) + # we need to copy the context, so it's available when the function is run + pool_func = PoolFunc(self, self.func, args, kwargs, context=self._habapp_ctx) + POOL.submit(pool_func.run) diff --git a/src/HABApp/rule/rule.py b/src/HABApp/rule/rule.py index 38fb02dc..289ead2d 100644 --- a/src/HABApp/rule/rule.py +++ b/src/HABApp/rule/rule.py @@ -42,6 +42,7 @@ def send_warnings_to_log(message, category, filename, lineno, file=None, line=No class Rule(ContextProvidingObj): + def __init__(self): super().__init__(context=HABApp.rule_ctx.HABAppRuleContext(self))