fix: enforce one Pi runtime per user

This commit is contained in:
2026-07-21 16:13:17 +02:00
parent a040c083cc
commit 00761ae2ca
4 changed files with 125 additions and 19 deletions
+41
View File
@@ -95,6 +95,47 @@ test("oltre maxPiProcesses solleva errore", async () => {
mgr.teardown("a");
});
test("teardownForPrincipal stops only runtimes owned by that user", () => {
const aliceChild = recordingChild();
const bobChild = recordingChild();
aliceChild.kill = vi.fn();
bobChild.kill = vi.fn();
const children = [aliceChild, bobChild];
const mgr = new PiProcessManager(loadConfig({}), { spawnFn: () => children.shift() as any });
const alice = { issuer: "portal", subject: "alice", isAdmin: false };
const bob = { issuer: "portal", subject: "bob", isAdmin: false };
mgr.createFor("alice-session", { principal: alice });
mgr.createFor("bob-session", { principal: bob });
expect(mgr.teardownForPrincipal(alice)).toEqual(["alice-session"]);
expect(mgr.get("alice-session")).toBeUndefined();
expect(mgr.get("bob-session")).toBeDefined();
expect(aliceChild.kill).toHaveBeenCalledOnce();
expect(bobChild.kill).not.toHaveBeenCalled();
mgr.teardown("bob-session");
});
test("createFor keeps at most one runtime for the same user", () => {
const firstChild = recordingChild();
const secondChild = recordingChild();
firstChild.kill = vi.fn();
secondChild.kill = vi.fn();
const children = [firstChild, secondChild];
const mgr = new PiProcessManager(loadConfig({}), { spawnFn: () => children.shift() as any });
const principal = { issuer: "portal", subject: "alice", isAdmin: false };
mgr.createFor("first", { principal });
const second = mgr.createFor("second", { principal });
expect(mgr.count()).toBe(1);
expect(mgr.get("first")).toBeUndefined();
expect(mgr.get("second")).toBe(second);
expect(firstChild.kill).toHaveBeenCalledOnce();
expect(secondChild.kill).not.toHaveBeenCalled();
mgr.teardown("second");
});
function recordingChild() {
const ch: any = new EventEmitter();
ch.stdout = new EventEmitter();