1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-02 03:12:20 +01:00

[script] Fix parallel mode scripts with delays cancelling each other (#10324)

This commit is contained in:
J. Nick Koston
2025-08-24 22:49:52 +02:00
committed by Jesse Hills
parent fcc3c8e1b6
commit d560831d79
5 changed files with 147 additions and 9 deletions

View File

@@ -0,0 +1,45 @@
esphome:
name: test-parallel-delays
host:
logger:
level: DEBUG
api:
actions:
- action: test_parallel_delays
then:
# Start three parallel script instances with small delays between starts
- globals.set:
id: instance_counter
value: '1'
- script.execute: parallel_delay_script
- delay: 10ms
- globals.set:
id: instance_counter
value: '2'
- script.execute: parallel_delay_script
- delay: 10ms
- globals.set:
id: instance_counter
value: '3'
- script.execute: parallel_delay_script
globals:
- id: instance_counter
type: int
initial_value: '0'
script:
- id: parallel_delay_script
mode: parallel
then:
- lambda: !lambda |-
int instance = id(instance_counter);
ESP_LOGI("TEST", "Parallel script instance %d started", instance);
- delay: 1s
- lambda: !lambda |-
static int completed_counter = 0;
completed_counter++;
ESP_LOGI("TEST", "Parallel script instance %d completed after delay", completed_counter);

View File

@@ -89,3 +89,73 @@ async def test_delay_action_cancellation(
assert 0.4 < time_from_second_start < 0.6, (
f"Delay completed {time_from_second_start:.3f}s after second start, expected ~0.5s"
)
@pytest.mark.asyncio
async def test_parallel_script_delays(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""Test that parallel scripts with delays don't interfere with each other."""
loop = asyncio.get_running_loop()
# Track script executions
script_starts: list[float] = []
script_ends: list[float] = []
# Patterns to match
start_pattern = re.compile(r"Parallel script instance \d+ started")
end_pattern = re.compile(r"Parallel script instance \d+ completed after delay")
# Future to track when all scripts have completed
all_scripts_completed = loop.create_future()
def check_output(line: str) -> None:
"""Check log output for parallel script messages."""
current_time = loop.time()
if start_pattern.search(line):
script_starts.append(current_time)
if end_pattern.search(line):
script_ends.append(current_time)
# Check if we have all 3 completions
if len(script_ends) == 3 and not all_scripts_completed.done():
all_scripts_completed.set_result(True)
async with (
run_compiled(yaml_config, line_callback=check_output),
api_client_connected() as client,
):
# Get services
entities, services = await client.list_entities_services()
# Find our test service
test_service = next(
(s for s in services if s.name == "test_parallel_delays"), None
)
assert test_service is not None, "test_parallel_delays service not found"
# Execute the test - this will start 3 parallel scripts with 1 second delays
client.execute_service(test_service, {})
# Wait for all scripts to complete (should take ~1 second, not 3)
await asyncio.wait_for(all_scripts_completed, timeout=2.0)
# Verify we had 3 starts and 3 ends
assert len(script_starts) == 3, (
f"Expected 3 script starts, got {len(script_starts)}"
)
assert len(script_ends) == 3, f"Expected 3 script ends, got {len(script_ends)}"
# Verify they ran in parallel - all should complete within ~1.5 seconds
first_start = min(script_starts)
last_end = max(script_ends)
total_time = last_end - first_start
# If running in parallel, total time should be close to 1 second
# If they were interfering (running sequentially), it would take 3+ seconds
assert total_time < 1.5, (
f"Parallel scripts took {total_time:.2f}s total, should be ~1s if running in parallel"
)