Skip to content

Commit

Permalink
[test] Give more time to GC in test-shadow-realm-gc
Browse files Browse the repository at this point in the history
Partial cherry-pick of nodejs#50735
  • Loading branch information
pthier committed Nov 20, 2023
1 parent 26a112e commit 648158d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions test/common/gc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const wait = require('timers/promises').setTimeout;

// TODO(joyeecheung): merge ongc.js and gcUntil from common/index.js
// into this.

Expand Down Expand Up @@ -65,6 +67,15 @@ async function checkIfCollectable(
createObject();
}

// Repeat an operation and give GC some breathing room at every iteration.
async function runAndBreathe(fn, repeat, waitTime = 20) {
for (let i = 0; i < repeat; i++) {
await fn();
await wait(waitTime);
}
}

module.exports = {
checkIfCollectable,
runAndBreathe,
};
6 changes: 4 additions & 2 deletions test/parallel/test-shadow-realm-gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
*/

const common = require('../common');
const { runAndBreathe } = require('../common/gc');
const assert = require('assert');
const { isMainThread, Worker } = require('worker_threads');

for (let i = 0; i < 100; i++) {
runAndBreathe(() => {
const realm = new ShadowRealm();
realm.evaluate('new TextEncoder(); 1;');
}
}, 100).then(common.mustCall());

//Test it in worker too.
if (isMainThread) {
const worker = new Worker(__filename);
worker.on('exit', common.mustCall((code) => {
Expand Down

0 comments on commit 648158d

Please sign in to comment.