• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../../common');
3const assert = require('assert');
4const child_process = require('child_process');
5const path = require('path');
6const { Worker } = require('worker_threads');
7const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
8
9switch (process.argv[2]) {
10  case 'both':
11    require(binding);
12    // fallthrough
13  case 'worker-twice':
14  case 'worker':
15    const worker = new Worker(`require(${JSON.stringify(binding)});`, {
16      eval: true
17    });
18    if (process.argv[2] === 'worker-twice') {
19      worker.on('exit', common.mustCall(() => {
20        new Worker(`require(${JSON.stringify(binding)});`, {
21          eval: true
22        });
23      }));
24    }
25    return;
26  case 'main-thread':
27    process.env.addExtraItemToEventLoop = 'yes';
28    require(binding);
29    return;
30}
31
32// Use process.report to figure out if we might be running under musl libc.
33const glibc = process.report.getReport().header.glibcVersionRuntime;
34assert(typeof glibc === 'string' || glibc === undefined, glibc);
35
36const libcMayBeMusl = common.isLinux && glibc === undefined;
37
38for (const { test, expected } of [
39  { test: 'worker', expected: [ 'ctor cleanup dtor ' ] },
40  { test: 'main-thread', expected: [ 'ctor cleanup dtor ' ] },
41  // We always only have 1 instance of the shared object in memory, so
42  // 1 ctor and 1 dtor call. If we attach the module to 2 Environments,
43  // we expect 2 cleanup calls, otherwise one.
44  { test: 'both', expected: [ 'ctor cleanup cleanup dtor ' ] },
45  {
46    test: 'worker-twice',
47    // In this case, we load and unload an addon, then load and unload again.
48    // musl doesn't support unloading, so the output may be missing
49    // a dtor + ctor pair.
50    expected: [
51      'ctor cleanup dtor ctor cleanup dtor ',
52    ].concat(libcMayBeMusl ? [
53      'ctor cleanup cleanup dtor ',
54    ] : [])
55  },
56]) {
57  console.log('spawning test', test);
58  const proc = child_process.spawnSync(process.execPath, [
59    __filename,
60    test,
61  ]);
62  process.stderr.write(proc.stderr.toString());
63  assert.strictEqual(proc.stderr.toString(), '');
64  assert(expected.includes(proc.stdout.toString()),
65         `${proc.stdout.toString()} is not included in ${expected}`);
66  assert.strictEqual(proc.status, 0);
67}
68