node/test/node-api/test_worker_terminate/test.js
Anna Henningsen 8ebd339031
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>
2019-02-22 21:42:09 +01:00

29 lines
1008 B
JavaScript

'use strict';
const common = require('../../common');
const assert = require('assert');
const { Worker, isMainThread, workerData } = require('worker_threads');
if (isMainThread) {
// Load the addon in the main thread first.
// This checks that N-API addons can be loaded from multiple contexts
// when they are not loaded through NAPI_MODULE().
require(`./build/${common.buildType}/test_worker_terminate`);
const counter = new Int32Array(new SharedArrayBuffer(4));
const worker = new Worker(__filename, { workerData: { counter } });
worker.on('exit', common.mustCall(() => {
assert.strictEqual(counter[0], 1);
}));
worker.on('error', common.mustNotCall());
} else {
const { Test } = require(`./build/${common.buildType}/test_worker_terminate`);
const { counter } = workerData;
// Test() tries to call a function twice and asserts that the second call does
// not work because of a pending exception.
Test(() => {
Atomics.add(counter, 0, 1);
process.exit();
});
}