Files
hermes-agent/tests/gateway/test_settlement_off_loop.py
Teknium a98adf3d61 test(hx013): wait for conditions, not the clock, in gateway session tests
- test_api_responses_stream_retry, test_managed_worker_stop_before_hello: poll
  the state the old loop polled with async_wait_until.
- test_responses_idem_replay_record: the retry has joined once a second API
  observer is registered on the in-flight admission (was sleep(.2)).
- test_managed_worker_reader_thread: the reader is parked once its thread's
  stack is inside read_frame (was sleep(.3)).
- test_session_authority_cancel_settlement: wait for the drain to log two
  deferred claims behind the held writer (was sleep(.6)); assertions unchanged.
- test_settlement_off_loop: other work runs once the off-loop write signals it
  started (was sleep(.01)).
- test_session_bot_retry: wait until no receipt task, mailbox operation or
  drain is running before asserting no extra admission; the peer waiter is
  observed following the held retry admission; the mailbox-lock stall test
  waits for the write to park in flock off the loop (holder release bound 10 s,
  released by the test).
- test_gateway_queued_unknown: the fake worker loss waits for the view to own
  its admission, and (yield_between) for session.info to be rendered.
- test_runtime_bootstrap (Windows-only): the stalled handler blocks on an Event
  set at teardown; the injected client signals reaching the pipe
  (CreateFile/WaitNamedPipe) and the idle accept cycle is observed via _accept.

Waiver (1):
- tests/gateway/test_runtime_bootstrap.py:148 time.sleep(2.5): the handler must
  outlast the 2 s per-peer I/O budget; the delay is the behaviour under test.

(cherry picked from commit ed76f552c94c115d8c26649778b2da2eedbef929)
2026-10-11 15:00:29 -07:00

54 lines
2.5 KiB
Python

"""Settlement's write runs off the owner loop; completion, idle info and waiters keep their order."""
import asyncio
import threading
import pytest
from gateway.session_contract import Submission
from hermes_state_runtime import get_session_admission
from tests.gateway.test_session_authority_cancel_settlement import _authority, ACTOR, REF
from tests.waiting import async_wait_until
@pytest.mark.asyncio
async def test_settlement_write_leaves_the_loop_free_and_publishes_in_order(tmp_path, monkeypatch):
from gateway import session_results
db, authority = _authority(tmp_path, monkeypatch)
monkeypatch.setattr(authority, '_schedule', lambda ref: None)
loop_ran, writing, events, served_during_write = threading.Event(), threading.Event(), [], []
authority.sessions['s'].event_stream.observers.add(
lambda frame: events.append((frame['params']['type'], frame['params']['payload'].get('running'))))
async def execute(owner, ref, row):
owner.pending_results[row['admission_id']] = {'result': {'final_response': 'done'}, 'usage': {}}
return 'done'
monkeypatch.setattr('gateway.session_finite.execute_finite_admission', execute)
settle = session_results.settle_session_input
def slow_settle(*args, **kwargs):
# A contended writer: the owner loop must keep serving other work meanwhile.
writing.set()
served_during_write.append(loop_ran.wait(2))
return settle(*args, **kwargs)
monkeypatch.setattr(session_results, 'settle_session_input', slow_settle)
async def other_session_work():
# Runs on the owner loop only once the settlement write is in flight off it.
await async_wait_until(writing.is_set, timeout=10, what='the settlement write to start')
loop_ran.set()
with db:
receipt = await authority.submit(ACTOR, Submission('work', REF, {'text': 'work'}, 'queue'))
waiter = authority.waiters.setdefault(receipt.admission_id, asyncio.get_running_loop().create_future())
def resolved(_):
events.append(('waiter', get_session_admission(db, admission_id=receipt.admission_id)['status']))
waiter.add_done_callback(resolved)
events.clear()
other = asyncio.create_task(other_session_work())
await asyncio.wait_for(authority._drain(REF), 5)
await other
await asyncio.sleep(0)
assert served_during_write == [True], 'the settlement write blocked the owner loop'
assert events[-3:] == [('message.complete', None), ('session.info', False), ('waiter', 'terminal')], events