0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/test/js-native-api/test_finalizer/test.js
tannal 0b6b2a4dc7
test: merge ongc and gcutil into gc.js
PR-URL: https://github.com/nodejs/node/pull/54355
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-08-29 08:49:52 +01:00

46 lines
1.5 KiB
JavaScript

'use strict';
// Flags: --expose-gc
const common = require('../../common');
const test_finalizer = require(`./build/${common.buildType}/test_finalizer`);
const assert = require('assert');
const { gcUntil } = require('../../common/gc');
// The goal of this test is to show that we can run "pure" finalizers in the
// current JS loop tick. Thus, we do not use gcUntil function works
// asynchronously using micro tasks.
// We use IIFE for the obj scope instead of {} to be compatible with
// non-V8 JS engines that do not support scoped variables.
(() => {
const obj = {};
test_finalizer.addFinalizer(obj);
})();
for (let i = 0; i < 10; ++i) {
global.gc();
if (test_finalizer.getFinalizerCallCount() === 1) {
break;
}
}
assert.strictEqual(test_finalizer.getFinalizerCallCount(), 1);
// The finalizer that access JS cannot run synchronously. They are run in the
// next JS loop tick. Thus, we must use gcUntil.
async function runAsyncTests() {
// We do not use common.mustCall() because we want to see the finalizer
// called in response to GC and not as a part of env destruction.
let js_is_called = false;
// We use IIFE for the obj scope instead of {} to be compatible with
// non-V8 JS engines that do not support scoped variables.
(() => {
const obj = {};
test_finalizer.addFinalizerWithJS(obj, () => { js_is_called = true; });
})();
await gcUntil('ensure JS finalizer called',
() => (test_finalizer.getFinalizerCallCount() === 2));
assert(js_is_called);
}
runAsyncTests();