"""Exercise the real Dummy Boy desktop window on localhost:17959. Start the host with --debug test-cartridges/dummy-boy --port 17959 first. This finite smoke observes progress; its timeout is not a realtime guarantee. """ import json import socket import time def run(): with socket.create_connection(("127.0.0.1", 17959), timeout=10) as connection: connection.settimeout(10) with connection.makefile("rb") as incoming: def send(kind): connection.sendall((json.dumps({"type": kind}) + "\n").encode()) def state(): send("getState") deadline = time.monotonic() + 10 while time.monotonic() < deadline: line = incoming.readline() assert line, "Desktop disconnected" message = json.loads(line) assert message.get("event") != "fault", message if message.get("type") == "getState": return message raise TimeoutError("Desktop did not answer getState") def await_progress(previous): deadline = time.monotonic() + 5 current = state() while current["frame_index"] == previous and time.monotonic() < deadline: time.sleep(0.02) current = state() return current handshake = json.loads(incoming.readline()) assert handshake["type"] == "handshake", handshake assert handshake["cartridge"]["app_id"] == 2, handshake print(json.dumps(handshake), flush=True) send("start") time.sleep(0.5) first = state() assert first["app_id"] == 2, first for _ in range(32): send("pause") time.sleep(0.025) paused = state() time.sleep(0.025) still = state() assert (paused["pc"], paused["frame_index"]) == (still["pc"], still["frame_index"]) send("stepFrame") stepped = await_progress(paused["frame_index"]) assert stepped["frame_index"] == paused["frame_index"] + 1, (paused, stepped) send("resume") resumed = await_progress(stepped["frame_index"]) assert resumed["frame_index"] > stepped["frame_index"], (stepped, resumed) print(json.dumps({ "result": "pass", "cycles": 32, "first_frame": first["frame_index"], "last_frame": resumed["frame_index"], "last_pc": resumed["pc"], }), flush=True) send("resume") if __name__ == "__main__": run()