0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/addons/cppgc-object/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

52 lines
1.4 KiB
JavaScript

'use strict';
// Flags: --expose-gc
const common = require('../../common');
const { gcUntil } = require('../../common/gc');
// Verify that addons can create GarbageCollected objects and
// have them traced properly.
const assert = require('assert');
const {
CppGCed, states, kDestructCount, kTraceCount,
} = require(`./build/${common.buildType}/binding`);
assert.strictEqual(states[kDestructCount], 0);
assert.strictEqual(states[kTraceCount], 0);
let array = [];
const count = 100;
for (let i = 0; i < count; ++i) {
array.push(new CppGCed());
}
globalThis.gc();
setTimeout(async function() {
// GC should have invoked Trace() on at least some of the CppGCed objects,
// but they should all be alive at this point.
assert.strictEqual(states[kDestructCount], 0);
assert.notStrictEqual(states[kTraceCount], 0);
// Replace the old CppGCed objects with new ones, after GC we should have
// destructed all the old ones and called Trace() on the
// new ones.
for (let i = 0; i < count; ++i) {
array[i] = new CppGCed();
}
await gcUntil(
'All old CppGCed are destroyed',
() => states[kDestructCount] === count,
);
// Release all the CppGCed objects, after GC we should have destructed
// all of them.
array = null;
globalThis.gc();
await gcUntil(
'All old CppGCed are destroyed',
() => states[kDestructCount] === count * 2,
);
}, 1);