vats/counter.js keeps its count in the vat's storage; the test checks that it counts and that two counter vats count apart. Running it after the greeter test hung: a FinalizationRegistry callback in the vatAdmin, collecting an import, sent op:gc over the client's WebSocket, and workerd forbids I/O outside a request. The vatAdmin's and the vats' OCapN clients now run with enableImportCollection: false, so imports go when their session does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
80 lines
3.1 KiB
JavaScript
80 lines
3.1 KiB
JavaScript
/** @file OCapN vats: a client on the admin socket creates a vat and passes it a remote object. */
|
|
import { mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { after, before, test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { E, Far } from "@endo/far";
|
|
import { makeClient, swissnumFromBytes } from "@endo/ocapn";
|
|
import { newWebSocketRpcSession } from "capnweb";
|
|
import WebSocket from "ws";
|
|
import { locationOf, makeRpcNetLayer } from "../netlayer.js";
|
|
import { freePort, here, startWorkerd } from "./workerd.js";
|
|
|
|
const vatSrc = (name) => readFile(join(here, "vats", name), "utf8");
|
|
|
|
/**
|
|
* An OCapN client on the admin socket: registers its own gateway with the
|
|
* router there, and reaches every other peer through it.
|
|
*
|
|
* @param {string} adminSock
|
|
* @param {string} designator
|
|
*/
|
|
const connectClient = async (adminSock, designator) => {
|
|
const ws = new WebSocket(`ws+unix:${adminSock}:/`);
|
|
await new Promise((resolve, reject) => (ws.once("open", resolve), ws.once("error", reject)));
|
|
const session = newWebSocketRpcSession(ws);
|
|
const ocapn = makeClient({ debugLabel: designator, verbose: !!process.env.OCAPN_LOG });
|
|
const netlayer = await ocapn.registerNetlayer(
|
|
makeRpcNetLayer({ designator, openGateway: (d) => session.gateway(d) }),
|
|
);
|
|
await session.register(designator, netlayer.gateway);
|
|
const vatAdmin = await ocapn.enlivenSturdyRef(
|
|
ocapn.makeSturdyRef(locationOf("vatAdmin"), swissnumFromBytes(new TextEncoder().encode("vatAdmin"))),
|
|
);
|
|
return { vatAdmin, close: () => (ocapn.shutdown(), ws.close()) };
|
|
};
|
|
|
|
let dir, server, client;
|
|
|
|
before(async () => {
|
|
dir = await mkdtemp(join(tmpdir(), "toybox-ocapn-"));
|
|
await mkdir(join(dir, "state"));
|
|
const port = await freePort();
|
|
server = await startWorkerd(
|
|
"config.capnp",
|
|
[
|
|
"-s", `http=127.0.0.1:${port}`, "-s", `admin=unix:${join(dir, "admin.sock")}`,
|
|
"-d", `state=${join(dir, "state")}`, "-e", `switchboard=unix:${join(dir, "no-switchboard.sock")}`,
|
|
],
|
|
{ port, host: "127.0.0.1" },
|
|
[join(dir, "admin.sock")],
|
|
);
|
|
client = await connectClient(join(dir, "admin.sock"), "client-1");
|
|
});
|
|
|
|
after(async () => {
|
|
client?.close();
|
|
await server?.stop();
|
|
await rm(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
test("createVat passes a remote object in; the vat's root comes back", async () => {
|
|
const carol = Far("carol", { greet: (name) => `hi ${name}, from carol` });
|
|
const { id, root } = await E(client.vatAdmin).createVat(await vatSrc("greeter.js"), { carol });
|
|
assert.match(id, /^[0-9a-f]{64}$/);
|
|
assert.equal(await E(root).hello(), "hello from the vat");
|
|
assert.equal(await E(root).greetVia("vat"), "hi vat, from carol");
|
|
});
|
|
|
|
test("a counter vat counts; two counter vats count apart", async () => {
|
|
const src = await vatSrc("counter.js");
|
|
const { root: a } = await E(client.vatAdmin).createVat(src);
|
|
const { root: b } = await E(client.vatAdmin).createVat(src);
|
|
assert.equal(await E(a).increment(), 1);
|
|
assert.equal(await E(a).increment(), 2);
|
|
assert.equal(await E(b).increment(), 1);
|
|
assert.equal(await E(a).read(), 2);
|
|
assert.equal(await E(b).read(), 1);
|
|
});
|