diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-08-28 10:33:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-28 10:33:47 -0700 |
commit | 7dd861aa36974d5afa9633078b51c4c7f17cf181 (patch) | |
tree | 4cc81fba16d87433ab6d04d377ad0f2590560367 /tests/napi | |
parent | 0e50bb1d4abd80da3fc1be17978760ddfa8560fa (diff) |
fix(napi): Fix worker threads importing already-loaded NAPI addon (#25245)
Part of #20613.
If a node addon is using the legacy `napi_module_register` on ctor
approach to module registration, we have to store the registered module
so that other threads can load the addon (because `napi_module_register`
will only be called once per process).
Diffstat (limited to 'tests/napi')
-rw-r--r-- | tests/napi/init_test.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/napi/init_test.js b/tests/napi/init_test.js index 9db99d8a0..948682478 100644 --- a/tests/napi/init_test.js +++ b/tests/napi/init_test.js @@ -2,6 +2,7 @@ import { Buffer } from "node:buffer"; import { assert, libSuffix } from "./common.js"; +import { Worker } from "node:worker_threads"; const ops = Deno[Deno.internal].core.ops; @@ -13,3 +14,37 @@ Deno.test("ctr initialization (napi_module_register)", { assert(obj != null); assert(typeof obj === "object"); }); + +Deno.test("ctr initialization by multiple threads (napi_module_register)", { + ignore: Deno.build.os == "windows", +}, async function () { + const path = new URL(`./module.${libSuffix}`, import.meta.url).pathname; + const obj = ops.op_napi_open(path, {}, Buffer, reportError); + const common = import.meta.resolve("./common.js"); + assert(obj != null); + assert(typeof obj === "object"); + + const worker = new Worker( + ` + import { Buffer } from "node:buffer"; + import { parentPort } from "node:worker_threads"; + import { assert } from "${common}"; + + const ops = Deno[Deno.internal].core.ops; + const obj = ops.op_napi_open("${path}", {}, Buffer, reportError); + assert(obj != null); + assert(typeof obj === "object"); + parentPort.postMessage("ok"); + `, + { + eval: true, + }, + ); + + const p = Promise.withResolvers(); + worker.on("message", (_m) => { + p.resolve(); + }); + + await p.promise; +}); |