Skip to content

Commit a278814

Browse files
BridgeARtargos
authored andcommitted
test: make sure weak references are not GCed too early
This fixes flaky `util.inspect` tests by making sure that the inspected WeakMap and WeakSet only contains objects that still have other hard references. Otherwise the garbage collection could collect these objects to early. PR-URL: #27482 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent aa281d2 commit a278814

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

test/parallel/test-util-inspect.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ util.inspect(process);
16141614
assert.strict.equal(out, expected);
16151615
}
16161616

1617-
{ // Test WeakMap
1617+
{ // Test WeakMap && WeakSet
16181618
const obj = {};
16191619
const arr = [];
16201620
const weakMap = new WeakMap([[obj, arr], [arr, obj]]);
@@ -1634,16 +1634,16 @@ util.inspect(process);
16341634
out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true });
16351635
// It is not possible to determine the output reliable.
16361636
expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }';
1637-
const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' +
1638-
'extra: true }';
1637+
let expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' +
1638+
'extra: true }';
16391639
assert(out === expect || out === expectAlt,
16401640
`Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`);
1641-
}
16421641

1643-
{ // Test WeakSet
1644-
const weakSet = new WeakSet([{}, [1]]);
1645-
let out = util.inspect(weakSet, { showHidden: true });
1646-
let expect = 'WeakSet { [ 1, [length]: 1 ], {} }';
1642+
// Test WeakSet
1643+
arr.push(1);
1644+
const weakSet = new WeakSet([obj, arr]);
1645+
out = util.inspect(weakSet, { showHidden: true });
1646+
expect = 'WeakSet { [ 1, [length]: 1 ], {} }';
16471647
assert.strictEqual(out, expect);
16481648

16491649
out = util.inspect(weakSet);
@@ -1658,10 +1658,12 @@ util.inspect(process);
16581658
out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true });
16591659
// It is not possible to determine the output reliable.
16601660
expect = 'WeakSet { {}, ... 1 more item, extra: true }';
1661-
const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, ' +
1662-
'extra: true }';
1661+
expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, extra: true }';
16631662
assert(out === expect || out === expectAlt,
16641663
`Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`);
1664+
// Keep references to the WeakMap entries, otherwise they could be GCed too
1665+
// early.
1666+
assert(obj && arr);
16651667
}
16661668

16671669
{ // Test argument objects.

0 commit comments

Comments
 (0)