Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
STOP can lose to a command already in flight: the relay must drop what it holds, and the Pi must refuse commands older than its last STOP #435
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/434-stop-can-lose-to-a-command-already-in-fl → main 3 commits fork 2 threads · 2 unresolved
8 files
changed +128
−30
Software/so101/src/command-gate.test.ts +18 −2
| @@ -10,8 +10,8 @@ const SKEW = 5_000_000; | ||
| 10 | 10 | |
| 11 | 11 | describe("parseCommand", () => { |
| 12 | 12 | test("reads the joints, and seq and t only when both are well formed", () => { |
| 13 | expect(parseCommand(frame(3, 1000))).toEqual({ seq: 3, t: 1000, joints: new Map([[1, 2000]]) }); | |
| 14 | expect(parseCommand('{"1": 2000}')).toEqual({ seq: null, t: null, joints: new Map([[1, 2000]]) }); | |
| 13 | expect(parseCommand(frame(3, 1000))).toEqual({ seq: 3, t: 1000, epoch: null, joints: new Map([[1, 2000]]) }); | |
| 14 | expect(parseCommand('{"1": 2000}')).toEqual({ seq: null, t: null, epoch: null, joints: new Map([[1, 2000]]) }); | |
| 15 | 15 | for (const bad of ['{"1":2000,"seq":-1,"t":5}', '{"1":2000,"seq":1.5,"t":5}', '{"1":2000,"seq":1}', '{"1":2000,"seq":1,"t":"now"}', '{"1":2000,"seq":1,"t":0}']) |
| 16 | 16 | expect(parseCommand(bad)).toMatchObject({ seq: null, t: null }); |
| 17 | 17 | expect(parseCommand("[1]")).toBeNull(); |
| @@ -141,3 +141,19 @@ describe("a hold kept in a file", () => { | ||
| 141 | 141 | expect(new CommandGate(f).held).toBeNull(); |
| 142 | 142 | }); |
| 143 | 143 | }); |
| 144 | ||
| 145 | describe("STOP epoch", () => { | |
| 146 | test("a frame stamped before the last STOP is refused whatever its seq and age; a newer or unstamped one is not", () => { | |
| 147 | const gate = new CommandGate(); | |
| 148 | const now = 1_000_000; | |
| 149 | const s = gate.open(new Clock(), false); | |
| 150 | const f = (seq: number, epoch?: number) => JSON.stringify({ 1: 2000, seq, t: now, ...(epoch === undefined ? {} : { epoch }) }); | |
| 151 | expect(s.admit(f(1, 5), now)).toMatchObject({ ok: true }); | |
| 152 | gate.stopped(7); | |
| 153 | gate.stopped(6); // never goes down | |
| 154 | expect(gate.stopEpoch).toBe(7); | |
| 155 | expect(s.admit(f(2, 6), now)).toEqual({ ok: false, drop: "before-stop" }); | |
| 156 | expect(s.admit(f(3, 7), now)).toMatchObject({ ok: true }); | |
| 157 | expect(s.admit(f(4), now)).toMatchObject({ ok: true }); // a sender with no epoch (the LAN leader) is not held to one | |
| 158 | }); | |
| 159 | }); |
Software/so101/src/command-gate.ts +17 −3
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * replayed its seq is not above the last one this stream sent (a duplicate, or out of order) |
| 9 | 9 | * stale it is older than MAX_AGE_MS: it sat in a buffer somewhere and is no longer what the hand meant |
| 10 | 10 | * before-resume it was sent before the latest resume, however it got here |
| 11 | * before-stop its epoch is below the epoch of the last STOP this gate was told of (see below) | |
| 11 | 12 | * unstamped it has no seq and t where they are required (behind the relay; the CLI leader on a LAN sends none) |
| 12 | 13 | * |
| 13 | 14 | * The page's clock is not the server's. Each sender keeps a `Clock`: the smallest gap seen between a frame's `t` and |
| @@ -32,12 +33,14 @@ export interface Held { | ||
| 32 | 33 | at: string; |
| 33 | 34 | } |
| 34 | 35 | |
| 35 | export type Drop = "malformed" | "unstamped" | "replayed" | "held" | "stale" | "before-resume"; | |
| 36 | export type Drop = "malformed" | "unstamped" | "replayed" | "held" | "stale" | "before-resume" | "before-stop"; | |
| 36 | 37 | export type Verdict = { ok: true; joints: Map<number, number> } | { ok: false; drop: Drop }; |
| 37 | 38 | |
| 38 | 39 | export interface Command { |
| 39 | 40 | seq: number | null; |
| 40 | 41 | t: number | null; |
| 42 | /** The relay's STOP epoch when it sent the frame; null from a sender that has none (the CLI leader on a LAN). */ | |
| 43 | epoch: number | null; | |
| 41 | 44 | joints: Map<number, number>; |
| 42 | 45 | } |
| 43 | 46 | |
| @@ -45,9 +48,9 @@ export interface Command { | ||
| 45 | 48 | export function parseCommand(text: string): Command | null { |
| 46 | 49 | const joints = parseFrame(text); |
| 47 | 50 | if (!joints) return null; |
| 48 | const { seq, t } = JSON.parse(text) as { seq?: unknown; t?: unknown }; | |
| 51 | const { seq, t, epoch } = JSON.parse(text) as { seq?: unknown; t?: unknown; epoch?: unknown }; | |
| 49 | 52 | const stamped = Number.isSafeInteger(seq) && (seq as number) >= 0 && typeof t === "number" && Number.isFinite(t) && t > 0; |
| 50 | return { seq: stamped ? (seq as number) : null, t: stamped ? (t as number) : null, joints }; | |
| 53 | return { seq: stamped ? (seq as number) : null, t: stamped ? (t as number) : null, epoch: Number.isSafeInteger(epoch) && (epoch as number) >= 0 ? (epoch as number) : null, joints }; | |
| 51 | 54 | } |
| 52 | 55 | |
| 53 | 56 | /** One sender's clock, seen from here: when a frame it stamped would have arrived had nothing held it up. */ |
| @@ -103,6 +106,11 @@ export class CommandGate { | ||
| 103 | 106 | held: Held | null = null; |
| 104 | 107 | /** When the arm was last resumed, on this clock, ms; frames sent before it never count. */ |
| 105 | 108 | private resumedAt = 0; |
| 109 | /** | |
| 110 | * The newest STOP epoch seen. The relay numbers its STOPs with a rising epoch, carries it on the STOP and stamps it on | |
| 111 | * every frame it forwards; a frame from before that STOP is refused here however late the network delivers it. | |
| 112 | */ | |
| 113 | stopEpoch = 0; | |
| 106 | 114 | private readonly streams = new Set<GateStream>(); |
| 107 | 115 | private readonly file: string | undefined; |
| 108 | 116 | |
| @@ -131,6 +139,11 @@ export class CommandGate { | ||
| 131 | 139 | return true; |
| 132 | 140 | } |
| 133 | 141 | |
| 142 | /** A STOP carried this epoch: frames stamped with a lower one never count. Never goes down. */ | |
| 143 | stopped(epoch: unknown): void { | |
| 144 | if (Number.isSafeInteger(epoch) && (epoch as number) > this.stopEpoch) this.stopEpoch = epoch as number; | |
| 145 | } | |
| 146 | ||
| 134 | 147 | /** Answers whether it was held. Nothing moves until a frame sent after this comes in. Not held, it changes nothing. */ |
| 135 | 148 | resume(now = Date.now()): boolean { |
| 136 | 149 | if (!this.held) return false; |
| @@ -175,6 +188,7 @@ export class GateStream { | ||
| 175 | 188 | admit(text: string, now = Date.now()): Verdict { |
| 176 | 189 | const c = parseCommand(text); |
| 177 | 190 | if (!c) return { ok: false, drop: "malformed" }; |
| 191 | if (c.epoch !== null && c.epoch < this.gate.stopEpoch) return { ok: false, drop: "before-stop" }; | |
| 178 | 192 | if (c.seq === null || c.t === null) { |
| 179 | 193 | if (this.stamped) return { ok: false, drop: "unstamped" }; |
| 180 | 194 | return this.gate.held ? { ok: false, drop: "held" } : { ok: true, joints: c.joints }; |
Software/so101/src/fake-serve.test.ts +3 −1
| @@ -16,7 +16,9 @@ afterEach(() => { | ||
| 16 | 16 | |
| 17 | 17 | /** Start the fake arm with this environment and wait for its "arm on http://host:port" line. */ |
| 18 | 18 | async function start(env: Record<string, string>): Promise<{ host: string; port: number; line: string }> { |
| 19 | child = Bun.spawn(["bun", "run", script], { env: { ...process.env, ...env }, stdout: "pipe", stderr: "inherit" }); | |
| 19 | // Hermetic: a cell's own ARM_SERVER (the real Pi) must never reach the child. | |
| 20 | const inherited = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("ARM_"))); | |
| 21 | child = Bun.spawn(["bun", "run", script], { env: { ...inherited, ...env }, stdout: "pipe", stderr: "inherit" }); | |
| 20 | 22 | const reader = (child.stdout as ReadableStream<Uint8Array>).getReader(); |
| 21 | 23 | let text = ""; |
| 22 | 24 | while (!text.includes("\n")) { |
Software/so101/src/frame-limit.test.ts +4 −2
| @@ -10,6 +10,8 @@ import { FakeBus } from "./fake-bus.ts"; | ||
| 10 | 10 | import { BURST, FLOOD_CLOSE, FLOOD_MS, FrameRate, frameBytes, MAX_FRAME_BYTES, TOO_BIG_CLOSE } from "./frame-limit.ts"; |
| 11 | 11 | import { LOST_CLOSE, parseRemote, startRelay, type RunningRelay } from "./relay.ts"; |
| 12 | 12 | import { startServer, type RunningServer } from "./server.ts"; |
| 13 | /** A frame as the arm sees it, without the STOP epoch the relay stamps on it. */ | |
| 14 | const unstamped = (frame: string) => JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(frame) as object).filter(([key]) => key !== "epoch"))); | |
| 13 | 15 | |
| 14 | 16 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); |
| 15 | 17 | const shut = (s: { close(): Promise<void> } | undefined) => (s ? Promise.race([s.close(), sleep(500)]) : undefined); |
| @@ -179,12 +181,12 @@ describe("the relay's /follow", () => { | ||
| 179 | 181 | const stamp = stamper(); |
| 180 | 182 | arm.ws.send(stamp({ 1: 2050 })); |
| 181 | 183 | await sleep(30); |
| 182 | expect(arm.got).toEqual(['{"1":2050}']); | |
| 184 | expect(arm.got.map(unstamped)).toEqual(['{"1":2050}']); | |
| 183 | 185 | const code = closed(arm.ws); |
| 184 | 186 | arm.ws.send(JSON.stringify({ 1: 2060, seq: 99, t: Date.now(), pad: "x".repeat(MAX_FRAME_BYTES) })); |
| 185 | 187 | expect(await code).toBe(TOO_BIG_CLOSE); |
| 186 | 188 | await sleep(30); |
| 187 | expect(arm.got).toEqual(['{"1":2050}']); | |
| 189 | expect(arm.got.map(unstamped)).toEqual(['{"1":2050}']); | |
| 188 | 190 | expect(arm.ended).toEqual([1000]); // not LOST_CLOSE: the arm server holds rather than parks |
| 189 | 191 | expect(arm.ended).not.toContain(LOST_CLOSE); |
| 190 | 192 | } finally { |
Software/so101/src/relay.test.ts +5 −3
| @@ -13,6 +13,8 @@ import { FakeBus } from "./fake-bus.ts"; | ||
| 13 | 13 | import { OFFLINE_FRAME } from "./offline-frame.ts"; |
| 14 | 14 | import { parseRemote, sameOrigin, startRelay, type Remote, type RunningRelay } from "./relay.ts"; |
| 15 | 15 | import { startServer, type RunningServer } from "./server.ts"; |
| 16 | /** A frame as the arm sees it, without the STOP epoch the relay stamps on it. */ | |
| 17 | const unstamped = (frame: string) => JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(frame) as object).filter(([key]) => key !== "epoch"))); | |
| 16 | 18 | |
| 17 | 19 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); |
| 18 | 20 | // Bun's node:http can keep a server-closed connection from ending (see server.test.ts): cap every close. |
| @@ -425,20 +427,20 @@ describe("relay", () => { | ||
| 425 | 427 | const stamp = stamper(); |
| 426 | 428 | ws.send(stamp({ 1: 2050 })); |
| 427 | 429 | await sleep(30); |
| 428 | expect(got).toEqual(['{"1":2050}']); // the joints alone: the stamps stay here | |
| 430 | expect(got.map(unstamped)).toEqual(['{"1":2050}']); // the joints alone: the stamps stay here | |
| 429 | 431 | watching = false; |
| 430 | 432 | await sleep(100); |
| 431 | 433 | expect(relay.link().mode).toBe("reconnecting"); |
| 432 | 434 | ws.send(stamp({ 1: 2060 })); |
| 433 | 435 | await sleep(30); |
| 434 | expect(got).toEqual(['{"1":2050}']); // the arm server's dead-man sees silence, and holds | |
| 436 | expect(got.map(unstamped)).toEqual(['{"1":2050}']); // the arm server's dead-man sees silence, and holds | |
| 435 | 437 | expect(ws.readyState).toBe(WebSocket.OPEN); |
| 436 | 438 | watching = true; |
| 437 | 439 | await sleep(80); |
| 438 | 440 | expect(relay.link().mode).toBe("live"); |
| 439 | 441 | ws.send(stamp({ 1: 2070 })); |
| 440 | 442 | await sleep(30); |
| 441 | expect(got).toEqual(['{"1":2050}', '{"1":2070}']); | |
| 443 | expect(got.map(unstamped)).toEqual(['{"1":2050}', '{"1":2070}']); | |
| 442 | 444 | ws.close(); |
| 443 | 445 | } finally { |
| 444 | 446 | clearInterval(pump); |
Software/so101/src/relay.ts +26 −8
| @@ -620,17 +620,23 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 620 | 620 | |
| 621 | 621 | if (req.method === "GET" && PAGE.test(url.pathname)) return await page(req, res, await ask(simulation + path, req, undefined, null, false)); |
| 622 | 622 | if (req.method === "GET" && STATIC.test(url.pathname) && acceptsGzip(req.headers)) return await packed(res, await ask(simulation + path, req, undefined, null, false), path); |
| 623 | // Who stopped or cleared the arm is the relay's to say, never the page's; every other command is held to the admin's speed cap. | |
| 624 | const raw = reading || route === "POST /estop" || route === "POST /estop/clear" || route === "POST /stop" || route === "POST /resume" ? undefined : await readBody(req); | |
| 625 | const body = reading ? undefined : !raw ? Buffer.from(JSON.stringify({ by })) : lobby ? capBody(url.pathname, raw, lobby.maxSpeedPercent) : raw; | |
| 626 | const logged = !reading && !LOCAL.test(url.pathname); | |
| 627 | if (logged) machine.note("sent", route, { by }); | |
| 628 | const answered = (status: number) => logged && machine.note("received", route, { status }); | |
| 629 | 623 | if (route === "POST /stop") { |
| 630 | 624 | // Shut before anything is asked of an arm: a frame already on its way finds the gate closed. |
| 631 | 625 | gate.hold(by); |
| 626 | epoch = Math.max(epoch + 1, Date.now()); | |
| 627 | // Whatever the arm-side sockets have accepted but not written is dropped with them, ahead of the STOP. A clean close | |
| 628 | // would flush that queue first, so it is cut. The arm server reads the cut as a lost driver and would park the arm, | |
| 629 | // but it parks only while not held, and the STOP it is sent at once holds it and aborts a park under way; frames | |
terminate() means the Pi sees a lost driver and may begin a park before the STOP HTTP request arrives. The comment says the STOP aborts it, but no test pins that window. Add one, or send the STOP first. |
||
| 630 | // that did get out carry the old epoch, which it refuses once it has the STOP. | |
| 631 | for (const arm of armSides.values()) arm.terminate(); | |
| 632 | 632 | for (const browser of streams.keys()) browser.close(HELD_CLOSE, `stopped by ${by}`); |
| 633 | 633 | } |
| 634 | // Who stopped or cleared the arm is the relay's to say, never the page's; every other command is held to the admin's speed cap. | |
| 635 | const raw = reading || route === "POST /estop" || route === "POST /estop/clear" || route === "POST /stop" || route === "POST /resume" ? undefined : await readBody(req); | |
| 636 | const body = reading ? undefined : !raw ? Buffer.from(JSON.stringify(route === "POST /stop" ? { by, epoch } : { by })) : lobby ? capBody(url.pathname, raw, lobby.maxSpeedPercent) : raw; | |
| 637 | const logged = !reading && !LOCAL.test(url.pathname); | |
| 638 | if (logged) machine.note("sent", route, { by }); | |
| 639 | const answered = (status: number) => logged && machine.note("received", route, { status }); | |
| 634 | 640 | if (route === "POST /resume") return await resume(res, by); |
| 635 | 641 | if (LOCAL.test(url.pathname) || !onArm() || !remote) { |
| 636 | 642 | const answer = await ask(simulation + path, req, body, null, false); |
| @@ -681,6 +687,14 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 681 | 687 | const sendable = (code: number) => (code >= 1000 && code <= 1003) || (code >= 1007 && code <= 1014) || (code >= 3000 && code <= 4999); |
| 682 | 688 | /** Each open /follow stream and whose it is, so a turn that ends can close it. */ |
| 683 | 689 | const streams = new Map<WebSocket, string>(); |
| 690 | /** The arm-side socket of each stream, so a STOP can cut what it has queued but not yet written. */ | |
| 691 | const armSides = new Map<WebSocket, WebSocket>(); | |
| 692 | /** | |
| 693 | * The STOP epoch: rises with every STOP (and never repeats across a restart, being a clock to begin with). It rides on | |
| 694 | * the STOP to the arm server and on every frame forwarded, so the arm server refuses a frame older than its last STOP | |
| 695 | * however the network orders them. | |
| 696 | */ | |
| 697 | let epoch = Date.now(); | |
| 684 | 698 | /** |
| 685 | 699 | * `base` is the arm server's HTTP side, asked where the joints are so the speed cap starts from there. On the real arm, |
| 686 | 700 | * frames are not passed on while the link reconnects: the stream goes quiet, and the arm server's dead-man holds the arm. |
| @@ -688,11 +702,13 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 688 | 702 | const bridge = (browser: WebSocket, arm: WebSocket, base: string, driver: string | null) => { |
| 689 | 703 | const real = base !== simulation; |
| 690 | 704 | streams.set(browser, driver ?? ""); |
| 705 | armSides.set(browser, arm); | |
| 691 | 706 | const clock = clocks.get(driver ?? "") ?? new Clock(); |
| 692 | 707 | clocks.set(driver ?? "", clock); |
| 693 | 708 | const admission = gate.open(clock, true); |
| 694 | 709 | browser.on("close", () => { |
| 695 | 710 | streams.delete(browser); |
| 711 | armSides.delete(browser); | |
| 696 | 712 | admission.close(); |
| 697 | 713 | }); |
| 698 | 714 | arm.on("message", (data, binary) => browser.readyState === WebSocket.OPEN && browser.send(data, { binary })); |
| @@ -719,6 +735,8 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 719 | 735 | /** A frame as the panel shows it: servo id → ticks. */ |
| 720 | 736 | const values = (text: string) => perServo(parseFrame(text) ?? []); |
| 721 | 737 | const rate = new FrameRate(); |
| 738 | /** A frame as it goes to the arm server: carrying the STOP epoch it was sent under. */ | |
| 739 | const stamped = (frame: string) => JSON.stringify({ ...(JSON.parse(frame) as object), epoch }); | |
| 722 | 740 | /** Set when this relay ends the stream for breaking the limits: the arm then holds, as for a stream closed on purpose. */ |
| 723 | 741 | let overLimit = false; |
| 724 | 742 | browser.on("message", (raw) => { |
| @@ -752,7 +770,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 752 | 770 | if (percent >= 100) { |
| 753 | 771 | if (lobby) cap.cap(text, percent); // keeps each joint's last target, so a cap set mid-stream starts from there |
| 754 | 772 | machine.note("sent", "follow frame", { values: values(text) }); |
| 755 | arm.send(data); | |
| 773 | arm.send(stamped(data)); | |
| 756 | 774 | return; |
| 757 | 775 | } |
| 758 | 776 | if (!seeded) seed(); |
| @@ -760,7 +778,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 760 | 778 | if (capped !== text) machine.note("clamp", "speed cap", { percent, asked: values(text), sent: capped === null ? {} : values(capped) }); |
| 761 | 779 | if (capped === null) return; |
| 762 | 780 | machine.note("sent", "follow frame", { values: values(capped) }); |
| 763 | arm.send(capped); | |
| 781 | arm.send(stamped(capped)); | |
| 764 | 782 | }); |
| 765 | 783 | arm.on("close", (code, why) => browser.close(sendable(code) ? code : 1011, why.length ? why.toString() : "the arm server went away")); |
| 766 | 784 | // A browser that closed the stream on purpose leaves the arm holding; one that vanished leaves it parking. |
Software/so101/src/server.ts +1
| @@ -862,6 +862,7 @@ export async function startServer(bus: FeetechBus, options: ServerOptions = {}): | ||
| 862 | 862 | case "POST /stop": { |
| 863 | 863 | const body = await readBody(req); |
| 864 | 864 | // Held first, before anything awaits: a frame already on its way finds the gate shut. |
| 865 | gate.stopped(body.epoch); | |
This line is the Pi half of the fix and no test covers it. Add a server-level test that fails when this line is removed. |
||
| 865 | 866 | if (body.hold !== false && gate.hold(actor(body))) metrics.event("hold", { by: actor(body) }); |
| 866 | 867 | const was = driver; |
| 867 | 868 | if (was === "stream") for (const client of wss.clients) end(client, 1000, "stopped from the viewer"); // its close handler holds the arm |
Software/so101/src/stop.test.ts +54 −11
| @@ -42,16 +42,25 @@ class FakePi { | ||
| 42 | 42 | readonly got: Array<{ at: number; n: number }> = []; |
| 43 | 43 | readonly http: Server; |
| 44 | 44 | port = 0; |
| 45 | stopEpoch = 0; | |
| 45 | 46 | : ReturnType<typeof setInterval> | undefined; |
| 46 | 47 | private readonly timeline: Entry[]; |
| 47 | constructor(timeline: Entry[]) { | |
| 48 | /** A slow link to the relay: each frame is taken this long after it came in; a STOP is not delayed. */ | |
| 49 | private readonly linkMs: number; | |
| 50 | constructor(timeline: Entry[], linkMs = 0) { | |
| 48 | 51 | this.timeline = timeline; |
| 52 | this.linkMs = linkMs; | |
| 49 | 53 | const follow = new WebSocketServer({ noServer: true }); |
| 50 | 54 | const watch = new WebSocketServer({ noServer: true }); |
| 51 | 55 | this.http = createServer((req, res) => { |
| 52 | req.resume(); | |
| 56 | const chunks: Buffer[] = []; | |
| 57 | req.on("data", (c: Buffer) => chunks.push(c)); | |
| 53 | 58 | req.on("end", () => { |
| 54 | 59 | if (req.method === "POST") this.timeline.push({ at: Date.now(), who: "pi", what: `POST ${req.url}` }); |
| 60 | if (req.method === "POST" && req.url === "/stop") { | |
| 61 | const epoch = (JSON.parse(Buffer.concat(chunks).toString() || "{}") as { epoch?: unknown }).epoch; | |
| 62 | if (typeof epoch === "number") this.stopEpoch = Math.max(this.stopEpoch, epoch); | |
| 63 | } | |
| 55 | 64 | res.writeHead(200, { "content-type": "application/json" }); |
| 56 | 65 | res.end(req.url === "/status" ? JSON.stringify(this.status) : "{}"); |
| 57 | 66 | }); |
| @@ -62,9 +71,16 @@ class FakePi { | ||
| 62 | 71 | to.handleUpgrade(req, socket, head, (ws) => { |
| 63 | 72 | if (to !== follow) return; |
| 64 | 73 | ws.on("message", (d) => { |
| 65 | const n = Number(JSON.parse(String(d))["1"]) - 1000; | |
| 66 | this.got.push({ at: Date.now(), n }); | |
| 67 | this.timeline.push({ at: Date.now(), who: "pi", what: `target #${n}`, n }); | |
| 74 | const frame = JSON.parse(String(d)) as { "1": number; epoch?: number }; | |
| 75 | const n = frame["1"] - 1000; | |
| 76 | // Like the real server: a frame stamped before the last STOP it heard of is refused, however late it comes. | |
| 77 | const take = () => { | |
| 78 | if (typeof frame.epoch === "number" && frame.epoch < this.stopEpoch) return void this.timeline.push({ at: Date.now(), who: "pi", what: `refused #${n} (before STOP)`, n }); | |
| 79 | this.got.push({ at: Date.now(), n }); | |
| 80 | this.timeline.push({ at: Date.now(), who: "pi", what: `target #${n}`, n }); | |
| 81 | }; | |
| 82 | if (this.linkMs) setTimeout(take, this.linkMs); | |
| 83 | else take(); | |
| 68 | 84 | }); |
| 69 | 85 | }); |
| 70 | 86 | }); |
| @@ -167,9 +183,9 @@ describe("STOP through the relay, to a Pi that holds nothing itself", () => { | ||
| 167 | 183 | pi?.close(); |
| 168 | 184 | relay = lobby = undefined; |
| 169 | 185 | }); |
| 170 | const start = async () => { | |
| 186 | const start = async (linkMs = 0) => { | |
| 171 | 187 | timeline = []; |
| 172 | pi = new FakePi(timeline); | |
| 188 | pi = new FakePi(timeline, linkMs); | |
| 173 | 189 | const piPort = await pi.listen(); |
| 174 | 190 | lobby = new Lobby({ identityKey: KEY }); |
| 175 | 191 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, remote: parseRemote({ ARM_SERVER: `ws://127.0.0.1:${piPort}` })!, link: FAST, lobby }); |
| @@ -213,10 +229,9 @@ describe("STOP through the relay, to a Pi that holds nothing itself", () => { | ||
| 213 | 229 | print("(b) replay: 100 commands while held, STOP, resume", timeline); |
| 214 | 230 | }); |
| 215 | 231 | |
| 216 | test("(a) any interleaving of drag, slider, STOP, resume, disconnect and turn change: nothing reaches the Pi between a STOP and a resume plus a new command", async () => { | |
| 217 | const SEEDS = 8, STEPS = 40; | |
| 232 | const interleavings = async (linkMs: number, SEEDS: number, STEPS: number) => { | |
| 218 | 233 | for (let seed = 1; seed <= SEEDS; seed++) { |
| 219 | const port = await start(); | |
| 234 | const port = await start(linkMs); | |
| 220 | 235 | const random = mulberry32(seed); |
| 221 | 236 | const pages = [new Page("alice", person("alice"), () => port, timeline), new Page("bob", person("bob"), () => port, timeline)]; |
| 222 | 237 | lobby!.turns.join({ id: "u-alice", name: "alice", owner: false }); |
| @@ -297,7 +312,11 @@ describe("STOP through the relay, to a Pi that holds nothing itself", () => { | ||
| 297 | 312 | pi.close(); |
| 298 | 313 | relay = lobby = undefined; |
| 299 | 314 | } |
| 300 | }, 120_000); | |
| 315 | }; | |
| 316 | ||
| 317 | test("(a) any interleaving of drag, slider, STOP, resume, disconnect and turn change: nothing reaches the Pi between a STOP and a resume plus a new command", () => interleavings(0, 8, 40), 120_000); | |
| 318 | ||
| 319 | test("(c) the same on a slow Pi link (each write lands 50 ms late): a frame in flight when STOP is pressed is refused by the Pi", () => interleavings(50, 4, 30), 120_000); | |
| 301 | 320 | }); |
| 302 | 321 | |
| 303 | 322 | describe("STOP on the arm server itself", () => { |
| @@ -361,6 +380,30 @@ describe("STOP on the arm server itself", () => { | ||
| 361 | 380 | await sleep(50); |
| 362 | 381 | }); |
| 363 | 382 | |
| 383 | test("a STOP carrying an epoch makes the server refuse any frame stamped before it, even after a resume: frames from the relay's queue never move the arm", async () => { | |
| 384 | const N = Date.now() + 1_000_000; | |
| 385 | const first = await stream(); | |
| 386 | first.send(JSON.stringify({ 1: 2100, seq: 1, t: Date.now(), epoch: N - 5 })); | |
| 387 | await sleep(100); | |
| 388 | expect(await (await post("/stop", { by: "relay", epoch: N })).json()).toEqual({ stopped: "stream" }); | |
| 389 | await sleep(100); // the stream's close settles the arm where it is | |
| 390 | expect((await post("/resume", { by: "relay" })).status).toBe(200); | |
| 391 | const from = fake.writes.length; | |
| 392 | const s = await stream(); | |
| 393 | // stamped before the STOP, however fresh its seq and t: refused | |
| 394 | s.send(JSON.stringify({ 1: 2300, seq: 1, t: Date.now(), epoch: N - 1 })); | |
| 395 | await sleep(100); | |
| 396 | expect(goals(from).filter((w) => w.address === 42 && w.id === 1)).toEqual([]); | |
| 397 | // stamped at or after the STOP: goes | |
| 398 | s.send(JSON.stringify({ 1: 2150, seq: 2, t: Date.now(), epoch: N })); | |
| 399 | await sleep(100); | |
| 400 | const moved = goals(from).filter((w) => w.address === 42 && w.id === 1).map((w) => w.bytes[0]! | (w.bytes[1]! << 8)); | |
| 401 | expect(moved).toContain(2150); | |
| 402 | expect(moved).not.toContain(2300); | |
| 403 | s.close(); | |
| 404 | await sleep(50); | |
| 405 | }); | |
| 406 | ||
| 364 | 407 | test("a turn that ends stops what drives without holding", async () => { |
| 365 | 408 | const ws = await stream(); |
| 366 | 409 | ws.send(JSON.stringify({ 1: 2100 })); |
new above
offline
terminate()is an unclean close. The arm server can read it as a lost driver instead of a deliberate close. Use a clean close (1000), or document what the Pi does.