Skip to content

Commit 26a06b0

Browse files
joyeecheungrichardlau
authored andcommitted
test: give more time to GC in test-shadow-realm-gc-*
When --node-builtin-modules-path is used, we read and create new strings for builtins in each realm which increases the memory usage. As a result GC may not be able to keep up with the allocation done in the loop in the test. As a workaround, give GC a bit more time by waiting for a timer in the loop. PR-URL: #50735 Refs: #50726 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent db7459b commit 26a06b0

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

test/common/gc.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const wait = require('timers/promises').setTimeout;
4+
35
// TODO(joyeecheung): merge ongc.js and gcUntil from common/index.js
46
// into this.
57

@@ -65,6 +67,15 @@ async function checkIfCollectable(
6567
createObject();
6668
}
6769

70+
// Repeat an operation and give GC some breathing room at every iteration.
71+
async function runAndBreathe(fn, repeat, waitTime = 20) {
72+
for (let i = 0; i < repeat; i++) {
73+
await fn();
74+
await wait(waitTime);
75+
}
76+
}
77+
6878
module.exports = {
6979
checkIfCollectable,
80+
runAndBreathe,
7081
};

test/parallel/test-shadow-realm-gc-module.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
const common = require('../common');
1010
const fixtures = require('../common/fixtures');
11+
const { runAndBreathe } = require('../common/gc');
1112

12-
async function main() {
13-
const mod = fixtures.fileURL('es-module-shadow-realm', 'state-counter.mjs');
14-
for (let i = 0; i < 100; i++) {
15-
const realm = new ShadowRealm();
16-
await realm.importValue(mod, 'getCounter');
17-
}
18-
}
13+
const mod = fixtures.fileURL('es-module-shadow-realm', 'state-counter.mjs');
1914

20-
main().then(common.mustCall());
15+
runAndBreathe(async () => {
16+
const realm = new ShadowRealm();
17+
await realm.importValue(mod, 'getCounter');
18+
}, 100).then(common.mustCall());

test/parallel/test-shadow-realm-gc.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
*/
77

88
const common = require('../common');
9+
const { runAndBreathe } = require('../common/gc');
910
const assert = require('assert');
1011
const { isMainThread, Worker } = require('worker_threads');
1112

12-
for (let i = 0; i < 100; i++) {
13+
runAndBreathe(() => {
1314
const realm = new ShadowRealm();
1415
realm.evaluate('new TextEncoder(); 1;');
15-
}
16+
}, 100).then(common.mustCall());
1617

18+
// Test it in worker too.
1719
if (isMainThread) {
1820
const worker = new Worker(__filename);
1921
worker.on('exit', common.mustCall((code) => {

0 commit comments

Comments
 (0)