summaryrefslogtreecommitdiff
path: root/test/addons/worker-addon
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-02-17 23:45:14 +0100
committerAnna Henningsen <anna@addaleax.net>2019-02-22 21:42:09 +0100
commit8ebd339031cf9826629ad780dd35fee130e95985 (patch)
tree6a8a1b59040f55404e3cb3e3cf2860bd4879bd40 /test/addons/worker-addon
parent455b927be0111a6aa8f1e30ae5b21659afeba40d (diff)
downloadandroid-node-v8-8ebd339031cf9826629ad780dd35fee130e95985.tar.gz
android-node-v8-8ebd339031cf9826629ad780dd35fee130e95985.tar.bz2
android-node-v8-8ebd339031cf9826629ad780dd35fee130e95985.zip
worker: improve integration with native addons
Allow loading add-ons from multiple Node.js instances if they are declared context-aware; in particular, this applies to N-API addons. Also, plug a memory leak that occurred when registering N-API addons. Refs: https://github.com/nodejs/node/pull/23319 PR-URL: https://github.com/nodejs/node/pull/26175 Fixes: https://github.com/nodejs/node/issues/21481 Fixes: https://github.com/nodejs/node/issues/21783 Fixes: https://github.com/nodejs/node/issues/25662 Fixes: https://github.com/nodejs/node/issues/20239 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Diffstat (limited to 'test/addons/worker-addon')
-rw-r--r--test/addons/worker-addon/binding.cc2
-rw-r--r--test/addons/worker-addon/test.js60
2 files changed, 52 insertions, 10 deletions
diff --git a/test/addons/worker-addon/binding.cc b/test/addons/worker-addon/binding.cc
index faf3ba8647..01c857c43e 100644
--- a/test/addons/worker-addon/binding.cc
+++ b/test/addons/worker-addon/binding.cc
@@ -21,7 +21,7 @@ struct statically_allocated {
}
~statically_allocated() {
assert(count == 0);
- printf("dtor");
+ printf("dtor ");
}
} var;
diff --git a/test/addons/worker-addon/test.js b/test/addons/worker-addon/test.js
index 7fb7c1a87a..ef158e98b1 100644
--- a/test/addons/worker-addon/test.js
+++ b/test/addons/worker-addon/test.js
@@ -1,3 +1,4 @@
+// Flags: --experimental-report
'use strict';
const common = require('../../common');
const assert = require('assert');
@@ -6,21 +7,62 @@ const path = require('path');
const { Worker } = require('worker_threads');
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
-if (process.argv[2] === 'worker') {
- new Worker(`require(${JSON.stringify(binding)});`, { eval: true });
- return;
-} else if (process.argv[2] === 'main-thread') {
- process.env.addExtraItemToEventLoop = 'yes';
- require(binding);
- return;
+switch (process.argv[2]) {
+ case 'both':
+ require(binding);
+ // fallthrough
+ case 'worker-twice':
+ case 'worker':
+ const worker = new Worker(`require(${JSON.stringify(binding)});`, {
+ eval: true
+ });
+ if (process.argv[2] === 'worker-twice') {
+ worker.on('exit', common.mustCall(() => {
+ new Worker(`require(${JSON.stringify(binding)});`, {
+ eval: true
+ });
+ }));
+ }
+ return;
+ case 'main-thread':
+ process.env.addExtraItemToEventLoop = 'yes';
+ require(binding);
+ return;
}
-for (const test of ['worker', 'main-thread']) {
+// Use process.report to figure out if we might be running under musl libc.
+const glibc = JSON.parse(process.report.getReport()).header.glibcVersionRuntime;
+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: [
+ 'ctor cleanup dtor ctor cleanup dtor '
+ ].concat(libcMayBeMusl ? [
+ 'ctor cleanup cleanup dtor ',
+ ] : [])
+ },
+]) {
+ console.log('spawning test', test);
const proc = child_process.spawnSync(process.execPath, [
__filename,
test
]);
+ process.stderr.write(proc.stderr.toString());
assert.strictEqual(proc.stderr.toString(), '');
- assert.strictEqual(proc.stdout.toString(), 'ctor cleanup dtor');
+ assert(expected.includes(proc.stdout.toString()),
+ `${proc.stdout.toString()} is not included in ${expected}`);
assert.strictEqual(proc.status, 0);
}