Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions examples/js_dsl/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ describe("class return interop", () => {

describe("module lifecycle - worker threads", () => {
it("worker thread increments refcount and cleanup decrements it", async () => {
const { once } = await import("node:events");
const { Worker } = await import("node:worker_threads");
const { resolve } = await import("node:path");
const { fileURLToPath } = await import("node:url");
Expand All @@ -691,22 +692,15 @@ describe("module lifecycle - worker threads", () => {
{ eval: true, workerData: { nativePath } },
);

// Worker should see an incremented refcount
const workerRefcount = await new Promise((resolve) => {
worker.on("message", (msg) => {
resolve(msg.refcount);
});
});
expect(workerRefcount).toBeGreaterThan(refcountBefore);

// Wait for worker to exit (triggers cleanup hook)
await new Promise((resolve) => {
worker.on("exit", () => resolve(undefined));
});
// Node may drain the queued message and emit exit in the same turn.
// Register both listeners before yielding so neither event can be missed.
const [[message], [exitCode]] = await Promise.all([
once(worker, "message"),
once(worker, "exit"),
]);

// After worker exits, refcount should be back to what it was
// Give a small delay for cleanup hook to fire
await new Promise((resolve) => setTimeout(resolve, 100));
expect(message.refcount).toBeGreaterThan(refcountBefore);
expect(exitCode).toEqual(0);
expect(mod.getEnvRefcount()).toEqual(refcountBefore);
});
});
Expand Down
Loading