node/test/js-native-api/test_finalizer/test_fatal_finalize.js
Vladimir Morozov b38e312486 node-api: run finalizers directly from GC
PR-URL: https://github.com/nodejs/node/pull/42651
Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
2023-10-03 10:06:01 -04:00

32 lines
963 B
JavaScript

'use strict';
const common = require('../../common');
if (process.argv[2] === 'child') {
const test_finalizer = require(`./build/${common.buildType}/test_finalizer`);
(() => {
const obj = {};
test_finalizer.addFinalizerFailOnJS(obj);
})();
// Collect garbage 10 times. At least one of those should throw the exception
// and cause the whole process to bail with it, its text printed to stderr and
// asserted by the parent process to match expectations.
let gcCount = 10;
(function gcLoop() {
global.gc();
if (--gcCount > 0) {
setImmediate(() => gcLoop());
}
})();
return;
}
const assert = require('assert');
const { spawnSync } = require('child_process');
const child = spawnSync(process.execPath, [
'--expose-gc', __filename, 'child',
]);
assert.strictEqual(child.signal, common.isWindows ? null : 'SIGABRT');
assert.match(child.stderr.toString(), /Finalizer is calling a function that may affect GC state/);