2019-07-15 00:21:13 +00:00
|
|
|
'use strict';
|
|
|
|
// Test API calls for instance data.
|
|
|
|
|
|
|
|
const common = require('../../common');
|
|
|
|
|
2020-03-11 22:44:31 +00:00
|
|
|
if (module !== require.main) {
|
2019-07-15 00:21:13 +00:00
|
|
|
// When required as a module, run the tests.
|
|
|
|
const test_instance_data =
|
|
|
|
require(`./build/${common.buildType}/test_instance_data`);
|
|
|
|
|
|
|
|
// Test that instance data can be used in an async work callback.
|
|
|
|
new Promise((resolve) => test_instance_data.asyncWorkCallback(resolve))
|
|
|
|
|
|
|
|
// Test that the buffer finalizer can access the instance data.
|
|
|
|
.then(() => new Promise((resolve) => {
|
|
|
|
test_instance_data.testBufferFinalizer(resolve);
|
|
|
|
global.gc();
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Test that the thread-safe function can access the instance data.
|
|
|
|
.then(() => new Promise((resolve) =>
|
|
|
|
test_instance_data.testThreadsafeFunction(common.mustCall(),
|
|
|
|
common.mustCall(resolve))));
|
|
|
|
} else {
|
|
|
|
// When launched as a script, run tests in either a child process or in a
|
|
|
|
// worker thread.
|
2020-02-04 04:46:37 +00:00
|
|
|
const assert = require('assert');
|
2019-07-15 00:21:13 +00:00
|
|
|
const requireAs = require('../../common/require-as');
|
|
|
|
const runOptions = { stdio: ['inherit', 'pipe', 'inherit'] };
|
2020-02-04 04:46:37 +00:00
|
|
|
const { spawnSync } = require('child_process');
|
2019-07-15 00:21:13 +00:00
|
|
|
|
|
|
|
// Run tests in a child process.
|
|
|
|
requireAs(__filename, ['--expose-gc'], runOptions, 'child');
|
|
|
|
|
|
|
|
// Run tests in a worker thread in a child process.
|
|
|
|
requireAs(__filename, ['--expose-gc'], runOptions, 'worker');
|
2020-02-04 04:46:37 +00:00
|
|
|
|
|
|
|
function testProcessExit(addonName) {
|
|
|
|
// Make sure that process exit is clean when the instance data has
|
|
|
|
// references to JS objects.
|
2024-10-22 07:49:19 +00:00
|
|
|
const path = require.resolve(`./build/${common.buildType}/${addonName}`);
|
|
|
|
const child = spawnSync(process.execPath, ['-e', `require(${JSON.stringify(path)});`]);
|
2020-02-04 04:46:37 +00:00
|
|
|
assert.strictEqual(child.signal, null);
|
|
|
|
assert.strictEqual(child.status, 0);
|
|
|
|
assert.strictEqual(child.stderr.toString(), 'addon_free');
|
|
|
|
}
|
|
|
|
|
|
|
|
testProcessExit('test_ref_then_set');
|
|
|
|
testProcessExit('test_set_then_ref');
|
2019-07-15 00:21:13 +00:00
|
|
|
}
|