1
0
silverbullet/plugos/runtime.test.ts

90 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-01-14 12:38:39 +00:00
import { createSandbox } from "./sandboxes/deno_worker_sandbox.ts";
import { System } from "./system.ts";
import { assert, assertEquals } from "../test_deps.ts";
import { compileManifest } from "./compile.ts";
import { esbuild } from "./deps.ts";
import {
createSandbox as createNoSandbox,
runWithSystemLock,
} from "./sandboxes/no_sandbox.ts";
import { sleep } from "$sb/lib/async.ts";
import { SysCallMapping } from "./system.ts";
Deno.test("Run a deno sandbox", async () => {
const system = new System("server");
system.registerSyscalls([], {
addNumbers: (_ctx, a, b) => {
return a + b;
},
failingSyscall: () => {
throw new Error("#fail");
},
} as SysCallMapping);
system.registerSyscalls(["restricted"], {
2022-03-25 11:03:06 +00:00
restrictedSyscall: () => {
return "restricted";
},
});
system.registerSyscalls(["dangerous"], {
2022-03-25 11:03:06 +00:00
dangerousSyscall: () => {
return "yay";
},
});
const tempDir = await Deno.makeTempDir();
2022-10-13 13:16:18 +00:00
const workerPath = await compileManifest(
new URL("test.plug.yaml", import.meta.url).pathname,
tempDir,
);
const plug = await system.load(
"test",
createSandbox(new URL(`file://${workerPath}`)),
);
2024-01-14 12:38:39 +00:00
assertEquals({
addedNumbers: 3,
yamlMessage: "hello: world\n",
}, await plug.invoke("boot", []));
2024-01-14 12:38:39 +00:00
await system.unloadAll();
// Now load directly from module
const { plug: plugExport } = await import(
`file://${workerPath}`
);
const plug2 = await system.load("test", createNoSandbox(plugExport));
2024-01-14 12:38:39 +00:00
let running = false;
await Promise.all([
runWithSystemLock(system, async () => {
console.log("Starting first run");
running = true;
await sleep(5);
assertEquals({
addedNumbers: 3,
yamlMessage: "hello: world\n",
}, await plug2.invoke("boot", []));
console.log("Done first run");
running = false;
}),
runWithSystemLock(system, async () => {
assert(!running);
console.log("Starting second run");
assertEquals({
addedNumbers: 3,
yamlMessage: "hello: world\n",
}, await plug2.invoke("boot", []));
console.log("Done second run");
}),
]);
2022-03-25 11:03:06 +00:00
2022-03-21 14:21:34 +00:00
await system.unloadAll();
await Deno.remove(tempDir, { recursive: true });
esbuild.stop();
});