Skip to content

Commit

Permalink
tc oh scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Oct 21, 2024
1 parent e8d1b54 commit 400d1dd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
13 changes: 13 additions & 0 deletions run/conf_testing/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ openhab:
general:
listen_only: false # If True HABApp will not change anything on the openHAB instance.
wait_for_openhab: true # If True HABApp will wait for items from the openHAB instance before loading any rules on startup


habapp:
debug:
watch event loop:
enabled: True
reset every: 10
timeout: 25

periodic traceback:
enabled: True
delay: 20
interval: 600
7 changes: 2 additions & 5 deletions run/conf_testing/rules/habapp/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from time import monotonic, sleep

from HABAppTests import TestBaseRule, get_random_name
Expand All @@ -8,9 +7,6 @@
from HABApp.core.items import Item


log = logging.getLogger('HABApp.TestParameterFiles')


class TestSchedulerCallLive(Rule):
"""This rule is testing the Scheduler implementation"""

Expand Down Expand Up @@ -47,7 +43,7 @@ def __init__(self) -> None:

def test_scheduler_every(self) -> None:

executions = 10
self.item_states.clear()
calls = []

def called() -> None:
Expand All @@ -56,6 +52,7 @@ def called() -> None:
job = self.run.every(None, 0.5, called)
job.to_item(self.item)

executions = 10
try:
started = monotonic()
while monotonic() - started < executions * 0.6 + 1:
Expand Down
37 changes: 37 additions & 0 deletions run/conf_testing/rules/openhab/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from datetime import timedelta
from time import sleep

from HABAppTests import OpenhabTmpItem, TestBaseRule

from HABApp.core.events import ValueUpdateEventFilter
from HABApp.core.items.base_valueitem import datetime
from HABApp.openhab.items import DatetimeItem


class TestSchedulerOhInteraction(TestBaseRule):
def __init__(self) -> None:
super().__init__()

self.add_test('Test scheduler oh_item', self.test_scheduler_every)

@OpenhabTmpItem.create('DateTime', arg_name='tmp_item')
def test_scheduler_every(self, tmp_item: OpenhabTmpItem) -> None:

item_states = []
item = DatetimeItem.get_item(tmp_item.name)
listener = item.listen_event(lambda x: item_states.append(x), ValueUpdateEventFilter())

next_hour = datetime.now().replace(second=0, microsecond=0) + timedelta(hours=1)

try:
job = self.run.once(next_hour, lambda: 1/0)
job.to_item(item)
job.cancel()
sleep(0.2)
finally:
listener.cancel()

assert item_states == [next_hour, None]


TestSchedulerOhInteraction()
8 changes: 5 additions & 3 deletions src/HABApp/config/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,27 @@ def setup_debug() -> None:
@log_exception
async def dump_traceback_task(delay: int, interval: int) -> None:

TRACEBACK_FILE.write('Dumping traceback:\n')
await sleep(0.2)
TRACEBACK_FILE.write('Dumping traceback\n')
TRACEBACK_FILE.write(f'Start: {datetime.now()}\n')
TRACEBACK_FILE.write(f'Delay: {delay:d}s Interval: {interval:d}s\n')
TRACEBACK_FILE.write(f'\n{"-" * 80}\n')
TRACEBACK_FILE.flush()

await sleep(delay)

while True:
TRACEBACK_FILE.write(f'\n{"-" * 80}\n')
TRACEBACK_FILE.write(f'{datetime.now()}\n\n')
faulthandler.dump_traceback(TRACEBACK_FILE, all_threads=True)
TRACEBACK_FILE.write(f'\n{"-" * 80}\n')

await sleep(interval)


@log_exception
async def watch_event_loop_task(sleep_secs: int, timeout_secs: int) -> None:

TRACEBACK_FILE.write(f'Watching event loop\nReset: {sleep_secs:d}s Timeout: {timeout_secs:d}s\n')
TRACEBACK_FILE.write(f'Watching event loop\nReset: {sleep_secs:d}s Timeout: {timeout_secs:d}s\n\n')
TRACEBACK_FILE.flush()

while True:
Expand Down

0 comments on commit 400d1dd

Please sign in to comment.