23 lines
1.4 KiB
JavaScript
23 lines
1.4 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { WorkerClient, CONTEXT_ID } from "./worker-client.mjs";
|
|
|
|
class StalledWorker {
|
|
constructor() { this.sent = []; this.onmessage = null; this.onerror = null; }
|
|
postMessage(message) { this.sent.push(message); if (message.kind === "HELLO") queueMicrotask(() => this.onmessage?.({ data: { schema_version: 1, kind: "CAPABILITIES", context_id: message.context_id, session_id: message.session_id, seq: message.seq, payload: { status: "verified" } } })); }
|
|
terminate() { this.terminated = true; }
|
|
}
|
|
|
|
const worker = new StalledWorker();
|
|
const client = new WorkerClient({ worker, contextId: CONTEXT_ID, sessionId: "runtime-client", timeoutMs: 20, maxPending: 1 });
|
|
assert.equal((await client.hello()).kind, "CAPABILITIES");
|
|
const first = client.send("SIM_STEP", { cycles: 1 }, { operationId: "long" });
|
|
await assert.rejects(client.send("SIM_STEP", { cycles: 2 }, { operationId: "blocked" }), (error) => error.code === "worker_backpressure");
|
|
const cancelled = await client.cancel("long");
|
|
assert.equal(cancelled.cancelled, true);
|
|
await assert.rejects(first, (error) => error.code === "worker_cancelled");
|
|
const timeout = client.send("SIM_STEP", { cycles: 1 }, { operationId: "timeout", timeoutMs: 10 });
|
|
await assert.rejects(timeout, (error) => error.code === "worker_timeout");
|
|
client.close();
|
|
assert.equal(worker.terminated, true);
|
|
console.log(`worker_client_runtime=passed sent=${worker.sent.length} cancel=${cancelled.cancelled}`);
|