Skip to content

The agent that grows with you

test(gateway): loop-heartbeat probes use pairwise and annotate the measured sleeps

Tekniumb7e40d548168
diff --git a/tests/gateway/test_managed_worker_reservation_off_loop.py b/tests/gateway/test_managed_worker_reservation_off_loop.py
index 41dcb599c83..569e78d02d6 100644
--- a/tests/gateway/test_managed_worker_reservation_off_loop.py
+++ b/tests/gateway/test_managed_worker_reservation_off_loop.py
@@ -2,6 +2,7 @@
writer held by another process (up to _WRITE_PATIENCE_S) must not freeze every other session,
timer, socket and Stop on the loop. Real child, real SQLite, a real competing writer."""
import asyncio
+from itertools import pairwise
import sqlite3
import subprocess
import sys
@@ -12,6 +13,7 @@ from types import SimpleNamespace
import pytest
from gateway.session_contract import Principal, SessionRef
+from tests.waiting import wait_until
CHILD = """import json, os, sys, time, psutil
me = psutil.Process()
@@ -63,12 +65,11 @@ def test_reservation_and_loss_writes_never_block_the_loop(tmp_path, monkeypatch,
def competing_writer():
if held_during == 'lose':
- while not bootstrapped.exists():
- time.sleep(.01)
+ wait_until(bootstrapped.exists, timeout=30, what='the worker to receive its bootstrap')
rival = sqlite3.connect(tmp_path / 'state.db', isolation_level=None)
rival.execute('BEGIN IMMEDIATE')
held.set()
- time.sleep(HOLD_S)
+ time.sleep(HOLD_S) # health: allow HX013 -- the held writer's duration IS the behaviour under test
rival.execute('ROLLBACK')
rival.close()
@@ -84,7 +85,7 @@ def test_reservation_and_loss_writes_never_block_the_loop(tmp_path, monkeypatch,
async def ticker():
while True:
ticks.append(time.monotonic())
- await asyncio.sleep(.05)
+ await asyncio.sleep(.05) # health: allow HX013 -- the loop heartbeat probe; its gaps are the measurement
beat = asyncio.create_task(ticker())
try:
async with asyncio.timeout(60):
@@ -94,7 +95,7 @@ def test_reservation_and_loss_writes_never_block_the_loop(tmp_path, monkeypatch,
finally:
beat.cancel()
await asyncio.to_thread(writer.join, 10)
- return row, max(b - a for a, b in zip(ticks, ticks[1:]))
+ return row, max(b - a for a, b in pairwise(ticks))
with db:
row, gap = asyncio.run(scenario())
status = db._read_one('SELECT status FROM session_admissions WHERE admission_id=?', (row['admission_id'],))[0]
diff --git a/tests/gateway/test_session_kanban_run_off_loop.py b/tests/gateway/test_session_kanban_run_off_loop.py
index 3c93600b13d..44614233174 100644
--- a/tests/gateway/test_session_kanban_run_off_loop.py
+++ b/tests/gateway/test_session_kanban_run_off_loop.py
@@ -2,6 +2,7 @@
Kanban DB is the cross-profile dispatch bus (busy_timeout up to 120 s), so a dispatcher holding
its writer must not freeze every session on the loop. Real Kanban SQLite, a real held writer."""
import asyncio
+from itertools import pairwise
from contextlib import closing
import sqlite3
import threading
@@ -54,7 +55,7 @@ def test_run_task_kanban_write_never_blocks_the_loop(tmp_path, monkeypatch):
rival = sqlite3.connect(board_db, isolation_level=None)
rival.execute('BEGIN IMMEDIATE')
held.set()
- time.sleep(HOLD_S)
+ time.sleep(HOLD_S) # health: allow HX013 -- the held writer's duration IS the behaviour under test
rival.execute('ROLLBACK')
rival.close()
monkeypatch.setattr('gateway.session_kanban.asdict', lambda receipt: {})
@@ -68,7 +69,7 @@ def test_run_task_kanban_write_never_blocks_the_loop(tmp_path, monkeypatch):
async def ticker():
while True:
ticks.append(time.monotonic())
- await asyncio.sleep(.05)
+ await asyncio.sleep(.05) # health: allow HX013 -- the loop heartbeat probe; its gaps are the measurement
beat = asyncio.create_task(ticker())
try:
async with asyncio.timeout(30):
@@ -77,7 +78,7 @@ def test_run_task_kanban_write_never_blocks_the_loop(tmp_path, monkeypatch):
beat.cancel()
ticks.append(time.monotonic()) # a loop blocked from the first tick still yields one gap
await asyncio.to_thread(writer.join, 10)
- return max(b - a for a, b in zip(ticks, ticks[1:]))
+ return max(b - a for a, b in pairwise(ticks))
gap = asyncio.run(scenario())
assert calls == ['resume', 'submit']
with closing(connect(board='owned')) as conn: