Files
NODEDC_MISSION_CORE/apps/vps-host/qualify_receipts.py
T

83 lines
2.9 KiB
Python

"""Two real loopback streams, one synthetic B2, no physical commands or state."""
import asyncio
import json
import struct
import tempfile
import time
import uuid
from datetime import UTC, datetime
from pathlib import Path
from ndc_vps import VERSION
from qualify_faults import Harness
async def qualify():
result = {
"session_id": uuid.uuid4().hex,
"version": VERSION,
"started_utc": datetime.now(UTC).isoformat(),
"monotonic": time.monotonic(),
"physical_devices_used": False,
"production_state_used": False,
}
with tempfile.TemporaryDirectory(prefix="ndc-vps-receipts-") as name:
harness = Harness(Path(name))
alternate = None
await harness.start()
try:
await harness.connect()
alternate = harness.peer
harness.peer = None # Keep the first monitoring stream open.
await harness.connect()
body = harness.body()
await harness.submit(body)
wire = await harness.frame() # Most recently active stream wins dispatch.
async def reply(code):
data = struct.pack("<I", int(time.time())) + bytes((code,)) + wire[5:9]
alternate[1].write(b"[\x01\x09\x05\0" + data + bytes([sum(data) % 256]) + b"]")
await alternate[1].drain()
assert await asyncio.wait_for(alternate[0].readexactly(4), 2) == b"{\0\x01}"
await reply(6)
accepted = await harness.get(body)
assert accepted["state"] == "dispatched" and accepted["device_code"] == 6
assert accepted["reply_via_alternate_session"] is True
await reply(0)
confirmed = await harness.get(body)
assert confirmed["state"] == "device_confirmed"
assert confirmed["device_accepted_at"] == accepted["device_accepted_at"]
await reply(5) # A duplicate cannot downgrade a committed result.
assert await harness.get(body) == confirmed
await harness.no_frame()
alternate[1].transport.abort()
alternate = None
await harness.restart()
assert await harness.get(body) == confirmed
with harness.store.db() as db:
assert db.execute("SELECT count(*) FROM commands").fetchone()[0] == 1
result.update(
passed=True,
sends=len(harness.frames),
alternate_stream_confirmed=True,
ack_not_final=True,
result_survived_sigkill=True,
elapsed_seconds=time.monotonic() - result["monotonic"],
)
return result
finally:
if alternate:
alternate[1].transport.abort()
await harness.stop()
async def main():
async with asyncio.timeout(20):
print(json.dumps(await qualify(), indent=2))
if __name__ == "__main__":
asyncio.run(main())