forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
51 lines (41 loc) · 1.33 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
// Flags: --expose-gc
const common = require('../../common');
// 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 common.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 common.gcUntil(
'All old CppGCed are destroyed',
() => states[kDestructCount] === count * 2,
);
}, 1);