fix(ext/node): emit online event after worker thread is initialized (#25243)

Fixes #23281. Part of #20613.

We were emitting the `online` event in the constructor, so the caller
could never receive it (since there was no time for them to add a
listener). Instead, emit the event where it's intended – after the
worker is initialized.

---

After this parcel no longer freezes, but still will fail due to other
bugs (which will be fixed in other PRs)
This commit is contained in:
Nathan Whitaker 2024-08-27 20:05:32 -07:00 committed by GitHub
parent 3dba98532a
commit 511d13abaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 3 deletions

View file

@ -590,3 +590,34 @@ Deno.test({
channel.port2.close();
},
});
Deno.test({
name: "[node/worker_threads] Emits online event",
async fn() {
const worker = new workerThreads.Worker(
`
import { parentPort } from "node:worker_threads";
const p = Promise.withResolvers();
let ok = false;
parentPort.on("message", () => {
ok = true;
p.resolve();
});
await Promise.race([p.promise, new Promise(resolve => setTimeout(resolve, 20000))]);
if (ok) {
parentPort.postMessage("ok");
} else {
parentPort.postMessage("timed out");
}
`,
{
eval: true,
},
);
worker.on("online", () => {
worker.postMessage("ok");
});
assertEquals((await once(worker, "message"))[0], "ok");
worker.terminate();
},
});