From 5820b864808c7be2f0abaa7b46cdd6fd6fa92db0 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 20 Jul 2025 16:37:00 -0600 Subject: [PATCH] Disabling all perpetual signals during testing, except switch_worker. --- wrolpi/api_utils.py | 9 ++++++++- wrolpi/switches.py | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/wrolpi/api_utils.py b/wrolpi/api_utils.py index f6623d8a..78aceec1 100644 --- a/wrolpi/api_utils.py +++ b/wrolpi/api_utils.py @@ -1,6 +1,8 @@ import asyncio import json import logging +import multiprocessing +import sys from asyncio import CancelledError from datetime import datetime, timezone, date from decimal import Decimal @@ -13,6 +15,7 @@ from sanic import response, HTTPResponse, Request, Sanic, SanicException from wrolpi.common import Base, get_media_directory, logger, LOGGING_CONFIG, TRACE_LEVEL from wrolpi.errors import APIError +from wrolpi.vars import PYTEST logger = logger.getChild(__name__) @@ -141,11 +144,15 @@ async def start_perpetual_tasks(app: Sanic): logger.debug('start_perpetual_tasks completed') -def perpetual_signal(event: str = None, sleep: int | float = 1): +def perpetual_signal(event: str = None, sleep: int | float = 1, run_while_testing: bool = False): """Use Sanic signals to continually call the wrapped function. The wrapped function will continually be called, even if it has errors. If the function is long-running, it will only be called again after it has finished.""" def wrapper(func: callable): + if PYTEST and not run_while_testing: + # Do not run perpetual signal worker while testing, unless explicitly required. + return func + # Create a Sanic "signal" for the provided function. event_ = event or f'wrolpi.perpetual.{func.__name__}' diff --git a/wrolpi/switches.py b/wrolpi/switches.py index 908ca4f6..e9d438ff 100644 --- a/wrolpi/switches.py +++ b/wrolpi/switches.py @@ -85,7 +85,7 @@ def register_switch_handler(switch_name: str): DEBUG_LOGGED = False -@perpetual_signal(sleep=0.1) +@perpetual_signal(sleep=0.1, run_while_testing=True) async def switch_worker(): """Watches `api_app.shared_ctx.switches` for new activated switches, handles each one at a time.""" global DEBUG_LOGGED