1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00
denoland-deno/tests/napi/init_test.js
Nathan Whitaker 7dd861aa36
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).
2024-08-28 10:33:47 -07:00

50 lines
1.5 KiB
JavaScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { Buffer } from "node:buffer";
import { assert, libSuffix } from "./common.js";
import { Worker } from "node:worker_threads";
const ops = Deno[Deno.internal].core.ops;
Deno.test("ctr initialization (napi_module_register)", {
ignore: Deno.build.os == "windows",
}, function () {
const path = new URL(`./module.${libSuffix}`, import.meta.url).pathname;
const obj = ops.op_napi_open(path, {}, Buffer, reportError);
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;
});