Skip to content

Commit 2d88a7c

Browse files
committed
test: add maxCount and gcOptions to gcUntil()
1 parent b22c3d3 commit 2d88a7c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

test/common/gc.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,29 @@ function onGC(obj, gcListener) {
4040

4141
/**
4242
* Repeatedly triggers garbage collection until a specified condition is met or a maximum number of attempts is reached.
43+
* This utillity must be run in a Node.js instance that enables --expose-gc.
4344
* @param {string|Function} [name] - Optional name, used in the rejection message if the condition is not met.
4445
* @param {Function} condition - A function that returns true when the desired condition is met.
46+
* @param {number} maxCount - Maximum number of garbage collections that should be tried.
47+
* @param {object} gcOptions - Options to pass into the global gc() function.
4548
* @returns {Promise} A promise that resolves when the condition is met, or rejects after 10 failed attempts.
4649
*/
47-
function gcUntil(name, condition) {
50+
function gcUntil(name, condition, maxCount = 10, gcOptions) {
4851
if (typeof name === 'function') {
4952
condition = name;
53+
maxCount = condition;
54+
gcOptions = maxCount;
5055
name = undefined;
5156
}
5257
return new Promise((resolve, reject) => {
5358
let count = 0;
5459
function gcAndCheck() {
5560
setImmediate(() => {
5661
count++;
57-
global.gc();
62+
global.gc(gcOptions);
5863
if (condition()) {
5964
resolve();
60-
} else if (count < 10) {
65+
} else if (count < maxCount) {
6166
gcAndCheck();
6267
} else {
6368
reject(name === undefined ? undefined : 'Test ' + name + ' failed');

0 commit comments

Comments
 (0)