96 lines
3.8 KiB
Python
96 lines
3.8 KiB
Python
"""Durable intent versus attempts over real isolated sockets and hard process loss."""
|
|
|
|
import asyncio
|
|
import json
|
|
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-operations-") as name:
|
|
h = Harness(Path(name))
|
|
await h.start()
|
|
try:
|
|
body = h.body()
|
|
body.pop("not_after")
|
|
|
|
async def get():
|
|
status, value = await h.http("GET", "/v1/operations/" + body["id"])
|
|
assert status == 200, value
|
|
return value
|
|
|
|
# Truncated client request admits nothing. Stable ID recovers admission.
|
|
await h.http("POST", "/v1/operations", body, loss="before-body")
|
|
assert (await h.http("GET", "/v1/operations/" + body["id"]))[0] == 400
|
|
assert (await h.http("POST", "/v1/operations", body))[0] == 200
|
|
await h.restart()
|
|
assert (await get())["attempt_count"] == 0
|
|
await h.connect()
|
|
first = await h.frame()
|
|
assert (await get())["state"] == "dispatched"
|
|
# Lose response, hard kill VPS. The user job survives its uncertain attempt.
|
|
await h.restart()
|
|
pending = await get()
|
|
assert pending["state"] == "queued" and pending["attempt_count"] == 1
|
|
await asyncio.sleep(max(0, pending["next_attempt_at"] - time.time()) + 0.1)
|
|
await h.connect()
|
|
second = await h.frame()
|
|
assert first[5:9] != second[5:9]
|
|
await h.reply(first) # Delayed receipt must not finish a newer attempt.
|
|
assert (await get())["state"] == "dispatched"
|
|
await h.reply(second)
|
|
confirmed = await get()
|
|
assert confirmed["state"] == "device_confirmed" and confirmed["attempt_count"] == 2
|
|
await h.no_frame()
|
|
await h.restart()
|
|
assert await get() == confirmed
|
|
assert (await h.http("POST", "/v1/operations", body))[1] == confirmed
|
|
status, history = await h.http("GET", "/v1/operations/" + body["id"] + "/attempts")
|
|
assert status == 200 and len(history["items"]) == 2
|
|
assert [i["state"] for i in history["items"]] == ["device_confirmed", "uncertain"]
|
|
# Cancellation arriving before a delayed POST survives a crash as a tombstone.
|
|
stopped = {**body, "id": uuid.uuid4().hex}
|
|
await h.http(
|
|
"POST", "/v1/operations/" + stopped["id"] + "/cancel", {"kind": stopped["kind"]}
|
|
)
|
|
await h.restart()
|
|
assert (await h.http("POST", "/v1/operations", stopped))[1]["state"] == "stopped"
|
|
with h.store.db() as db:
|
|
assert db.execute("SELECT count(*) FROM operations").fetchone()[0] == 1
|
|
assert db.execute("SELECT count(*) FROM commands").fetchone()[0] == 2
|
|
result.update(
|
|
passed=True,
|
|
attempts=2,
|
|
intents=1,
|
|
hard_restart_recovery=True,
|
|
late_receipt_isolated=True,
|
|
cancelled_post_not_admitted=True,
|
|
elapsed_seconds=time.monotonic() - result["monotonic"],
|
|
)
|
|
return result
|
|
finally:
|
|
await h.stop()
|
|
|
|
|
|
async def main():
|
|
async with asyncio.timeout(25):
|
|
print(json.dumps(await qualify(), indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|