asyncio.timeout + sleep poll loops become async_wait_until (same ceilings) or a monotonic-deadline poll. test_session_cron's 0.3 s "the firer polls once during the drain" nap now waits on an Event set when session_cron.operation answers a status call while runner._draining is true, which proves the claim instead of assuming it. test_parked_profile_served_set waits for the released turn thread and its session task instead of napping 50 ms before closing the DB. Files: - tests/gateway/test_claim_reopen_off_loop.py - tests/gateway/test_internal_wake_never_interrupts.py - tests/gateway/test_managed_turn_shutdown_drain.py - tests/gateway/test_managed_worker_frozen_config.py - tests/gateway/test_managed_worker_turn_flags.py - tests/gateway/test_msgraph_authority.py - tests/gateway/test_parked_profile_served_set.py - tests/gateway/test_session_cron.py - tests/gateway/test_session_hosted_daemon.py - tests/gateway/test_session_policy_verbs.py - tests/gateway/test_used_delete_restart.py Waivers (1): - test_claim_reopen_off_loop.py ticker: its fixed 20 ms period IS the event-loop stall probe (asserts max gap < 0.5 s); there is no condition to wait for. (cherry picked from commit 41e9a0fe8ccf3d8fbd7a85b2798143b263409267)
98 lines
4.9 KiB
Python
98 lines
4.9 KiB
Python
"""Graph boundary uses the canonical internal queue, not a background ACK."""
|
|
import asyncio
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def test_graph_boundary_commits_only_valid_notifications(tmp_path):
|
|
result = subprocess.run([sys.executable, __file__], env=dict(os.environ, HERMES_HOME=str(tmp_path)),
|
|
capture_output=True, text=True, timeout=90, check=False)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
print((tmp_path / 'graph-receipt.json').read_text())
|
|
|
|
|
|
async def probe(peer):
|
|
import aiohttp
|
|
import ipaddress
|
|
from gateway.config import Platform, PlatformConfig
|
|
from gateway.platforms.msgraph_webhook import MSGraphWebhookAdapter
|
|
from gateway.run import GatewayRunner
|
|
from gateway.session_authority import initialize_session_authority
|
|
from gateway.session_envelope import restore_native
|
|
from hermes_state_runtime import list_session_admissions
|
|
from tests.waiting import async_wait_until
|
|
runner = GatewayRunner()
|
|
authority = await initialize_session_authority(runner, profile_id='default', instance_id='graph-test')
|
|
adapter = MSGraphWebhookAdapter(PlatformConfig(enabled=True, extra={
|
|
'host': '127.0.0.1', 'port': 0, 'client_state': 'owned-state',
|
|
'accepted_resources': ['users/owned/messages'], 'prompt': 'BLOCK_FIFO'}))
|
|
runner.adapters[Platform.MSGRAPH_WEBHOOK] = adapter
|
|
runner._wire_adapter_handlers(adapter)
|
|
assert await adapter.connect()
|
|
port = next(iter(adapter._runner.sites))._server.sockets[0].getsockname()[1]
|
|
url = f'http://127.0.0.1:{port}{adapter._webhook_path}'
|
|
notification = {'id': 'graph-1', 'subscriptionId': 'owned-sub',
|
|
'resource': 'users/owned/messages/one', 'clientState': 'owned-state'}
|
|
def rows():
|
|
return [r for sid in authority.sessions for r in list_session_admissions(
|
|
authority.db, session_id=sid, pending_only=False)]
|
|
try:
|
|
async with aiohttp.ClientSession() as client:
|
|
for change, status in [({'clientState': 'wrong'}, 403), ({'resource': 'users/foreign'}, 400)]:
|
|
response = await client.post(url, json={'value': [{**notification, **change}]})
|
|
assert response.status == status and not rows()
|
|
adapter._allowed_source_networks = [ipaddress.ip_network('192.0.2.0/24')]
|
|
denied = await client.post(url, json={'value': [notification]})
|
|
assert denied.status == 403 and not rows()
|
|
adapter._allowed_source_networks = []
|
|
authority.db._execute_write(lambda conn: conn.execute("CREATE TRIGGER fail_graph BEFORE INSERT ON session_admissions BEGIN SELECT RAISE(ABORT, 'owned storage failure'); END"))
|
|
response = await client.post(url, json={'value': [notification]})
|
|
assert response.status == 503, response.status
|
|
assert not rows() and not peer.requests
|
|
authority.db._execute_write(lambda conn: conn.execute('DROP TRIGGER fail_graph'))
|
|
response = await client.post(url, json={'value': [notification]})
|
|
assert response.status == 202 and len(rows()) == 1
|
|
assert restore_native(rows()[0]['payload'], runner).internal is True
|
|
assert await asyncio.to_thread(peer.blocked.wait, 10)
|
|
adapter._seen_receipts.clear()
|
|
duplicate = await client.post(url, json={'value': [notification]})
|
|
assert duplicate.status == 202 and len(rows()) == 1
|
|
peer.release.set()
|
|
await async_wait_until(lambda: rows()[0]['status'] == 'terminal', timeout=25,
|
|
what='the Graph notification turn to settle')
|
|
assert len(peer.requests) == 1
|
|
# A plugin scheduler owns this route exclusively; do not also infer.
|
|
calls = []
|
|
async def scheduler(raw, event):
|
|
calls.append(raw['id'])
|
|
adapter.set_notification_scheduler(scheduler)
|
|
response = await client.post(url, json={'value': [{**notification, 'id': 'plugin-only'}]})
|
|
assert response.status == 202 and calls == ['plugin-only']
|
|
assert len(rows()) == len(peer.requests) == 1
|
|
Path(os.environ['HERMES_HOME'], 'graph-receipt.json').write_text(json.dumps({
|
|
'admissions': len(rows()), 'inferences': len(peer.requests), 'boundary_controls': True,
|
|
'internal': True, 'storage_retry': True, 'scheduler_exclusive': True}))
|
|
finally:
|
|
peer.release.set()
|
|
await adapter.disconnect()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
sys.path.insert(1, str(Path(__file__).parent))
|
|
import test_webhook_authority as harness
|
|
import traceback
|
|
harness.probe = probe
|
|
status = 0
|
|
try:
|
|
harness.main()
|
|
except BaseException:
|
|
traceback.print_exc()
|
|
status = 1
|
|
sys.stdout.flush()
|
|
sys.stderr.flush()
|
|
os._exit(status)
|