2018-12-06 09:23:56 +08:00
|
|
|
'use strict';
|
|
|
|
const common = require('../../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const child_process = require('child_process');
|
|
|
|
const path = require('path');
|
|
|
|
const { Worker } = require('worker_threads');
|
|
|
|
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
|
|
|
|
|
2019-02-18 06:45:14 +08:00
|
|
|
switch (process.argv[2]) {
|
|
|
|
case 'both':
|
|
|
|
require(binding);
|
|
|
|
// fallthrough
|
|
|
|
case 'worker-twice':
|
2022-01-03 12:21:46 +08:00
|
|
|
case 'worker': {
|
2019-02-18 06:45:14 +08:00
|
|
|
const worker = new Worker(`require(${JSON.stringify(binding)});`, {
|
2022-11-22 01:40:12 +08:00
|
|
|
eval: true,
|
2019-02-18 06:45:14 +08:00
|
|
|
});
|
|
|
|
if (process.argv[2] === 'worker-twice') {
|
|
|
|
worker.on('exit', common.mustCall(() => {
|
|
|
|
new Worker(`require(${JSON.stringify(binding)});`, {
|
2022-11-22 01:40:12 +08:00
|
|
|
eval: true,
|
2019-02-18 06:45:14 +08:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return;
|
2022-01-03 12:21:46 +08:00
|
|
|
}
|
2019-02-18 06:45:14 +08:00
|
|
|
case 'main-thread':
|
|
|
|
process.env.addExtraItemToEventLoop = 'yes';
|
|
|
|
require(binding);
|
|
|
|
return;
|
2019-01-19 07:24:20 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 06:45:14 +08:00
|
|
|
// Use process.report to figure out if we might be running under musl libc.
|
2019-07-11 04:27:44 +08:00
|
|
|
const glibc = process.report.getReport().header.glibcVersionRuntime;
|
2019-02-18 06:45:14 +08:00
|
|
|
assert(typeof glibc === 'string' || glibc === undefined, glibc);
|
|
|
|
|
|
|
|
const libcMayBeMusl = common.isLinux && glibc === undefined;
|
|
|
|
|
|
|
|
for (const { test, expected } of [
|
|
|
|
{ test: 'worker', expected: [ 'ctor cleanup dtor ' ] },
|
|
|
|
{ test: 'main-thread', expected: [ 'ctor cleanup dtor ' ] },
|
|
|
|
// We always only have 1 instance of the shared object in memory, so
|
|
|
|
// 1 ctor and 1 dtor call. If we attach the module to 2 Environments,
|
|
|
|
// we expect 2 cleanup calls, otherwise one.
|
|
|
|
{ test: 'both', expected: [ 'ctor cleanup cleanup dtor ' ] },
|
|
|
|
{
|
|
|
|
test: 'worker-twice',
|
|
|
|
// In this case, we load and unload an addon, then load and unload again.
|
|
|
|
// musl doesn't support unloading, so the output may be missing
|
|
|
|
// a dtor + ctor pair.
|
|
|
|
expected: [
|
2021-03-26 23:51:08 +08:00
|
|
|
'ctor cleanup dtor ctor cleanup dtor ',
|
2019-02-18 06:45:14 +08:00
|
|
|
].concat(libcMayBeMusl ? [
|
|
|
|
'ctor cleanup cleanup dtor ',
|
2022-11-22 01:40:12 +08:00
|
|
|
] : []),
|
2019-02-18 06:45:14 +08:00
|
|
|
},
|
|
|
|
]) {
|
|
|
|
console.log('spawning test', test);
|
2018-12-06 09:23:56 +08:00
|
|
|
const proc = child_process.spawnSync(process.execPath, [
|
|
|
|
__filename,
|
2021-03-26 23:51:08 +08:00
|
|
|
test,
|
2018-12-06 09:23:56 +08:00
|
|
|
]);
|
2019-02-18 06:45:14 +08:00
|
|
|
process.stderr.write(proc.stderr.toString());
|
2018-12-06 09:23:56 +08:00
|
|
|
assert.strictEqual(proc.stderr.toString(), '');
|
2019-02-18 06:45:14 +08:00
|
|
|
assert(expected.includes(proc.stdout.toString()),
|
|
|
|
`${proc.stdout.toString()} is not included in ${expected}`);
|
2018-12-06 09:23:56 +08:00
|
|
|
assert.strictEqual(proc.status, 0);
|
|
|
|
}
|