132 lines
4.6 KiB
Python
132 lines
4.6 KiB
Python
"""Bounded release self-check on isolated synthetic state; no physical tracker."""
|
|
|
|
import asyncio
|
|
import json
|
|
import struct
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from ndc_vps.commands import Commands
|
|
from ndc_vps.service import Service
|
|
from ndc_vps.store import Store
|
|
|
|
|
|
async def qualify():
|
|
started = time.monotonic()
|
|
with tempfile.TemporaryDirectory(prefix="ndc-vps-runtime-") as directory:
|
|
root = Path(directory)
|
|
(root / "host.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"host_id": "vps_" + "a" * 64,
|
|
"tracker": {"host": "synthetic.example", "port": 9922},
|
|
}
|
|
)
|
|
)
|
|
store = Store(root)
|
|
imei = "123456789012345"
|
|
store.set_devices([{"imei": imei, "instance_id": "c" * 32}])
|
|
body = {
|
|
"id": "a" * 32,
|
|
"imei": imei,
|
|
"kind": "request-position",
|
|
"model": 231,
|
|
"firmware": "0.01",
|
|
"not_after": time.time() + 20,
|
|
}
|
|
commands = Commands(store)
|
|
first = commands.submit(body)
|
|
commands = Commands(store)
|
|
assert commands.get(body["id"])["state"] == "queued"
|
|
assert commands.submit(body) == first
|
|
commands.sessions[imei] = {"new"}
|
|
wire = commands.take(imei, "new")
|
|
assert wire and commands.take(imei, "new") is None
|
|
data = struct.pack("<I", int(time.time())) + b"\0" + wire[5:9]
|
|
reply = b"[\x01\x09\x05\0" + data + bytes([sum(data) % 256]) + b"]"
|
|
commands.sessions[imei].add("second-monitoring-stream")
|
|
commands.receive(imei, reply, "second-monitoring-stream")
|
|
assert commands.submit(body)["state"] == "device_confirmed"
|
|
assert commands.take(imei, "new") is None
|
|
assert Commands(store).get(body["id"])["state"] == "device_confirmed"
|
|
with store.db() as db:
|
|
assert db.execute("SELECT COUNT(*) FROM commands").fetchone()[0] == 1
|
|
plan = db.execute(
|
|
"EXPLAIN QUERY PLAN SELECT id,body FROM commands WHERE imei=? "
|
|
"AND json_extract(body,'$.state') IN ('queued','dispatched') ORDER BY rowid",
|
|
(imei,),
|
|
).fetchall()
|
|
assert any("USING INDEX commands_active" in r[-1] for r in plan)
|
|
effect = {**body, "id": "b" * 32, "kind": "pulse-output", "pin": 4, "seconds": 1}
|
|
commands.submit(effect)
|
|
assert commands.take(imei, "new")
|
|
commands = Commands(store)
|
|
commands.sessions[imei] = {"after-crash"}
|
|
assert commands.submit(effect)["state"] == "uncertain"
|
|
assert commands.take(imei, "after-crash") is None
|
|
late = {**effect, "id": "d" * 32, "not_after": time.time() - 1}
|
|
assert commands.submit(late)["state"] == "expired"
|
|
assert commands.take(imei, "after-crash") is None
|
|
assert commands.wait_delay(imei) is None
|
|
|
|
service = Service(store)
|
|
service.commands.sessions[imei] = {"idle"}
|
|
original = service.commands.take_delivery
|
|
polls = []
|
|
written = asyncio.Event()
|
|
frames = []
|
|
|
|
def take(*args):
|
|
polls.append(time.monotonic())
|
|
return original(*args)
|
|
|
|
service.commands.take_delivery = take
|
|
|
|
class Writer:
|
|
def is_closing(self):
|
|
return False
|
|
|
|
async def drain(self):
|
|
pass
|
|
|
|
class Delivery:
|
|
def ready(self):
|
|
return True
|
|
|
|
def write(self, data, deadline):
|
|
frames.append(data)
|
|
written.set()
|
|
|
|
def abort(self):
|
|
pass
|
|
|
|
task = asyncio.create_task(service.send_commands(imei, Writer(), "idle", Delivery()))
|
|
try:
|
|
await asyncio.sleep(1.1)
|
|
assert len(polls) == 1
|
|
service.commands.submit({**body, "id": "f" * 32})
|
|
service.notify(imei)
|
|
await asyncio.wait_for(written.wait(), 1)
|
|
assert len(frames) == 1
|
|
finally:
|
|
task.cancel()
|
|
await asyncio.gather(task, return_exceptions=True)
|
|
assert not service.wake
|
|
return {
|
|
"ok": True,
|
|
"monotonic_seconds": time.monotonic() - started,
|
|
"queued_read_survived_restart": True,
|
|
"duplicate_post_single_operation": True,
|
|
"dispatched_effect_not_replayed": True,
|
|
"expired_effect_not_dispatched": True,
|
|
"active_query_uses_index": True,
|
|
"idle_checks_over_1_second": 1,
|
|
"new_command_woke_idle_sender": True,
|
|
"physical_devices_used": False,
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(json.dumps(asyncio.run(qualify()), sort_keys=True))
|