summaryrefslogtreecommitdiff
path: root/tests/napi/init_test.js
blob: 9486824780de3b59b5cdc397f31aaa65dbc7aca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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;
});